Hello
Attached is the code to traverse the assembly and subassemblies.
What was blocking the code was the position of j=1 which made it loop on this value and therefore it systematically tried to rename with the 0001 increment.
For the update in the explorer, you have to save so that the impact is reflected on the file record name since the rename function only temporarily changes the file names (updated in the featuremanager only).
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swRootComp As SldWorks.Component2
Dim Children As Variant
Dim swChild As SldWorks.Component2
Dim SwSelData As SldWorks.SelectData
Dim ChildCount As Long
Dim oldName As String
Dim newName As String
Dim i As Long
Dim j As Long
Dim NomParent As String
Dim errorsRename As Long
Dim status As Boolean
Dim warnings As Long
Dim errorsSave As Long
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
NomParent = Left(swModel.GetTitle, 7)
Set swRootComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(True)
j = 1
TraverseComponent swRootComp, 1
swModel.ForceRebuild3 True
status = swModel.Save3(swSaveAsOptions_SaveReferenced, errorsSave, warnings)
End Sub
Sub TraverseComponent _
(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim swCompConfig As SldWorks.Configuration
Dim sPadStr As String
Dim i As Long
For i = 0 To nLevel - 1
sPadStr = sPadStr + " "
Next i
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
swChildComp.Select4 False, SwSelData, False
newName = "Test" & "-" & "000" & j
errorsRename = swModel.Extension.RenameDocument(newName)
j = j + 1
Next i
End Sub
The code still needs to be adapted to target the files to be renamed, moreover I didn't look any longer but the order is a bit random.
3 Likes
Super Cyril,
Thank you so much, I would love it to be as easy as it sounds!
All I have to do now is test if a configuration property of each of the children exists and if so, I rename it, otherwise I move on to the next child. ( no matter the order.. )
And then I have to manage the counter correctly so that the total of my filename doesn't exceed 12 digits.
Otherwise with the MycadTools tool that you must have access to given your medal on your profile, it must be doable via ProjectManager without having any programming knowledge
2 Likes
Hello sbadenis,
Yes, projectManager works, but it's too long for the little programming it costs.
Thanks to Cyril, all I need to do is access the configuration properties that I can't find any information about on the internet and that'll be it:)
Thank you for the advice,
Kind regards
No worries @vincent.bottier for the configuration properties, here is an example of my macrotec:
'---------------------------------------------------------------------------
' Preconditions:
' 1. Open a part document.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Adds a date custom property to the part's configuration.
' 2. Tests whether the custom property is editable, and if so,
' edits it.
' 3. Gets all custom properties in the configuration.
' 4. Deletes a custom property.
' 5. Examine the Immediate window.
'---------------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim config As SldWorks.Configuration
Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim lRetVal As Long
Dim vPropNames As Variant
Dim vPropTypes As Variant
Dim vPropValues As Variant
Dim ValOut As String
Dim ResolvedValOut As String
Dim wasResolved As Boolean
Dim linkToProp As Boolean
Dim resolved As Variant
Dim linkProp As Variant
Dim nNbrProps As Long
Dim j As Long
Dim custPropType As Long
Dim bRet As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set config = swModel.GetActiveConfiguration
Set cusPropMgr = config.CustomPropertyManager
' Get the number of custom properties for this configuration
nNbrProps = cusPropMgr.Count
Debug.Print "Number of properties for this configuration: " & nNbrProps
' Gets the custom properties
lRetVal = cusPropMgr.GetAll3(vPropNames, vPropTypes, vPropValues, resolved, linkProp)
' For each custom property, print its name, type, and evaluated value
For j = 0 To nNbrProps - 1
custPropType = cusPropMgr.GetType2(vPropNames(j))
Debug.Print " Name, swCustomInfoType_e value, and resolved value: " & vPropNames(j) & ", "; custPropType & ", " & vPropValues(j)
Next j
' Get the number of custom properties for this configuration
nNbrProps = cusPropMgr.Count
Debug.Print "Number of properties for this configuration: " & nNbrProps
End Sub
1 Like
Hello
Here is a small example to check the value of your property:
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp As Variant
Dim swChildComp As Component2
Dim i As Long
Dim swModelChild As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
swChildComp.Select4 False, SwSelData, False
Set swModelChild = swChildComp.GetModelDoc2
Set swCustProp = swModelChild.Extension.CustomPropertyManager("Défaut") 'mettre le nom de la configuration comprenant la propriété
status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
If valout <> "" Then
newName = Left(swModelChild.GetTitle, 4) & "-" & "000" & j
errorsRename = swModel.Extension.RenameDocument(newName)
Debug.Print swModelChild.GetTitle & " : " & j & " - " & errorsRename
j = j + 1
End If
Next i
End Sub
Small information also, if a part is present several times in your assembly then it will be treated as many times and will therefore take the name of the last counter with which it was treated, I don't know if this is annoying for your application.
Kind regards
5 Likes
Hello
Thank you for your answers, I'll try to adapt this code!
@d.Roger : I should be able to tell him that if he has so many digits in his name and the first 7 are equal to the first 7 of the assembly then he doesn't process and it won't duplicate in this case:)
I'll keep you posted and post my final code!
Thank you again for your help.
1 Like
As promised by d.roger;
I tried with your code. I have an error on the line: Set swCustProp = swModelChild.Extension.CustomPropertyManager("Default")
Yet the configuration of all my rooms is indeed this. Do you have an idea?
Thank you
Good evening
There is probably a file that does not have this configuration in its settings.
Attached is the code modified to take into account the existence or not of the Default configuration
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp As Variant
Dim swChildComp As Component2
Dim i As Long
Dim swModelChild As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
swChildComp.Select4 False, SwSelData, False
Set swModelChild = swChildComp.GetModelDoc2
Set swCustProp = swModelChild.Extension.CustomPropertyManager("Défaut") 'mettre le nom de la configuration comprenant la propriété
If Not swCustProp Is Nothing Then
status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
If valout <> "" Then
newName = Left(swModelChild.GetTitle, 4) & "-" & "000" & j
errorsRename = swModel.Extension.RenameDocument(newName)
Debug.Print swModelChild.GetTitle & " : " & j & " - " & errorsRename
j = j + 1
End If
End If
Next i
End Sub
2 Likes
Hello
I'm starting to do the real tests and indeed I see what you wanted to tell me.
The component name in the properties does not match the 3D file name.
I added a line and I thought it would work even if it didn't take all the numbers. And I also have parts that have a "default" configuration (damn American :)).
If Not swCustProp Is Nothing Then
status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
If valout <> "" Then
newName = ParentName & "-" & "000" & j
errorsRename = swModel.Extension.RenameDocument(newName)
swChildComp.Name2 = newName
Debug.Print swModelChild.GetTitle & " : " & j & " - " & errorsRename
j = d + 1
End If
End If
For the time being, there are almost no components that have the same name in the tree as in the windows folder.
Hello
I don't understand what you're trying to do with the added line.
Also, as I already said in the first answer, if there is no record with the children's update settings, there will be no change to the record names of the files and subassemblies, hence the lack of visibility in Windows Explorer.
Hello
I'm trying to make the Windows filename match the component name.
And for me, I do make a backup see below
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swRootComp As SldWorks.Component2
Dim Children As Variant
Dim swChild As SldWorks.Component2
Dim SwSelData As SldWorks.SelectData
Dim ChildCount As Long
Dim oldName As String
Dim newName As String
Sun i As Long
Sun j As Long
Dim ParentName As String
Dim errorsRename As Long
Dim status As Boolean
Dim warnings As Long
Save As Long
Dim swModelDocExt As ModelDocExtension
Dim swCustProp As CustomPropertyManager
Dim bool As Boolean
Dim val As String
Dim valout As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
ParentName = Left(swModel.GetTitle, 7)
Set swRootComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(True)
j = 1
TraverseComponent swRootComp, 1
swModel.ForceRebuild3 True
status = swModel.Save3(swSaveAsOptions_SaveReferenced, errorsSave, warnings)
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp as Variant
Dim swChildComp As Component2
Sun i As Long
Dim swModelChild As SldWorks.ModelDoc2
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String
vChildComp = swComp.GetChildren
For i = 0 TB UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
swChildComp.Select4 False, SwSelData, False
Set swModelChild = swChildComp.GetModelDoc2
Set swCustProp = swModelChild.Extension.CustomPropertyManager("Default") ' set the name of the configuration including the
If Not swCustProp Is Nothing Then
status = swCustProp.Get4("SWOODCP_PanelStockLength", False, val, valout)
If valout <> "" Then
newName = ParentName & "-" & "000" & j
errorsRename = swModel.Extension.RenameDocument(newName)
swChildComp.Name2 = newName
Debug.Print swModelChild.GetTitle & " : " & j & " - " & errorsRename
j = d + 1
End If
End If
Next i
End Sub
capture.png
The line swChildComp.Name2 = newName does not add anything more than the renaming function already does.
For the screenshot, I don't see what's wrong. The file name is good.
All that remains is to check that the files are not open in read-only, I can only see that.
Yes because I renamed it by hand, but when I run my macro with an assembly somewhere, it doesn't give the same name as if the counter didn't act.
And when I rename a part by hand, by right-clicking rename the part, it changes the windows name but not the name of the component.
Is this normal?
I don't understand the problem or there is a subtlety with Swood that I don't master.
Well I'm a bit of a maniac and I find it confusing that the name displayed in the featuremanager is not the same as the name of the windows file. No?
Is there a command line that allows me to rename the name of the component so that I can match it with the name of the file directly ?
It's more about setting up the models in this case. In my case, filename = name displayed in the featuremanager.
All right
well I don't have the option to display the name of the file which seems weird to me...
capture.png
It's hidden in "Show component names". The component name is the same as the record name.
Otherwise, you should look in the general options at the external reference level if the option "Update component names when documents are replaced" is checked.
1 Like
Great, this option of external references was not checked :)!
I've been testing it on several assemblies since this morning, and I still have a sorry question^^
Are there cases where the rename function doesn't work?
Because here I have two parts that are not renamed, yet I pass the program in step by step it passes on them and triggers the line: errorsRename = swModel.Extension.RenameDocument(newName)
But don't rename it. Do you have an idea?
Have a good weekend