Revision Journal - issue with deleting objects

Hello,

I made a journal which displays coordinates of all revision triangles placed on the drawing and change color of triangles on red (for better clarity). User can just copy coordinates and describe purpose of change. Journal works perfect on all sheet sizes (metric and imperial). The only issue is that user cannot delete triangles after running this journal. I was traying to solve it, but I cannot find the solution. I would be grateful for any suggestions :)

P.S. If journal will ask about number of EDO sheets you can type "0".

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

Module Module6

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession

Sub Main()

If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Dim theUISession As UI = UI.GetUI
Dim answer As String = ""
Dim myInt As Double
Dim myNumber As Double

Do
answer = NXInputBox.GetInputNumber("Enter number of EDO sheets", " ")
'if cancel is pressed, exit sub
If answer = "" Then Exit Sub
Loop Until Integer.TryParse(answer, myInt)

lw.Open()

Dim dicDraftingDatums As New Dictionary(Of String, List(Of String))

For Each temp As Annotations.IdSymbol In workpart.Annotations.IdSymbols

dim myIdBuilder As Annotations.IdSymbolBuilder
myIdBuilder = workPart.Annotations.IdSymbols.CreateIdSymbolBuilder(temp)

If myIdBuilder.Type = Annotations.IdSymbolBuilder.SymbolTypes.TriangleUp Then
Dim myDraftingDatum As Annotations.IdSymbol = temp
Dim myDraftingDatumZone As string
myDraftingDatumZone = ReportAnnotationSheetZone(AskDrawingSheet(myDraftingDatum), myDraftingDatum)

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
displayModification1.ApplyToAllFaces = True
displayModification1.ApplyToOwningParts = False
displayModification1.NewColor = 186
Dim objects1(0) As DisplayableObject
objects1(0) = temp
displayModification1.Apply(objects1)
displayModification1.Dispose()

If dicDraftingDatums.ContainsKey(myIdBuilder.UpperText) Then
'add zone info to the existing key
dicDraftingDatums.Item(myIdBuilder.UpperText).Add(myDraftingDatumZone)
Else
'add to dictionary
dicDraftingDatums.Add(myIdBuilder.UpperText, New List(Of String)({myDraftingDatumZone}))
End If
End If
Next

For Each pair As KeyValuePair(Of String, List(Of String)) In dicDraftingDatums
'mySheets.Sort(AddressOf CompareSheetNames)
dim finalstring As String

'lw.WriteLine(" Datum label: " & pair.Key & " [" & pair.Value.Count.ToString & " places]")
pair.Value.Sort(AddressOf CompareZones)
For Each temp As String In pair.Value

dim sheetnumber_str As String = mid(temp,1,1)
dim sheetnumber_int As Integer = CDbl(sheetnumber_str) - myInt
dim finalsheetnum As String = sheetnumber_int
if sheetnumber_int > 0 then

finalstring = finalstring + "SH" + finalsheetnum + ", " + "ZONE" + " " + mid(temp,2,3) + ",; "

end if

Next
lw.WriteLine("")
lw.WriteLine("" &finalstring)

Next

lw.Close()

End Sub

Function AskDrawingSheet(ByVal theObject As TaggedObject) As Drawings.DrawingSheet
'Code written by Amy Webster of GTAC
' see nx_api4936 or nx_api4937
' This function will work for:
' an object which "Resides on drawing" or is "View Dependent In" a DraftingView
' a DraftingView
' a DrawingSheet.View
' Returns Nothing for all other (ie. model mode) objects

Dim theView As View = TryCast(theObject, View)
If Not theView Is Nothing Then
Dim sheetTag As Tag = Nothing
Try
theUfSession.Draw.AskDrawingOfView(theView.Tag, sheetTag)
Return Utilities.NXObjectManager.Get(sheetTag) ' the drawing it is on
Catch ex As NXException
Return Nothing ' it is a model view
End Try
End If

Dim viewName As String = Nothing
Dim status As Integer = Nothing
Try
theUfSession.View.AskViewDependentStatus(theObject.Tag, status, viewName)
Catch ex As NXException
Return Nothing
End Try
If status = 0 Then Return Nothing ' it is a model mode object

Dim viewTag As Tag = Nothing
theUfSession.View.AskTagOfViewName(viewName, viewTag)
Dim viewType As Integer = Nothing
Dim viewSubtype As Integer = Nothing
theUfSession.View.AskType(viewTag, viewType, viewSubtype)
If viewType = 0 Then Return Nothing ' it is view dependent in a modeling view

Dim drawingTag As Tag = Nothing
theUfSession.Draw.AskDrawingOfView(viewTag, drawingTag)
Return Utilities.NXObjectManager.Get(drawingTag) ' the drawing it is on!

End Function

