Body Mass in Part

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
senuba
Posts: 11
Joined: Thu Oct 26, 2023 1:37 am
Answers: 0
x 13
x 1

Body Mass in Part

Unread post by senuba »

Hello,

I have a body in part where I created separate bodies without using "merge the end" while extruding on a part. The weight of this part is 45kg. However, it consists of 3 different bodies weighing 10kg+15kg+20kg. How can I obtain these masses with a macro?

I can retrieve the body names using this link. Now, I need the masses. Thank you in advance for your help.

https://help.solidworks.com/2022/englis ... ple_vb.htm
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Body Mass in Part

Unread post by AlexLachance »

I could provide a note that links to the property of the body within the view it is attached to. This is how our multibody weights are displayed

Edit: can't attach it so here's some images to show
image.png
image.png
senuba
Posts: 11
Joined: Thu Oct 26, 2023 1:37 am
Answers: 0
x 13
x 1

Re: Body Mass in Part

Unread post by senuba »

AlexLachance wrote: Tue Feb 27, 2024 7:47 am I could provide a note that links to the property of the body within the view it is attached to. This is how our multibody weights are displayed

Edit: can't attach it so here's some images to show

image.png
image.png

I don't want the technical drawing part. When I run the macro within the part, it will provide me with the weight of "Pah5" using debug.print, which is 258kg. I want the weights of "Pah5" and the others. I can get all the names here, but I can't get their weights.
Attachments
2.JPG
1.JPG
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Body Mass in Part

Unread post by AlexLachance »

senuba wrote: Tue Feb 27, 2024 8:42 am I don't want the technical drawing part. When I run the macro within the part, it will provide me with the weight of "Pah5" using debug.print, which is 258kg. I want the weights of "Pah5" and the others. I can get all the names here, but I can't get their weights.
Unfortunately I can't really help macro-wise. Perhaps @gupta9665 could be of help. I don't think it's complicated since the evaluate tool can call them out.
User avatar
AlexB
Posts: 436
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 22
x 242
x 384

Re: Body Mass in Part

Unread post by AlexB »

I think this is what you're looking for. You would have to pre-select the components you would like measured before calling this method.

https://help.solidworks.com/2016/englis ... ties2.html
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: Body Mass in Part

Unread post by gupta9665 »

You can use GetMassProperties Method (IBody2) method.

https://help.solidworks.com/2021/englis ... rties.html
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
JSculley
Posts: 577
Joined: Tue May 04, 2021 7:28 am
Answers: 54
x 7
x 809

Re: Body Mass in Part

Unread post by JSculley »

Here's a C# macro that will print the mass of each body in a multi-body part:

Code: Select all

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace BodyWeights.csproj
{
    public partial class SolidWorksMacro
    {
        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;      

        public void Main()
        {
            ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2;
            ModelDocExtension mExt = mDoc.Extension;
            MassProperty massProp = mExt.CreateMassProperty() as MassProperty;
            massProp.UseSystemUnits = false;
            FeatureManager fMgr = mDoc.FeatureManager;
            object[] featObjArray = fMgr.GetFeatures(false) as object[];
            foreach (object o in featObjArray)
            {
                Feature nextFeat = o as Feature;
                if (!nextFeat.GetTypeName2().Equals("SolidBodyFolder"))
                {
                    continue;
                }
                BodyFolder bFolder = nextFeat.GetSpecificFeature2() as BodyFolder;
                object[] bodyObjArray = bFolder.GetBodies() as object[];
                foreach (object b in bodyObjArray)
                {
                    Body2 nextBody = b as Body2;
                    object[] vBodies = new object[1];
                    vBodies[0] = nextBody;
                    DispatchWrapper[] dispArray = ObjectArrayToDispatchWrapperArray(vBodies);
                    massProp.AddBodies((dispArray));
                    double bodyMass = massProp.Mass;
                    Debug.Print("Body: " + nextBody.Name + " Mass: " + bodyMass);                  
                }
                break;
            }         
        }

        public DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] Objects)
        {
            int ArraySize = 0;
            ArraySize = Objects.GetUpperBound(0);
            DispatchWrapper[] d = new DispatchWrapper[ArraySize + 1];
            int ArrayIndex = 0;
            for (ArrayIndex = 0; ArrayIndex <= ArraySize; ArrayIndex++)
            {
                d[ArrayIndex] = new DispatchWrapper(Objects[ArrayIndex]);
            }
            return d;
        }        
    }
}
A sample part:
image.png

The results:

Body: Tube (square) TS2X2X0.1875(1) Mass: 1.08582871900086
Body: Mirror2[2] Mass: 0.0649513950420028
Body: 9/16 (0.5625) Diameter Hole1[1] Mass: 0.0649513950420025
Body: Mirror2[1] Mass: 0.0649513950420026
Body: 9/16 (0.5625) Diameter Hole1[2] Mass: 0.064951395042003

Basically, you use the FeatureManager object to get the BodyFolder for the solid bodies in the model and then loop through each body in the folder passing it to the AddBodies method of the MassProperty object.
senuba
Posts: 11
Joined: Thu Oct 26, 2023 1:37 am
Answers: 0
x 13
x 1

Re: Body Mass in Part

Unread post by senuba »

JSculley wrote: Tue Feb 27, 2024 11:38 am Here's a C# macro that will print the mass of each body in a multi-body part:

Code: Select all

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;

