Files
Code
Module: mMain
'Demonstrates how to get the mass properties of a component at the model level _
and the assembly level.As of SolidWorks 2016 SP3, getting the mass _
properties of selected components at the assembly level is only possible via _
IModelDocExtension::GetMassProperties2, which was introduced in SWX 2016. _
Supposedly the API dev team is going to introduce a new version of _
IMassProperty (or at least its accessor) that will obtain the mass properties _
of selected assembly components only. See SPR 864297 for the status of this.
'main - get mass property data at model level.
'main2 - get mass property data at assembly level.
'Preconditions: component is selected.
'Result: Mass and COG of selected component are printed to the immediate window.
'Notes:
'-IMassProperty allows you to collect more mass property data, specify certain _
bodies, and override values, but it is usally overkill. If you only need _
common mass property values, use the IModelDocExtension methods.
'-When using IMassProperty, if your mass property values are overridden, then _
only obtaining the desired bodies via IPartDoc::GetBodies2 appears to return _
the correct values.
'-The values obtained will be in meters and kg, not necessarily the doc units.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim vMassProps As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObject6(1, -1)
Set swCompModel = swComp.GetModelDoc2
vMassProps = swCompModel.Extension.GetMassProperties(1, Empty)
Debug.Print "Mass: " & Round(vMassProps(5), 6)
Debug.Print "X: " & Round(vMassProps(0), 6)
Debug.Print "Y: " & Round(vMassProps(1), 6)
Debug.Print "Z: " & Round(vMassProps(2), 6)
End Sub
Sub main2()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vMassProps As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
vMassProps = swModel.Extension.GetMassProperties2(1, Empty, True)
Debug.Print "Mass: " & Round(vMassProps(5), 6)
Debug.Print "X: " & Round(vMassProps(0), 6)
Debug.Print "Y: " & Round(vMassProps(1), 6)
Debug.Print "Z: " & Round(vMassProps(2), 6)
End Sub