Function ReportAnnotationSheetZone(ByVal theSheet As Drawings.DrawingSheet, ByVal theAnnotation As Annotations.Annotation) As String

'sheet number / vertical zone / horizontal zone

Dim borderBuilder As Drawings.BordersAndZonesBuilder

borderBuilder = theSession.Parts.Work.Drafting.BordersAndZonesObjects.CreateBordersAndZonesBuilder(theSheet.BordersAndZones)

dim cell_number_x As Integer = 12
dim cell_number_y As Integer = 8
dim SheetA0 As Integer = 0
dim cord_x As Integer = ((841/25.4)-1) / cell_number_x
dim cord_y As Double = ((594/25.4)-1) / cell_number_y
dim Hcell As Double
dim Vcell As Double
dim const1 As Integer = 0
dim st_cell_size As Double
dim st_cell_size1 As Double
dim cell_size As Double
dim cell_size1 As Double

' A0 size
if theSheet.Length = 1189 then

cell_number_y = 12
cell_number_x = 16
cord_y = ((841/25.4))
cord_x = ((1189/25.4))
st_cell_size = ((cord_x - 1) / cell_number_x) + .5
cell_size = ((cord_x - 1) / cell_number_x)
st_cell_size1 = ((cord_y - 1) / cell_number_y) + .5
cell_size1 = ((cord_y - 1) / cell_number_y)
const1 = 1

end if

' A2 size
if theSheet.Length = 594 then

cell_number_y = 6
cell_number_x = 8
cord_y = (420/25.4)
cord_x = (594/25.4)
st_cell_size = ((cord_x - 1) / cell_number_x) + .5
cell_size = ((cord_x - 1) / cell_number_x)
st_cell_size1 = ((cord_y - 1) / cell_number_y) + .5
cell_size1 = ((cord_y - 1) / cell_number_y)

end if

' A3 size
if theSheet.Length = 420 then

cell_number_y = 4
cell_number_x = 8
cord_y = ((297/25.4))
cord_x = ((420/25.4))
st_cell_size = ((cord_x - 1) / cell_number_x) + .5
cell_size = ((cord_x - 1) / cell_number_x)
st_cell_size1 = ((cord_y - 1) / cell_number_y) + .5
cell_size1 = ((cord_y - 1) / cell_number_y)

end if

' A4 size
if theSheet.Length = 297 then

cell_number_y = 4
cell_number_x = 6
cord_y = ((210/25.4))
cord_x = ((297/25.4))
st_cell_size = ((cord_x - 1) / cell_number_x) + .5
cell_size = ((cord_x - 1) / cell_number_x)
st_cell_size1 = ((cord_y - 1) / cell_number_y) + .5
cell_size1 = ((cord_y - 1) / cell_number_y)

end if

'A1 size
if theSheet.Length = 841 then

cell_number_y = 8
cell_number_x = 12
cord_y = ((594/25.4))
cord_x = ((841/25.4))
st_cell_size = ((cord_x - 1) / cell_number_x) + .5
cell_size = ((cord_x - 1) / cell_number_x)
st_cell_size1 = ((cord_y - 1) / cell_number_y) + .5
cell_size1 = ((cord_y - 1) / cell_number_y)

end if

'D size
if theSheet.Length = 34 then
cell_number_y = 8
cell_number_x = 12
cord_y = 22 / cell_number_y
cord_x = 34 / cell_number_x
st_cell_size = 2.75 + 0.5
cell_size = 2.75
st_cell_size1 = 21/8 + 0.5
cell_size1 = 21/8

end if

'E size
if theSheet.Length = 44 then
cell_number_y = 12
cell_number_x = 16
cord_y = 34 / cell_number_y
cord_x = 44 / cell_number_x
st_cell_size = 3.1875
cell_size = 2.6875
st_cell_size1 = 33/12 + .5
cell_size1 = 33/12
const1=1

end if

'C size
if theSheet.Length = 22 then
cell_number_y = 6
cell_number_x = 8
cord_y = 17 / cell_number_y
cord_x = 22 / cell_number_x
st_cell_size = 2.625 + 0.5
cell_size = 2.625
st_cell_size1 = 16/6 + .5
cell_size1 = 16/6

end if

'B size
if theSheet.Length = 17 then
cell_number_y = 4
cell_number_x = 6
cord_y = 11 / cell_number_y
cord_x = 17 / cell_number_x
st_cell_size = 2.666666666666667 + 0.5
cell_size = 2.666666666666667
st_cell_size1 = 10/4 + .5
cell_size1 = 10/4

end if

'A size
if theSheet.Length = 11 then
cell_number_y = 4
cell_number_x = 6
cord_y = 8.5 / cell_number_y
cord_x = 11 / cell_number_x
st_cell_size = 1.666666666666667 + 0.5
cell_size = 1.666666666666667
st_cell_size1 = 1.875 + .5
cell_size1 = 1.875

