Macro to rename an assembly and its components

Hello

 

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.

 

If you have any proposals, I'm all ears!

 

Thank you for those who will answer my question.

 

PS: I'm using Solidworks 2013

 

 

 

 

 

 

 

 

Hello, by any chance, you can't use this?

 

http://www.lynkoa.com/store/fr/tools-et-macros/utilitaires/utilitaire-projectmanager.html

1 Like
Project manager he must know how to do that
2 Likes

Thank you for the speed of your answers.

I don't know about ProjectManager. By following the link, I saw that the utility is paid.

Isn't there a free or cheaper way?

Hello

 

 

The code should look like this:

 

Sub Main()

Dim swApp As SldWorks.SldWorks
Dim Document As SldWorks.ModelDoc2
Dim retval() As String
Dim Text As String
Dim Name() As String

Set swApp = CreateObject("SldWorks.Application")
Set Document = swApp.ActiveDoc
 

retval = Document.GetDependencies2(False, True, False)

'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

 

MsgBox Text

End Sub

 

 

Source: http://www.forum-cao-3d.fr/viewtopic.php?t=5432

 

 

If you have any other questions about macros, feel free to open another question about Lynkoa!

Hello

 

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

 

 

8 Likes

Hello

 

Jfradon, I followed your advice. And I wrote a macro that tends towards what I want.

 

But I can't save the model of my components.

I have an error at (swCompModel.SaveAs2 NewFilePath, 0, False, False)

 

 


renommer_fichier.txt

...

Gulp I can't cancel DSL

Yes the macro looks good to me

simply you have to manage differently the 1° components (root components of the main assembly) 

in fact it is not a component 

so if after

Set swCompModel = swComp.GetModelDoc

 

swCompModel  is nothing so don't use swCompModel  but swModel which is the assembly document

7 Likes

If it's ok, do you close the position?

4 Likes