Changing Font style in TextBox - Inventor VBA

Changing Font style in TextBox - Inventor VBA

Anonymous
Not applicable
3,606 Views
7 Replies
Message 1 of 8

Changing Font style in TextBox - Inventor VBA

Anonymous
Not applicable

Hello,

I am begginer at writing code in Inventor VBA Editor, and this is probably something easy, but... I am stuck.

 

I am editing some code from 'Sample Programs' to get a program, which  creates new sketch with text box.

It's working well, but I don't know how to change font and font size of text in text box.

 

Code:

 

Sub textbox()
'Create a new part document, using the defauld part template.
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))


'Set a reference to the component definition.
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition

'Create a new sketch on the X-Y work plane
Dim oSketch As PlanarSketch
Set oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))


' Set a reference to the transient geometry object.
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry

' Create a text box at (5mm,5mm)
Dim oTextBox As textbox
Set oTextBox = oSketch.TextBoxes.AddFitted(oTransGeom.CreatePoint2d(0.5, 0.5), "HELLO")
oTextBox.Style.Font = "Verdana"
oTextBox.Style.FontSize = 5

End Sub

 

But it leaves text with standard font and font size:

 
 
 

font.png

 

Of course I searched forum for solution, but none of the topics with solution helped.

 
I would be grateful if anyone would help.
 
Regards,
Jan

 

 

 

0 Likes
Accepted solutions (1)
3,607 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Generally what you would want to do in this case, is to have a pre-existing TextStyle set-up and available within that part document the way you want it (like in your template file), then simply assign that TextStyle to the TextBox.

Something like this:

Dim oTextStyle As Inventor.TextStyle
Set oTextStyle = oPartDoc.TextStyles.Item("Verdana 5")
oTextBox.Style = oTextStyle

where "Verdana 5" is what you named your TextStyle in the Styles Editor dialog box.

  What is happening the way you have it worded right now is, it is trying to change settings within the TextStyle itself, which would effect all other text in your document using that same TextStyle, instead of just making a change that would only effect that one TextBox.  Those changes would have to be saved to the TextStyle for them to take effect, so they are lost, and had no effect.  Or if you don't know the name of the style you want, you could loop through the existing ones, checking their Font and FontSize properties, until you find the one you want, then set it to the TextBox.

 

    One other important thing to note when working within the VBA Editor.  There are 'References' that can be turned on within that VBA Editor, which allow it to be able to interact with many other application's resources.  It is very possible that some of those other resources have named their Classes (object types) the same way, so when you set your variable as a "TextBox", it might be understood as a Windows.Forms.TextBox, or a MSForm.TextBox, or an Access.TextBox.  So when coding in the VBA Editor, it working with common types of objects that may not be unique to Inventor, it is always a good idea to declare you variables with the "Inventor." in front of the type, like this "Inventor.TextBox". 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

Anonymous
Not applicable

@WCrihfield 

Thank you very much for your response. It is more than i needed! I am really happy that you explained to me where I am making mistakes, and how should I do better, instead of just giving me a code.

 

I have added new text style called "Verdana 5" in Styles Editor, with font "Verdana" and font size 5.

Now part of the code with creating text box looks like this:

 

Dim oTextStyle As Inventor.TextStyle
Set oTextStyle = oPartDoc.TextStyles.Item("Verdana 5")
' Create a text box at (2mm,2mm)
Dim oTextBox As TextBox
Set oTextBox = oSketch.TextBoxes.AddFitted(oTransGeom.CreatePoint2d(0.5, 0.5), "HELLO", oTextStyle)

 

And it works just great.

 

Also thanks for explaining why I should declare my variables with the "Inventor".

 

Best regards,

Jan

 

 

 

0 Likes
Message 4 of 8

Anonymous
Not applicable

@WCrihfield 

I've got one more problem according to text in TextBox.

I wrote code that creates new part and new sketch with textbox. 

I divided text into few lines, using 'vbNewLine'.

Everything works, but the spaces between lines are too big:

janekl125_0-1610376018418.png

Then, when I open textbox to edit (manually):

janekl125_1-1610376104284.png

and close it (without changing anything),  then the spaces are as they meant to be:

janekl125_2-1610376163852.png

 

Code:

Public Sub TextBox()

Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))

Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition

Dim oSketch As PlanarSketch
Set oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry

Dim sText1 As String
sText1 = "LINE 1" _
& vbNewLine & "LINE 2" _
& vbNewLine & "LINE 3" _
& vbNewLine & "LINE 4"

Dim oTextBox As Inventor.TextBox
Set oTextBox = oSketch.TextBoxes.AddFitted(oTransGeom.CreatePoint2d(1, 3.8), sText1)


oTextBox.FormattedText = "<StyleOverride FontSize='0,2' Font='Arial'>" & sText1 & "</StyleOverride>"

End Sub

 

Do you know what the problem is?

 

 

 

 

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

Although using the TextBox's FormattedText property and XML tags to inject the formatting you want is sometimes all that is necessary for some situations, it is unfortunately both limited and complicated.  As far as I know, you can't specify paragraph/line spacing using that route.  Here is the link to the current help page where it it attempts to inform us how to use XML tags within Inventor's FormattedText scenarios.   It shows you how to change Font, FontSize, Bold, Italic, and Underline, but not line spacing.  Fortunately though, the TextStyle I mentioned earlier does include this under its "Paragraph Settings" section (when your looking at it through the Style Editor dialog box).  It has font height based line spacing multipliers and an exact setting, which will allow you to set a specific decimal height you want for the offset.  It also includes a % stretch setting, and a color setting.   So if the line spacing is giving you trouble, maybe try setting up a TextStyle the way you want it.  Then if that TextStyle works good, and you want it to be available to all future parts, just copy it into your part template file.  And if you want it included in all your older part files, you may have to Export that TextStyle from the style editor dialog box to an external file.  Then you can import it into other older part files, as needed (or using a batch process).

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

jacsoa
Participant
Participant

Hi,

 

It works fine with a new textbox, but what if I would like to change the font of an existing textbox?
I can change the font of the style used by the textbox, but it still remains the original font.

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Hi @jacsoa.  Please explain the whole exact scenario you are trying to do, in detail, so I can better understand the situation.  In some situations, simply changing some settings in a TextStyle by code is not enough, if you don't save those changes to the style.  And in some cases, like the case of the sketch behind a TitleBlockDefinition, the style changes you make to a TextBox within the sketch will not be kept if you do not save the edits when you are done editing it.  Sometimes, the document just needs to be updated before the changes are seen too.  Are we talking about a sketch in a part document sketch, or a sketch within a drawing document.  If it is a sketch in a drawing document, then does the sketch belong to a drawing view, a SketchedSymbolDefinition, a BorderDefinition, a TitleBlockDefinition, or some other scenario?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

jacsoa
Participant
Participant

Hi WCrihfield,

 

I have a lot of sheetmetal parts, all with emboss definition. In the sketch of the emboss there is a textbox, with the part number. I would like to change the text font of this textbox. I can change the textbox's style font. If I create a new textbox with this style than the font is right, but if there is an existing textbox, the font of the textbox doesn't changes.

 

0 Likes