iLogic to Change Text Height

iLogic to Change Text Height

b.mccarthy
Collaborator Collaborator
1,846 Views
24 Replies
Message 1 of 25

iLogic to Change Text Height

b.mccarthy
Collaborator
Collaborator

Hello.

 

I found this code written by Graham Harrison, but I cannot get it to run (comments are closed on his site):

 

oSketch = ThisDoc.Document.ComponentDefinition.Sketches("Sketch1")
oTextbox = oSketch.TextBoxes(1)

Dim sFormattedtext As String
Dim theight As Single

theight = TEXTHEIGHT
theight = theight / 10

Dim tstart As Integer
Dim tend As Integer
Dim curtext As String

tstart = InStr(oTextbox.Formattedtext, "'>") + 1
If tstart <> 1 Then
    tend = InStr(oTextbox.Formattedtext, "</Style") - 1
    curtext = Mid(oTextbox.Formattedtext, tstart + 1, tend - tstart)
Else
    curtext = oTextbox.Formattedtext  
End If

sFormattedtext = "<StyleOverride FontSize='" & theight & "'>" & curtext 
sFormattedtext = sFormattedtext & "</StyleOverride>"
oTextbox.Formattedtext = sFormattedtext

My sketch name and parameter match, but every time I run it, I get this error.

2021-10-15 397.jpg

I have tested it on numerous parts, and the error is the same.

Of the many code offerings I have found here, this will give me what I need, if I can get it to work.

Any ideas why it will not run?

TIA

 

0 Likes
Accepted solutions (1)
1,847 Views
24 Replies
Replies (24)
Message 2 of 25

JelteDeJong
Mentor
Mentor

I'm not sure how that is supposed to work. But maybe this will work for you.

Dim textHeight As String = "1" 

Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition
Dim sketch As PlanarSketch = def.Sketches.Item(1)
Dim textBox As Inventor.TextBox = sketch.TextBoxes.Item(1)
Dim text As String = textBox.FormattedText

Dim startTag = "<StyleOverride FontSize='" & textHeight & "'>"
If (text.Contains("FontSize")) Then
    text = System.Text.RegularExpressions.Regex.Replace(
        text,
        "<StyleOverride FontSize='[\d.,]+'>",
        startTag)
Else
    Dim endTag = "</StyleOverride>"
    text = startTag & text & endTag
End If

textBox.FormattedText = text

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 25

b.mccarthy
Collaborator
Collaborator

The code works... almost..

The initial text height was 0.5". The target was 0.75". The result was 0.393"

0 Likes
Message 4 of 25

b.mccarthy
Collaborator
Collaborator

Do you have any idea what is happening?

 

Thank you for offering to help.

0 Likes
Message 5 of 25

A.Acheson
Mentor
Mentor

If your using the rule as above then it is using system units so cm.

Dim textHeight As String = "1"

 Text height of .75”

Dim textHeight As String = "1.905"

 or replace with your parameter. Hopefully that is all it is. 

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

JelteDeJong
Mentor
Mentor

Be aware that that depending on your local settings it might need to be:

Dim textHeight As String = "1,905"

with a comma instead of a point.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 7 of 25

b.mccarthy
Collaborator
Collaborator

Hello, again.

 

Making the changes suggested results in a new error:

 

2021-10-16 397.jpg

 

Thank you.

0 Likes
Message 8 of 25

A.Acheson
Mentor
Mentor

Can you show the more info tab of the error message? The first tab is only a general error and not always that helpful. 

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

b.mccarthy
Collaborator
Collaborator

2021-10-16 398.jpg

0 Likes
Message 10 of 25

JelteDeJong
Mentor
Mentor

Are you sure that the sketch you use has a textbox? Try the following (updated) rule

Dim textHeight As String = "1.905" 

Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition
Dim sketch As PlanarSketch = def.Sketches.Item(1)
Dim textboxes As TextBoxes = sketch.TextBoxes
If (textboxes.Count = 0) Then
	
	MsgBox(String.Format("Sketch '{0}' does not contain any textboxes.",
			sketch.Name))
	return
End If
Dim textBox As Inventor.TextBox = sketch.TextBoxes.Item(1)
Dim text As String = textBox.FormattedText


Dim startTag = "<StyleOverride FontSize='" & textHeight & "'>"
If (Text.Contains("FontSize")) Then
    Text = System.Text.RegularExpressions.Regex.Replace(
        Text,
        "<StyleOverride FontSize='[\d.,]+'>",
        startTag)
Else
    Dim endTag = "</StyleOverride>"
    text = startTag & text & endTag
End If

textBox.FormattedText = text

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 11 of 25

b.mccarthy
Collaborator
Collaborator

This is a simple part, simulating an engraved plate. The plate has a sketch named "Base" and the engraved text has sketch named "Sketch1", which has a textbox:

2021-10-16 402.jpg

 

When I run the updated code, this presents...

2021-10-16 403.jpg

...and the code exits.

 

The "Base" sketch only contains the profile of the plate.

0 Likes
Message 12 of 25

A.Acheson
Mentor
Mentor

