TitleBlockDefinition cannot be deleted even though all the referenced TitleBlocks are deleted

Issue

All of the TitleBlocks which reference a TitleBlockDefinition have been deleted. The IsReferenced property still returns true and an error occurs when I call the delete method. Is there a solution for this?

Solution

If the IsReferenced property returns true it means that the TitleBlockDefinition is still being used. SheetFormats can also reference a TitleBlockDefinition. If the SheetFormats are deleted then the TitleBlockDefinition can be deleted. Access to a SheetFormat became available from Inventor 2009 API. In previous releases you have to delete SheetFormats in the User Interface.

Here is a sample from  Inventor 2009 that deletes a TitleBlockDefinition, SheetFormats and TitleBlocks. 

In VB.Net

Sub DeleteTBD()
 
        If m_inventorApp.ActiveDocument.DocumentType 
           DocumentTypeEnum.kDrawingDocumentObject Then
            MsgBox("Make an idw the active document")
            End
        End If
 
        Dim oDoc As DrawingDocument
        oDoc = m_inventorApp.ActiveDocument
 
        ' Find out if TitleBlockDefinition exists
        Dim bTBDfound As Boolean
        bTBDfound = False
 
        Dim oTBD As TitleBlockDefinition
        For Each oTBD In oDoc.TitleBlockDefinitions
            Debug.Print(oTBD.Name)
            If oTBD.Name = "ANSI - Large" Then
                bTBDfound = True
                Exit For
            End If
        Next oTBD
 
        If bTBDfound = False Then
            MsgBox("ANSI - Large Title block already deleted")
            End
        End If
 
        oTBD = oDoc.TitleBlockDefinitions("ANSI - Large")
        If oTBD.IsReferenced = True Then
            Dim oSheetFormat As SheetFormat, oSheet As Sheet
 
            ' Check if there is any instance in SheetFormat/Sheet
            ' that refers to the TitleBlockDefinition
            For Each oSheet In oDoc.Sheets
                If oSheet.TitleBlock.Definition Is oTBD Then
 
                    ' Delete this instance which refers to the
                    ' TitleBlockDefinition
                    oSheet.TitleBlock.Delete()
                End If
            Next
 
            For Each oSheetFormat In oDoc.SheetFormats
                If oSheetFormat.ReferencedTitleBlockDefinition Is
                  oTBD Then
                    ' Delete this instance which refers to the
                    ' TitleBlockDefinition
                    oSheetFormat.Delete()
                End If
            Next
 
            ' Now you can delete the TitleBlockDefinition.
            oTBD.Delete()
        Else
            ' TitleBlockDefinition is not referenced go ahead and
            ' delete
            oTBD.Delete()
        End If
    End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading