Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set bounding box dims as Part Attribute wrt optional user-selected CSYS 1

Status
Not open for further replies.

potrero

Mechanical
Aug 30, 2007
516
0
0
US
NX8.0.3 Win7x64

User cowski provided some great VB journals in this old thread: thread561-323165, which allow the setting of bounding box dimensions as Part Attributes. One of the journals he provided allows a user to select their own coordinate system in order to align the bounding box to a part which may not be aligned with the absolute coordinate system. I'm attaching links (provided originally by cowski) to this script (Bounding_box_select_csys.vb) as well as a related simpler script which doesn't allow selection of an arbitrary csys but rather defaults to using the absolute csys (bounding_dims.vb). I'm trying to figure out how to modify the csys-selectable journal as follows:

It'd be great if the code defaulted to selection of the absolute coordinate system, if no specific CSYS was selected or named, but the "OK" button was clicked. If the CSYS selection dialogue box was Canceled, then the function would exit without writing any values (this is the default behavior for the simpler script, even if the OK button is clicked but no csys is selected)

I've tried but can't figure out how to do this. Would appreciate any help!

On a related note, does the absolute CSYS in NX have a Name? And by this I mean even in a part which does not have an actual CSYS "Feature" in the Feature Navigator -- all parts have the concept of Absolute and Work CSYS's, but what are their names -- ie to type into the dialogue box in cowski's code?
 
Replies continue below

Recommended for you

Try the following code:

Code:
'NXJournaling.com
'June 9, 2014
'journal to report bounding box dimensions based on selected solid and selected csys
'dimensions, vector directions, and timestamp will be assigned to part attributes

'for NX 8 and above only

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

