Hide Table of Contents

Get Mass Properties using IMassProperty (C#)

This example shows how to get the mass of a selected  assembly component.

//---------------------------------------------
// This example shows how to get the mass of a selected
// assembly component.
//
// Preconditions:
// 1. Specified assembly document exists.
// 2. Open the Immediate window.
// 3. Run the macro.
//
// Postconditions: Mass of the selected component printed to
// Immediate window.
//----------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace AddBodiesMassPropertyCSharp.csproj
{

    
partial class SolidWorksMacro
    {

        
public void Main()
        {

            ModelDoc2 swModelDoc2;
            Component2 swComponent;
            SelectionMgr swSelMgr;
            
object bodyInfo = null;
            
object[] bodies = null;
            
double val = 0;
            
double[] parms = null;
            MassProperty swMass;
            
bool boolstatus = false;
            
int errors = 0;
            
int warnings = 0;

            swModelDoc2 = (ModelDoc2)swApp.OpenDoc6(
"C:\\program files\\solidworks corp\\solidworks\\samples\\tutorial\\edraw\\claw\\claw-mechanism.sldasm", (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            boolstatus = swModelDoc2.Extension.SelectByID2(
"collar-1@claw-mechanism", "COMPONENT", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
            swSelMgr = (SelectionMgr)swModelDoc2.SelectionManager;
            swComponent = (Component2)swSelMgr.GetSelectedObject6(1, 0);
            bodies = (
object[])swComponent.GetBodies3((int)swBodyType_e.swAllBodies, out (object)bodyInfo);

            swMass = (MassProperty)swModelDoc2.Extension.CreateMassProperty();

            
// Convert .NET objects to IDispatch by using DispatchWrapper
            // for call to IMassProperty::AddBodies, whose input objects
            // must be marshaled to Dispatch object arrays
            bodyArray = (DispatchWrapper[])ObjectArrayToDispatchWrapperArray(bodies);

            boolstatus = swMass.AddBodies((bodyArray));
            swMass.UseSystemUnits =
false;
            val = swMass.Mass;
            
Debug.Print(" Mass - " + val);
            val = swMass.Volume;
            
Debug.Print(" Volume - " + val);
            val = swMass.Density;
            
Debug.Print(" Density - " + val);
            val = swMass.SurfaceArea;
            
Debug.Print(" Surface area - " + val);
            parms = (
double[])swMass.CenterOfMass;


            
Debug.Print(" Center of mass - X: " + parms[0] + " ,Y: " + parms[1] + " ,and Z: " + parms[2]);
        }

        
public DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] SwObjects)
        {
            
int arraySize = 0;
            arraySize = SwObjects.GetUpperBound(0);
            
DispatchWrapper[] dispwrap = new DispatchWrapper[arraySize + 1];
            
int arrayIndex = 0;
            
for (arrayIndex = 0; arrayIndex <= arraySize; arrayIndex++)
            {
                dispwrap[arrayIndex] =
new DispatchWrapper(SwObjects[arrayIndex]);
            }
            
return dispwrap;
        }


        
/// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
        
public DispatchWrapper
[] bodyArray;


    }
}




 








 



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 using IMassProperty (C#)
*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) 2010 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.