Submitted by mikefrank on Thu, 02/09/2017 - 12:55
Forums:
Hello all,
I am looking for a way to select components in an assembly and then not only delete them from the model but also delete them from the computer. Something along the lines of selecting multiple components, hitting delete, have a confirmation window to confirm your action, if yes is selected the components are deleted from the model and from their respective file locations on the computer.
Thank you for your time.
So far I have tried compiling
So far I have tried compiling together some code after some research. It errors out due to:
For Each tempComp As Component In theSession.Parts.Work.Assemblies
I am still trying to find a way to also record the paths of the selected components. From what I've found I think IO.file.delete will allow the deletion of the files from their respective paths.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
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, "remove ID symbol leaders")
lw.Open()
Dim FullCompDelete As New List(Of Component)
If SelectComponents("Select Components to Delete Fully", FullCompDelete) = Selection.Response.Cancel Then
Return
End If
For Each tempComp As Component In theSession.Parts.Work.Assemblies
If FullCompDelete.Contains(tempComp) Then
'user selected this symbol, we want to keep its leaders
End If
Next
DeleteObjects(FullCompDelete.ToArray)
lw.Close()
End Sub
Sub DeleteObjects(ByVal garbage() As NXObject)
Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete
theSession.UpdateManager.ClearErrorList()
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Delete")
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(garbage)
Dim notifyOnDelete2 As Boolean
notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete
Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)
End Sub
Function SelectComponents(ByVal prompt As String, ByRef selComponents As List(Of Component)) As Selection.Response
Dim selObj() As TaggedObject
Dim theUI As UI = UI.GetUI
Dim title As String = "Select Components"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = 0
.Subtype = 0
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt,
title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj)
If resp = Selection.Response.Ok Then
For Each temp As Component In selObj
selComponents.Add(temp)
Next
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
re: delete components
I've modified your code to delete the file from the users computer (well, actually move it to the recycle bin so it can be recovered if needed). In addition to deleting the component, I would suggest deleting any assembly constraints connected to the component (the code below does not do this).
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Assemblies
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, "Delete components")
lw.Open()
Dim FullCompDelete As New List(Of Component)
Dim compPaths As New List(Of String)
If SelectComponents("Select Components to Delete Fully", FullCompDelete) = Selection.Response.Cancel Then
Return
End If
'save path to file for later deletion
For Each temp As Component In FullCompDelete
compPaths.Add(temp.Prototype.OwningPart.FullPath)
Next
'delete components from assembly
DeleteObjects(FullCompDelete.ToArray)
'delete part files (move to recycle bin)
For Each temp As String In compPaths
My.Computer.FileSystem.DeleteFile(temp, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
Next
lw.Close()
End Sub
Sub DeleteObjects(ByVal garbage() As NXObject)
Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete
theSession.UpdateManager.ClearErrorList()
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Delete")
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(garbage)
Dim notifyOnDelete2 As Boolean
notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete
Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)
End Sub
Function SelectComponents(ByVal prompt As String, ByRef selComponents As List(Of Component)) As Selection.Response
Dim selObj() As TaggedObject
Dim theUI As UI = UI.GetUI
Dim title As String = "Select Components"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_component_subtype
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObjects(prompt,
title, scope, selAction, includeFeatures, keepHighlighted, selectionMask_array, selObj)
If resp = Selection.Response.Ok Then
For Each temp As Component In selObj
selComponents.Add(temp)
Next
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 very much for your
Thank you very much for your help. Your help with these journals has been extremely informative and educational. I hoping to take the Open API programming class soon to learn more about programming within NX.