Module Module2

    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work

    Sub Main()

        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim displayPart As Part = theSession.Parts.Display
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        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 minCorner(2) As Double
        Dim boxDirections(2, 2) As Double
        Dim boxDistances(2) As Double
        Dim useACS As Boolean = False
        Dim dirX As New Vector3d(1, 0, 0)
        Dim dirY As New Vector3d(0, 1, 0)
        Dim dirZ As New Vector3d(0, 0, 1)

        Dim solid1 As Body
        If SelectSolid("Select solid", solid1) = Selection.Response.Cancel Then
            Return
        End If

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

        Dim myCsys As CoordinateSystem = Nothing
        If SelectCSYS("Select a saved CSYS, 'OK' to use ACS", myCsys) = Selection.Response.Cancel Then
            Exit Sub
        End If

        If IsNothing(myCsys) Then
            useACS = True

            dirX.X = 1
            dirX.Y = 0
            dirX.Z = 0

            dirY.X = 0
            dirY.Y = 1
            dirY.Z = 0

            dirZ.X = 0
            dirZ.Y = 0
            dirZ.Z = 1

        Else
            With myCsys.Orientation.Element

                dirX.X = .Xx
                dirX.Y = .Xy
                dirX.Z = .Xz

                dirY.X = .Yx
                dirY.Y = .Yy
                dirY.Z = .Yz

                dirZ.X = .Zx
                dirZ.Y = .Zy
                dirZ.Z = .Zz

            End With

        End If


        'get volume
        dblAcc_Value(0) = 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, 0.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)

        If useACS Then
            'get solid body bounding box extents aligned to absolute csys
            ufs.Modl.AskBoundingBox(solid1.Tag, bbox)
            boundX = bbox(3) - bbox(0)
            boundY = bbox(4) - bbox(1)
            boundZ = bbox(5) - bbox(2)

        Else
            'get solid body bounding box extents aligned to work csys (pass null tag to use work csys)
            ufs.Modl.AskBoundingBoxAligned(solid1.Tag, myCsys.Tag, expand:=False, min_corner:=minCorner, directions:=boxDirections, distances:=boxDistances)
            boundX = boxDistances(0)
            boundY = boxDistances(1)
            boundZ = boxDistances(2)

        End If

        AttributeLength("Bounds X", boundX)
        AttributeLength("Bounds Y", boundY)
        AttributeLength("Bounds Z", boundZ)

        AttributeDirection("Direction X.X", dirX.X)
        AttributeDirection("Direction X.Y", dirX.Y)
        AttributeDirection("Direction X.Z", dirX.Z)

        AttributeDirection("Direction Y.X", dirY.X)
        AttributeDirection("Direction Y.Y", dirY.Y)
        AttributeDirection("Direction Y.Z", dirY.Z)

        AttributeDirection("Direction Z.X", dirZ.X)
        AttributeDirection("Direction Z.Y", dirZ.Y)
        AttributeDirection("Direction Z.Z", dirZ.Z)

        AttributeTimeStamp()

    End Sub

    '**********************************************************
    Function SelectSolid(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a solid body"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

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

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selObj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    '*******************

    Function SelectCSYS(ByVal prompt As String, ByRef csysObj As CoordinateSystem) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = prompt
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_coordinate_system_type
            .Subtype = UFConstants.UF_all_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         csysObj, cursor)

        If resp = Selection.Response.ObjectSelected OrElse _
            resp = Selection.Response.ObjectSelectedByName OrElse _
            resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    Sub AttributeLength(ByVal theName As String, ByVal theLength As Double)

        Dim objects1(0) As NXObject
        objects1(0) = workPart
        Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
        attributePropertiesBuilder1 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects1, AttributePropertiesBuilder.OperationType.None)

        With attributePropertiesBuilder1

            .IsArray = False
            .DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number
            .SetAttributeObjects(objects1)

            If workPart.PartUnits = BasePart.Units.Inches Then
                .Units = "Inch"
            Else
                .Units = "Millimeter"
            End If

            .Category = "BoundingBox"
            .Title = theName
            .NumberValue = theLength

            Dim nXObject1 As NXObject
            nXObject1 = .Commit()

        End With

        attributePropertiesBuilder1.Destroy()

    End Sub

    Sub AttributeDirection(ByVal theName As String, ByVal theDirection As Double)

        Dim objects6(0) As NXObject
        objects6(0) = workPart
        Dim attributePropertiesBuilder2 As AttributePropertiesBuilder
        attributePropertiesBuilder2 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects6, AttributePropertiesBuilder.OperationType.None)

        With attributePropertiesBuilder2
            .IsArray = False
            .DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number
            .SetAttributeObjects(objects6)
            .Category = "BoundingBox"
            .Title = theName
            .Units = ""
            .NumberValue = theDirection

            Dim nXObject4 As NXObject
            nXObject4 = attributePropertiesBuilder2.Commit()

        End With

        attributePropertiesBuilder2.Destroy()

    End Sub

    Sub AttributeTimeStamp()

        Dim myDateTime As DateTime = Now

        Dim objects11(0) As NXObject
        objects11(0) = workPart
        Dim attributePropertiesBuilder3 As AttributePropertiesBuilder
        attributePropertiesBuilder3 = theSession.AttributeManager.CreateAttributePropertiesBuilder(workPart, objects11, AttributePropertiesBuilder.OperationType.None)
        With attributePropertiesBuilder3
            .IsArray = False
            .DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Date
            .Category = "BoundingBox"
            .Title = "TimeStamp"
            .DateValue.DateItem.Day = myDateTime.Day - 1
            .DateValue.DateItem.Month = myDateTime.Month - 1
            .DateValue.DateItem.Year = myDateTime.Year.ToString
            .DateValue.DateItem.Time = myDateTime.ToString("HH:mm:ss")

            .SetAttributeObjects(objects11)

            Dim nXObject7 As NXObject
            nXObject7 = .Commit()

        End With

        attributePropertiesBuilder3.Destroy()

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

        '----Other unload options-------
        'Unloads the image immediately after execution within NX
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------

    End Function

End Module

www.nxjournaling.com
 
Thank you for the sample code. Unfortunately I'm getting a number of errors:

