Cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the selected body

Creator
Creator

I have a step file which contains multiple bodies and I would like to get the body that I selected. In this case, it is Body(7).  I used the record Journal feature but it does not capture the selection event - please help.

 

Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String) 

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

Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

 

 

2017-06-01_18-01-13.png

10 REPLIES

Re: How to get the selected body

Siemens Phenom Siemens Phenom
Siemens Phenom

 

Do you want to pick the body while your journal is running, or do you want to use a body that is already selected?

 

If you want to pick it in your journal, add in a function like this (from the GTAC examples):

 

    Function select_a_body(ByVal prompt As String) As Body
        Dim mask() As Selection.MaskTriple = {New Selection.MaskTriple( _
            UF.UFConstants.UF_solid_type, 0, UF.UFConstants.UF_UI_SEL_FEATURE_BODY)}
        Dim cursor As Point3d = Nothing
        Dim obj As TaggedObject = Nothing

        Dim resp As Selection.Response = _
            UI.GetUI().SelectionManager.SelectTaggedObject("Select a body", prompt, _
            Selection.SelectionScope.AnyInAssembly, _
            Selection.SelectionAction.ClearAndEnableSpecific, _
            False, False, mask, obj, cursor)

        Return obj
    End Function

 

And call it like this:

 

Dim theBody as Body = select_a_body("Pick the body:")

Re: How to get the selected body

Honored Contributor
Honored Contributor

The screenshot in your post shows a body feature selected (not a body object); it is a technicality, but an important one when programming with the NXOpen API. If you want to work with pre-selected objects (objects that are selected before your code starts), the journal below shows one way of doing it. Note - the code only looks for body objects and body (Brep) features; it could be made to work with other features that create bodies (such as extrude, revolve, etc), but this simple example does not.

 

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()

    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJ")

        lw.Open()

        Dim preSelBodies As New List(Of Body)
        Dim selobj As NXObject

        Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()
        For inx As Integer = 0 To numsel - 1
            selobj = theUI.SelectionManager.GetSelectedTaggedObject(inx)

            lw.WriteLine("object type: " & selobj.GetType.ToString)

            If TypeOf (selobj) Is Body Then
                preSelBodies.Add(selobj)
            End If

            If TypeOf (selobj) Is Features.Brep Then
                Dim featBrep As Features.Brep = selobj
                For Each tempBody As Body In featBrep.GetBodies
                    preSelBodies.Add(tempBody)
                Next
            End If

        Next

        lw.WriteLine("Bodies collected: " & preSelBodies.Count.ToString)

        lw.Close()

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

    End Function

End Module

Re: How to get the selected body

Creator
Creator

I am not a developr/CAD designer so many terms I may not understand them 100% precisely. And it is great that you provided great explanation.

 

Just a quick question, I tried to get the selected object name as showned in Journal record when I hided it 

UNPARAMETERIZED_FEATURE(7)

but when I tried to use .name, it does not show anything

 

Dim objects1(1) As NXOpen.DisplayableObject
Dim body1 As NXOpen.Body = CType(workPart.Bodies.FindObject("UNPARAMETERIZED_FEATURE(7)"), NXOpen.Body)

 

 

 

 

Re: How to get the selected body

Honored Contributor
Honored Contributor

Assuming that UNPARAMETERIZED_FEATURE(7) is the journal identifier of a body object in the workPart, the body1 variable will now reference it. The code that you have posted won't show anything different on the screen; what were you expecting/wanting to happen?

Re: How to get the selected body

Siemens Phenom Siemens Phenom
Siemens Phenom

But if UNPARAMETERIZED_FEATURE(7) is actually a feature, and not a body, which seems likely, then it will not be found with the code shown,  because it is not the right kind of object. 

Re: How to get the selected body

Honored Contributor
Honored Contributor

@SteveLabout,

I ran a quick test and it looks like the journal identifier of the body is the same as the journal identifier of the Brep feature (NX 9). I was a bit surprised.

 

preselect.PNG

 

@Secret,

Note that the journal identifier property is separate and distinct from the name property of an object. The journal identifier is assigned by NX to each object; the name can be assigned by NX or the user. For many objects, the name property will initally be empty. NX will assign names for some objects, sketch curves being one example.

Re: How to get the selected body

Creator
Creator

It is not important but I just would like to understand/know how to get the value 

 UNPARAMETERIZED_FEATURE(7)

like NX does with the FindObject

 

CType(workPart.Bodies.FindObject("UNPARAMETERIZED_FEATURE(7)"), NXOpen.Body)

Re: How to get the selected body

Siemens Phenom Siemens Phenom
Siemens Phenom

You can get it as shown below - just ask for the JournalIdentifier on the Body object.

 

Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.Features

Module report_journal_identifiers

    Dim theSession As Session = Session.GetSession()
    Dim theUFSession As UFSession = UFSession.GetUFSession()

    Sub Main()

        Dim dp As Part = theSession.Parts.Display
        Dim theBodies() As Body = dp.Bodies.ToArray()
        Dim theFeatures() As Feature = dp.Features.ToArray()

        Echo("Journal Identifiers for the Body objects:")
        For Each thisBody As Body In theBodies
            Echo(thisBody.JournalIdentifier())
        Next
        Echo("=========================================")

        Echo("Journal Identifiers for the Feature objects:")
        For Each thisFeat As Feature In theFeatures
            Echo(thisFeat.JournalIdentifier())
        Next

    End Sub

    Sub Echo(ByVal output As String)

        theSession.ListingWindow.Open()
        theSession.ListingWindow.WriteLine(output)
        theSession.LogFile.WriteLine(output)

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        Return Session.LibraryUnloadOption.Immediately
    End Function

End Module

Re: How to get the selected body

Esteemed Contributor
Esteemed Contributor

Don't waste your time on journal identifiers, they are only valid for recoded journals and are not portable.

The number in braces is the creation index of the feature and is nowhere used except for the display in the PNT.

If you don't know NX by heart, it will be very hard and frustrating to use the NXOpen API, since there needs to be a fundamental understanding of how you do things interactively to be able to create automation code.

If you need to find objects, find them by attribute anything else can be change within the fraction of a second and your code will fail.

Colors and names are not reliable, users can easily change them. Attributes can be locked, so you have a bit of a secure way to keep users from changing them.

Stefan Pendl, Systemmanager CAx, HAIDLMAIR GmbH
Production: NX10.0.3, VERICUT 8.0, FBM, MRL 3.1.4 | TcUA 10.1 MP7 Patch 0 (10.1.7.0) | TcVis 10.1
Development: VB.NET, Tcl/Tk    Testing: NX12.0

How to Get the Most from Your Signature in the Community