Journal to create bounding box values

Good day group,

NX seems to be capable of creating the XYZ values of a parts bounding box and store them in Teamcenter. However, I haven't been able to get those values available in NX. I would like to query/update the XYZ values of the bbox and store them as properties/attributes in the part. Anyone who happens to have such a journal (needs to work in NX10 and later)

Thanks!
Eric

The code below will allow the user to select a solid body, then it will calculate the bounding box dimensions and add them as part attributes. The code was written a number of years ago; it works with NX 10, though it makes use of some functions that are now deprecated.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Module NXJournal

Sub Main

Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = UFSession.GetUFSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display

Dim bbox(5) as double
Dim dblAcc_Value(11) as double
Dim dblMass_Props(46) as double
Dim dblStats(12) as double
dim strOutput as string
dim boundX as double
dim boundY as double
dim boundZ as double

Dim solid1 As Body = SelectSolid()
If solid1 Is Nothing Then
Return
End If

Dim tagList(0) As NXOpen.Tag
tagList(0) = solid1.Tag

'get volume
dblAcc_Value(0)=.999
'AskMassProps3d(in_Tags(),in_num_objs,in_type,in_units,in_density,in_accuracy,in_accuracy_values(),out_mass_props(),out_stats())
ufs.Modl.AskMassProps3d(tagList,1,1,1,.0375,1,dblAcc_Value,dblMass_Props,dblStats)
strOutput = "Surface Area: " & dblMass_Props(0) & vbcrlf
strOutput = strOutput & "Volume: " & dblMass_Props(1) & vbcrlf
strOutput = strOutput & "Mass: " & dblMass_Props(2) & vbcrlf
strOutput = strOutput & "COG: " & dblMass_Props(3) & ", " & dblMass_Props(4) & ", " & dblMass_Props(5) & vbcrlf
strOutput = strOutput & "Density: " & dblMass_Props(46)
' msgbox (strOutput, vbokonly)

'get solid body bounding box extents
ufs.Modl.AskBoundingBox(solid1.Tag,bbox)
' msgbox ("min X: " & bbox(0) & " max X: " & bbox(3) & vbcrlf _
' & "min Y: " & bbox(1) & " max Y: " & bbox(4) & vbcrlf _
' & "min Z: " & bbox(2) & " max Z: " & bbox(5) & vbcrlf & vbcrlf & _
' "X dim: " & bbox(3) - bbox(0) & vbcrlf & _
' "Y dim: " & bbox(4) - bbox(1) & vbcrlf & _
' "Z dim: " & bbox(5) - bbox(2), vbokonly)

boundX = bbox(3) - bbox(0)
boundY = bbox(4) - bbox(1)
boundZ = bbox(5) - bbox(2)

workPart.SetAttribute("Bounding_X", boundX.ToString("0.000"))
workPart.SetAttribute("Bounding_Y", boundY.ToString("0.000"))
workPart.SetAttribute("Bounding_Z", boundZ.ToString("0.000"))

End Sub

'**********************************************************
Public Function SelectSolid() As Body

Dim ui As UI = ui.GetUI
Dim message As String = "Select solid body"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True
Dim selectionAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim selectionMask_array(1) As Selection.MaskTriple
Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

ui.SelectionManager.SelectObject(message, title, scope, _
selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim solid As Body = CType(selectedObject, Body)

If solid Is Nothing Then
Return Nothing
End If

Return solid

End Function
'*******************
End Module

Hello NX JOurnaling,
I tried running the above code and the code has thrown the error message " Unable to cast object of type 'NXOpen.Features.Extrude to type 'NXOpen.Body" for the lines 25 and 90. Also the method workpart.SetAttribute has depreciated. Could you please help me out to fix this.Im using NX 12. Thanks, B

Balaji

In the selection function, change the "includeFeatures" option to "False". This should limit the selection to bodies only, eliminating the cast error.

hi, thanks :):) it worked

Balaji

Almost perfect! Thank you so much! Is it possible to auto-select the work part, so this could be run as a pre-save action?
Kind regards, Eric

The journal operates on the current work part; I assume you want to auto-select the model body. If so, you'll need to supply some logic so the journal can consistently find the body of interest. For example, it might look for a solid body on layer 1, or a body with a given name, or a body with a specific attribute, etc. Also, you'll need to decide what to do if no body, or multiple bodies, match the given criteria.

This is mainly to get an idea of the size (for packaging/transport) of the manufactured part or assembly. So it would be best if the program could select all visible geometry except for datums and coordinate systems.

The .AskBoundingBox method takes a single object as input; it will not accept an array of objects and calculate a box that encompasses them all. If you are working with an assembly, you might be able to create an "envelope" using NX commands (these will probably require an advanced assemblies license).
https://docs.plm.automation.siemens.com/tdoc/nx/1847/nx_help/#uid:xid112...

Alternately, you could create a bounding box for each object then loop through the results to find the min/max values for X, Y, and Z and calculate the total bounding box.