Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

VB code for NX7.5 to Select Entities 3

Status
Not open for further replies.

NXProf

Mechanical
Nov 28, 2012
45
Hello,

Does anyone know VB code using NX7.5 to select entities with a filter selection set and grab only certain entities [i.e. - notes, a view’s attribute text (the scale, view name, etc.), lines, etc.] on a sheet within the Drafting module? And if so, how would you then select each entity within the overall selection set?

Thank you for your help!
 
Replies continue below

Recommended for you

Below is example code for two specific conditions. For the first selection I have used the selection mask to include all types of curves (including body edges). Just remove any masks not required. For this selection I have set the selection scope as Selection.SelectionScope.AnyInAssembly. To select on a drafting sheet you need to set the cursor view ufs.Ui.SetCursorView(0). For the second selection I have shown the mask to notes and labels. In this case I have set the selection scope as Selection.SelectionScope.WorkPart. In this case you do not have to set the cursor view. Hope this helps.

Code:
Option Strict Off
Imports System
Imports System.Environment
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF
Imports NXOpen.Annotations
Imports NXOpen.Utilities


Module NXSelection
    Dim s As Session = Session.GetSession()
    Dim ui As UI = ui.GetUI()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Sub Main()
        Dim edge1 As NXObject = Nothing
        Dim obj As NXObject = Nothing
        Dim cursor As Point3d = Nothing
        Dim response1 As Selection.Response = Selection.Response.Cancel
        response1 = selectDraftingEdgeorCurve("Select a Curve", edge1, cursor)
        If response1 = Selection.Response.Cancel Then GoTo trynote1
trynote1:
        response1 = SelectNoteorLabel(obj, cursor)
        If response1 = Selection.Response.Cancel Then GoTo end1

end1:


    End Sub
    Function selectDraftingEdgeorCurve(ByVal prompt As String, ByRef obj As NXObject, _
                                   ByRef cursor As Point3d) As Selection.Response
        ufs.Ui.SetCursorView(0)
        Dim mask(8) As Selection.MaskTriple
        mask(0).Type = UFConstants.UF_solid_type
        mask(0).Subtype = UFConstants.UF_solid_body_subtype
        mask(0).SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE
        mask(1).Type = UFConstants.UF_line_type
        mask(1).Subtype = 0
        mask(1).SolidBodySubtype = 0
        mask(2).Type = UFConstants.UF_circle_type
        mask(2).Subtype = 0
        mask(2).SolidBodySubtype = 0
        mask(3).Type = UFConstants.UF_conic_type
        mask(3).Subtype = 0
        mask(3).SolidBodySubtype = 0
        mask(4).Type = UFConstants.UF_spline_type
        mask(4).Subtype = 0
        mask(4).SolidBodySubtype = 0
        mask(5).Type = UFConstants.UF_solid_silhouette_type
        mask(5).Subtype = 0
        mask(5).SolidBodySubtype = 0
        mask(6).Type = UFConstants.UF_section_edge_type
        mask(6).Subtype = 0
        mask(6).SolidBodySubtype = 0
        mask(7).Type = UFConstants.UF_section_line_type
        mask(7).Subtype = 0
        mask(7).SolidBodySubtype = 0
        mask(8).Type = UFConstants.UF_section_segment_type
        mask(8).Subtype = 0
        mask(8).SolidBodySubtype = 0
        Dim resp As Selection.Response = _
    ui.SelectionManager.SelectObject(prompt, prompt, _
        Selection.SelectionScope.AnyInAssembly, _
        Selection.SelectionAction.ClearAndEnableSpecific, _
        False, False, mask, obj, cursor)
        If resp = Selection.Response.ObjectSelected Or _
           resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
    End Function
    Public Function SelectNoteorLabel(ByRef obj As NXObject, _
                                   ByRef cursor As Point3d) As Selection.Response
        Dim mask(1) As Selection.MaskTriple
        mask(0).Type = NXOpen.UF.UFConstants.UF_drafting_entity_type
        mask(0).Subtype = NXOpen.UF.UFConstants.UF_draft_note_subtype
        mask(1).Type = NXOpen.UF.UFConstants.UF_drafting_entity_type
        mask(1).Subtype = NXOpen.UF.UFConstants.UF_draft_label_subtype
        Dim resp As Selection.Response = ui.SelectionManager.SelectObject("Select Note or Label", _
                                        "Select Note or Label", Selection.SelectionScope.WorkPart, _
                                         Selection.SelectionAction.ClearAndEnableSpecific, False, _
                                         False, mask, obj, cursor)
        If resp = Selection.Response.ObjectSelected Or _
           resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
    End Function
    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
 
