Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Text Color

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
imajar
3224 Views, 8 Replies

Text Color

Hello,

 

How can I change the color of text in a sketch using ilogic?

 

I have a couple hundred text entries in a part sketch that I wish to control the font size and color, I have an ilogic rule that can update all of the text entries for font size, but I cannot figure out how to update the color as well.  IV2018.

 

Thanks in advance.


Aaron Jarrett, PE
Inventor 2019 | i7-6700K 64GB NVidia M4000
LinkedIn

Life is Good.
8 REPLIES 8
Message 2 of 9
JamieVJohnson2
in reply to: imajar

You have textbox.color property to play with.  I was not able to use xml format codes to assign individual colors within a text box, so it all text in a textbox to 1 color.

 

FormattedText="<StyleOverride FontSize='0.45466' Bold='True' Italic='True' Underline='True'>red green</StyleOverride>" 

jvj
Message 3 of 9
imajar
in reply to: JamieVJohnson2

Thank you for responding,

 

I have been trying to use the textbox.color, but it is not working for me. . . perhaps I am not doing it right?

 

oTextbox.Color.SetColor(255,0,255)

 When I run this, it completes without errors, but the colors of the text dont change. . . For reference, this is the pertinent part of the code:

 

For Each oTextbox In oSketch.TextBoxes
	'oTextbox = oSketch.TextBoxes(1)
	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 & "</StyleOverride>"
	sFormattedText="<StyleOverride FontSize='0.45466' Bold='True' Italic='True' Underline='True'>red green</StyleOverride>" 
	oTextbox.Formattedtext = sFormattedtext
	'oTextbox.Color.SetColor(255,0,255)
Next

 


Aaron Jarrett, PE
Inventor 2019 | i7-6700K 64GB NVidia M4000
LinkedIn

Life is Good.
Message 4 of 9
JamieVJohnson2
in reply to: imajar

try declaring a color variable, setting that, then assigning it to the textbox

dim oColor as color = thisapplication.transientobjects.createcolor(r,g,b)

textbox.color = color

using the assignment (=) may do more in the background to force the change that were not being told.

 

 

jvj
Message 5 of 9
imajar
in reply to: JamieVJohnson2

Hmmm. . . I cant get that to work without throwing errors, see comments in the code below:

 

Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(255, 0, 255)

For Each oTextbox In oSketch.TextBoxes
	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
oTextbox.Color.setcolor = oColor 'Throws "member not found" error
'oTextbox.Color = oColor 'Throws "member not found" error
Next  

 

Here is the full code, it is meant to work in an active sketch with at least one text box. . .

Dim oSketch As PlanarSketch
If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
	oSketch = ThisApplication.ActiveEditObject
	'MessageBox.Show(oSketch.Name & "is the activeSketch", "iLogic")

	Dim theight As Single
	Dim Message As String
	theight = TextHeight*2.54
	'Message="Text height From Parameter ''TextHeight'' is: " & theight
	'MessageBox.Show(Message,"iLogic")

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

	'Create my Color
	Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(255, 0, 255)

	For Each oTextbox In oSketch.TextBoxes
		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 & "</StyleOverride>"
		'sFormattedText="<StyleOverride FontSize='0.8' Bold='True' Italic='True' Underline='True'>red green</StyleOverride>" 
		'MessageBox.Show("Madeit", "iLogic") 
		oTextbox.color = oColor
		'oTextbox.Formattedtext = sFormattedtext
	Next

	Else
		MessageBox.Show("Active Edit Object is not a Sketch, please activate a sketch", "iLogic")
	End If

 


Aaron Jarrett, PE
Inventor 2019 | i7-6700K 64GB NVidia M4000
LinkedIn

Life is Good.
Message 6 of 9
imajar
in reply to: imajar

And this command throws and "out of present range" error:

 

oTextbox.color.SetColor(oColor)

 


Aaron Jarrett, PE
Inventor 2019 | i7-6700K 64GB NVidia M4000
LinkedIn

Life is Good.
Message 7 of 9
JamieVJohnson2
in reply to: imajar

This test routine just worked:

    Public Sub TestRoutine()
        'use this sub to run test, change as you see fit
        GetInventorApplication()
        If invApp.ActiveDocument IsNot Nothing Then
            Dim tb As Inventor.TextBox = invApp.ActiveDocument.SelectSet(1)
            tb.Color = invApp.TransientObjects.CreateColor(128, 128, 128)

        End If
    End Sub
jvj
Message 8 of 9
imajar
in reply to: JamieVJohnson2

That did it.  Thank You.  Final and complete code is below for anyone interested.

 

'***********Set Variables*************
'Text Height in inches
Dim theight As Single = 2

'Choose or create a Color
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(0,0,0) 'Black
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(255,0,0) 'Red
Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(0,125,0) 'Dark Green
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(6,0,236) 'Dark Blue
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(191,23,174) 'Magenta
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(251,163,0) 'Orange
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(146,90,56) 'Brown
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(20,171,171) 'Cyan
'Dim oColor As Color = ThisApplication.TransientObjects.CreateColor(92,92,92) 'Dark Gray

'***********End of Setting variables*************

Dim oSketch As PlanarSketch
Dim Message As String
Dim tstart As Integer
Dim tend As Integer
Dim curtext As String

oSketch = ThisApplication.ActiveEditObject
theight = theight * 2.54 'Scale the text height from inches to cm

MessageBox.Show("Subroutine to update text size and color for all text boxes in an sketch that is open.  Text height and color are embedded in the ilogic rule","iLogic")
If TypeOf ThisApplication.ActiveEditObject Is Sketch Then 'Verify that a sketch is active
	For Each oTextbox In oSketch.TextBoxes
		Dim tb As Inventor.TextBox = oTextbox
		tb.Color = oColor
		
		'Determine what is in the box
		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 & "</StyleOverride>"
		'sFormattedText="<StyleOverride FontSize='0.8' Bold='True' Italic='True' Underline='True'>red green</StyleOverride>" 
		oTextbox.Formattedtext = sFormattedtext
	Next

	Else
		MessageBox.Show("Active Edit Object is not a Sketch, please activate a sketch", "iLogic")
	End If

 


Aaron Jarrett, PE
Inventor 2019 | i7-6700K 64GB NVidia M4000
LinkedIn

Life is Good.
Message 9 of 9
Ethibaudeau
in reply to: imajar

I figured out why this code was producing these errors:

oTextbox.Color.setcolor = oColor 'Throws "member not found" error
        'oTextbox.Color = oColor 'Throws "member not found" error

 

Per this post: " Solved: Re: ilogic - get value of prompted entry from sketched symbol - Autodesk Community", the textbox needs to be dim'ed as an inventor textbox:

Dim oTextBox As Inventor.TextBox
For Each oTextbox In oSketch.TextBoxes

Adding "Dim oTextBox As Inventor.TextBox" prior to the For loop lets Inventor know you mean an Inventor TextBox, which does have the member "Color". It's a gotcha that I've fallen into before.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report