I would like to create a macro on Solidworks in order to rename an assembly and its components according to the following principle:
"Project Number-ES00" for assembly
"Project Number-ES0i" for the i-th sub-assembly (1st level)
"Project Number-ES0i-C00j" for the jth part of the i-th sub-assembly (2nd level)
For example, the creation tree should look like this:
INT846-ES00
INT846-ES01
INT846-ES01-C001
INT846-ES01-C002
INT846-ES01-C003
INT846-ES01-C004
INT846-ES02
INT846-ES02-C001
INT846-ES02-C002
INT846-ES03
INT846-ES04
I can code the recurrence principle in VBA but I don't know how to re-register an assembly and its components taking into account the tree as a whole. Then I don't know how to go through the creation tree of the assembly in question.
'Lines to have just the file names ReDim Name(1 TB ((UBound(retval) - 1) / 2) + 1) For i = 0 TB (UBound(retval) - 1) / 2 Text = Text & retval(2 * i) & vbCr Name(i + 1) = retval(2 * i) Next i i = 0
In principle, the most efficient would be to rename the files outside of SW and then redirect the external references of the documents...
But it's complicated for qlq who doesn't have the abitude of SW APIs
The simplest development side is to open the assembly and browse the components then save each of the documents, as the main assembly is open SW will itself redirect the references ...
simply there will be the old files with the old names to be deleted manually
To create a recursion on components (walk through the entire assembly tree)
retrieve the sample from the API help
"Traverse Assembly at Component Level Example (VBA)"
The entry point for the marco is the MAin procedure
In this example the TraverseComponent function will be executed on each component, the swComp object contains the component, and the nLevel variable contains the level of the component in the tree (e.g. 1 pror the 1st level, 2 for the component of a subassembly, 3 etc ...)
you still need to add the document registration code (the document is obtained from the "component" by the GetmodelDoc API)
Be careful to manage the occurrences of components (the same component can be present several times) only the first occurrence should be processed