Thank you Frank!

Wow! This is more than what I was hoping for. I'll have to try this when returning to work tomorrow after the Memorial Holiday.

Sorry for the additional questions prior to testing out this program at work, but I couldn't wait.

It appears this code is designed to have the user select with the mouse at response1 = selectDraftingEdgeorCurve("Select a Curve", edge1, cursor) and at "Select Note or Label". How would you let the program simply select objects automatically without user input? And once the desired objects are selected, I assume a loop would be performed to select each individual object to obtain its coordinates, attribute info, etc.; but how would you grab each object to strip out such info?

Thank you again for your help!
 
NX has a large number of collection classes. The program below shows using collection classes for dimensions and notes. Now it happens that for NX7.5 there is no collection class for labels so I have included code which searches through the database and finds labels. The label collection class is available in NX8.5. I don't keep the reference documentation for earlier NX releases. It may have been introduced in NX8.

Once you have the collection you certainly check for drafting objects 1) if they are on the sheet of interest and 2) if the object's position lies within the rectangle that I think you are asking about.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module NXCollection

    Dim s As Session = Session.GetSession()
    Dim dp As Part = s.Parts.Display
    Dim ufs As UFSession = UFSession.GetUFSession()

    Sub Main()
        ' For dimensions
        Dim dims() As Annotations.Dimension = dp.Dimensions.ToArray
        For Each dim1 As Dimension In dims
            ' do something with dimensions
        Next
        ' For notes
        Dim nc As NoteCollection = dp.Notes
        For Each aNote As Note In nc
            ' do something with notes
        Next
        ' For labels
        '   Dim lbs As LabelCollection = dp.Labels 
        ' This is not available in NX7.5 Therefore do a search
        Dim NULL_TAG As NXOpen.Tag = NXOpen.Tag.Null
        Dim obj As NXOpen.Tag = NULL_TAG
        Do
            obj = ask_next_drf_entity(obj)
            If obj = NULL_TAG Then
                GoTo end1
            End If
            ' Check whether returned Tag is UF_draft_label_subtype
            Dim type As Integer = Nothing
            Dim subtype As Integer = Nothing
            ufs.Obj.AskTypeAndSubtype(obj, type, subtype)
            If subtype <> 2 Then
                Continue Do
            Else
                ' we have a tag for a label
            End If
            ' Alternatively we can check that the returned NXObject is a Label
            Dim nxobj As NXObject = NXObjectManager.Get(obj)
            If nxobj.GetType().ToString() <> "NXOpen.Annotations.Label" Then
                Continue Do
            Else
                ' do something with the label
            End If
        Loop Until obj = NULL_TAG
end1:
    End Sub

    Public Function ask_next_drf_entity(ByRef obj As NXOpen.Tag) As NXOpen.Tag
        Dim part As NXOpen.Tag = dp.Tag
        ufs.Obj.CycleObjsInPart(part, UFConstants.UF_drafting_entity_type, obj)
        Return obj
    End Function

    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

Frank Swinkels
 
Thank you Frank for your response!

Sorry if I am not too clear. I've attached an example of the kinds of objects (residing on numerous sheets). In the attached jpg, it has 1) find numbers, 2) flag notes, 3) a label for the section with the sheet zone & sheet # underneath, and 4) a Sheet Zone label, which is part of a large sheet zone grid.

What I ultimately would like to do is to (1) extract the find numbers, flag numbers, and sections' (x, y) coordinate information as well as the objects' text properties (i.e. - <%T25> for flag note 25) in order to define which sheet zone each object resides within and then (2) have an output to the user showing the sheet zone information of all such objects.

I'm pretty sure I can get the second part of this program since I know how to flip through each sheet and sort information in VB. I'm just stumped over getting the first part of extracting the coordinate information and description of each object.

Thank you again for your help, which is quite overwhelming!
 
 http://files.engineering.com/getfile.aspx?folder=349b205b-d4d9-43db-88fb-d978e6a606f5&file=Example_of_labels_and_sheet-zones.jpg
Below is a further small journal which gets notes origin and note text. The same commands are available for labels. The commands for the origin is origin1 = a_note.AnnotationOrigin with a_note being the note object and origin1 is a Point3d. The text is obtained using notestring = a_note.GetText(). Note that notestring must be dimensioned using Dim notestring() As String because notes can have multiple lines of text.

