Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Change specific Text format in the Title block

Alex.Shechter
Contributor

Change specific Text format in the Title block

Alex.Shechter
Contributor
Contributor

Hello everyone,

 

I am trying to automate inventor drawing for specific cases and I want to create a code that checks Titleblock textformat with weight. I want to be able to format the weight (how many digits after the comma, unite etc) or to promt the user for specific text.
This seams to be way more complicated than I thought so here is the code that i managed to put together with a lot of help from chatGPT.

 

I think the error is in the fact that the code does not specify whitch textformat in the title block sketch should be worked on.

The question:

How do I make the code to check for a specific text format in the sketch of the title block?

 

The code:

' Get the active document.
Dim oDoc As Document = ThisApplication.ActiveDocument

' Update the physical properties and calculate the weight.
oDoc.Update2()
Dim weight As Double = oDoc.ComponentDefinition.MassProperties.Mass

' Get the title block definition.
Dim titleBlockDef As TitleBlockDefinition = oDoc.ActiveSheet.TitleBlock.Definition

' Loop through all the text formats in the "Schriftfeld" sketch.
For Each TextFormat As TextFormat In titleBlockDef.Sketches.Item("Schriftfeld").TextFormats
    ' Check if the text format contains the word "weight".
    If TextFormat.Text.Contains("weight") Then
        ' Delete the text and variable.
        TextFormat.Text = ""
        TextFormat.Variable = Nothing
    End If
Next

' Prompt the user for the weight.
Dim weightPrompt As String = InputBox("What weight should it have?", "Weight Prompt", weight.ToString())

' Add the new text format with the user's weight.
Dim newFormat As TextFormat = titleBlockDef.Sketches.Item("Schriftfeld").TextFormats.Add(weightPrompt, 0, 0)
newFormat.Field = Nothing

 

 

 

 

 

0 Likes
Reply
139 Views
1 Reply
Reply (1)

A.Acheson
Mentor
Mentor

Hi @Alex.Shechter 

I would caution the use of change and even more so if your a beginner. It really comes up with code that does not exist in the Inventor API. Instead stick with published sources such as the API Help.

 

For example this  loop right here will go no where because there is no such object as textformats and nothing even related inside the sketch object shown here. Maybe it was intending to use text boxes. 

For Each TextFormat As TextFormat In titleBlockDef.Sketches.Item("Schriftfeld").TextFormats
   

 I am not entirely sure what your intending to do by your description of the worklfow so maybe can you provide an image of your intended changes? 

Also changing items in a titleblock is not best practice as you would normally change the titleblock to a custom one and simply swap that.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes