Cancel
Showing results for 
Search instead for 
Did you mean: 

Issue in deleting Balloons.

Pioneer
Pioneer

Hi All,

I am performing a functionality to delete all my triangle balloons in drawing sheet.

I am able to get triangle balloons but I am not able to delete it. 

PFB code. Line hilighted in Bold is the line i am facing exception. 

//---------------------------------------------------------------------------------------------------------------------------------------------

Sub CollectAutoBalloons(ByRef theList As List(Of Annotations.IdSymbol), ByRef sample2 As String)
Dim myIdBuilder As Annotations.IdSymbolBuilder
Dim objects1(0) As NXOpen.TaggedObject
For Each tempID As Annotations.IdSymbol In theSession.Parts.Work.Annotations.IdSymbols

Dim theCallout As Tag = Nothing
theUfSession.Drf.AskCalloutOfAnnotation(tempID.Tag, theCallout)
If theCallout = Tag.Null Then
End If

myIdBuilder = theSession.Parts.Work.Annotations.IdSymbols.CreateIdSymbolBuilder(tempID)

sample2 = myIdBuilder.BalloonType()
If (sample2 = "3") Then

objects1(0) = myIdBuilder
theSession.UpdateManager.AddObjectsToDeleteList(objects1)
Dim notifyOnDelete2 As Boolean = Nothing
notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete
End If
myIdBuilder.Destroy()
Next

End Sub

//------------------------------------------------------------------------------------------------------------------------------------------

At the line Add objects to delete list I am facing exception.

Internal Error- Memmory access violation

 

Can anyone please help.

 

Thanks in advance

5 REPLIES 5

Re: Issue in deleting Balloons.

Solution Partner Legend Solution Partner Legend
Solution Partner Legend

Hi,

 

shouldn't you delete the symbol itself and not the builder object?

 

maybe try the following:

...
...
If (sample2 = "3") Then objects1(0) = tempID theSession.UpdateManager.AddObjectsToDeleteList(objects1)
...
...
'and before "End Sub" don't forget to do:
theSession.UpdateManager.DoUpdate(theSession.NewestVisibleUndoMark)

regards,

MGeyer

Re: Issue in deleting Balloons.

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

Never delete a member of a collection while you are iterating through said collection. Doing so will lead to errors and/or unexpected behavior. In this case, I'd suggest creating a list of ID symbols, iterating through the ID symbol collection and if you want to delete it, add it to the list. When the loop completes, add the items from the list to the NX delete list and perform a .DoUpdate. Something like below (pseudocode):

 

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "delete stuff")

Dim idSymbolsToDelete as new list(of Idsymbol) for each tempID as idsymbol in workpart.idsymbols if tempID = garbage then idSymbolsToDelete.Add(tempID) end if next
theSession.UpdateManager.AddToDeleteList(idSymbolsToDelete.ToArray)
theSession.UpdateManager.DoUpdate(markId1)

Re: Issue in deleting Balloons.

Pioneer
Pioneer

Hi All,

 

Thank you very much for the reply. I've modified the code as suggested I've got my exception cleared. 

But I did not met my requirement.

 

This is how I modified my code. I did not get any error or exception.

My code have run fine.

But Triangular balloons were not deleted.

Sub CollectAutoBalloons()
        Dim myIdBuilder As Annotations.IdSymbolBuilder
        Dim objects1() As NXOpen.TaggedObject = Nothing
        Dim iCounter As Integer = 0
        Dim idSymbolsToDelete As New List(Of NXOpen.TaggedObject)
        Dim sample2 As String = String.Empty

        Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
        markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Delete")

        For Each tempID As Annotations.IdSymbol In theSession.Parts.Work.Annotations.IdSymbols
            Dim theCallout As Tag = Nothing
            theUfSession.Drf.AskCalloutOfAnnotation(tempID.Tag, theCallout)
            If theCallout = Tag.Null Then
            End If
            myIdBuilder = theSession.Parts.Work.Annotations.IdSymbols.CreateIdSymbolBuilder(tempID)
            sample2 = myIdBuilder.BalloonType()
            If (sample2 = "3") Then
                idSymbolsToDelete.Add(tempID)
            End If
            'myIdBuilder.Destroy()
        Next
        theSession.UpdateManager.AddObjectsToDeleteList(idSymbolsToDelete.ToArray)
        Dim notifyOnDelete2 As Boolean = Nothing
        notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete
        Dim nErrs2 As Integer = Nothing
        nErrs2 = theSession.UpdateManager.DoUpdate(markId2)
    End Sub

