Use iLogic to assign a color to one part face

Use iLogic to assign a color to one part face

Anonymous
Not applicable
5,413 Views
13 Replies
Message 1 of 14

Use iLogic to assign a color to one part face

Anonymous
Not applicable

Hi.

 

I am trying to create an iLogic rule that will change the colour on one specific face of a part (a simple cuboid shape).  When I open the part and the form opens to specify the part parameters, there is a "yes/no" checkbox in there.  Selecting "Yes" means change to a specified colour (in this case green) and "No" means leave it as default.

 

Could someone please advise of the code required to do this.

 

 

Thanks,

 

Terry 

0 Likes
Accepted solutions (1)
5,414 Views
13 Replies
Replies (13)
Message 2 of 14

mcgyvr
Consultant
Consultant

Do you need the code to do everything? Form,etc...?

Will this always be the same face on the same part or will you need to determine the face name for each different part you open?

Can you post your ipt file?

What version of Inventor?

 

The basic code to select a named face is..

Dim partDoc As PartDocument = ThisDoc.Document
Dim namedEntities = iLogicVb.Automation.GetNamedEntities(partDoc)
Dim face0 As Face = namedEntities.FindEntity("Face0")
face0.Appearance = partDoc.AppearanceAssets(2)


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 3 of 14

Anonymous
Not applicable

Thanks for the reply.

 

I already have a functioning part and form / rules set up.  All I am after is the code/snippets to colour one face (the same face every time) based on a true / false input for a named parameter.

 

It is just a simple box (cuboid).  When the part is opened the input form appears to enter Job No., Drg. No., Date etc. then the Length, Width Height.  

 

I have added a true/false parameter so that a "Yes" at the prompt can be used to colour the named face green.  Otherwise a "No" leaves it alone.

 

I did right click and name the face (listed in iLogic Geometry tab) hoping that this could then be picked up in the code, but it is saying that the name is not recognized.

 

I am using Inventor 2020 Professional.

 

Hope this all makes sense.

 

 

Thanks,

 

Terry

 

PS Sorry can't post the files, company rules.

 

Message 4 of 14

Curtis_Waguespack
Consultant
Consultant

Hi @Anonymous 

 

Here is an attached Inventor 2020 file as an example:

https://forums.autodesk.com/autodesk/attachments/autodesk/78/775245/1/Cube%20Color%20Face.ipt

 

Also just as a tip, you can search and ask programming questions of this type on the Inventor Customization forum too:
http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Here's the code (for future searches) and a couple of screen shots concerning the face naming steps, in case those help someone else in the future as well.

 

Assign Name.png

 

assign name2.PNG

 

'set this rule to watch these parameters
oTrigger = FaceIsColored
oTrigger = FaceColor

Dim oDoc As PartDocument 
oDoc = ThisApplication.ActiveDocument

'create list of colors
Dim oList As New ArrayList
Dim oStyle As RenderStyle

For Each oStyle In oDoc.RenderStyles
  oList.Add(oStyle.Name)
Next

MultiValue.List("FaceColor") = oList

'get color name to use
Dim oColor As String
oColor = Parameter("FaceColor")

Dim oAssetLib As AssetLibrary
oAssetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")

' Get an asset in the library
Dim oLibAsset As Asset
oLibAsset = oAssetLib.AppearanceAssets.Item(oColor)


Dim oLocalAsset As Asset
Try 'Copy the asset locally.
	oLocalAsset = oLibAsset.CopyTo(oDoc)
Catch 'or just use it if it's already local
	oLocalAsset = oDoc.Assets.Item(oColor)
End Try

Dim oEntities As Object 
oEntities = iLogicVb.Automation.GetNamedEntities(oDoc)

'get the named face
Dim oFace As Face 
oFace = oEntities.FindEntity("Target Face")

'set the color if paramter is true
If Parameter("FaceIsColored") = True Then
	oFace.Appearance = oLocalAsset
Else
	oFace.Appearance = oDoc.ActiveAppearance
End If


 

 

EESignature

Message 5 of 14

Curtis_Waguespack
Consultant
Consultant

Hi @Anonymous 

 

It occurred to me after reading your second reply, that if you're just wanting to set the face to a known local color, then you can use a much simpler rule similar to what mcgyvr  suggested, here's and example of that too.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

'set this rule to watch this parameter
oTrigger = FaceIsColored

Dim oDoc As PartDocument 
oDoc = ThisApplication.ActiveDocument

Dim oEntities As Object 
oEntities = iLogicVb.Automation.GetNamedEntities(oDoc)

'get the named face
Dim oFace As Face 
oFace = oEntities.FindEntity("Target Face")

'set the color if paramter is true
If Parameter("FaceIsColored") = True Then
	oFace.Appearance = oDoc.Assets.Item("Dark Green")
Else
	oFace.Appearance = oDoc.ActiveAppearance
End If 

EESignature

Message 6 of 14

Anonymous
Not applicable

Hi.

 

Thanks very much for the replies.

 

The simplified code that you sent in the last post Curtis looks like what I am after.  I put this in a test part and tried it but it is throwing up an error and I am not quite sure what it is?

 

Any ideas how to solve this?

 

I have attached the ipt and jpegs of the error messages

 

