I'm trying to extract mass properties of different solidworks parts or subassemblies in a main assembly. However I get the total mass and properties however not the individual properties of the parts. I think I need to use some kind of selection loop to select the parts individually but I cannot make it work.
This is my code so far:
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelExt As SldWorks.ModelDocExtension
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim nStatus As Long
Dim vMassProp As Variant
Dim i As Long
Dim nbrSelections As Long
Dim partie As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelExt = swModel.Extension
Set swSelMgr = swModel.SelectionManager
nbrSelections = swSelMgr.GetSelectedObjectCount2(-1)
If nbrSelections = 0 Then
Debug.Print "Please select one or more components and rerun the macro."
Exit Sub
End If
nbrSelections = nbrSelections - 1
Debug.Print "Getting mass properties for components: "
For i = 0 To nbrSelections
Set Part = swApp.ActiveDoc
Set swComp = swSelMgr.GetSelectedObject6(i + 1, -1)
Debug.Print " " & swComp.Name2
swSelMgr.GetSelectedObject6(i+1, -1)
vMassProp = swModelExt.GetMassProperties2(1, nStatus, True)
Debug.Print "Status as defined in swMassPropertiesStatus_e (0 = Mass properties calculation successful) = " & nStatus
If Not IsEmpty(vMassProp) Then
Debug.Print "Center of mass:"
Debug.Print " X-coordinate = " & vMassProp(0)
Debug.Print " Y-coordinate = " & vMassProp(1)
Debug.Print " Z-coordinate = " & vMassProp(2)
Debug.Print "Mass = " & vMassProp(5)
End If
Next
End Sub
