Change ID Symbol Type

When creating an ID symbol in NX, the dialog box provides a drop down menu with a variety of choices for the shape of the symbol. You pick one that should work and go about finishing the drawing. Unfortunately, the checker returns your drawing with a note explaining that you picked the wrong symbol shape for that particular usage. "No problem", you think. "I'll simply edit the symbol and change the type." You double click the symbol, the edit dialog box appears but the symbol type option is missing. After a few minutes of fumbling around, the realization begins to dawn on you that NX won't allow you to edit the shape of an existing ID symbol. What you anticipated to be a quick 5 minute edit job turned into a boring 30 minutes of recreating each incorrect symbol.

If the situation above sounds familiar, I've got good news: the NXOpen API gives you access to the symbol's type. The bad news is: you still can't change the symbol's type directly, even using the API. Wait... where are you going? That's not the end of the story!

The API will allow you to quickly recreate the symbols as shown in the journal code below.

The Code

This journal will iterate through all the ID symbols in the work part; if and when a triangle up symbol shape is found, its properties will be copied to a new square shaped symbol and finally the triangle up symbol will be deleted.


'NXJournaling.com
'January 31, 2014
'
'This journal illustrates how to change the "type" of an ID symbol.
'Interactive NX commands do not allow you to change the type; this journal
'cannot change the type directly, but creates a new symbol of the desired type
'and copies the properties from the old symbol to the new.
'
'caveat: if other annotations were associated to the old symbol (eg a note
' was associatively aligned to the old ID symbol), those associativities
' will be broken.

'Update September 26, 2016
'Previous version attempted to delete the ID symbol in the processing loop. Deleting an object while in a loop can lead to errors.
'This verison saves the old symbols in a list then deletes them after the processing loop finishes.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen

Module change_id_symbol_type

Sub Main()

Dim theSession As Session = Session.GetSession()

If IsNothing(theSession.Parts.Work) Then
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "NXJ ChangeID Symbol Type"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

If workPart.Annotations.IdSymbols.ToArray.Length = 0 Then
lw.WriteLine("No ID symbols found in current work part")
Return
End If

Dim symbolsToDelete As New List(Of Annotations.IdSymbol)

'loop through all the ID symbols in the part
For Each tempID As Annotations.IdSymbol In workPart.Annotations.IdSymbols

Dim oldIdBuilder As Annotations.IdSymbolBuilder

oldIdBuilder = workPart.Annotations.IdSymbols.CreateIdSymbolBuilder(tempID)

'type of symbol to change
If oldIdBuilder.Type = Annotations.IdSymbolBuilder.SymbolTypes.TriangleUp Then

'add old symbol to delete list
symbolsToDelete.Add(tempID)

'create a new symbol of the desired type
Dim nullSymbol As Annotations.IdSymbol = Nothing
Dim newIdBuilder As Annotations.IdSymbolBuilder
newIdBuilder = workPart.Annotations.IdSymbols.CreateIdSymbolBuilder(nullSymbol)

With newIdBuilder

'desired symbol type
.Type = Annotations.IdSymbolBuilder.SymbolTypes.Square

'copy properties of existing symbol to new symbol
.Size = oldIdBuilder.Size
.UpperText = oldIdBuilder.UpperText
.LowerText = oldIdBuilder.LowerText
.Style.LetteringStyle.AlignPosition = oldIdBuilder.Style.LetteringStyle.AlignPosition
.Origin.Anchor = oldIdBuilder.Origin.Anchor
.Origin.SetAssociativeOrigin(oldIdBuilder.Origin.GetAssociativeOrigin)
.Leader.Leaders.SetContents(oldIdBuilder.Leader.Leaders.GetContents)

.Origin.OriginPoint = oldIdBuilder.Origin.OriginPoint

End With

Dim newSymbol As NXObject
Try
newSymbol = newIdBuilder.Commit

Catch ex As NXException
MsgBox(ex.Message)
theSession.UndoToMark(markId1, undoMarkName)

Finally
newIdBuilder.Destroy()

End Try

End If
oldIdBuilder.Destroy()

Next

'delete the old symbols
theSession.UpdateManager.ClearErrorList()

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Delete old symbols")

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(symbolsToDelete.ToArray)

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)

lw.Close()

End Sub

End Module

Caveat

The new symbol will be associated to the same objects (if any), but if any other annotations were associated to the old ID symbol, those associations will be lost. Code could be written to iterate through the other annotations to check for and recreate these associations, but that is beyond the scope of this example.

Conclusion

The code above will need to be modified for your situation. Of course, if your goal is to change all symbols from one type to another, only minimal modifications will be necessary. Other ideas for customizations:

  • add an interactive selection routine to allow the user to select the desired symbols to change
  • add a form that allows the user to select the desired symbol type
  • add code to report the changes made to the ID symbols
  • add additional error checking/reporting code

Comments

getting internal error, while replacing TriangleUp (with two arrows(leader)) with custom symbol.

Can you post your code?

Code is in C++, It is same as above,But it doesn't work with two leaders on same bubble.

Please re-download the code and try it again. The previous version attempted to delete the old symbol while iterating through the collection. This is bad practice and can lead to errors. The code has been updated to save a list of old symbols and delete them after the processing loop completes. It may solve the error that you are experiencing.

it is still giving the error, its not executing beyond commit

Does the code work to replace one type of ID symbol with another? If so, it may be a problem with the custom symbol builder, or with your custom symbol itself.

Hi,
There seems to be problem with the part only, anyways thanks :)

The journal itself works nice but I'm not able to delete any symbols after running the journal.
However, if I save and open the part again I can delete symbols.
Any idea why?

Best Regards
Gunnar

There was an error in the code. The object "oldIdBuilder" wasn't being disposed of properly. As a result, the symbols were left in "limbo" and NX couldn't properly delete them.

I've corrected the code above; please try it again.