Hide Table of Contents

Get and Override Mass Properties Example (C#)

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.
//----------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
namespace OverrideMassProperties_CSharp.csproj
{
    
partial class SolidWorksMacro
    {
        
ModelDocExtension Extn;
        
MassProperty MyMassProp;
        
ModelDoc2 swModelDoc;
        
double[] pmoi;
        
double[] vValue;
        
object com;
        
double[] value = new double[3];
        
int errors;
        
int warnings;
        
double val;

        
double[] @params;

        
public void Main()
        {
            swModelDoc = swApp.OpenDoc6(
"C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\samples\\tutorial\\api\\MassPropertiesDemo.sldprt", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            Extn = swModelDoc.Extension;

            
// Create mass properties
            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.0, (int)swInConfigurationOpts_e.swThisConfiguration, null));
            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", (int)swInConfigurationOpts_e.swThisConfiguration, null));
            @params = (
double[])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 = (
double[])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), (int)swInConfigurationOpts_e.swThisConfiguration, null));
            pmoi = (
double[])MyMassProp.PrincipleMomentsOfInertia;
            
Debug.Print("Principal moments of inertia after override: Px: " + pmoi[0] + ", Py: " + pmoi[1] + ", and Pz: " + pmoi[2]);

            vValue = (
double[])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((int)swMomentsOfInertiaReferenceFrame_e.swMomentsOfInertiaReferenceFrame_UserCoordinateSystem, "Coordinate System1", (vValue), (int)swInConfigurationOpts_e.swThisConfiguration, null));
            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]);
        }

        
public SldWorks swApp;
    }

}





 








 



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 and Override Mass Properties Example (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) 2018 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.