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!

Help me with a Journal to report the text strings in a Multisheet drawing (label or notes)

Status
Not open for further replies.

Venkadakrishnan

Automotive
Jun 23, 2014
6
thread561-352708

Hi The below Journal doest work for label notes, NX 8.5... can somebody help me here

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

Module GetWordInfo
    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow

    Sub Main()
        Dim dp As Part = s.Parts.Display
        Dim nc As NoteCollection = dp.Notes
        Dim textin As String = "text to find"
        Try
            textin = NXInputBox.GetInputString("Get Note Info", "Text to Find", textin)
        Catch ex As Exception
            GoTo end1
        End Try
        Dim dwgs As Drawings.DrawingSheetCollection
        dwgs = dp.DrawingSheets
        Dim notestring() As String
        lw.Open()
        For Each sheet As Drawings.DrawingSheet In dwgs
            sheet.Open()
            For Each a_note As Note In nc
                notestring = a_note.GetText()
                If notestring(0) = textin Then
                    lw.WriteLine(sheet.Name & "  ,  " & notestring(0))
                End If
            Next
        Next
end1:
    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
 
Replies continue below

Recommended for you

Hi

The above code is from a closed thread, Which is used search for a particular text in Drawing notes and replace..
Whereas my requirement is different, but with little modification to above code will work (i feel)

I want to search for all Annotation.labels (which contains particular text anywhere in it) in drawing sheet and list the text string in Information window.

Can somebody help me out here
 
Don't know if I'm going saft, but I don't think I know how to create an instance of a Label [neutral]
Anyways, the code below should work.
Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module GetWordInfo
    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow

    Sub Main()
        Dim dp As Part = s.Parts.Display
        Dim lc As LabelCollection = dp.Labels
        Dim textin As String = "text to find"
        Try
            textin = NXInputBox.GetInputString("Get Label Info", "Text to Find", textin)
        Catch ex As Exception
            GoTo end1
        End Try
        Dim dwgs As Drawings.DrawingSheetCollection
        dwgs = dp.DrawingSheets
        Dim labelstring() As String
        lw.Open()
        For Each sheet As Drawings.DrawingSheet In dwgs
            sheet.Open()
            For Each a_label As Label In lc
                labelstring = a_label.GetText()
                If labelstring(0).Contains(textin) Then
	                lw.WriteLine(labelstring(0))
	              End If
            Next
       Next
end1:
    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

Graham Inchley, Systems Developer.
NX6, NX8.5(testing)
 
Hi Grinch33

The code only searches for labelstrings in the first line of the text, If the "text to find" is in second or other lines, its not working..

If suppose i change the code to
Code:
If labelstring(1).Contains(textin) Then
	                lw.WriteLine(labelstring(1))
Then the it is working only for the second line...

But, i want to search for the "text to find" in the entire label note and list in information window....
 
Here is the modified journal to take into account multiple line labels

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

Module GetWordInfo
    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = s.ListingWindow

    Sub Main()
        Dim dp As Part = s.Parts.Display
        Dim lc As LabelCollection = dp.Labels
        Dim textin As String = "text to find"
        Dim textfound As Boolean = False
        Try
            textin = NXInputBox.GetInputString("Get Label Info", "Text to Find", textin)
        Catch ex As Exception
            GoTo end1
        End Try
        Dim dwgs As Drawings.DrawingSheetCollection
        dwgs = dp.DrawingSheets
        Dim labelstring() As String
        lw.Open()
        For Each sheet As Drawings.DrawingSheet In dwgs
            sheet.Open()
            For Each a_label As Label In lc
                labelstring = a_label.GetText()
                For i As Integer = 0 To labelstring.Length - 1
                    If labelstring(i).Contains(textin) Then
                        textfound = True
                    End If
                Next
                If textfound = True Then
                    For i As Integer = 0 To labelstring.Length - 1
                        lw.WriteLine(labelstring(i))
                    Next
                    lw.WriteLine(vbCrLf)
                End If
                textfound = False
            Next
        Next
