Advertisement
makos

id macro

Jul 1st, 2024 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Dim swApp As SldWorks.SldWorks
  4. Dim swModel As SldWorks.ModelDoc2
  5. Dim featMgr As SldWorks.FeatureManager
  6. Dim curMod As SldWorks.ModelDoc2 'currently traversed Model
  7. Dim swBodyFolder As BodyFolder 'bodyfolder = top-level cut-list folder
  8. Dim vComponents As Variant 'all components in the assy
  9. Dim id As Integer
  10. Dim swFeat As Feature
  11.  
  12. Sub main()
  13.     Set swApp = Application.SldWorks
  14.     Set swModel = swApp.ActiveDoc
  15.     Set featMgr = swModel.FeatureManager
  16.    
  17.     If swModel.GetType <> swDocASSEMBLY Then
  18.         MsgBox "Please open an assembly!", vbCritical, "Error"
  19.         Exit Sub
  20.     End If
  21.  
  22.     vComponents = swModel.GetComponents(False)
  23.    
  24.     Dim i As Integer
  25.    
  26.     id = CInt(InputBox("Start ID:", "ID Macro", "1"))
  27.     'id = 1
  28.    For i = 0 To UBound(vComponents)
  29.         Set curMod = vComponents(i).GetModelDoc2()
  30.         Set swFeat = curMod.FirstFeature
  31.    
  32.         Do While Not swFeat Is Nothing
  33.             If swFeat.GetTypeName2 = "SolidBodyFolder" Then
  34.                 Set swBodyFolder = swFeat.GetSpecificFeature2
  35.                 swBodyFolder.UpdateCutList
  36.             ElseIf swFeat.GetTypeName2 = "CutListFolder" Then
  37.                 Dim vProperties As Long
  38.                 Dim vPropnames As Variant
  39.                 Dim vProptypes As Variant
  40.                 Dim vPropvalues As Variant
  41.                 Dim vResolved As Variant
  42.                
  43.                 vProperties = swFeat.CustomPropertyManager.GetAll2(vPropnames, vProptypes, vPropvalues, vResolved)
  44.                 If vProperties <> 0 Then
  45.                     'Debug.Print "Number props: " & vProperties
  46.                    Dim j As Integer
  47.                     For j = 0 To vProperties - 1
  48.                         'Debug.Print "Name: " & vPropnames(i)
  49.                        'Debug.Print "Value: " & vPropvalues(i)
  50.                        
  51.                         If vPropnames(j) = "id" Then
  52.                             If vPropvalues(j) >= id Then
  53.                                 'Debug.Print "Found id, assigning " & id
  54.                                swFeat.CustomPropertyManager.Set2 vPropnames(j), CStr(id)
  55.                                 id = id + 1
  56.                             End If
  57.                         End If
  58.                     Next j
  59.                 End If
  60.             End If
  61.            
  62.             Set swFeat = swFeat.GetNextFeature
  63.         Loop
  64.     Next i
  65.     'Debug.Print "Last ID assigned: " & id - 1
  66.    MsgBox "Last ID: " & id - 1, vbOKOnly, "ID macro"
  67. End Sub
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement