Sample NX Open .NET Visual Basic program : delete all objects with a specified name 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.Collections Imports NXOpen Imports NXOpen.UF Imports NXOpen.Utilities Public Class NXJournal Shared theSession As Session = Session.GetSession() Shared theUFSession As UFSession = UFSession.GetUFSession() Shared workPart As Part = theSession.Parts.Work Private Shared Sub DoIt() Do Until False Dim theName As String = PromptForText("Find objects by Name") If String.IsNullOrEmpty(theName) Then Exit Do Dim theObjects() = AskAllObjectsOfName(theName) If theObjects.Length = 0 Then Exit Do For Each theObject As DisplayableObject In theObjects theObject.Highlight() Next 'NOTE: if the object(s) found was a group ALL of the member objects will be highlighted and deleted If AskYesOrNo(theName, "Delete these?") Then Try Dim theMarkId As Session.UndoMarkId = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete Objects Named" + theName) theSession.UpdateManager.AddToDeleteList(theObjects) theSession.UpdateManager.DoUpdate(theMarkId) Catch ex As NXException Echo("Delete failed: " & ex.Message) End Try Else For Each theObject As DisplayableObject In theObjects theObject.Unhighlight() Next End If Loop End Sub Private Shared Function AskAllObjectsOfName(ByVal theName As String) As NXObject() Dim objList As ArrayList = New ArrayList Dim aTag As Tag = Tag.Null Do theUFSession.Obj.CycleByName(theName, aTag) If Not aTag = Tag.Null Then objList.Add(NXObjectManager.Get(aTag)) Loop Until aTag = Tag.Null Return objList.ToArray(GetType(NXObject)) End Function Private Shared Function PromptForText(ByVal thePrompt As String, Optional ByVal theText As String = "") As String Dim theUFSession As UFSession = UFSession.GetUFSession() Dim theLength As Integer theUFSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) Dim theResponse As Integer = theUFSession.Ui.AskStringInput(thePrompt, theText, theLength) theUFSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) If (theResponse = 3) Or (theResponse = 5) Then Return theText Else Return Nothing End If End Function Private Shared Function AskYesOrNo(ByVal title As String, ByVal message As String) As Boolean Dim messages As String() = {message} Dim buttons As UFUi.MessageButtons With buttons .button1 = True .button2 = False .button3 = True .label1 = "Yes" .label2 = Nothing .label3 = "No" .response1 = 1 .response2 = 0 .response3 = 2 End With Dim resp As Integer theUFSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) theUFSession.Ui.MessageDialog(title, UiMessageDialogType.UiMessageQuestion, messages, 1, True, buttons, resp) theUFSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) If resp = 1 Then Return True Else Return False End Function Public Shared Sub Main(ByVal args As String()) If workPart IsNot Nothing Then DoIt() Return End If Dim loadStatus As PartLoadStatus = Nothing For ii As Integer = 0 To args.Length - 1 Echo("Processing: " & args(ii)) workPart = DirectCast(theSession.Parts.OpenBaseDisplay(args(ii), loadStatus), Part) reportPartLoadStatus(loadStatus) If workPart IsNot Nothing Then DoIt() workPart.Close(BasePart.CloseWholeTree.True, BasePart.CloseModified.CloseModified, Nothing) End If Next End Sub Private Shared Sub Echo(ByVal output As String) theSession.ListingWindow.Open() theSession.ListingWindow.WriteLine(output) theSession.LogFile.WriteLine(output) End Sub Private Shared Sub reportPartLoadStatus(ByVal load_status As PartLoadStatus) If load_status.NumberUnloadedParts = 0 Then Return End If Echo(" Load notes:") For ii As Integer = 0 To load_status.NumberUnloadedParts - 1 Echo(" " & load_status.GetPartName(ii) & " - " & load_status.GetStatusDescription(ii)) Next End Sub Public Shared Function GetUnloadOption(ByVal arg As String) As Integer Return Session.LibraryUnloadOption.Immediately End Function End Class