Many thanks,

 

Terry

0 Likes
Message 7 of 14

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @Anonymous 

The error is coming from the fact that Dark Green is not a local asset.... so if this is a template you can just add Dark Green to the part file manually as shown below, and then save the template.

 

Or if you're looking to make this rule more universal you'd need to use something similar to the longer version posted yesterday, which gets Dark Green from the Appearance Library and loads it locally. I've added that part of the code to your rule in the example below.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Local Asset.PNG

 

 

'set this rule to watch this parameter
oTrigger = Green

Dim oDoc As PartDocument 
oDoc = ThisApplication.ActiveDocument

'get color name to use
Dim oColor As String
oColor = "Dark Green"

Dim oAssetLib As AssetLibrary
oAssetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")

' Get an asset in the library
Dim oLibAsset As Asset
oLibAsset = oAssetLib.AppearanceAssets.Item(oColor)


Dim oLocalAsset As Asset
Try 'Copy the asset locally.
	oLocalAsset = oLibAsset.CopyTo(oDoc)
Catch 'or just use it if it's already local
	oLocalAsset = oDoc.Assets.Item(oColor)
End Try


Dim oEntities As Object 
oEntities = iLogicVb.Automation.GetNamedEntities(oDoc)

'get the named face
Dim oFace As Face 
oFace = oEntities.FindEntity("GreenFace")

'set the color if paramter is true
If Parameter("Green") = True Then
	oFace.Appearance = oDoc.Assets.Item(oColor)
Else
	oFace.Appearance = oDoc.ActiveAppearance
End If 

EESignature

Message 8 of 14

mcgyvr
Consultant
Consultant

In the code provided by Curtis (that caused the error for you)...

If the color you want isn't "Dark Green" replace that in the code with whatever appearance name you want to use. Just spell it exactly as it is in the library.  In your case that might be "Green (Clear)" or whatever local appearance you want applied to that face.. Curtis just used "Deep Green" as an example.. Change as needed..

 

oFace.Appearance = oDoc.Assets.Item("Green (Clear)")

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 9 of 14

Anonymous
Not applicable

Thanks very much for the replies.

 

The last amended code works perfectly.  Thanks very much for all your help!!

 

Cheers,

 

Terry

0 Likes
Message 10 of 14

Anonymous
Not applicable

I'm trying to use something almost identical to this, but I'd like to have the color name be a variable that is defined using a mix of text and parameters.   It will work if I define the variable using just a text string, but when I try to combine a text string with a parameter to form one long text string, it errors out.  Is this functionality not supported? 

 

ColorName1 = "SomeTxt-" & Width & "in-" & Color1'errors out
ColorName1 = "SomeTxt-36in-Color"'works fine

Width and Color1 are parameters.

0 Likes
Message 11 of 14

Curtis_Waguespack
Consultant
Consultant

Hi @Anonymous ,

 

If you add a message box line as shown below, does that return the expected string? and does that string match the name of your color (aka Appearance style name) ?

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

ColorName1 = "SomeTxt-" & Width & "in-" & Color1
MessageBox.Show(ColorName1, "iLogic")

 

EESignature

0 Likes
Message 12 of 14

Anonymous
Not applicable

Yep, I did try that and the two strings do match.  That's what's puzzling me.

0 Likes
Message 13 of 14

WCrihfield
Mentor
Mentor

Try the line this way.  It may just be a information type mis-match.

ColorName1 = "SomeTxt-" & Width.ToString & "in-" & Color1.ToString
MessageBox.Show(ColorName1, "iLogic")

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 14

PCP7
Participant
Participant

Hey, I used your code to change the color of selected faces, but it only works if I run it within the part. If I try to run it from the assembly where components are used it gives me following error :

 

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.PartDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)

at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

CODE USED :

 

  1. Inside part : 

    Dim oDoc As PartDocument 
    oDoc = ThisApplication.ActiveDocument
    
    'get color name to use
    Dim oColor As String
    oColor = "Black"
    
    Dim oAssetLib As AssetLibrary
    oAssetLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")
    
    ' Get an asset in the library
    Dim oLibAsset As Asset
    oLibAsset = oAssetLib.AppearanceAssets.Item(oColor)
    
    
    Dim oLocalAsset As Asset
    Try 'Copy the asset locally.
    	oLocalAsset = oLibAsset.CopyTo(oDoc)
    Catch 'or just use it if it's already local
    	oLocalAsset = oDoc.Assets.Item(oColor)
    End Try
    
    
    Dim oEntities As Object 
    oEntities = iLogicVb.Automation.GetNamedEntities(oDoc)
    
    'get the named face
    Dim oFace1 As Face 
    Dim oFace2 As Face 
    oFace1 = oEntities.FindEntity("Tail A1")
    oFace2 = oEntities.FindEntity("Tail A2")
    
    oFace1.Appearance = oDoc.Assets.Item(oColor)
    oFace2.Appearance = oDoc.Assets.Item(oColor)
  2. From assembly:

    iLogicVb.RunRule("INSIDE RAIL", "Tail A")
    iLogicVb.RunRule("OUTSIDE RAIL", "Tail A")

    Is there a way to make it run from the assembly ? 
0 Likes