end1:
    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
 
OK, to do this you need to cycle through the members of the 'labelstring' array and do the 'Contains' on each.
The following modified loop should work OK:
Code:
For Each sheet As Drawings.DrawingSheet In dwgs
  sheet.Open()
  For Each a_label As Label In lc
    labelstring = a_label.GetText()
    For Each labelstringitem As String In labelstring
      If labelstringitem.Contains(textin) Then
        lw.WriteLine(labelstringitem)
      End If
    Next
  Next
Next
Note that this will only write the individual text that contains the required string to the listing window. If you want to see all the current members of 'labelstring' if any of them contain the required string you just need to add another loop inside the 'if' statement to write all current members of 'labelstring' to the listing window.

Graham Inchley, Systems Developer.
NX6, NX8.5(testing)
 
Hi

Thanks Frank & Grinch for the codes...

I have made changes to my code and now it works fine.. but some improvement is needed

Meanwhile i found out, that looping through sheets is giving repeated data, because the text string is searched in label.collection instead of the displayed sheets. I understand label.collection holds all the label string in a drawing, irrespective of displayed sheet. So, looping through sheets is not required.

Also, I want to search for two different words in label.collection and all permutation of two words should yield results, and i don't want user interaction there.. I have included the "text to find" in code itself. (this part is completed and working fine)

According to the above, i have modified the code...
Now, i want to have few improvements.
1. Sheet.name for the found text string is needed, Example (Sheet1 Text srting1 ; Text string2 ; ......etc)
2. I want to have a separator between the text strings " ; " (line1 textstring ; line2 text string ; and so on till it ends..) because i have to import the list window data to excel...

Help me out here

Code:
' NX 8.0.3.4

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal

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

Sub Main

	  Dim dp As Part = s.Parts.Display
        Dim lbs As NXOpen.Annotations.LabelCollection = dp.Labels
        Dim textin As String = "XXX" : Dim textin2 As String = "YYY"
        Dim dwgs As Drawings.DrawingSheetCollection
        dwgs = dp.DrawingSheets
        Dim Labelstring() As String

        If Not lw.IsOpen Then lw.Open()

        
            For Each a_Label As NXOpen.Annotations.Label In lbs
                Labelstring = a_Label.GetText()
                Dim TempStr As String = String.Empty
                Dim IsValid As Boolean = False

                For ii As Integer = 0 To Labelstring.Length- 1
                    If Labelstring(ii).Contains(textin) Or Labelstring(ii).Contains(textin2) Then
                        IsValid = True
                    End If
                Next
                If IsValid Then
                    For ii As Integer = 0 To Labelstring.Length- 1
                        If Not String.IsNullOrEmpty(TempStr) Then
                            TempStr = TempStr & " " & Labelstring(ii)
                        Else
                            TempStr = Labelstring(ii)
                        End If
                    Next
			lw.WriteLine("Found" & "  ,  " & TempStr)
                End If
              Next
        
End Sub
End Module
 
OK, that makes it a little more complicated. There is no direct way I know of to get the name of the drawing an object resides on. Instead you have to get the visible objects of the sheet view and cycle through those.
Below is your code modified to cycle through the visible objects of each drawing sheet looking for Label objects. When one is found it then does the check to see if the required texts exist in the Label and if so writes the drawing sheet name and the label's text to the listing window.
Code:
' NX 8.0.3.4

Option Strict Off
Imports System
Imports NXOpen

Module NXJournal

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

