Get body name API

I'm working on a macro that applies changes to the model (part), and
measures the volume of only one body. I need to get the volume of the
selected solidbody. To select the body, I need to get the name of the
body. But I can't get it. Any suggestions??
Reply to
Loading thread data ...
I used the code below to select a body by passing one of the features used to create the body.
Public Sub SelectBody(Feat As SldWorks.Feature, Append As Boolean, Mark As Long) 'this proceedure assumes that you are attached to sldworks and a doc is opened
Dim FeatFaces As Variant Dim FirstFace As SldWorks.face2 Dim FeatBody As SldWorks.body2
FeatFaces = Feat.GetFaces Set FirstFace = FeatFaces(1) Set FeatBody = FirstFace.GetBody
FeatBody.Select Append, Mark
End Sub
Reply to
Corey Scheich
You don't have to "select" the body in order to measure its volume. Instead you can traverse through all bodies in the model to find the wanted body, and then use MassProperty api to get its volume.
First, use PartDoc::GetBodies2 method to get all bodies in the model. Assuming that the variable objModel contains the active part, you could use the following code..
Dim vntBodies As Variant vntBodies = objModel.GetBodies2(swSolidBody, False)
Then loop through the bodies to find the correct body. What might be confusing is, that the bodies don't have a name, but instead they have "Selection ID". For example, if you'd like to measure a body named "Foo", you could use the following piece of code to loop through the returned bodies...
Dim i As Integer For i = 0 To UBound(vntBodies) Dim objBody As SldWorks.body2 Set objBody = vntBodies(i) If objBody.GetSelectionId = "Foo" Then Exit For Next
Finally you have to obtain the MassPropery object to measure the volume, for example...
'Get the massProperty object... Dim objMassProperty As SldWorks.MassProperty Set objMassProperty = objModel.Extension.CreateMassProperty
'Declare an array for bodies... Dim arrBodies(0 To 0) As SldWorks.body2 Set arrBodies(0) = objBody
With objMassProperty .AddBodies arrBodies 'Output the volume MsgBox "The volume is " & .Volume & " in model units" End With
Hope this helps! More specific information can be found in API help.
Reply to
Heikki Leivo
Thanks for your reply. It's working now.
Thanks JJ
Reply to

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.