Inventor: Change text items in TitleBlockDefinition

by Vladimir Ananyev

Issue

Question: How do I change text items in the TitleBlockDefinition in Inventor?

Solution

Answer: Assuming you have an active drawing document open, first you need to access the TitleBlockDefinitions collection. Step through the collection to find the Title Block of interest, as shown in the following code fragment, the "ANSI – Large" title block definition.

When the TitleBlockDefinition of interest is found, we select the TextBoxes collection. Step through the collection until the TextBox of interest is found. For a TextBox the 'Text' property is a read/write property. The most important code in the following code fragment is placing the TitleBlockDefinition into edit mode with a call to 'Edit' and exiting the edit mode with a call to 'ExitEdit'.

Two code samples and a sample drawing document ("Drawing.zip") demonstrate this.

 Download Drawing

VBA:

Private Sub ChgTitleBlkDef()

 

  Dim objDoc As Document

  Set objDoc = ThisApplication.ActiveDocument

 

  If objDoc Is Nothing Then

    MsgBox "No Active Document"

    Set objDoc = Nothing

    Exit Sub

  End If

 

  'Is the active document a drawing document?

  If objDoc.DocumentType <> kDrawingDocumentObject Then

    MsgBox "Not a drawing document"

    Set objDoc = Nothing

    Exit Sub

  End If

 

  Dim objDrawDoc As DrawingDocument

  Set objDrawDoc = objDoc

 

  Dim colTitleBlkDefs As TitleBlockDefinitions

  Set colTitleBlkDefs = objDrawDoc.TitleBlockDefinitions

 

  Dim objTitleBlkDef As TitleBlockDefinition

 

  For Each objTitleBlkDef In colTitleBlkDefs

    If objTitleBlkDef.Name = "ANSI – Large" Then

      'we have the title blk we want

      Exit For

    End If

  Next objTitleBlkDef

 

  If objTitleBlkDef Is Nothing Then

    MsgBox "ANSI – Large TitleBlockDefinition not found!"

    Set objDrawDoc = Nothing

    Set colTitleBlkDefs = Nothing

    Set objTitleBlkDef = Nothing

    Exit Sub

  End If

 

  ' If we are here we have the title block of interest.

  ' Get the title block sketch and set it active

  Dim objDrwSketch As DrawingSketch

  Call objTitleBlkDef.Edit(objDrwSketch)

 

  Dim colTextBoxes As TextBoxes

  Set colTextBoxes = objDrwSketch.TextBoxes

 

  Dim objTextBox As TextBox

  For Each objTextBox In colTextBoxes

    If objTextBox.Text = "TITLE" Then

      objTextBox.Text = "Captain CAD Engineering"

      Exit For

    End If

  Next objTextBox

 

  Call objTitleBlkDef.ExitEdit(True)

 

  ' Clean up

  Set objDrawDoc = Nothing

  Set colTitleBlkDefs = Nothing

  Set objTitleBlkDef = Nothing

  Set objDrwSketch = Nothing

  Set colTextBoxes = Nothing

  Set objTextBox = Nothing

 

  Beep

End Sub

 

VB .NET:

Sub ChgTitleBlkDef()

 

  Dim objDrawDoc As DrawingDocument _

      = CType(oApp.ActiveDocument, DrawingDocument)

 

  Dim colTitleBlkDefs As TitleBlockDefinitions _

      = objDrawDoc.TitleBlockDefinitions

 

  Dim objTitleBlkDef As TitleBlockDefinition = Nothing

  For Each objTitleBlkDef In colTitleBlkDefs

    If objTitleBlkDef.Name = "ANSI – Large" Then

      'we have the title blk we want

      Exit For

    End If

  Next

 

  ' If we are here we have the title block of interest.

  ' Get the title block sketch and set it active

  Dim objDrwSketch As DrawingSketch = Nothing

  objTitleBlkDef.Edit(objDrwSketch)

 

  Dim colTextBoxes As TextBoxes _

        = objDrwSketch.TextBoxes

 

  For Each objTextBox As TextBox In colTextBoxes

    If objTextBox.Text = "TITLE" Then

      objTextBox.Text = "Captain CAD Engineering"

      Exit For

    End If

  Next

  objTitleBlkDef.ExitEdit(True)

 

  Beep()

End Sub


Comments

One response to “Inventor: Change text items in TitleBlockDefinition”

  1. Peter D. Schell Avatar
    Peter D. Schell

    VB.NET working code inventor pro 2018:
    Sub Main ChgTitleBlkDef()
    Dim objDoc As Document
    objDoc = ThisApplication.ActiveDocument
    If objDoc Is Nothing Then
    MsgBox(“No Active Document”)
    objDoc = Nothing
    Exit Sub
    End If
    ‘Is the active document a drawing document?
    If objDoc.DocumentType <> kDrawingDocumentObject Then
    MsgBox(“Not a drawing document”)
    objDoc = Nothing
    Exit Sub
    End If
    Dim objDrawDoc As DrawingDocument
    objDrawDoc = objDoc
    Dim colTitleBlkDefs As TitleBlockDefinitions
    colTitleBlkDefs = objDrawDoc.TitleBlockDefinitions
    Dim objTitleBlkDef As TitleBlockDefinition
    For Each objTitleBlkDef In colTitleBlkDefs
    If objTitleBlkDef.Name = “ANSI – Large” Then
    ‘we have the title blk we want
    Exit For
    End If
    Next objTitleBlkDef
    If objTitleBlkDef Is Nothing Then
    MsgBox(“ANSI – Large TitleBlockDefinition not found!”)
    objDrawDoc = Nothing
    colTitleBlkDefs = Nothing
    objTitleBlkDef = Nothing
    Exit Sub
    End If
    ‘ If we are here we have the title block of interest.
    ‘ Get the title block sketch and set it active
    Dim objDrwSketch As DrawingSketch
    objTitleBlkDef.Edit(objDrwSketch)
    Dim colTextBoxes As TextBoxes
    colTextBoxes = objDrwSketch.TextBoxes
    Dim objTextBox As TextBox
    For Each objTextBox In colTextBoxes
    If objTextBox.Text = “TITLE” Then
    objTextBox.Text = “Captain CAD Engineering”
    Exit For
    End If
    Next objTextBox
    objTitleBlkDef.ExitEdit(True)
    ‘ Clean up
    objDrawDoc = Nothing
    colTitleBlkDefs = Nothing
    objTitleBlkDef = Nothing
    objDrwSketch = Nothing
    colTextBoxes = Nothing
    objTextBox = Nothing
    Beep
    End Sub

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading