Get and Override Mass Properties Example (VBA)
This example shows how
to get
and override some mass properties of a part.
'-----------------------------------------------------------------------
' Preconditions:
' 1. Ensure the specified document exists.
' 2. Open the Immediate window.
'
' Postconditions: Inspect the Immediate window for the mass properties of
' the part.
'----------------------------------------------------------------------
Dim swApp As SldWorks.SldWorks
Dim Extn As SldWorks.ModelDocExtension
Dim MyMassProp As SldWorks.MassProperty
Dim swModelDoc As SldWorks.ModelDoc2
Dim pmoi As Variant
Dim vValue As Variant
Dim com As Variant
Dim value(2) As Double
Dim errors As Long
Dim warnings As Long
Dim val As Double
Dim params As Variant
Option Explicit
Sub main()
Set swApp = Application.SldWorks
Set swModelDoc = swApp.OpenDoc6("c:\Program
Files\SolidWorks Corp\SolidWorks\samples\tutorial\api\MassPropertiesDemo.sldprt",
swDocPART, swOpenDocOptions_Silent, "", errors, warnings)
Set Extn = swModelDoc.Extension
' Create mass properties
Set MyMassProp = Extn.CreateMassProperty
' Use document property units (MKS)
MyMassProp.UseSystemUnits = False
val = MyMassProp.Volume
Debug.Print "Volume: " & val
val = MyMassProp.Density
Debug.Print "Density: " & val
val = MyMassProp.SurfaceArea
Debug.Print "Surface area: " & val
Debug.Print ""
Debug.Print "OverrideMass property is set to " & MyMassProp.OverrideMass
Debug.Print "Mass is overridden? " & MyMassProp.SetOverrideMassValue(100,
swThisConfiguration, Nothing)
val = MyMassProp.Mass
Debug.Print "Mass: " & val
Debug.Print ""
Debug.Print "OverrideCenterOfMass property
is set to " & MyMassProp.OverrideCenterOfMass
value(0) = 0.09
value(1) = 0.05
value(2) = 0.06
com = value
Debug.Print "Center of mass is overridden? " & MyMassProp.SetOverrideCenterOfMassValue((com),
"Coordinate System1", swThisConfiguration, Nothing)
params = MyMassProp.CenterOfMass
Debug.Print "Center of mass: X: " & params(0) & " , Y: " &
params(1) & ", and Z: " & params(2)
Debug.Print ""
Debug.Print "OverrideMomentsOfInertia
property is set to " & MyMassProp.OverrideMomentsOfInertia
pmoi = MyMassProp.PrincipleMomentsOfInertia
Debug.Print "Principal moments of inertia before override: Px:
" & pmoi(0) & ", Py: " & pmoi(1) & ", and Pz: " & pmoi(2)
pmoi(0) = 0.14
Debug.Print "Principal moments of inertia are overridden? " & MyMassProp.SetOverridePrincipleMomentsOfInertia((pmoi),
swThisConfiguration, Nothing)
pmoi = MyMassProp.PrincipleMomentsOfInertia
Debug.Print "Principal moments of inertia after override: Px:
" & pmoi(0) & ", Py: " & pmoi(1) & ", and Pz: " & pmoi(2)
vValue = MyMassProp.GetMomentOfInertia(0)
Debug.Print "Moments of inertia before override: Lxx: " &
vValue(0) & ", Lxy: " & vValue(1) & ", Lxz: " & vValue(2) & ", Lyx: " &
vValue(3) & ", Lyy: " & vValue(4) & ", Lyz: " & vValue(5) & ", Lzx: " &
vValue(6) & ", Lzy: " & vValue(7) & ", Lzz: " & vValue(8)
Debug.Print "Moments of inertia are overridden? " &
MyMassProp.SetOverrideMomentsOfInertiaValue(swMomentsOfInertiaReferenceFrame_UserCoordinateSystem,
"Coordinate System1", (vValue), swThisConfiguration, Nothing)
vValue(0) = 0.1
Debug.Print "Moments of inertia after override: Lxx: " &
vValue(0) & ", Lxy: " & vValue(1) & ", Lxz: " & vValue(2) & ", Lyx: " &
vValue(3) & ", Lyy: " & vValue(4) & ", Lyz: " & vValue(5) & ", Lzx: " &
vValue(6) & ", Lzy: " & vValue(7) & ", Lzz: " & vValue(8)
End Sub