It looks like the code is looking at the first sketch which does not contain the text. Either change the index number if the  text is in the second sketch or reference the sketch name.

Dim sketch As PlanarSketch = def.Sketches.Item(2)
Dim sketch As PlanarSketch = def.Sketches.Item(“Sketch1”)

 

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

b.mccarthy
Collaborator
Collaborator

Thank you.

 

I chose the 2nd option, and the error dialog disappeared. However, another issue arose...

 

The initial text height was .5". I changed the "textHeight" parameter to .75", ran the rule, clicked the update icon, and the text height updated. However, changing the values for the textHeight (.25", 1.0" 15mm) had no effect and the text stayed at .75".

 

I then modified the sketch, and changed it to different height values, and ran the rule. In each case, the rule changed the height of the text to .75". regardless of the value of the textHeight parameter.

 

I then created a new part, set the textHeight parameter to .375" and created text with the same height. I ran the rule expecting nothing to change, but the text changed to .75"

 

My guess was that the cause lies in this line:

 

Dim textHeight As String = "1.905" 

I tested this by changing "1.905" to "1.27", ran the rule and the text updated to this new height. It appears the rule is not referencing the parameter, though I do not know how to change that.

 

Thanks again.

0 Likes
Message 14 of 25

A.Acheson
Mentor
Mentor

You are correct the string value of textHeight is set at the top of the rule to the static value in JelteDeJong sample. It does not reference any user parameter. 

Dim textHeight As String = "1.905" 

 To change it to your user parameter  write it as below. Declaring variable  type and setting its name on the left and value on the right. In this case the value is your user parameter. 

Dim textHeight As String = textHeight 
If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 15 of 25

b.mccarthy
Collaborator
Collaborator

I changed the line as you suggested. The "textHeight" parameter was set to .375". After running the rule, the text size changed to .120". Modifying "textHeight" now has no effect.

 

I inserted a message box after the new line to display the "textHeight" value, and this shows:

 

2021-10-17 403.jpg

0 Likes
Message 16 of 25

A.Acheson
Mentor
Mentor

Can you post the full rule you are using?

Is the rule run as an internal rule? If so the parameter 

textHeight

will be blue in colour.

If your running it as an  external rule this will be Parameter("textHeight"). 

 

 

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

b.mccarthy
Collaborator
Collaborator

This will be run as an external rule, and includes your change. The color is orange:

 

Dim textHeight As String = Parameter("textHeight")

Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition
Dim sketch As PlanarSketch = def.Sketches.Item("Sketch1")
Dim textboxes As TextBoxes = sketch.TextBoxes
If (textboxes.Count = 0) Then
	
	MsgBox(String.Format("Sketch '{0}' does not contain any textboxes.",
			sketch.Name))
	Return
End If
Dim textBox As Inventor.TextBox = sketch.TextBoxes.Item(1)
Dim text As String = textBox.FormattedText

Dim startTag = "<StyleOverride FontSize='" & textHeight & "'>"
If (text.Contains("FontSize")) Then
    text = System.Text.RegularExpressions.Regex.Replace(
        text,
        "<StyleOverride FontSize='[\d.,]+'>",
        startTag)
Else
    Dim endTag = "</StyleOverride>"
    text = startTag & text & endTag
End If

textBox.FormattedText = text 

 

The rule now updates, but the scale is still off. I set textHeight to 1 in and the result is 0.393700787 in. 

 

So all it needs is a unit conversion...?

0 Likes
Message 18 of 25

WCrihfield
Mentor
Mentor

Yep...very common, and very irritating.  All raw numbers used in iLogic, if they represent any type of measurement, they will likely be automatically understood as being in 'database' units, instead of in 'document' units.  The database units for length is centimeters.  So it is understanding your inches units as if they were centimeters (1 / 2.54 = 0.3937007).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 19 of 25

WCrihfield
Mentor
Mentor

Your parameter named "textHeight" is a numerical parameter, and not a text type user parameter, right?  If so, then why are you setting its numerical value as the value of a String type variable instead of a Double type variable?  I noticed that you are using the Parameter("textHeight") iLogic snippet, which will actually return the value of numerical parameters (that are not unitless) in document units, so there shouldn't be any initial conversion of units in this case.  Generally all other methods of getting a parameter's value results in getting a value in database units.  However, if the parameter just has a simple numerical value and not an equation, you can get its Expression (a String) instead of its Value (Double), and that String can then be converted to Double using one of the data type conversion tools like CDbl().  The Expression definitely won't be understood as database units because it is a String, and converting it from a String to a Double won't convert its units because it won't be understood as a type of measurement, just a simple, direct data type conversion.

 

 

Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition
oTHExp = def.Parameters.Item("textHeight").Expression
Dim oTextHeight As Double = CDbl(oTHExp)

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 20 of 25

WCrihfield
Mentor
Mentor

Sorry for the runaround, but I just double checked the online help page for dealing with the XML stuff in FormattedText situations, and found another key piece of information that will help you understand what's going on here.  It basically tells you directly that it is expecting you to input the text size ("FontSize") in centimeters.  So it looks like you will actually need to do some unit conversion on purpose just for that situation.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes