Delete Bend Notes only in selected drawing view

Delete Bend Notes only in selected drawing view

chewy_robot
Contributor Contributor
620 Views
3 Replies
Message 1 of 4

Delete Bend Notes only in selected drawing view

chewy_robot
Contributor
Contributor

I have an ilogic rule started that is intended to delete flat pattern bend notes only in the selected flat pattern drawing view that is clicked on. I think I am close but can't quite get it. When I run this rule and click on the drawing view I want to delete the existing bend notes on (if any exist), it deletes the bend notes found on the entire drawing for all views. I want it so it only deletes the bend notes (if they exist) on the view that is selected after the rule is ran. How can I do this? Here is my current iLogic rule:

Imports System
Imports Inventor

Sub Main()
' Ensure you are in a drawing document
If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MessageBox.Show("This tool can only be run in a drawing file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End If

Dim oDoc As DrawingDocument = CType(ThisApplication.ActiveDocument, DrawingDocument)
Dim oView As DrawingView = Nothing
Dim continueSelection As Boolean = True

While continueSelection
	Try
		' Use SelectionFilterEnum.kDrawingViewFilter for selecting drawing views
		oView = CType(ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a Drawing View to delete bend notes or ESC to exit."), DrawingView)
	Catch ex As Exception
		' User cancelled the selection or an error occurred
		MessageBox.Show("Selection cancelled or error occurred: " & ex.Message, "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		continueSelection = False
		Exit While
	End Try

	If oView Is Nothing Then
		MessageBox.Show("No valid view selected.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		continueSelection = False
	Else
		' Directly delete bend notes in the drawing sheet of the selected view
		For Each oSheet As Sheet In oDoc.Sheets
			If oSheet Is oView.Parent Then
				Dim bendNotesToDelete As New List(Of BendNote)
				For Each oNote As DrawingNote In oSheet.DrawingNotes
					If TypeOf oNote Is BendNote Then
						bendNotesToDelete.Add(CType(oNote, BendNote))
					End If
				Next
				' Delete the collected bend notes
				For Each bn In bendNotesToDelete
					bn.Delete()
				Next
			End If
		Next

		MessageBox.Show("Bend notes deleted successfully from the selected view's sheet.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
		continueSelection = False ' Exit after processing
	End If
End While
End Sub
0 Likes
Accepted solutions (1)
621 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

You need to check the attached geometry is member of the selected view. Here is relevant part of your code.

...
For Each oNote As DrawingNote In oSheet.DrawingNotes
    If TypeOf oNote Is BendNote Then
        Dim oBendNote As BendNote = oNote
        If oBendNote.BendEdge.Parent Is oView Then
            bendNotesToDelete.Add(oBendNote)
        End If
    End If
Next
...
0 Likes
Message 3 of 4

chewy_robot
Contributor
Contributor

Thanks for the feedback! This helped me to get it to work. Here is the full code for anyone that may need it:

Imports System
Imports Inventor

Sub Main()
' Ensure you are in a drawing document
If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MessageBox.Show("This tool can only be run in a drawing file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
	Return
End If

Dim oDoc As DrawingDocument = CType(ThisApplication.ActiveDocument, DrawingDocument)
Dim oView As DrawingView = Nothing
Dim continueSelection As Boolean = True

While continueSelection
	Try
		' Use SelectionFilterEnum.kDrawingViewFilter for selecting drawing views
		oView = CType(ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a Drawing View to delete bend notes or ESC to exit."), DrawingView)
	Catch ex As Exception
		' User cancelled the selection or an error occurred
		MessageBox.Show("Selection cancelled or error occurred: " & ex.Message, "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		continueSelection = False
		Exit While
	End Try

	If oView Is Nothing Then
		MessageBox.Show("No valid view selected.", "Selection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		continueSelection = False
	Else
		' Collect bend notes associated with the selected view
		Dim bendNotesToDelete As New List(Of BendNote)
		For Each oNote As DrawingNote In oView.Parent.DrawingNotes
			If TypeOf oNote Is BendNote Then
				Dim oBendNote As BendNote = CType(oNote, BendNote)
				' Check if the bend note's associated edge's parent is the selected view
				If oBendNote.BendEdge.Parent Is oView Then
					bendNotesToDelete.Add(oBendNote)
				End If
			End If
		Next

		' Delete the collected bend notes
		For Each bn In bendNotesToDelete
			bn.Delete()
		Next

		MessageBox.Show("Bend notes deleted successfully from the selected view.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
		continueSelection = False ' Exit after processing
	End If
End While
End Sub
0 Likes
Message 4 of 4

Michael.Navara
Advisor
Advisor

Plase use code sample block instead of plain text and keep indentation for better readability. You can find it in editor menu under </> button.