end if

if theAnnotation.AnnotationOrigin.X < st_cell_size then
Hcell = 1
end if

if st_cell_size < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*1 then
Hcell = 2
end if

if st_cell_size + cell_size*1 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*2 then
Hcell = 3
end if

if st_cell_size + cell_size*2 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*3 then
Hcell = 4
end if

if st_cell_size + cell_size*3 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*4 then
Hcell = 5
end if

if st_cell_size + cell_size*4 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*5 then
Hcell = 6
end if

if st_cell_size + cell_size*5 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*6 then
Hcell = 7
end if

if st_cell_size + cell_size*6 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*7 then
Hcell = 8
end if

if st_cell_size + cell_size*7 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*8 then
Hcell = 9
end if

if st_cell_size + cell_size*8 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*9 then
Hcell = 10
end if

if st_cell_size + cell_size*9 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*10 then
Hcell = 11
end if

if st_cell_size + cell_size*10 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*11 then
Hcell = 12
end if

if st_cell_size + cell_size*11 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*12 then
Hcell = 13
end if

if st_cell_size + cell_size*12 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*13 then
Hcell = 14
end if

if st_cell_size + cell_size*13 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*14 then
Hcell = 15
end if

if st_cell_size + cell_size*14 < theAnnotation.AnnotationOrigin.X and theAnnotation.AnnotationOrigin.X < st_cell_size + cell_size*15 then
Hcell = 16
end if

'poczatek

if theAnnotation.AnnotationOrigin.Y < st_cell_size1 then
Vcell = 1
end if

if st_cell_size1 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*1 then
Vcell = 2
end if

if st_cell_size1 + cell_size1*1 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*2 then
Vcell = 3
end if

if st_cell_size1 + cell_size1*2 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*3 then
Vcell = 4
end if

if st_cell_size1 + cell_size1*3 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*4 then
Vcell = 5 + const1
end if

if st_cell_size1 + cell_size1*4 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*5 then
Vcell = 6 + const1
end if

if st_cell_size1 + cell_size1*5 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*6 then
Vcell = 7 + const1
end if

if st_cell_size1 + cell_size1*6 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*7 then
Vcell = 8 + const1
end if

if st_cell_size1 + cell_size1*7 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*8 then
Vcell = 9 + const1
end if

if st_cell_size1 + cell_size1*8 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1 + cell_size1*9 then
Vcell = 10 + const1
end if

if st_cell_size1+ + cell_size1*9 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1+ + cell_size1*10 then
Vcell = 11 + const1
end if

if st_cell_size1+ + cell_size1*10 < theAnnotation.AnnotationOrigin.Y and theAnnotation.AnnotationOrigin.Y < st_cell_size1+ + cell_size1*11 then
Vcell = 12 + const1
end if

'koniec

Dim verticalLetterNum As Integer
Dim verticalLetter As Char

Dim horizontalNum As Integer

horizontalNum = Hcell
verticalLetterNum = Asc("A") + cell_number_y - Vcell + const1
verticalLetter = Chr(verticalLetterNum)

Dim theSheetNum As String = SheetNumber(theSheet)
Dim theSheetTag As String = SheetTitle(theSheet)

Dim searchChar As String = "EDO"

Dim edo_counter As Integer = 0
dim edo_number As Integer= InStr(1, theSheetTag, SearchChar, CompareMethod.Text)

' if edo_number = 1 then return nothing
Return theSheetNum & verticalLetter & horizontalNum.ToString
End Function

Function SheetNumber(ByVal theSheet As Drawings.DrawingSheet) As String

Dim sheetNum As String
Dim theSheetBuilder As Drawings.DrawingSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
sheetNum = theSheetBuilder.Number

Return sheetNum
theSheetBuilder.Destroy()

End Function

Function SheetTitle(ByVal theSheet As Drawings.DrawingSheet) As String

Dim sheetTag As String
Dim theSheetBuilder As Drawings.DrawingSheetBuilder = theSession.Parts.Work.DrawingSheets.DrawingSheetBuilder(theSheet)
sheetTag = theSheetBuilder.Name

Return sheetTag.ToString
theSheetBuilder.Destroy()

End Function

Private Function CompareZones(ByVal x As String, ByVal y As String) As Integer

'case-insensitive sort
Dim myStringComp As StringComparer = StringComparer.CurrentCultureIgnoreCase

'for a case-sensitive sort (A-Z then a-z), change the above option to:
'Dim myStringComp As StringComparer = StringComparer.CurrentCulture

Return myStringComp.Compare(x, y)

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

Whenever you create and use a builder object, make sure to destroy the builder when you are done with it. Not doing so can lead to strange behavior in the file (such as not being able to delete the objects edited by the builder). There are at least 2 builder objects in your code that are created but never destroyed.