I made a macro that iterates over all features of a part file, when it finds the cut-list folder it goes over it and changes a custom property I want. That property is an ID number I use internally. Problem is, I often have a part with, let's say, 8 bodies. But the ID goes to 10 or 12 or some other random number and assigns ID's to bodies that do not exist in the part file? Here's my code:
Code: Select all
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim featMgr As SldWorks.FeatureManager
Dim curMod As SldWorks.ModelDoc2 'currently traversed Model
Dim swBodyFolder As BodyFolder 'bodyfolder = top-level cut-list folder
Dim vComponents As Variant 'all components in the assy
Dim id As Integer
Dim swFeat As Feature
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set curMod = swApp.ActiveDoc
Set featMgr = swModel.FeatureManager
If (swModel.GetType <> swDocPART) Then
MsgBox "Please open a part file", vbCritical, "Error"
Exit Sub
End If
id = CInt(InputBox("Start ID:", "ID Macro", "1"))
'id = 1
If Not curMod Is Nothing Then
Set swFeat = curMod.FirstFeature
Do While Not swFeat Is Nothing
If Not swFeat.IsSuppressed Then
'Debug.Print swFeat.Name
If swFeat.GetTypeName2 = "SolidBodyFolder" Then
Set swBodyFolder = swFeat.GetSpecificFeature2
swBodyFolder.UpdateCutList
ElseIf swFeat.GetTypeName2 = "CutListFolder" Then
Dim vProperties As Long
Dim vPropnames As Variant
Dim vProptypes As Variant
Dim vPropvalues As Variant
Dim vResolved As Variant
vProperties = swFeat.CustomPropertyManager.GetAll2(vPropnames, vProptypes, vPropvalues, vResolved)
If vProperties <> 0 Then
'Debug.Print "Number props: " & vProperties
Dim j As Integer
For j = 0 To vProperties - 1
If vPropnames(j) = "Opis" Then
Debug.Print "Opis: " & vPropvalues(j)
End If
If vPropnames(j) = "DŁUGOŚĆ" Then
Debug.Print "Długość: " & vPropvalues(j)
End If
If vPropnames(j) = "id" Then
Debug.Print "id: " & vPropvalues(j)
If vPropvalues(j) >= id Then
' Debug.Print "Found id value: " & vPropvalues(j)
' Debug.Print "Found id, assigning " & id
swFeat.CustomPropertyManager.LinkProperty "id", False
swFeat.CustomPropertyManager.Set2 vPropnames(j), CStr(id)
id = id + 1
End If
End If
Next j
Debug.Print "============================================================="
End If
End If
End If
Set swFeat = swFeat.GetNextFeature
Loop
End If
'Debug.Print "Last ID assigned: " & id - 1
MsgBox "Last ID: " & id - 1, vbOKOnly, "ID macro"
End Sub
Here's the ID's assigned by the macro, as you can see they are in wrong order, with some missing:
And here's my Immediate output, showing that it finds erroneous members that do not exist and assign ID's to them:
Code: Select all
(...)
=============================================================
Długość: "LENGTH@@@RP 40x20x2<1>@Powtarzalny front.SLDPRT"
Opis: RP 40x20x2
id: 3
=============================================================
Długość: "LENGTH@@@PL 40x5<1>@Powtarzalny front.SLDPRT"
Opis: PL 40x5
id: 4
=============================================================
(...)
Info: "Długość" is the length property, "Opis" is the description. Any idea what happens here?
Edit: checking for body count seems to have solved the issue. Thanks!