Sub Main

	Dim dp As Part = s.Parts.Display
	Dim textin As String = "XXX" : Dim textin2 As String = "YYY"
	Dim dwgs As Drawings.DrawingSheetCollection
	dwgs = dp.DrawingSheets
	Dim Labelstring() As String

	If Not lw.IsOpen Then lw.Open()

	For Each sheet As Drawings.DrawingSheet In dwgs
		sheet.Open
		Dim dos() As DisplayableObject=sheet.View.AskVisibleObjects
		For Each ado As DisplayableObject In dos
			If Not TypeOf ado Is NXOpen.Annotations.Label Then Continue For
			Dim a_Label As NXOpen.Annotations.Label=DirectCast(ado,NXOpen.Annotations.Label)
			Labelstring = a_Label.GetText()
			Dim TempStr As String = String.Empty
			Dim IsValid As Boolean = False
			For ii As Integer = 0 To Labelstring.Length- 1
				If Labelstring(ii).Contains(textin) Or Labelstring(ii).Contains(textin2) Then
					IsValid = True
				End If
			Next
			If IsValid Then
				For ii As Integer = 0 To Labelstring.Length- 1
					If Not String.IsNullOrEmpty(TempStr) Then
						TempStr = TempStr & ";" & Labelstring(ii)
					Else
						TempStr = Labelstring(ii)
					End If
				Next
				lw.WriteLine("Found '" & sheet.Name & ";" & TempStr & "'")
			End If
		Next
	Next
	
End Sub
End Module

Graham Inchley, Systems Developer.
NX6, NX8.5(testing)
 
Thanks Grinch

Your code works like a charm [bigsmile], I have around 50 sheets in the drawing for a big assembly [hourglass]. Hence looping sheets take almost 20 to 25 minutes....
I would prefer to search for the "text to find" in the label.colletction instead on the displayed objects for each sheet...
Journal running time will be reduced to seconds....

Thanks Cowski

Thanks for your input from the GTAC, the reference code in the GTAC solution is preferred for the User selected objects...
I'm not sure, how to use those codes in my journal... (i'm a newbie here)

I would thanks and appreciate, if you can help me with a journal without looping sheets and returning sheet.name of the label..
Meanwhile i'll also work on my journal for improvements...

Thanks in Advance...
 
The code is written as a function; once you find a note object that matches your desired text, pass the note object to the function - it will return the sheet the note is on.

Code written by Amy Webster of GTAC fame.
Code:
    ' This function will work for:
    '     an object which "Resides on drawing" or is "View Dependent In" a DraftingView
    '     a DraftingView
    '     a DrawingSheet.View
    ' Returns Nothing for all other (ie. model mode) objects
    Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet

        Dim theView As View = TryCast(theObject, View)
        If Not theView Is Nothing Then
            Dim sheetTag As Tag = Nothing
            Try
                theUFSession.Draw.AskDrawingOfView(theView.Tag, sheetTag)
                Return NXObjectManager.Get(sheetTag) ' the drawing it is on
            Catch ex As NXException
                Return Nothing  ' it is a model view
            End Try
        End If

        Dim viewName As String = Nothing
        Dim status As Integer = Nothing
        Try
            theUFSession.View.AskViewDependentStatus(theObject.Tag, status, viewName)
        Catch ex As NXException
            Return Nothing
        End Try
        If status = 0 Then Return Nothing ' it is a model mode object

        Dim viewTag As Tag = Nothing
        theUFSession.View.AskTagOfViewName(viewName, viewTag)
        Dim viewType As Integer = Nothing
        Dim viewSubtype As Integer = Nothing
        theUFSession.View.AskType(viewTag, viewType, viewSubtype)
        If viewType = 0 Then Return Nothing ' it is view dependent in a modeling view

        Dim drawingTag As Tag = Nothing
        theUFSession.Draw.AskDrawingOfView(viewTag, drawingTag)
        Return NXObjectManager.Get(drawingTag)  ' the drawing it is on!

    End Function

www.nxjournaling.com
 
Thanks Cowski, Frank & Grinch for the support

Finally i was able to compile the journal which works 100% persent to match my requirement.. Obsivously taking less time for excution also...
Now the journal running time is reduced to 2 sec from 20 min earlier for 45 sheet drawing.

Here is detailed function of the Journal..
The journal searches for two text stings in all Notes and labels of a multisheet drawing, and reports the Text stings with page number..