So I've recorded a journal and executed the same one. It is not deleting the Triangular balloons. I tried to modify the journal code as per my logic and executed it as shown below. 

But it did not delete. I did not get any exceptions or errors.

 '' ----------------------------------------------
        ''   Menu: Edit->Delete...
        '' ----------------------------------------------
        'Dim myIdBuilder As Annotations.IdSymbolBuilder
        'Dim sample2 As String = String.Empty
        'For Each tempID As Annotations.IdSymbol In theSession.Parts.Work.Annotations.IdSymbols
        '    myIdBuilder = theSession.Parts.Work.Annotations.IdSymbols.CreateIdSymbolBuilder(tempID)
        '    sample2 = myIdBuilder.BalloonType()
        '    If (sample2 = "3") Then
        '        Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
        '        markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Delete")

        '        Dim notifyOnDelete1 As Boolean = Nothing
        '        notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

        '        theSession.UpdateManager.ClearErrorList()

        '        Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
        '        markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Delete")

        '        Dim objects1(0) As NXOpen.NXObject
        '        Dim idSymbol1 As NXOpen.Annotations.IdSymbol = CType(workPart.FindObject(tempID.JournalIdentifier), NXOpen.Annotations.IdSymbol)

        '        objects1(0) = idSymbol1
        '        Dim nErrs1 As Integer = Nothing
        '        nErrs1 = theSession.UpdateManager.AddToDeleteList(objects1)

        '        Dim notifyOnDelete2 As Boolean = Nothing
        '        notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete

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

        '        theSession.DeleteUndoMark(markId1, Nothing)
        '    End If
        'Next
        '' ----------------------------------------------
        ''   Menu: Tools->Journal->Stop Recording
        '' ----------------------------------------------

Can anyone pls help me.

 

Re: Issue in deleting Balloons.

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

@Shashank1 wrote:

Hi All,

 

Thank you very much for the reply. I've modified the code as suggested I've got my exception cleared. 

But I did not met my requirement.

 

This is how I modified my code. I did not get any error or exception.

My code have run fine.

But Triangular balloons were not deleted.

Sub CollectAutoBalloons()
        Dim myIdBuilder As Annotations.IdSymbolBuilder
        Dim objects1() As NXOpen.TaggedObject = Nothing
        Dim iCounter As Integer = 0
        Dim idSymbolsToDelete As New List(Of NXOpen.TaggedObject)
        Dim sample2 As String = String.Empty

        Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
        markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Delete")

        For Each tempID As Annotations.IdSymbol In theSession.Parts.Work.Annotations.IdSymbols
            Dim theCallout As Tag = Nothing
            theUfSession.Drf.AskCalloutOfAnnotation(tempID.Tag, theCallout)
            If theCallout = Tag.Null Then
            End If
            myIdBuilder = theSession.Parts.Work.Annotations.IdSymbols.CreateIdSymbolBuilder(tempID)
            sample2 = myIdBuilder.BalloonType()
            If (sample2 = "3") Then
                idSymbolsToDelete.Add(tempID)
            End If
            'myIdBuilder.Destroy()
        Next
        theSession.UpdateManager.AddObjectsToDeleteList(idSymbolsToDelete.ToArray)
        Dim notifyOnDelete2 As Boolean = Nothing
        notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete
        Dim nErrs2 As Integer = Nothing
        nErrs2 = theSession.UpdateManager.DoUpdate(markId2)
    End Sub

 


I'd suggest 2 modifications to the code. First:

sample2 = myIdBuilder.BalloonType()
If (sample2 = "3") Then
  idSymbolsToDelete.Add(tempID)
End If

if myIdBuilder.BalloonType = NXOpen.Annotations.BalloonTypes.TriangleUp then
  idSymbolsToDelete.Add(tempID)
End if

The current code compares the balloon type (an integer) to a string value; this will always return false for the comparison. I suggest comparing to the named enumeration value (BalloonTypes.TriangleUp) as this will work and make your code intention clear and thus easier to maintain.

 

Second:

'myIdBuilder.Destroy()

Uncomment this line of code. You definitely want to destroy the builder object when you are done with it. Not doing so will lead to errors or unexpected behavior. If you run the code and do not destroy the id symbol builder, you most likely won't be able to delete the triangle id symbols, even through the interactive NX commands. Update your code, restart NX and try it again.

Re: Issue in deleting Balloons.

Pioneer
Pioneer

Yes it is working if I use 

NXOpen.Annotations.BalloonTypes.TriangleUp

 

Thank you very much.