Change component color while working in top level assembly.

Change component color while working in top level assembly.

Anonymous
Not applicable
2,539 Views
8 Replies
Message 1 of 9

Change component color while working in top level assembly.

Anonymous
Not applicable

Hello all,

 

I am trying to write an iLogic code that will allow me to change the color of components while working in the top level of an assembly.  I have written a code that allows me to turn off the visibility of certain components in the top level as well as any sub assembly level, so I was hoping that I could just modify that to change the color of the component rather than just hide it, but i'm coming up with no luck.  The code that I have for the component visiblity is below, in case anyone knows of how it could potentially be modified for component color.


Any help is greatly appreciated.

 

Thanks!

 

 

SyntaxEditor Code Snippet

 ' Rule toggles all components visibility
Dim oAsmDef As AssemblyComponentDefinition _
= ThisApplication.ActiveDocument.ComponentDefinition
Dim oList As ComponentOccurrencesEnumerator = oAsmDef.Occurrences.AllLeafOccurrences
For Each oOcc As ComponentOccurrence In oList




'Get the browser name of the component
oOccName = oOcc.Name
'Check if the desired bit of text is in the component's name
If InStr(oOccName, "Component") > 0 Then
If oOcc.Visible = False Then
oOcc.Visible = False
Else

End If
End If
Next

 

0 Likes
2,540 Views
8 Replies
Replies (8)
Message 2 of 9

MechMachineMan
Advisor
Advisor
At what level of the part do you want the colour to be changed?

You will need to access the appearance of the file.

The question is whether you want it to be changed at a part/assembly level, OR JUST overridden for the top level.

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 9

Anonymous
Not applicable

MechMachineMan,

 

I really just need to overide it at the top level.  The actual components may be inside of some sub assemblies.  But the color change is only needed in the top level that is currently being worked in.

 

Thanks!

0 Likes
Message 4 of 9

Curtis_Waguespack
Consultant
Consultant

Hi crenshap,

 

Here's a quick example that might or might not get you pointed in the right direction. The main thing to remember is that the color must already be locally stored in the assembly or the code must copy it down from the appearance library first.

 

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

 

 

'get user input
oRed = InputRadioBox("Prompt", "Red", "Black", True, "iLogic")

'define the assembly
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

'define the appearance library
Dim oLib As AssetLibrary
oLib = ThisApplication.AssetLibraries("Autodesk Appearance Library")

'make sure colors are in assembly doc
Dim libAsset As Asset
libAsset = oLib.AppearanceAssets.Item("Smooth - Red")
Try 
localAsset = libAsset.CopyTo(oAssyDoc)
Catch
'catch error if it's already local
End Try

libAsset = oLib.AppearanceAssets.Item("Smooth - Black")
Try 
localAsset = libAsset.CopyTo(oAssyDoc)
Catch
'catch error if it's already local
End Try

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAssyDoc.ComponentDefinition
 
Dim oOcc As ComponentOccurrence
Dim oAsset As Asset

'set color overrides of components at the assembly level
For Each oOcc In oAsmCompDef.Occurrences.AllLeafOccurrences
	If oRed = True Then
	oAsset = oAssyDoc.Assets.Item("Smooth - Red")
	Else
	oAsset = oAssyDoc.Assets.Item("Smooth - Black")
	End If
	oOcc.Appearance = oAsset
Next

EESignature

0 Likes
Message 5 of 9

Anonymous
Not applicable

Thank you Curtis for your assistance, however, I am getting the following error with the iRule code that you suggested: "AssetLibrary and oAsset are not defined".  I tried just changing any reference to Asset or AssetLibrary to StyleLibrary, but no luck.  Any suggestions?

 

Thanks!

0 Likes
Message 6 of 9

Curtis_Waguespack
Consultant
Consultant

Hi crenshap,

 

What version of Inventor are you using?

 

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

EESignature

0 Likes
Message 7 of 9

Anonymous
Not applicable

Curtis,

 

I am using Inventor 2012.

0 Likes
Message 8 of 9

Curtis_Waguespack
Consultant
Consultant

Hi crenshap,

 

Ahhh, I think Inventor 2013 is when Appearance Libraries were introduced.

 

Have a look at this thread, and I think you can glean from it how to set the color of an occurence in Inventor 2012 and previous:

http://forums.autodesk.com/t5/inventor-customization/occurrence-material-color-change/m-p/3181552#M3...

 

Or to find other examples search for: RenderStyle

 

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

EESignature

0 Likes
Message 9 of 9

Anonymous
Not applicable

Curtis,

 

Thanks,  I didn't have much luck trying to use something similar to the link that you sent,  but I did find something that I modified that allows me to change the part color in the top level asy, but it will not let me change the color of the part if it is contained within a sub assembly.  I've posted the code below. Would you happen to know how I could make it look into sub assemblies, (i.e leaf occurrences)?

 

SyntaxEditor Code Snippet

Private Sub Main
UpdateAssyBrowser(ThisDoc.Document)
End Sub

Private Sub UpdateAssyBrowser(ByVal oDoc As Inventor.Document)
Dim oAssy As Inventor.AssemblyDocument
Dim oComp As Inventor.ComponentOccurrence
Dim oSubDoc As Inventor.Document

Dim NodeName() As String
Dim InstNum As String

If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
oAssy = CType(oDoc, AssemblyDocument)

For Each oAssy In oAssy.ComponentDefinition.Occurrences
oSubDoc = CType(oAssy.Definition.Document, Document)
If Left(oAssy.Name,5) = "80299" Then
If oSubDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
'MessageBox.show("Matching Part file found!")
Component.Color(oComp.Name) = "cyan"
End If
End If
If oSubDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
Call UpdateAssyBrowser(oSubDoc)
End If

Next oComp
End If
End Sub
0 Likes