namespace BodyWeights.csproj
{
    public partial class SolidWorksMacro
    {
        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;      

        public void Main()
        {
            ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2;
            ModelDocExtension mExt = mDoc.Extension;
            MassProperty massProp = mExt.CreateMassProperty() as MassProperty;
            massProp.UseSystemUnits = false;
            FeatureManager fMgr = mDoc.FeatureManager;
            object[] featObjArray = fMgr.GetFeatures(false) as object[];
            foreach (object o in featObjArray)
            {
                Feature nextFeat = o as Feature;
                if (!nextFeat.GetTypeName2().Equals("SolidBodyFolder"))
                {
                    continue;
                }
                BodyFolder bFolder = nextFeat.GetSpecificFeature2() as BodyFolder;
                object[] bodyObjArray = bFolder.GetBodies() as object[];
                foreach (object b in bodyObjArray)
                {
                    Body2 nextBody = b as Body2;
                    object[] vBodies = new object[1];
                    vBodies[0] = nextBody;
                    DispatchWrapper[] dispArray = ObjectArrayToDispatchWrapperArray(vBodies);
                    massProp.AddBodies((dispArray));
                    double bodyMass = massProp.Mass;
                    Debug.Print("Body: " + nextBody.Name + " Mass: " + bodyMass);                  
                }
                break;
            }         
        }

        public DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] Objects)
        {
            int ArraySize = 0;
            ArraySize = Objects.GetUpperBound(0);
            DispatchWrapper[] d = new DispatchWrapper[ArraySize + 1];
            int ArrayIndex = 0;
            for (ArrayIndex = 0; ArrayIndex <= ArraySize; ArrayIndex++)
            {
                d[ArrayIndex] = new DispatchWrapper(Objects[ArrayIndex]);
            }
            return d;
        }        
    }
}
A sample part:image.png


The results:

Body: Tube (square) TS2X2X0.1875(1) Mass: 1.08582871900086
Body: Mirror2[2] Mass: 0.0649513950420028
Body: 9/16 (0.5625) Diameter Hole1[1] Mass: 0.0649513950420025
Body: Mirror2[1] Mass: 0.0649513950420026
Body: 9/16 (0.5625) Diameter Hole1[2] Mass: 0.064951395042003

Basically, you use the FeatureManager object to get the BodyFolder for the solid bodies in the model and then loop through each body in the folder passing it to the AddBodies method of the MassProperty object.
The result I want is exactly this. How it's written in VBA, the AI couldn't convert it.
User avatar
JSculley
Posts: 577
Joined: Tue May 04, 2021 7:28 am
Answers: 54
x 7
x 809

Re: Body Mass in Part

Unread post by JSculley »

Here's a VBA version, using PartDoc::GetBodies instead of traversing the feature tree, which shortens the code quite a bit:

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks

Sub main()
    Dim mDoc As ModelDoc2
    Dim pDoc As PartDoc
    Dim mExt As ModelDocExtension
    Dim massProp As MassProperty
    Dim vBodies As Variant
    Dim i As Integer
    Dim bodyMass As Double
    Set swApp = Application.SldWorks
    Set mDoc = swApp.ActiveDoc
    Set pDoc = mDoc
    Set mExt = mDoc.Extension
    Set massProp = mExt.CreateMassProperty
    massProp.UseSystemUnits = False
    vBodies = pDoc.GetBodies2(swBodyType_e.swSolidBody, False)
    For i = 0 To UBound(vBodies)
        Dim nextBod(0) As Body2
        Set nextBod(0) = vBodies(i)
        massProp.AddBodies (nextBod)
        bodyMass = massProp.Mass
        Debug.Print ("Body: " & nextBod(0).Name & " Mass: " & bodyMass)
    Next i
End Sub

senuba
Posts: 11
Joined: Thu Oct 26, 2023 1:37 am
Answers: 0
x 13
x 1

Re: Body Mass in Part

Unread post by senuba »

JSculley wrote: Wed Feb 28, 2024 9:24 am Here's a VBA version, using PartDoc::GetBodies instead of traversing the feature tree, which shortens the code quite a bit:

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks

Sub main()
    Dim mDoc As ModelDoc2
    Dim pDoc As PartDoc
    Dim mExt As ModelDocExtension
    Dim massProp As MassProperty
    Dim vBodies As Variant
    Dim i As Integer
    Dim bodyMass As Double
    Set swApp = Application.SldWorks
    Set mDoc = swApp.ActiveDoc
    Set pDoc = mDoc
    Set mExt = mDoc.Extension
    Set massProp = mExt.CreateMassProperty
    massProp.UseSystemUnits = False
    vBodies = pDoc.GetBodies2(swBodyType_e.swSolidBody, False)
    For i = 0 To UBound(vBodies)
        Dim nextBod(0) As Body2
        Set nextBod(0) = vBodies(i)
        massProp.AddBodies (nextBod)
        bodyMass = massProp.Mass
        Debug.Print ("Body: " & nextBod(0).Name & " Mass: " & bodyMass)
    Next i
End Sub

Here it is :) You're awesome. Thank you so much. I've been struggling with this for so long, turns out it's this simple :)
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Body Mass in Part

Unread post by AlexLachance »

senuba wrote: Thu Feb 29, 2024 12:13 am Here it is :) You're awesome. Thank you so much. I've been struggling with this for so long, turns out it's this simple :)
If you ever have any other struggles, don't be shy to come here and ask. The community is great in helping each other.
Post Reply