Line 203: 'AttributeManager' is not a member of 'NXOpen.Session'.
Line 203: 'OperationType' is not a member of 'NXOpen.AttributePropertiesBuilder'.
Line 209: 'SetAttributeObjects' is not a member of 'NXOpen.AttributePropertiesBuilder'.

Lines 235, 240, 262, and 273 have related errors.

Could you advise?
 
I was on NX 8.5 when I tested the code above. There were some changes to the API between 8 and 8.5, the code below should work with NX 8.

Code:
'NXJournaling.com
'June 10, 2014
'journal to report bounding box dimensions based on selected solid and selected csys
'dimensions, vector directions, and timestamp will be assigned to part attributes

'for NX 8 only

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

Module Module3

    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work

    Sub Main()

        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim displayPart As Part = theSession.Parts.Display
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        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 minCorner(2) As Double
        Dim boxDirections(2, 2) As Double
        Dim boxDistances(2) As Double
        Dim useACS As Boolean = False
        Dim dirX As New Vector3d(1, 0, 0)
        Dim dirY As New Vector3d(0, 1, 0)
        Dim dirZ As New Vector3d(0, 0, 1)

        Dim solid1 As Body
        If SelectSolid("Select solid", solid1) = Selection.Response.Cancel Then
            Return
        End If

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

        Dim myCsys As CoordinateSystem = Nothing
        If SelectCSYS("Select a saved CSYS, 'OK' to use ACS", myCsys) = Selection.Response.Cancel Then
            Exit Sub
        End If

        If IsNothing(myCsys) Then
            useACS = True

            dirX.X = 1
            dirX.Y = 0
            dirX.Z = 0

            dirY.X = 0
            dirY.Y = 1
            dirY.Z = 0

            dirZ.X = 0
            dirZ.Y = 0
            dirZ.Z = 1

        Else
            With myCsys.Orientation.Element

                dirX.X = .Xx
                dirX.Y = .Xy
                dirX.Z = .Xz

                dirY.X = .Yx
                dirY.Y = .Yy
                dirY.Z = .Yz

                dirZ.X = .Zx
                dirZ.Y = .Zy
                dirZ.Z = .Zz

            End With

        End If


        'get volume
        dblAcc_Value(0) = 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, 0.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)

        If useACS Then
            'get solid body bounding box extents aligned to absolute csys
            ufs.Modl.AskBoundingBox(solid1.Tag, bbox)
            boundX = bbox(3) - bbox(0)
            boundY = bbox(4) - bbox(1)
            boundZ = bbox(5) - bbox(2)

        Else
            'get solid body bounding box extents aligned to work csys (pass null tag to use work csys)
            ufs.Modl.AskBoundingBoxAligned(solid1.Tag, myCsys.Tag, expand:=False, min_corner:=minCorner, directions:=boxDirections, distances:=boxDistances)
            boundX = boxDistances(0)
            boundY = boxDistances(1)
            boundZ = boxDistances(2)

        End If

        AttributeLength("Bounds X", boundX)
        AttributeLength("Bounds Y", boundY)
        AttributeLength("Bounds Z", boundZ)

        AttributeDirection("Direction X.X", dirX.X)
        AttributeDirection("Direction X.Y", dirX.Y)
        AttributeDirection("Direction X.Z", dirX.Z)

        AttributeDirection("Direction Y.X", dirY.X)
        AttributeDirection("Direction Y.Y", dirY.Y)
        AttributeDirection("Direction Y.Z", dirY.Z)

        AttributeDirection("Direction Z.X", dirZ.X)
        AttributeDirection("Direction Z.Y", dirZ.Y)
        AttributeDirection("Direction Z.Z", dirZ.Z)

        AttributeTimeStamp()

    End Sub

    '**********************************************************
    Function SelectSolid(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = "Select a solid body"
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

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

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selObj, cursor)
        If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    '*******************

    Function SelectCSYS(ByVal prompt As String, ByRef csysObj As CoordinateSystem) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim title As String = prompt
        Dim includeFeatures As Boolean = False
        Dim keepHighlighted As Boolean = False
        Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
        Dim cursor As Point3d
        Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
        Dim selectionMask_array(0) As Selection.MaskTriple

        With selectionMask_array(0)
            .Type = UFConstants.UF_coordinate_system_type
            .Subtype = UFConstants.UF_all_subtype
        End With

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         csysObj, cursor)

        If resp = Selection.Response.ObjectSelected OrElse _
            resp = Selection.Response.ObjectSelectedByName OrElse _
            resp = Selection.Response.Ok Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

    Sub AttributeLength(ByVal theName As String, ByVal theLength As Double)

        Dim objects1(0) As NXObject
        objects1(0) = workPart
        Dim attributePropertiesBuilder1 As AttributePropertiesBuilder
        attributePropertiesBuilder1 = workPart.PropertiesManager.CreateAttributePropertiesBuilder(objects1)

        With attributePropertiesBuilder1
            .ObjectPicker = AttributePropertiesBaseBuilder.ObjectOptions.Object
            .IsArray = False
            .DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number

            If workPart.PartUnits = BasePart.Units.Inches Then
                .Units = "Inch"
            Else
                .Units = "Millimeter"
            End If

            .Category = "BoundingBox"
            .Title = theName
            .NumberValue = theLength

            Dim nXObject1 As NXObject
            nXObject1 = attributePropertiesBuilder1.Commit()

        End With

        attributePropertiesBuilder1.Destroy()

    End Sub

    Sub AttributeDirection(ByVal theName As String, ByVal theDirection As Double)

        Dim objects6(0) As NXObject
        objects6(0) = workPart
        Dim attributePropertiesBuilder2 As AttributePropertiesBuilder
        attributePropertiesBuilder2 = workPart.PropertiesManager.CreateAttributePropertiesBuilder(objects6)

        With attributePropertiesBuilder2
            .IsArray = False
            .DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Number
            .Category = "BoundingBox"
            .Title = theName
            .Units = ""
            .NumberValue = theDirection

            Dim nXObject4 As NXObject
            nXObject4 = attributePropertiesBuilder2.Commit()

        End With

        attributePropertiesBuilder2.Destroy()

    End Sub

    Sub AttributeTimeStamp()

        Dim myDateTime As DateTime = Now

        Dim objects11(0) As NXObject
        objects11(0) = workPart
        Dim attributePropertiesBuilder3 As AttributePropertiesBuilder
        attributePropertiesBuilder3 = workPart.PropertiesManager.CreateAttributePropertiesBuilder(objects11)
        With attributePropertiesBuilder3
            .IsArray = False
            .DataType = AttributePropertiesBaseBuilder.DataTypeOptions.Date
            .Category = "BoundingBox"
            .Title = "TimeStamp"
            .DateValue.DateItem.Day = myDateTime.Day - 1
            .DateValue.DateItem.Month = myDateTime.Month - 1
            .DateValue.DateItem.Year = myDateTime.Year.ToString
            .DateValue.DateItem.Time = myDateTime.ToString("HH:mm:ss")

            Dim nXObject7 As NXObject
            nXObject7 = .Commit()

        End With

        attributePropertiesBuilder3.Destroy()

    End Sub

End Module

www.nxjournaling.com
 
@cowski

Awesome, thanks, that works. I can customize the code from here.

Out of curiosity, how do you handle your development of code like this? Are you actually journaling or do you somehow have an intimate knowledge of the NX API?
 
I'm slowly building my knowledge of the API through the use of the journal recorder. The Attribute subroutines in the journals above started life as a recorded journal. The attribute system got an overhaul in NX 8 and I had not worked much with attributes recently, so I recorded a journal, deleted the stuff that was unnecessary and tweaked it into a subroutine. The journal recorder is a good teacher of how to use the objects that the API makes available; however, it is not very good at writing concise, reusable code for you...

www.nxjournaling.com
 
Status
Not open for further replies.
Back
Top