Hide Table of Contents

Get Mass Properties Example (VBA)

This example shows how to retrieve the mass properties of selected components in an assembly.

'----------------------------------------------------------------------------
' Preconditions:
' 1. Open an assembly with one or more components.
' 2. Multi-select components for which to get mass properties.
' 3. Open an Immediate window.
'
' Postconditions:
' 1. Gets the mass properties of the selected components
'    in the assembly.
' 2. Inspect the Immediate window for the mass properties of
'    the selected components.
'----------------------------------------------------------------------------

Option Explicit

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

    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 swComp = swSelMgr.GetSelectedObject6(i + 1, -1)
         Debug.Print "  " & swComp.Name2
    Next
   

    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 "Volume = " & vMassProp(3)
        Debug.Print "Surface area = " & vMassProp(4)
        Debug.Print "Mass = " & vMassProp(5)
        Debug.Print "Density = " & vMassProp(5) / vMassProp(3)
        Debug.Print "Moments of inertia taken at the center of mass and aligned with the output coordinate system:"
        Debug.Print "  Lxx = " & vMassProp(6)
        Debug.Print "  Lyy = " & vMassProp(7)
        Debug.Print "  Lzz = " & vMassProp(8)
        Debug.Print "  Lxy = " & vMassProp(9)
        Debug.Print "  Lzx = " & vMassProp(10)
        Debug.Print "  Lyz = " & vMassProp(11)
   

    End If

End Sub



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Mass Properties Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2016 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.