iLogic Code runs only once per drawing file

iLogic Code runs only once per drawing file

rnandan
Explorer Explorer
845 Views
11 Replies
Message 1 of 12

iLogic Code runs only once per drawing file

rnandan
Explorer
Explorer

I am attempting to update the font and font size of a general note object in a drawing. The note contains text in different fonts and sizes.
From the code below I was able to change it once. I retried the code with a different font and size but the text format does not change. 
It works on a different drawing though, but exactly once. I'm new to writing iLogic, so not sure what basic thing I'm missing.


Dim oDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oPickedDrawingNote As Inventor.DrawingNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select text")
'oPickedDrawingNote.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
Dim eText As String = oPickedDrawingNote.FormattedText

oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='0.12*2.54' Bold='False' Underline='False'>" & eText & "</StyleOverride>"

oDrawDoc.Update()



0 Likes
846 Views
11 Replies
Replies (11)
Message 2 of 12

Curtis_W
Consultant
Consultant

@rnandan, I think you just want to use the Text rather than FormatedText for this line, and everything else can stay the same. 

 

Hope that helps, Curtis

 

Dim eText As String = oPickedDrawingNote.Text


 

 EESignature

0 Likes
Message 3 of 12

Curtis_W
Consultant
Consultant

If you want to have a toggle, you can use something like this:

Dim oDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument

While True

	Dim oPickedDrawingNote As Inventor.DrawingNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select text (ESC to continue)")

	If IsNothing(oPickedDrawingNote) Then Exit While

	'oPickedDrawingNote.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
	Dim eText As String = oPickedDrawingNote.Text
	
	Logger.Info(oPickedDrawingNote.FormattedText)
	
	If oPickedDrawingNote.FormattedText.Contains("Tahoma") Then
		oPickedDrawingNote.FormattedText = "<StyleOverride Font='Segoe Print' FontSize='0.12*2.54' Bold='False' Underline='False'>" & eText & "</StyleOverride>"
	Else 'if it's anything else, format like this:
		oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='0.12*2.54' Bold='False' Underline='False'>" & eText & "</StyleOverride>"
	End If

	oDrawDoc.Update()

End while

 

Note too the logger line output might be something to be aware of, for seeing the formatting, etc.

Curtis_Waguespack_0-1764877510277.png

See link for info about using the logger if that's new to you:
https://help.autodesk.com/view/INVNTOR/2026/ENU/?guid=GUID-BE996AA3-DE49-4765-86A7-129DD7B42A4A

 EESignature

0 Likes
Message 4 of 12

rnandan
Explorer
Explorer

I changed the declaration for eText to Text rather than FormattedText. The code runs and changes the Font and size reliably but now all the break lines are gone. The multiline text is just one single line.

Dim eText As String = oPickedDrawingNote.Text
oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='0.3' Bold='False' Underline='False'>" & eText & "</StyleOverride>"



Out of curiosity I tried the following as well where the FormattedText was swapped. The result was that the fonts and font size changed every time, but the textbox now contained the xml tags as well and one single line again.

Dim eText As String = oPickedDrawingNote.FormattedText
oPickedDrawingNote.Text = "<StyleOverride Font='Tahoma' FontSize='0.3' Bold='False' Underline='False'>" & eText & "</StyleOverride>"

 
So. my observation is that "FormattedText" cannot come consecutively. I'm curious to know why. 

0 Likes
Message 5 of 12

jnowel
Collaborator
Collaborator

For a single line text (w/o any StyleOverrides), the "oPickedDrawingNote.Text" and "oPickedDrawingNote.FormattedText" is the same

That is the reason why your code initially works the first time. After the code ran, Text and FormattedText won't be the same anymore

For multi-line text (w/o any StyleOverrides), the "oPickedDrawingNote.Text" and "oPickedDrawingNote.FormattedText" is NOT the same .
FormattedText has additional line breaks "<Br\> inside that isn't shown in Text property.

As for a solution, you can try below code

Dim oDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
	Dim oPickedDrawingNote As Inventor.DrawingNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select text")
	
	'Logger.Info("Method2")
	Dim eText As String = oPickedDrawingNote.FormattedText
	Logger.Info(eText)
	
	'Remove All the styleoverrides
	Dim sText As String() = eText.Split("<StyleOverride ", StringSplitOptions.RemoveEmptyEntries)
	For Each subText In sText
		Dim sOldStyle As String = Left(subText, InStr(subText, ">") -1)
		eText = eText.Replace("<StyleOverride " & sOldStyle & ">", String.Empty)
		eText = eText.Replace("</StyleOverride>", String.Empty)
	Next
	
	'Apply New Style Overrides
	oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='0.3' Bold='False' Underline='False'>" & eText & "</StyleOverride>"


I'm not too much familiar with the XML formatting, but it seems it is smart enough to apply the styleoverrides after the line breaks.
as an example:

jnowel_0-1764917083322.png

 

the Formatted text of that is

TEXT<Br/>TEST

after the rule, the expected value of the FormattedText is

<StyleOverride Font='Tahoma' FontSize='0.3' Bold='False' Underline='False'>TEXT<Br/>TEST</StyleOverride>

but surprising enough, the FormattedText Property is able to "correct" it to 

<StyleOverride Font='Tahoma' FontSize='0.3'>TEXT</StyleOverride><Br/><StyleOverride Font='Tahoma' FontSize='0.3'>TEST</StyleOverride>


-----------
btw, I think it cannot do math expressions for the font size in XML format directly. so have to separate it (or just assign the computed value)

so instead of doing it as 

oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='0.12*2.54' Bold='False' Underline='False'>" & eText & "</StyleOverride>"

do it as 

oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='" & 0.12*2.54 & "' Bold='False' Underline='False'>" & eText & "</StyleOverride>"



Message 6 of 12

rnandan
Explorer
Explorer

Hi jnowel,
The code returned a compile error.

Error on Line 9 : Argument matching parameter 'separator' narrows from 'String' to '1-dimensional array of Char'.

0 Likes
Message 7 of 12

jnowel
Collaborator
Collaborator

weird, it's working on my end w/o issues in Inventor 2026

anyway, try this instead

Dim oDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oPickedDrawingNote As Inventor.DrawingNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select text")

'Logger.Info("Method2")
Dim eText As String = oPickedDrawingNote.FormattedText
'Logger.Info(eText)

'Remove All the styleoverrides
'Dim sText As String() = eText.Split("<StyleOverride ", StringSplitOptions.RemoveEmptyEntries)
Dim sText As String() = eText.Split("<StyleOverride ")
For Each subText In sText
	If InStr(subText, ">") < 1 Then Continue For
	Dim sOldStyle As String = Left(subText, InStr(subText, ">") -1)
	eText = eText.Replace("<StyleOverride " & sOldStyle & ">", String.Empty)
	eText = eText.Replace("</StyleOverride>", String.Empty)
Next

'Apply New Style Overrides
oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='0.3' Bold='False' Underline='False'>" & eText & "</StyleOverride>"

 

@rnandan can you check this new line12

Message 8 of 12

rnandan
Explorer
Explorer

I'm on Inventor 2024. The new code gave an error on line 13

Argument 'Length' must be greater or equal to zero.

0 Likes
Message 9 of 12

jnowel
Collaborator
Collaborator

weird. i though the current line12 will prevent that issue on line13.

can you send me the idw/dwg just to check what i missed?

anyway, i'll edit line12 to a different one in a minute

Message 10 of 12

Curtis_W
Consultant
Consultant

@jnowel wrote:

I'm not too much familiar with the XML formatting...

@jnowel, in case it's helpful you can find some info for this online here:
https://help.autodesk.com/view/INVNTOR/2025/ENU/?guid=GUID-915B75BA-4696-4fc7-A2C6-54B2BACDE7E1

Or you can find the same reference material, but a bit easier to read in your local API help by searching the API help for "Text Tags" or "XML Tags for FormattedText", etc.

Curtis_Waguespack_1-1764949322765.png

 

Curtis_Waguespack_0-1764949301994.png

 

 EESignature

0 Likes
Message 11 of 12

Curtis_W
Consultant
Consultant

@rnandan wrote:

I'm on Inventor 2024. The new code gave an error on line 13

Argument 'Length' must be greater or equal to zero.


@rnandan , here is a version of @jnowel's code with some logger lines, this might help you determine what the issue is with line 13 ?

Curtis_Waguespack_0-1764949951853.png

 

iLogic Logger reference: https://help.autodesk.com/view/INVNTOR/2026/ENU/?guid=GUID-BE996AA3-DE49-4765-86A7-129DD7B42A4A

 

Dim oDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oPickedDrawingNote As Inventor.DrawingNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select text")

'Logger.Info("Method2")
Dim eText As String = oPickedDrawingNote.FormattedText
'Logger.Info(eText)

'Remove All the styleoverrides
'Dim sText As String() = eText.Split("<StyleOverride ", StringSplitOptions.RemoveEmptyEntries)
Dim sText As String() = eText.Split("<StyleOverride ")
Logger.Info(sText.Length & " items in the split array:")
For Each subText In sText
	Logger.Info(subText)
	If InStr(subText, ">") < 1 Then Continue For
	Dim sOldStyle As String = Left(subText, InStr(subText, ">") -1)
	eText = eText.Replace("<StyleOverride " & sOldStyle & ">", String.Empty)
	eText = eText.Replace("</StyleOverride>", String.Empty)
Next

'Apply New Style Overrides
oPickedDrawingNote.FormattedText = "<StyleOverride Font='Tahoma' FontSize='0.3' Bold='False' Underline='False'>" & eText & "</StyleOverride>"

 

 EESignature

0 Likes
Message 12 of 12

jnowel
Collaborator
Collaborator

I think i figured out why my previous line12 introduced some errors.
i only considered empty strings as the other values of subText which isn't the case

If Len(subText) < 1 Then Continue For

a single line with a combination of no styleoverride and styleoverrides can cause that error.

 

and with the link @Curtis_W gave about the XML tags,
it reminded me that certain characters/symbols need their styleoverrides. and removing the styleoverride will cause an issue
the symbols are mostly these ones.

jnowel_1-1765003073754.png

 

as for the updated code. it will look like this:

Dim oDrawDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim oPickedDrawingNote As Inventor.DrawingNote = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingNoteFilter, "Select text")

'Logger.Info("Method3")
Dim eText As String = oPickedDrawingNote.FormattedText
'Logger.Info(eText)

'Change All the styleoverrides, add Styleovverride when there is none applied
Dim sText As String() = eText.Split("<StyleOverride ")
Dim sNewStyle As String = "Font='Tahoma' FontSize='0.3' Bold='False' Underline='False'"
Dim sNewStyleSymbols As String = "Font='AIGDT' FontSize='0.3' Bold='False' Underline='False'" 'this is used for the 'special' drafting symbols that needs the AIGDT font to work
Dim eTextNew As String = String.Empty

For Each subText In sText
	'Logger.Info(subText)
	If Not subText.Contains("'>") Then
		'This infers that no style override is found, so apply the styleoverride
		subText = "<StyleOverride " & sNewStyle & ">" & subText & "</StyleOverride>"
	Else
		'With Styleoverrides
		Dim sOldStyle As String = Left(subText, InStr(subText, ">") -1)
		If sOldStyle.Contains("Font='AIGDT'") Then
			subText = "<StyleOverride " & subText.Replace(sOldStyle, sNewStyleSymbols)
		Else
			subText = "<StyleOverride " & subText.Replace(sOldStyle, sNewStyle)
		End If
		
		'Now we need to check if the subText has some portion w/o any styleoverrides
		Dim sText2 As String() = subText.Split("</StyleOverride>")
		sText2(0) = sText2(0) & "</StyleOverride>" 'this is the portion where the current styleoverride applies
		If sText2.Length > 1 Then 'this is just to prevent some error (as i'm not 100% sure the Split will always return 2 items as 2nd item, sText2(1), might be an empty string).
			sText2(1) = "<StyleOverride " & sNewStyle & ">" & sText2(1) & "</StyleOverride>" 'this portion is something the styleoverride isn't applied yet so add the styleoverride
			subText = sText2(0) & sText2(1)
		Else
			subText = sText2(0)
		End If
		
	End If
	eTextNew &= subText
Next

'Apply New Style Overrides
oPickedDrawingNote.FormattedText = eTextNew