I see no difficulties to seach for specific notes and then use the note origin to define the region for finding other notes and labels. By the way if, as I think, your example also has ID symbols these need to be treated a little different.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module DrgObjPosition

    Dim s As Session = Session.GetSession()
    Dim dp As Part = s.Parts.Display
    Dim lw As ListingWindow = s.ListingWindow
    Dim ufs As UFSession = UFSession.GetUFSession()

    Sub Main()
        Dim nc As NoteCollection = dp.Notes
        ' for notes get the text and the note location
        Dim notestring() As String
        Dim nolines As Integer = 0
        Dim origin1 As Point3d = Nothing
        lw.Open()
        For Each a_note As Note In nc
            notestring = a_note.GetText()
            nolines = notestring.Length
            If nolines = 1 Then ' notes can have multiple lines
                origin1 = a_note.AnnotationOrigin
                lw.WriteLine("Note Text: " & notestring(0) & "  Note Origin: " & origin1.X.ToString _
                             & "  ,  " & origin1.Y.ToString)
            End If
        Next
    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

Frank Swinkels
 
Thank you very much Frank!!! Wow!!! Awesome!!!

This was exactly what I was searching for. I cannot thank you enough!

Do you know if there is a book indicating all of the collection classes and their subsets? I might be able to figure out more of the NX code if I had a better understanding of NX's classification system of all objects.

After attempting to understand NX collection classes, I found one bit of code that I will have to see what it does, which is as follows:
"Dim idcol As Annotations.IdSymbolCollection = theSession.Parts.Work.Annotations.IdSymbols
Dim ids As Annotations.IdSymbol"
I'll have to keep in mind that this was extracted out of NX8 code and may not work for NX7.5. Although, we should be obtaining an NX upgrade in 3 - 6 months at most.

A couple of other items I'll work on will be 1) obtaining the sheet size dimensions, which will affect the sheet-zoning locations of all objects, and 2) other things like determining if a find number callout has more than 1 arrow attached to multiple identical parts.

Thank you again Frank!!! You've really helped me greatly!!!
 
Assuming you have the full help system installed, go to Open for .NET -> NX Open for .NET Reference Guide is the go to document for the NX object model, it lists all the objects, properties, and methods available.

I had a similar question recently about sheet sizes, here is some quick example code:

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

	Sub Main()

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

		Dim shtLength As Double
		Dim shtHeight As Double

		lw.Open()
		lw.WriteLine("number of sheets in part: " & workPart.DrawingSheets.ToArray.Length.ToString)
		lw.WriteLine("")
		For Each dwgSheet As Drawings.DrawingSheet In workPart.DrawingSheets
			lw.WriteLine("sheet name: " & dwgSheet.Name)
			lw.WriteLine("sheet units: " & dwgSheet.Units.ToString)

			'assign sheet properties to variables
			shtLength = dwgSheet.Length
			shtHeight = dwgSheet.Height

			'do something with the values
			lw.WriteLine("sheet length: " & shtLength.ToString & " " & dwgSheet.Units.ToString)
			lw.WriteLine("sheet height: " & shtHeight.ToString & " " & dwgSheet.Units.ToString)

			lw.WriteLine("number of views on sheet: " & dwgSheet.SheetDraftingViews.ToArray.Length)
			For Each shtView As Drawings.DraftingView In dwgSheet.SheetDraftingViews
				lw.WriteLine("  view: " & shtView.Name)
			Next
			lw.WriteLine("")
		Next


	End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

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

    End Function

End Module

The above code will report each drawing sheet's name, size, and the views it contains to the information window for the current work part.

www.nxjournaling.com
 
Thank you also cowski!

I was able to locate the NX Open for .NET Reference Guide (69.4mb or 71,074kb) and scanned though dozens of the objects, properties, and methods syntax for NX’s VB, which is very helpful. I’m assuming it has thousands of sub-classifications for the collection classes, which is a bit overwhelming; but achievable. I’ll just have to speed-up my learning process.

Thank you also for the code for obtaining the sheet sizes, which will help me to identify which sheet zone each of the find number notes and labels are within! But I just found another location I’ll have to have the final sheet-zone program to search, which is the flag note callouts (i.e. - <%T14>) located within a dimension when 1) clicking on a dimension (any type as indicated in attached jpg) and 2) hitting the up, down, left, or right arrow keys to insert the flag note callout. My guess is that it is somewhere in the Dimension annotation of the collection classes.

Thank you and Frank very much for all of your outstanding help!!!
 
 http://files.engineering.com/getfile.aspx?folder=7dc76849-7eba-420f-994a-d3a6e1e2146d&file=DimensionStyles.jpg
To get appended text applied to dimensions look at the small example code.