Code:
'NX 8.0.3.4

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.Annotations

Module NXJournal

 Dim theSession As Session = Session.GetSession()
 Dim theUFSession As UFSession = UFSession.GetUFSession()
 Dim workPart As Part = theSession.Parts.Work
 Dim dp As Part = theSession.Parts.Display
 Dim lw As ListingWindow = theSession.ListingWindow

Sub Main
        
        Dim lbs As NXOpen.Annotations.LabelCollection = dp.Labels
        Dim nts As NXOpen.Annotations.NoteCollection = dp.Notes
        Dim textin As String = "xxx" : Dim textin2 As String = "yyy"
        Dim dwgs As Drawings.DrawingSheetCollection
        dwgs = dp.DrawingSheets
        Dim Labelstring() As String
        Dim Notestring() As String
        
        If Not lw.IsOpen Then lw.Open()
            For Each a_Label As NXOpen.Annotations.Label In lbs
                Labelstring = a_Label.GetText()
                Dim TempStr As String = String.Empty
                Dim IsValid As Boolean = False
                Dim theDrawingSheet As Drawings.DrawingSheet = AskDrawingSheet(a_Label)

                For ii As Integer = 0 To Labelstring.Length- 1
                    If Labelstring(ii).Contains(textin) Or Labelstring(ii).Contains(textin2) Then
                        IsValid = True
                    End If
                Next
                If IsValid Then
                    For ii As Integer = 0 To Labelstring.Length- 1
                        If Not String.IsNullOrEmpty(TempStr) Then
                            TempStr = TempStr & " " & Labelstring(ii)
                        Else
                            TempStr = Labelstring(ii)
                        End If
                    Next
			lw.WriteLine(theDrawingSheet.name & ", " & TempStr)
                End If
              Next

             For Each a_Note As NXOpen.Annotations.Note In nts
                Notestring = a_Note.GetText()
                Dim TempStr As String = String.Empty
                Dim IsValid As Boolean = False
                Dim theDrawingSheet As Drawings.DrawingSheet = AskDrawingSheet(a_Note)

                For ii As Integer = 0 To Notestring.Length- 1
                    If Notestring(ii).Contains(textin) Or Notestring(ii).Contains(textin2) Then
                        IsValid = True
                    End If
                Next
                If IsValid Then
                    For ii As Integer = 0 To Notestring.Length- 1
                        If Not String.IsNullOrEmpty(TempStr) Then
                            TempStr = TempStr & " " & Notestring(ii)
                        Else
                            TempStr = Notestring(ii)
                        End If
                    Next
			lw.WriteLine(theDrawingSheet.name & ", " & TempStr)
                End If
              Next
   
End Sub

Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet

        Dim theView As View = TryCast(theObject, View)
        If Not theView Is Nothing Then
            Dim sheetTag As Tag = Nothing
            Try
                theUFSession.Draw.AskDrawingOfView(theView.Tag, sheetTag)
                Return NXObjectManager.Get(sheetTag) ' the drawing it is on
            Catch ex As NXException
                Return Nothing  ' it is a model view
            End Try
        End If

        Dim viewName As String = Nothing
        Dim status As Integer = Nothing
        Try
            theUFSession.View.AskViewDependentStatus(theObject.Tag, status, viewName)
        Catch ex As NXException
            Return Nothing
        End Try
        If status = 0 Then Return Nothing ' it is a model mode object

        Dim viewTag As Tag = Nothing
        theUFSession.View.AskTagOfViewName(viewName, viewTag)
        Dim viewType As Integer = Nothing
        Dim viewSubtype As Integer = Nothing
        theUFSession.View.AskType(viewTag, viewType, viewSubtype)
        If viewType = 0 Then Return Nothing ' it is view dependent in a modeling view

        Dim drawingTag As Tag = Nothing
        theUFSession.Draw.AskDrawingOfView(viewTag, drawingTag)
        Return NXObjectManager.Get(drawingTag)  ' the drawing it is on!

    End Function

End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor