iLogic to Change Text Height

iLogic to Change Text Height

b.mccarthy
Collaborator Collaborator
1,849 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,850 Views
24 Replies
Replies (24)
Message 21 of 25

b.mccarthy
Collaborator
Collaborator

So I have this line of code to convert units, but I do not know how to implement it:

 

oPartDoc.UnitsOfMeasure.ConvertUnits(textHeight,"cm","in")

 

0 Likes
Message 22 of 25

WCrihfield
Mentor
Mentor
Accepted solution

Sorry for the delays.  I've got stuff going on at work too.  😉

Actually, I'm thinking that since iLogic will automatically retrieve the value from that parameter as centimeters, when we get it through the normal API route, we can just use that directly in this situation.  We won't even need any math or units conversion tool/routine.  We will just collect its value to a Double type variable, then use that variable within that FormattedText line, instead of that unquoted local parameter name.  But, we will leave a dummy variable at the top of the rule, just so it will still fire when the value of that local parameter changes.  I'm not real familiar with the technique you are using within the Replace method there, but I'm going to assume it is working OK for you.

Try this version of your rule and see if it works better for you:

oDV = textHeight 'just to make the auto trigger functionality work
Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition

'this will get the value of that parameter in centimeter units by default
Dim oTextHeight As Double = def.Parameters.Item("textHeight").Value

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='" & oTextHeight & "'>"
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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 23 of 25

b.mccarthy
Collaborator
Collaborator

@WCrihfield 
Thank you for the information.

From everyone's help, I understand the necessity for the unit conversion. I did not write the initial code (thank you @JelteDeJong ). I am only trying to get it to work for my application. I found this line of code that will convert the units:

oPartDoc.UnitsOfMeasure.ConvertUnits(textHeight,"cm","in")

...but I do not know how to correctly format or implement it.

 

Thank you.

0 Likes
Message 24 of 25

WCrihfield
Mentor
Mentor

OK. So basically that method called "ConvertUnits" is a Function, so it will return a value.  The value it will return will be a Double.  So you would need to put a variable or something in front of that line to collect the value from it.  But of course, the oPartDoc variable at the start of that line must have been defined before that line of code, so it will be recognized.

Dim oPartDoc As PartDocument = ThisDoc.Document
oConvertedValue = oPartDoc.UnitsOfMeasure.ConvertUnits(textHeight, "cm", "in")
MsgBox(oConvertedValue,,"")

  In this case, since the unquoted name of that parameter will return the value of that parameter in document units (inches), and the way that line is currently laid out, it is going to try to convert the true inches units of that parameter as if it were already centimeters, back into inches.

Here's the thing to remember.

 

  • When using the unquoted name of a local parameter in your rule, it will represent the true document units value of that parameter.
  • When using the Parameter("textHeight") iLogic snippet, that too will return the true document units value of that parameter.
  • When getting the value of that parameter using the normal API route, (Document.ComponentDefinition.Parameters.Parameter.Value), that will return the database units version of that parameter's value.
  • When using this route to get a parameter's value it will also return database units:
Parameter.Param("textHeight").Value

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 25 of 25

b.mccarthy
Collaborator
Collaborator

@WCrihfield Your code works well enough for my needs.

 

Thank you, everyone, for your help!!

0 Likes