Is there a way to set the colour of a part using the RGB value? (or perhaps the hexdecimal value?)
I know I can set colours in iLogic by specifying an existing colour (ex... iProperties.PartColor = "Green")
I want to avoid adding the colour to Inventor as a custom colour, then referencing it in iLogic... The goal is, if a user specifies the finish on this part to be this colour, it will always be that RGB value (our standard in-house paint colour). I don't want to add it to the Inventor 'colors' design data... when we upgrade to the next version of Inventor, it's one more item we have to remember to carry over!
Solved! Go to Solution.
Solved by MjDeck. Go to Solution.
Here's one way to do it. This will make a copy of the color Green and create a new color style with your specified RGB values.
Sub Main SetPartColor(240, 200, 70) End Sub Sub SetPartColor(r As Byte, g as Byte, b As Byte) Dim partDoc As PartDocument = TryCast(ThisDoc.Document, PartDocument) If (partDoc Is Nothing) Then MessageBox.Show("This rule can only be run in a part document.", "iLogic") Return End If Dim colorName As String = String.Format("R{0}_G{1}_B{2}", r, g, b) Dim styles As RenderStyles = partDoc.RenderStyles Dim style As RenderStyle = FindStyle(styles, colorName) If (style Is Nothing) Then ' Trace.WriteLine("- Creating new style: " & colorName) style = CreateRgbStyle(styles, colorName, r, g, b) End If If (style IsNot Nothing) Then partDoc.ActiveRenderStyle = style partDoc.Update() End If End Sub Function FindStyle(styles As RenderStyles, name As String) As RenderStyle For Each style As RenderStyle In styles If (String.Equals(style.Name, name, StringComparison.OrdinalIgnoreCase)) Then Return style End If Next Return Nothing End Function Function CreateRgbStyle(styles As RenderStyles, colorName As String , r As Byte, g As Byte, b As Byte) As RenderStyle Dim baseStyle As RenderStyle = FindStyle(styles, "Green") If (baseStyle Is Nothing) Then Return Nothing Dim newStyle As RenderStyle = baseStyle.Copy(colorName) newStyle.SetAmbientColor(r, g, b) newStyle.SetDiffuseColor(r, g, b) Return newStyle End Function
Thanks!
(In the end I added the colour to the style library... since it's our standard in-house colour, we might as well have it in the software!)
Hi
This is a great looking piece of code and exactly what i need but i am having trouble converting it for use in 2010 vba
Is there any chance you could point out which routines i have to use in polace of the ilogic ones please
regards
Mark
Mark,
The routines in that code are mostly VB.NET. ThisDoc.Document is the only iLogic function.
Here's a VBA version (in the attached file SetPartRgbColor.txt).
This will work in Inventor 2010.
In Inventor 2011 and 2012, you should uncomment these lines at the end of the CreateRgbStyle function:
' newStyle.RealisticAppearanceName = ""
' newStyle.RealisticAppearanceInternalName = ""
These lines won't work in 2010. But in 2011 and 2012, they are required to get the correct color if you view the part in Realistic mode. I missed this in the iLogic version. These lines should be added in there as well.