Using a custom color to change parts colors on parts in an assembly with ilogic.

Using a custom color to change parts colors on parts in an assembly with ilogic.

ladderman
Contributor Contributor
461 Views
4 Replies
Message 1 of 5

Using a custom color to change parts colors on parts in an assembly with ilogic.

ladderman
Contributor
Contributor

Good morning,

 

I have  iLogic that I am currently using to change colors on parts a given assembly that works great that I will post at the end.  The problem that I am having is trying to use the same code with a color that I have added the the Inventor Material Library.  I did notice that if I have the color in the Document Appearances it will work, but that means I would have through dozens of assemblies to achieve this.

 

My question is there a way to use the ilogic I have to make either add the color needed to the document or have it only look at my Inventor Material Library???

 

ladderman_1-1674147486659.png

ladderman_3-1674148472267.png

 

 

 

I have only copied the iLogic that directly changes the color, if I need to post the rest please let me know.  I have also added *** to omit names.

 

Sub Main

	oTrigger = TREADCOLOR
	iLogicVb.UpdateWhenDone = True

	oColor = Parameter("TREADCOLOR")

	oColor = Replace(oColor, "****", "") 'replace ****with nothing (strips off ****if found)
	oColor = Replace(oColor, "****", "") 'replace ****with nothing (strips off ****if found)
	oColor = Replace(oColor, "****", "") 'replace OSHA with nothing (strips off ****if found)
	oColor = Trim(oColor) 'trim off extra spaces
	oColor = StrConv(oColor, VbStrConv.ProperCase) 'convert to Title Case (first letter cap)

	If oColor = "Blue" Then
		oColor = "Blue - Wall Paint - Glossy"
	End If
	
	If oColor = "Black" Then            'I added this if statement to change colors to the black needed.
		oColor = "Black - Smooth"   'I added this if statement to change colors to the black needed.
	End If                              'I added this if statement to change colors to the black needed.


	For i = 1 To 20
		
		If Parameter("TREADTYPE") = "DP" And Parameter("STAIRWIDTH") = 36 Then
			oCompName = "***-TREAD-2X10X36-ZINC:" & i
			
		Else If Parameter("TREADTYPE") = "DP" And Not Parameter("STAIRWIDTH") = 36 Then
			oCompName = "*** TREAD:" & i
			
		Else If Parameter("TREADTYPE") = "SG" Then
			oCompName = "*** TREAD SG:" & i
			
		End If
		
			oComp = Component.InventorComponent(oCompName)

			If oComp.Visible = False Then Continue For 'skip if not visible

			Call CopyColor(oComp, oColor) 'make sure color is in assembly 
			Call SetViewRep(oComp) 'set to default view rep
			
			'step through sub assembly and color all components
			Call ProcessComponents(oComp.Definition.Document, oCompName, oColor)
		Next

End Sub 

 

0 Likes
Accepted solutions (1)
462 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @ladderman.  Those objects you are calling colors, are know in the iLogic environment as Assets, or an Asset.  There are 3 main types/purposes of them (Material, Appearance, Physical).  Every material type Asset (MaterialAsset), also has an appearance asset and a physical asset associated with it, but each appearance type or physical type are not backwards associated with any material type.  You can access the local ones stored in your document through the PartDocument.Assets property.  That Assets collection has a Add method, which lets you create a new one within the document.  However, if you already have one made within a 'library' (AssetLibrary), you would need to get that 'library' version of it, then use the Asset.CopyTo method, to copy it to the document.  Then get a reference to the version of it in the document to use in your code.  As you have seen, only local copies of Assets, can be applied to geometry in your document.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

ladderman
Contributor
Contributor

@WCrihfield 

 

Thank you for your response.  I am able to follow what you are saying I need to do, I am just trying to figure out how to do it...  😄   I had help writing the code that I have shown and understand it fairly well, but I am not sure how to write in what you have suggested.  I do believe I need;

 

	If oColor = "Black" Then
		AssetLibrary.AppearanceAssets(kAssetTypeAppearance) As AssetsEnumerator
		Assets.Copyto (Document Appearances As Variant, Black - Smooth)
		oColor =  "Black - Smooth"
	End If

 But I am failing to use it properly it seems...  I was trying to figure what I need by looking at the examples but to no avail.  Do you have an example or anything that would point in the correct direction???

 

And part of the code that I did not include that might be relevant;

 

Sub ProcessComponents(oComp As Document, oCompName As String, oColor As String)

	oOccs = oComp.ComponentDefinition.Occurrences

	Dim oAsset As Asset
	oAsset = oComp.Assets.Item(oColor)

	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs

		'set the color
		oOcc.Appearance = oAsset
	Next


End Sub


Sub CopyColor(oOcc As ComponentOccurrence, oColor As String)

	Dim oDoc As Document
	oDoc = oOcc.Definition.Document

	Dim oAssetLib As AssetLibrary
	oAssetLib = ThisApplication.AssetLibraries.Item(2)

	Dim oAppearance As Asset

	'get color asset from libary
	Try
		oAppearance = oAssetLib.AppearanceAssets.Item(oColor)
	Catch 'catch error when color is not in appearance asset library
		Logger.Info("Error getting '" & oColor & "' from appearance asset library.")
		Exit Sub
	End Try
	
	Logger.Info(oOcc.name & "    " & oAppearance.DisplayName)

	Try ' Copy the asset locally.
		oLocalAsset = oAppearance.CopyTo(oDoc)
	Catch 'catch error if it is already local
		oLocalAsset = oAppearance
		Logger.Info("Could not copy appearance asset to " & oOcc.name)
	End Try


End Sub
 

In this code do I need it to look at the Inventor Material Library instead of;

oAssetLib.AppearanceAssets.Item

 

Thanks again

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @ladderman.  If you want to make sure you are working with the "Inventor Material Library", then you can replace this following block of code (within your CopyColor Sub routine block of code):

Dim oAssetLib As AssetLibrary
oAssetLib = ThisApplication.AssetLibraries.Item(2)

...with this block of code:

Dim oAssetLib As AssetLibrary
oAssetLib = ThisApplication.AssetLibraries.Item("Inventor Material Library")

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

ladderman
Contributor
Contributor

@WCrihfield 

 

Awesome, and thank you.  After I looked into the code, I did realized that specific line and thought that it might need to change somehow.  🙂

 

Thank you again!

0 Likes