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.
Solved! Go to Solution.
Solved by imajar. Go to Solution.
Solved by JamieVJohnson2. Go to Solution.
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>"
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
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.
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
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
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
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.