Visual Basic: Bounding Box of current part

Hello

I found this Journal which returns the bouding box (dimensions) of a part you can select. I tried to modify it so it doesn't need the selecting anymore, but wasn't successful. How can I do this for the current work part?

It doesn't have to work for assemblys.

Edit: I don't need the directions, just the dimensions of the part as information for shipping. For Example: 150x20x5.

Thank you and Regards

A similar question was asked in the thread that you linked to; as stated there, you will need to come up with some logic as to how to find the body of interest programmatically. How is the code going to distinguish the body you want vs. other bodies in the file? When you know that; then you can write code for that condition.

Some examples:
The body of interest has a certain name or attribute.
It is the only solid body on layer 1.
The body belongs to a certain reference set.

The above examples are not fool-proof, so you will also need to determine what to do if multiple bodies (or zero bodies) meet the criteria.

I only have one body in the files. I tried to loop trough all bodys and just pick the first one, but I got an error that the types of the variables didn't match.

Function AskAllBodies(ByVal thePart As Part) As List(Of Body)

Dim theBodies As New List(Of Body)
Try
Dim aBodyTag As Tag = Tag.Null
Do
ufs.Obj.CycleObjsInPart(thePart.Tag, _
UFConstants.UF_solid_type, aBodyTag)
If aBodyTag = Tag.Null Then
Exit Do
End If

Dim theType As Integer, theSubtype As Integer
ufs.Obj.AskTypeAndSubtype(aBodyTag, theType, theSubtype)
If theSubtype = UFConstants.UF_solid_body_subtype Then
theBodies.Add(Utilities.NXObjectManager.Get(aBodyTag))
End If
Loop While True
Catch ex As NXException
lw.WriteLine(ex.ErrorCode & ex.Message)
End Try
Return theBodies
End Function

The error is in german even though NX is set to english, sorry.

Instead of returning theBodies I used the first element of the list as a_body for the other script.
You could also loop trough theBodies, get the dimensions for every body and calculate a box that fits all.
But I don't quite understand how a_body is set.

The "AskAllBodies" function should work to get the bodies in your part file; it returns a list of solid body objects. If your file only has a single solid body, it will be the first (and only) object in the list (theBodies.Item(0)). The .AskBoundingBox method requires the tag of the body (not the body object itself); perhaps this is where your error comes in. Try passing in theBodies.Item(0).Tag.

Alternately, you could iterate through the part's .Bodies collection and grab the first one or use {myPart}.Bodies.ToArray(0) to get the first body.

"But I don't quite understand how a_body is set."

Please clarify this. I don't see a reference to "a_body" in this thread or the one you linked to.

Thank you very much! The .Tag did the trick. For now I will just get the first item, but maybe I'll update it for multiple bodies.

Regards