Code:
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module AppendedText

    Dim s As Session = Session.GetSession()
    Dim dp As Part = s.Parts.Display
    Dim lw As ListingWindow = s.ListingWindow

    Sub Main()
        Dim dims() As Annotations.Dimension = dp.Dimensions.ToArray
        lw.Open()
        For Each dim1 As Dimension In dims
            Dim appendedtext1 As Annotations.AppendedText = dim1.GetAppendedText()
            Dim aftertext() As String = appendedtext1.GetAfterText
            Dim beforetext() As String = appendedtext1.GetBeforeText
            Dim belowtext() As String = appendedtext1.GetBelowText
            Dim abovetext() As String = appendedtext1.GetAboveText
            If aftertext.Length > 0 Then
                lw.WriteLine("AfterText: " & aftertext(0))
            ElseIf beforetext.Length > 0 Then
                lw.WriteLine("BeforeText: " & beforetext(0))
            ElseIf belowtext.Length > 0 Then
                lw.WriteLine("BelowText: " & belowtext(0))
            ElseIf abovetext.Length > 0 Then
                lw.WriteLine("AboveText: " & abovetext(0))
            End If
        Next

    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

To get ID symbol origin and ID symbol text use the following code snippet

Code:
Dim idsb As IdSymbolBuilder = idCol.CreateIdSymbolBuilder(idsymbol)
id_textupper = idsb.UpperText
symorigin = idsb.Origin.OriginPoint

Regards

Frank

 
Thank you once again Frank for your outstanding help!

I had a chance to run the example code above, which was perfect for the additional text objects residing above, below, left, and right of the dimensions! I had to take off of work a little early today. But when I return Monday, I'll attempt incorporating the 3 additional lines of code after some modification and let you know how that runs.

I cannot thank you enough!

Best regards!

P.S. By the way, would you happen to know the code for deleting objects or select an object (i.e. - text) for testing, in which a program allows the user to select objects within a rectangular area from (x1, y1) to (x2,y2)? [URL unfurl="true"]http://www.eng-tips.com/viewthread.cfm?qid=345650[/url]
 
Here is the example code for selecting by rectangle. The mask is set for notes and labels only.

Code:
Option Strict Off
Imports System
Imports System.Environment
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.UF
Imports NXOpen.Annotations
Imports NXOpen.Utilities


Module NXSelection
    Dim s As Session = Session.GetSession()
    Dim ui As UI = ui.GetUI()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Sub Main()
        Dim edge1 As NXObject = Nothing
        Dim obj(-1) As NXObject
        Dim cursor As Point3d = Nothing
        Dim response1 As Selection.Response = Selection.Response.Cancel
        response1 = SelectNoteorLabel(obj, cursor)
        For Each obj1 As NXObject In obj
            'do something with each selected note or label
        Next
    End Sub
    Public Function SelectNoteorLabel(ByRef obj() As NXObject, _
                                   ByRef cursor As Point3d) As Selection.Response
        Dim mask(1) As Selection.MaskTriple
        mask(0).Type = NXOpen.UF.UFConstants.UF_drafting_entity_type
        mask(0).Subtype = NXOpen.UF.UFConstants.UF_draft_note_subtype
        mask(1).Type = NXOpen.UF.UFConstants.UF_drafting_entity_type
        mask(1).Subtype = NXOpen.UF.UFConstants.UF_draft_label_subtype
        Dim resp As Selection.Response = ui.SelectionManager.SelectObjects("Select Notes/Labels", _
                                        "Select Notes/Labels", Selection.SelectionScope.WorkPart, _
                                         Selection.SelectionAction.ClearAndEnableSpecific, False, _
                                         False, mask, obj)
        If resp = Selection.Response.ObjectSelected Or _
           resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If
    End Function
    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


Frank Swinkels
 
Thank you once again for your excellent code!

But I should have explained what I was attempting to use it for. I have a VB program that can flip through all of the sheets within an NX drawing, while turning layers on and off as well as inserting various decals (made from groups and saved on a server). Currently, the users have to go through each sheet (possibly 50 sheets in a drawing) and delete outdated decals (consisting of lines and text objects) that may or may not be grouped on each sheet. What I would like to add to my program is to have the user select 2 points (x1, y1) to (x2,y2) to create a rectangle, and when flipping through each sheet, the text and line objects within that area are deleted. I’ll have to warn the user to be certain of the 2 selected points in order that only the desired objects are deleted and nothing more.

And when I have a bit more time at work, I’ll still see if I can properly add the following code to the awesome code you’ve provided me on 31 May 13 at 3:49 :
Dim idsb As IdSymbolBuilder = idCol.CreateIdSymbolBuilder(idsymbol)
id_textupper = idsb.UpperText
symorigin = idsb.Origin.OriginPoint


Thank you again!

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor