Body Mass in Part
Body Mass in Part
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
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
- AlexLachance
- Posts: 2184
- Joined: Thu Mar 11, 2021 8:14 am
- Location: Quebec
- x 2364
- x 2013
Re: Body Mass in Part
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
Edit: can't attach it so here's some images to show
Re: Body Mass in Part
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.
- AlexLachance
- Posts: 2184
- Joined: Thu Mar 11, 2021 8:14 am
- Location: Quebec
- x 2364
- x 2013
Re: Body Mass in Part
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.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.
Re: Body Mass in Part
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
https://help.solidworks.com/2016/englis ... ties2.html
Re: Body Mass in Part
You can use GetMassProperties Method (IBody2) method.
https://help.solidworks.com/2021/englis ... rties.html
https://help.solidworks.com/2021/englis ... rties.html
Deepak Gupta
SOLIDWORKS Consultant/Blogger
SOLIDWORKS Consultant/Blogger
Re: Body Mass in Part
Here's a C# macro that will print the mass of each body in a multi-body part:
A sample part:
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.
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;
}
}
}
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.
Re: Body Mass in Part
The result I want is exactly this. How it's written in VBA, the AI couldn't convert it.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:A sample part:image.pngCode: 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; } } }
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.
Re: Body Mass in Part
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
Re: Body Mass in Part
Here it is You're awesome. Thank you so much. I've been struggling with this for so long, turns out it's this simpleJSculley 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
- AlexLachance
- Posts: 2184
- Joined: Thu Mar 11, 2021 8:14 am
- Location: Quebec
- x 2364
- x 2013