Deleting Pattern Formats in Drawings

I'm attempting to create a Journal script to search through all drawing sheets in a file and delete any Pattern features found. I found a post with similar code (https://community.sw.siemens.com/s/question/0D54O000061xREWSA2/nx-journa...) and tried modifying to suit my needs. I've been unable to get it to select Patterns. The code below currently finds all radius dimensions and deletes them. I suspect the displayPart.Dimensions needs to be something else but I've not been able to figure out what. I haven't found anything in the NX Open documentation that works.


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

Module delete_all_patterns

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

Sub Main()

Dim myUndoMark As Session.UndoMarkId =
s.SetUndoMark(Session.MarkVisibility.Visible,
"Delete Patterns")

Dim displayPart As Part = s.Parts.Display

For Each thisPattern As Object in displayPart.Dimensions

If is_Pattern(thisPattern) Then
s.UpdateManager.AddToDeleteList(thisPattern)
End If
Next

Dim count As Integer = s.UpdateManager.GetDeleteList.Length
s.UpdateManager.DoUpdate(myUndoMark)

lw.Open()
lw.WriteLine("Deleted " + count.ToString() + " Objects")

End Sub

Function is_Pattern(thePattern As Object) As Boolean
Dim objType As Integer
Dim objSubtype As Integer

ufs.Obj.AskTypeAndSubtype(thePattern.Tag, objType, objSubtype)

If objSubtype = 9 Then
Return True
End If

Return False

End Function

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

End Module

Is this one of the old drafting patterns that were sometimes used for title blocks? If so, a quick search didn't turn up a dedicated collection for the objects. We could use one of the cycle object functions to find them if we know the object type and subtype.

Select one or more objects of interest and run the journal below (taken from GTAC). It will report the object type and subtype (and a few other things).

'Amy Webster
'Date: 03/28/2011
'Subject: Sample NX Open .NET Visual Basic program : report type and subtype of preselected objects
'
'Note: GTAC provides programming examples for illustration only, and
'assumes that you are familiar with the programming language being
'demonstrated and the tools used to create and debug procedures. GTAC
'support professionals can help explain the functionality of a particular
'procedure, but we will not modify these examples to provide added
'functionality or construct procedures to meet your specific needs.

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Module journal

Sub Main
Dim s As Session = Session.GetSession()
Dim ufs As NXOpen.UF.UFSession = NXOpen.UF.UFSession.GetUFSession()
Dim selobj As NXObject
Dim type As Integer
Dim subtype As Integer
Dim lw As ListingWindow = s.ListingWindow

Dim theUI As UI = ui.GetUI
Dim numsel As Integer = theUI.SelectionManager.GetNumSelectedObjects()
lw.Open()
lw.WriteLine("Selected Objects: " & numsel.ToString())

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

ufs.Obj.AskTypeAndSubtype(selobj.Tag, type, subtype)
lw.WriteLine("Object: " & selobj.ToString())
lw.WriteLine(" GetType: " & selobj.GetType.ToString)
lw.WriteLine(" Tag: " & selobj.Tag.ToString())
lw.WriteLine(" Type: " & type.ToString())
lw.WriteLine(" Subtype: " & subtype.ToString())
lw.WriteLine(" Journal identifier: " & selobj.JournalIdentifier)
lw.WriteLine("")
Next

End Sub

End Module

Yes, it's the old Pattern feature and we'd been using it for the border and title block.

That looks like the code I ran and the results were:

Selected Objects: 1
Object: DisplayableObject 53117
Tag: 53117
Type: 10
Subtype: 0

Selected Objects: 1
Object: DisplayableObject 53118
Tag: 53118
Type: 10
Subtype: 0

https://nxjournaling.com/comment/4516#comment-4516

The function in the link above uses the CycleObjsInPart function to find all the solid bodies in a file. If you change UFConstants.UF_solid_type to UFConstants.UF_pattern_type and UFConstants.UF_solid_body_subtype to UFConstants.UF_pattern_subtype, it will find the patterns instead. Oh, also modify the list variable to hold patterns instead of bodies. Once you have them, you can add them to the delete list to get rid of them.

I was able to put together a journal to successfully do what I want. The code is shared below. Thanks for the help!


Option Strict Off

Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.UF

Module DeletePatterns

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

Sub Main()

lw.Open()

Dim myPatternList As List(Of Object) = New List(Of Object)
myPatternList = AskAllPatterns(workPart)

If myPatternList.Count = 0 Then
MsgBox("There are no Patterns in the Work Part")
End If

lw.WriteLine("Found " + myPatternList.count.ToString() + " Pattern(s)")

For Each thisObject as Object in myPatternList
theSession.UpdateManager.AddToDeleteList(thisObject)
Next

Dim myPatternCount As Integer = theSession.UpdateManager.GetDeleteList.Length

lw.WriteLine("Deleted " + myPatternCount.ToString() + " Pattern(s)")

workPart.Views.WorkView.Regenerate()

End Sub

Function AskAllPatterns(ByVal thePart As Part) As List(Of Object)

Dim thePatterns As New List(Of Object)
Try
Dim aPatternTag As Tag = NXOpen.Tag.Null
Do
ufs.Obj.CycleObjsInPart(thePart.Tag, _
UFConstants.UF_pattern_type, aPatternTag)
If aPatternTag = NXOpen.Tag.Null Then
Exit Do
End If

Dim theType As Integer, theSubtype As Integer
ufs.Obj.AskTypeAndSubtype(aPatternTag, theType, theSubtype)
If theSubtype = UFConstants.UF_pattern_subtype Then
thePatterns.Add(Utilities.NXObjectManager.Get(aPatternTag))
End If
Loop While True
Catch ex As NXException
lw.WriteLine(ex.ErrorCode & ex.Message)
End Try
Return thePatterns
End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

End Function
End Module

Thanks for sharing!