Files
Run code on every part in assembly
Code
Module: mMain
'Run code on each part component in an assembly.
'Preconditions: assembly is open.
'Results: see immediate window to see the configurations and custom _
properties belonging to the part.
'Notes:
'If a component is not fully loaded into memory (e.g., lightweight, _
suppression) then IComponent2::GetModelDoc2 returns Nothing.
'To allow code to run on every component in the assembly, simply _
remove the conditional that tests if the component is a part.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim i As Integer
Dim vComps As Variant
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
vComps = swAssy.GetComponents(False)
If IsEmpty(vComps) Then Exit Sub
For i = 0 To UBound(vComps)
Set swComp = vComps(i)
Set swCompModel = swComp.GetModelDoc2
If swCompModel.GetType = swDocPART Then
'RUN YOUR CODE HERE
Debug.Print "Component: " & swComp.Name2
'====sample code: print config names in that part====
Dim vConfigNames As Variant
Dim j As Integer
vConfigNames = swCompModel.GetConfigurationNames
For j = 0 To UBound(vConfigNames)
Debug.Print " Config name: " & vConfigNames(j)
Next j
'====================================================
'====sample code: get custom property data for each component====
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim vCustPropNames As Variant
Dim strVal As String
Dim strResVal As String
Dim k As Integer
Set swCustPropMgr = swCompModel.Extension.CustomPropertyManager(Empty)
vCustPropNames = swCustPropMgr.GetNames
If IsEmpty(vCustPropNames) = False Then
For k = 0 To UBound(vCustPropNames)
swCustPropMgr.Get4 vCustPropNames(k), True, strVal, strResVal
Debug.Print " " & vCustPropNames(k) & " " & strVal & " " & strResVal
Next k
End If
'====================================================
End If
Next i
End Sub