Get body name API

Discussion in 'SolidWorks' started by JJZ, Apr 16, 2004.

  1. JJZ

    JJZ Guest

    Hello,

    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??


    Thanx
    JJ
     
    JJZ, Apr 16, 2004
    #1
  2. 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
     
    Corey Scheich, Apr 16, 2004
    #2
  3. JJZ

    Heikki Leivo Guest

    Hello,
    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.

    -h-
     
    Heikki Leivo, Apr 16, 2004
    #3
  4. JJZ

    JJZ Guest

    Thanks for your reply. It's working now.

    Thanks
    JJ
     
    JJZ, Apr 19, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.