Submitted by MARTYNW on Mon, 11/29/2021 - 08:48
Forums:
I have a UDO attached to a part and I would like to delete it from my c# code. I am using the UDO to store additional data with the part but I cannot find a method in UserDefinedClassManager to call to remove the UDO when I don't need it anymore.
Any help greatly appreciated!
An exemple
Option Strict Off
Imports NXOpen
Imports System
Imports System.Reflection
Imports System.Collections
Imports System.Object
Imports System.Windows.Forms
'Imports System.Collections.Generics
Imports NXOpen.Drawings
Imports NXOpenUI
Imports NXOpen.UI
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.UserDefinedObjects
Module report_all_udos_in_work_part
Dim theSession As Session = Session.GetSession()
Dim ufs As UFSession = Nothing
Dim theUI As UI = Nothing
Dim lw As ListingWindow = theSession.ListingWindow
Dim wp As Part = theSession.Parts.Work
Dim subUdo As UserDefinedObject = Nothing
Dim subClass As UserDefinedClass = Nothing
'--------------------------------------------------------------------------------------------------
Sub Echo(ByVal output As String)
If Not lw.IsOpen() Then lw.Open()
lw.WriteLine(output)
End Sub
'--------------------------------------------------------------------------------------------------
Sub Main()
Dim NULL_TAG As NXOpen.Tag = NXOpen.Tag.Null
Dim udo As Tag
Dim n_udos As Integer = 0
Try
CreateUDO()
Catch ex As NXException
Echo("Caught Exception in Main: '" & ex.Message() & "'")
Echo("Create-????")
End Try
Do
udo = Ask_Next_UDO(udo)
If udo = NULL_TAG Then
Echo(vbCrLf & "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
Echo("NULL_TAG - No more udo types found.")
Echo("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
Exit Do
End If
Dim theUdo As UserDefinedObject = CType(NXObjectManager.Get(udo), UserDefinedObject)
Dim theUdoStrings As String() = theUdo.GetStrings()
Dim theUdoDoubles As Double() = theUdo.GetDoubles()
Dim theUdoIntegers As Integer() = theUdo.GetIntegers()
Echo(vbCrLf & "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
Echo(vbCrLf & "UDO: " & theUdo.ToString())
Echo(" Tag: " & theUdo.Tag.ToString)
Echo(" Name: " & theUdo.Name)
Echo(" Class Name: " & theUdo.ClassName)
Echo(" Layer: " + theUdo.Layer.ToString())
Echo(" Status: " + theUdo.GetUserDefinedObjectStatus().ToString())
'------------------------------------------------------------------------
Dim sString As String
Dim sStringValue As Integer = 0
For Each sString In theUdoStrings
sStringValue = sStringValue + 1
Next
If sStringValue >= 1 Then
Echo(" * * * * String * * * *")
End If
'D'abord, recherche parmis la liste "theUdoStrings" s'il y a un String qui contient "MATERIEL".
'Ensuite, s'il est trouvé, on remplace la valeur par "MATERIEL=TEST2".
'Aussi, on applique la valeur dans la liste "theUdoStrings" à la position (0).
'NB: avant d'appliquer la valeur, il faut savoir à qu'elle position le String se positionne.
Dim NoIndexItemRead As Integer = 0
Dim NoIndexItemReadList As Integer = -1
For Each sString In theUdoStrings
Dim sStringVal As String
'Dim RangItem As Integer = -1
NoIndexItemReadList = NoIndexItemReadList + 1
If sString.Contains("INVENTAIRE") Then
sStringVal = "INVENTAIRE=test"
theUdoStrings(NoIndexItemReadList) = sStringVal
NoIndexItemRead = NoIndexItemReadList + 1
Echo("***** Read List *****")
Echo("Item trouvé: " & sStringVal)
Echo("Rang Index Item: " & NoIndexItemReadList & " de " & sStringValue)
Echo("L'item est le No" & NoIndexItemRead & " dans la liste")
Dim itemindex As Int32 = Array.IndexOf(theUdoStrings, "INVENTAIRE=test")
Dim itemname As String = theUdoStrings(itemindex)
Echo("Item index: " & itemindex)
Echo("Item name: " & itemname)
End If
'Finalement, on repousse la liste "theUdoStrings" entièrement.
theUdo.SetStrings(theUdoStrings)
Next
'Test
Dim NoIndexItemDelete As Integer = 0
Dim NoIndexItemDeleteList As Integer = -1
For Each sString In theUdoStrings
Dim StringToDelete As String = "MATERIEL"
NoIndexItemDeleteList = NoIndexItemDeleteList + 1
If sString.Contains(StringToDelete) Then
'udoToDelete = theUdoStrings(0)
Echo("***** Delete list *****")
Echo("Item trouvé: " & StringToDelete)
Echo("Rang Index Item: " & NoIndexItemReadList & " de " & sStringValue)
Echo("L'item est le No" & NoIndexItemRead & " dans la liste")
'theSession.UpdateManager.AddToDeleteList(NXObjectManager.Get(udoToDelete))
theUdoStrings.GetEnumerator()
End If
Dim nbr_udoDelete As Integer = theSession.UpdateManager.GetDeleteList().Length
If nbr_udoDelete > 0 Then
Echo("Udo objects in delete list: " & nbr_udoDelete.ToString)
'Dim markId1 As Session.UndoMarkId
'markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Delete")
'theSession.UpdateManager.DoUpdate(markId1)
End If
Next
Echo("Liste (original) :")
For Each sString In theUdoStrings
Echo(" UDO String stored: " & sString)
Next
'------------------------------------------------------------------------
Dim sDouble As Double
Dim sDoubleValue As Integer = 0
For Each sDouble In theUdoDoubles
sDoubleValue = sDoubleValue + 1
Next
If sDoubleValue >= 1 Then
Echo(" * * * * Double * * * *")
End If
For Each sDouble In theUdoDoubles
Echo(" UDO Double stored: " & sDouble.ToString())
Next
'------------------------------------------------------------------------
Dim sInteger As Integer
Dim sIntegerValue As Integer = 0
For Each sInteger In theUdoDoubles
sIntegerValue = sIntegerValue + 1
Next
If sIntegerValue >= 1 Then
Echo(" * * * * Integer * * * *")
End If
For Each sInteger In theUdoIntegers
Echo(" UDO Integer stored: " & sInteger.ToString())
Next
'------------------------------------------------------------------------
Dim ii As Integer
Dim aI As NXObject.AttributeInformation() = theUdo.GetUserAttributes
'Dim aV As String() = theUdo.GetUserAttributesAsStrings()
For ii = 0 To aI.Length - 1
If aI(ii).Array Then
'Echo(" " + aI(ii).Title & "(" & aI(ii).ArrayElementIndex & ") = " & aV(ii) & " (" & aI(ii).Type.ToString() & ")")
Echo(" " + aI(ii).Title & "(" & aI(ii).ArrayElementIndex & ") = (" & aI(ii).Type.ToString() & ")")
Else
'Echo(" " + aI(ii).Title & " = " & aV(ii) & " (" & aI(ii).Type.ToString() & ")")
Echo(" " + aI(ii).Title & " = (" & aI(ii).Type.ToString() & ")")
End If
Next
sStringValue = 0
sDoubleValue = 0
sIntegerValue = 0
n_udos = n_udos + 1
'--------------------------------------------------------------------------------------------------
Dim rang As Integer = 4
Dim testString As String
Dim tempTab As String() = DirectCast(theUdoStrings.Clone(), String())
Dim i As Integer
Echo("Liste (temporaire) :")
For i = 0 To theUdoStrings.Length '- 1
Echo(" Val: " & i)
If i = rang Then
theUdoStrings(i) = tempTab(i + 1)
End If
Next
Echo("Liste (modifié) :")
For Each testString In tempTab
Echo(" UDO String stored TempTab: " & testString)
Next
'-------------------------------------------------------------------------------------------------
Loop Until udo = NULL_TAG
Echo(vbCrLf & "UserDefinedObjects in work part: " & n_udos.ToString)
End Sub
'--------------------------------------------------------------------------------------------------
Public Function Ask_Next_UDO(ByRef udo As NXOpen.Tag) As NXOpen.Tag
Dim part As NXOpen.Tag = theSession.Parts.Work.Tag
ufs.Obj.CycleObjsInPart(part, UFConstants.UF_user_defined_object_type, udo)
Return udo
End Function
'--------------------------------------------------------------------------------------------------
Public Function CreateUDO() As Integer
If theSession Is Nothing Then
theSession = Session.GetSession()
End If
If theUI Is Nothing Then
theUI = UI.GetUI()
End If
If ufs Is Nothing Then
ufs = NXOpen.UF.UFSession.GetUFSession()
End If
Try
subClass = theSession.UserDefinedClassManager.CreateUserDefinedObjectClass("SUB600", "SUB600 Test Class")
Echo("SUB600")
UI.GetUI.SelectionManager.SetSelectionStatusOfUserDefinedClass(subClass, True)
subUdo = wp.UserDefinedObjectManager.CreateUserDefinedObject(subClass)
subUdo.SetName("SUB600-Name value")
'Echo("SUB1600")
Catch ex As NXException
Echo("Caught Exception in Main: '" & ex.Message() & "'")
Echo("1-????")
End Try
CreateUDO = 1
End Function
'--------------------------------------------------------------------------------------------------
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function
End Module
Pat
Thank you for your help I
Thank you for your help I will go those methods a try
merci ça marche parfaitement
merci ça marche parfaitement