Change feature color in a component, from an assembly

Change feature color in a component, from an assembly

a81383
Enthusiast Enthusiast
815 Views
12 Replies
Message 1 of 13

Change feature color in a component, from an assembly

a81383
Enthusiast
Enthusiast

Hello,

I'm trying to change the color of two features (EXT FINE & INT FINE) within a component (GBG) .ipt. From the assembly level. I'm not sure if its model states that are messing me up or if its just the code itself.

Any help would be much appreciated! 

 

	Dim EXTCOLOR As String = Parameter("exterior_color")
	Dim INTCOLOR As String = Parameter("interior_color")
	
	MyBAR.MemberEditScope = MemberEditScopeEnum.kEditAllMembers
	Select Case GRL_TYPE
	Case "FL"
		Select Case EXTCOLOR
		Case "BLK" 'Black
			Feature.Color("GBG", "EXT FINE") = "Black"
		Case "CCBN" 'Cocoa Bean
			Feature.Color("GBG", "EXT FINE") = "Cocoa Bean"
		Case "DBRZ" 'Dark Bronze
			Feature.Color("GBG", "EXT FINE") = "Dark Bronze"
		Case "SND" 'Sandtone
			Feature.Color("GBG", "EXT FINE") = "Sandtone"
		Case "TT" 'Terratone
			Feature.Color("GBG", "EXT FINE") = "Terratone"
		Case "WHT" 'White
			Feature.Color("GBG", "EXT FINE") = "White"
		End Select
		Select Case INTCOLOR
		Case "BLK" 'Black
			Feature.Color("GBG", "INT FINE") = "Black"
		Case "DBRZ" 'Dark Bronze
			Feature.Color("GBG", "INT FINE") = "Dark Bronze"
		Case "SND" 'Sandtone
			Feature.Color("GBG", "INT FINE") = "Sandtone"
		Case "WHT" 'White
			Feature.Color("GBG", "INT FINE") = "White"
		End Select
	Case Else
		Feature.Color("GBG", "FDL") = "Aluminum - Flat"
	End Select
	MyBAR.MemberEditScope = MemberEditScopeEnum.kEditActiveMember

 

0 Likes
816 Views
12 Replies
Replies (12)
Message 2 of 13

Michael.Navara
Advisor
Advisor

I don't know if there is correctly filled arguments, but this works for me.

 

Feature.Color("Part1:1", "Extrusion1") = "Red"
UpdateWhenDone = True

 

 

0 Likes
Message 3 of 13

a81383
Enthusiast
Enthusiast

This is the latest error I receive.

 

a81383_0-1680008962043.png

 

0 Likes
Message 4 of 13

Michael.Navara
Advisor
Advisor

Use appropriate appearance name. Red is in default libraries

0 Likes
Message 5 of 13

a81383
Enthusiast
Enthusiast

I guess I'm confused. I'm using the exact names of the colors within my library and when I run the rule with the same color names within the part itself it works. But when running it from the assembly level it error on the color name.

0 Likes
Message 6 of 13

WCrihfield
Mentor
Mentor

Hi @a81383.  In a separate rule, just for temporary testing purposes, but stored in the same place as your working rule, can you try just that one line of code, but in reverse so that it 'gets' (instead of 'sets') the name of the current color or appearance of that feature in that component, or does that also throw an error?  Is the component a 'top level' component, or is it down within a sub-assembly?  If you can read the value, but just can't set the value, then something is making it ReadOnly.  It is usually the DVR's (DesignViewRepresentations) that record things like object visibility, colors, & appearances, so maybe make sure the current DVR is not 'Locked' or something odd like that.  If that line of code truly effects the document being referenced by that component, then the ModelState that the component is set to may be playing a role in this not working also, not just the ModelState setting of the main assembly.  For instance, when you have components in an assembly, and ModelStates are involved, they will be representing ModelState members versions of the Document they reference.  ModelState members are usually ReadOnly when the model file is open directly on your screen, but when accessed through components in an assembly they act a bit differently.  If you have two components in your assembly that both represent the same base file on disk, but they are set to two different ModelStates, and you are trying to make changes to both of them by code, I believe you will need to somehow update the next one before you will be able to make changes to it.  I think it has something to do with the previous edit to a document reference in that file not being fully registered yet, before trying to make another change to a different document reference in the same file, but I'm not sure.  I'm not sure if this is even a problem here, since you seem to just be working with one component.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 13

a81383
Enthusiast
Enthusiast

Hi @WCrihfield ,

When setting oColor equal to the feature color. It returns the correct value (see image below). 

I have it set to "Default" design rep and the component model state is set to master. But there is multiple other versions of the component not set to master.

 

Could this be a model state issue. Where its read-only due to the multiple different model states being used and it's unable to update all of them at once?

 

a81383_1-1680020862763.png

a81383_3-1680020882136.png

 

Dim EXTCOLOR As String = Parameter("exterior_color")
Dim INTCOLOR As String = Parameter("interior_color")

oColor = Feature.Color("GBG", "EXT FINE")
MsgBox(oColor)

Select Case EXTCOLOR
Case "BLK" 'Black
	Feature.Color("GBG", "EXT FINE") = "Black"
Case "CCBN" 'Cocoa Bean
	Feature.Color("GBG", "EXT FINE") = "Cocoa Bean"
Case "DBRZ" 'Dark Bronze
	Feature.Color("GBG", "EXT FINE") = "Dark Bronze"
Case "SND" 'Sandtone
	Feature.Color("GBG", "EXT FINE") = "Sandtone"
Case "TT" 'Terratone
	Feature.Color("GBG", "EXT FINE") = "Terratone"
Case "WHT" 'White
	Feature.Color("GBG", "EXT FINE") = "White"
End Select
Select Case INTCOLOR
Case "BLK" 'Black
	Feature.Color("GBG", "INT FINE") = "Black"
Case "DBRZ" 'Dark Bronze
	Feature.Color("GBG", "INT FINE") = "Dark Bronze"
Case "SND" 'Sandtone
	Feature.Color("GBG", "INT FINE") = "Sandtone"
Case "WHT" 'White
	Feature.Color("GBG", "INT FINE") = "White"
End Select

InventorVb.DocumentUpdate()

 

0 Likes
Message 8 of 13

WCrihfield
Mentor
Mentor

Yep.  I just tried it myself on a test assembly.  When I did this to a regular component that did not have any custom ModelStates, it worked just fine, but when trying to do this to a component, where there were multiple components for the same file, and were set to different ModelStates, it gave me the same error as you are getting.  I'm sure there is a way around this odd scenario with some trial & error testing, but you may end up having to do this by API code instead of using that iLogic shortcut snippet.  The API way requires a lot more code, is more complicated though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 13

a81383
Enthusiast
Enthusiast

@WCrihfield ,

That makes sense. I've had similar issues with model states in the past. This is my first attempt at an API approach. 

But I'll keep looking into it. 

 

Thank you!

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
MyBAR = oADef.Occurrences.ItemByName("GBG GRILLE").Definition

Dim oAsset As Asset
Dim oLib As AssetLibrary = ThisApplication.AssetLibraries("Andersen Custom 2022")
'oAsset = oLib.AppearanceAssets("Andersen C0001 White")

Dim AssyAsset As Asset
Try
	AssyAsset  = oLib.AppearanceAssets.Item("Andersen C0001 White")
Catch
'	AssyAsset = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets("Andersen C0001 White").CopyTo(oADoc)
	AssyAsset = oLib.AppearanceAssets("Andersen C0001 White").CopyTo(oADoc)
End Try

For Each Leafocc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences.AllLeafOccurrences
	If Leafocc.Name = "GBG GRILLE" Then
		
		Dim GBG As ComponentOccurrence = Leafocc
		Dim GBGDef As PartComponentDefinition = GBG.Definition
		
		Dim SbProxy As SurfaceBodyProxy

		For Each Sb As SurfaceBody In GBGDef.SurfaceBodies
		
			If Sb.Name = "FINE" Then
				
				'Create the Proxy Body.
				GBG.CreateGeometryProxy(Sb, SbProxy)

				SbProxy.Appearance = AssyAsset
			End If
		Next
	End If
Next
0 Likes
Message 10 of 13

WCrihfield
Mentor
Mentor

If you only want to change their colors at the assembly level, and not in the actual source files, then what you have there looks pretty good.  That way you only have to make sure the appearance is available in the main assembly, and using the 'proxy' versions of the geometry is getting the geometry that is in the context of the assembly, so I don't think the ModelState settings of the components should matter at all.  Is that code you posted working OK for you then?

If you want the color changes to effect the source files, then you do not need to get the proxy of the geometry, and will need to make sure that the 'appearance' is available as a local copy within each source document, and will need to make sure you are working with the 'factory' version document of the component's referenced file. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 13

a81383
Enthusiast
Enthusiast

@WCrihfield ,

It finds the solid just fine but is unable to make the change. I receive the following error for both the Proxy and direct method. 

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
MyBAR = oADef.Occurrences.ItemByName("GBG GRILLE").Definition

Dim oAsset As Asset
Dim oLib As AssetLibrary = ThisApplication.AssetLibraries("Andersen Custom 2022")
'oAsset = oLib.AppearanceAssets("Andersen C0001 White")

Dim AssyAsset As Asset
Try
	AssyAsset  = oLib.AppearanceAssets.Item("Andersen C0001 White")
Catch
'	AssyAsset = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets("Andersen C0001 White").CopyTo(oADoc)
	AssyAsset = oLib.AppearanceAssets("Andersen C0001 White").CopyTo(oADoc)
End Try

For Each occ As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	If occ.Name = "GBG GRILLE" Then
		
		Dim GBGdef As PartComponentDefinition = occ.Definition.FactoryDocument.ComponentDefinition
		
		Dim SbProxy As SurfaceBodyProxy

		For Each Sb As SurfaceBody In GBGdef.SurfaceBodies
			If Sb.Name = "FINE" Then
				'Create the Proxy Body.
'				occ.CreateGeometryProxy(Sb, SbProxy)
'				SbProxy.Appearance = AssyAsset
				
				Sb.Appearance = AssyAsset
			End If
		Next
	End If
Next

 

This line provides the following error: 

Sb.Appearance = AssyAsset

a81383_0-1680030228224.png

 

0 Likes
Message 12 of 13

WCrihfield
Mentor
Mentor

There will only be a value under FactoryDocument property when the document has more than one ModelState.  Otherwise it will be Nothing, and likely throw an error.

oFacDoc = CompDef.Document 'from component, then check

If CompDef.IsModelStateMember Then

oFacDoc = CompDef.FactoryDocument

Else 'keep original document reference.

End If

'''but only when you want to effect the referenced file

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 13

a81383
Enthusiast
Enthusiast

It's strange because the message box below MsgBox(Sb.Apperance.Name). Gives me the correct name of the current appearance. But its unable to change the value.

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
MyBAR = oADef.Occurrences.ItemByName("GBG GRILLE").Definition

oFacDoc = MyBAR.Document
If MyBAR.IsModelStateMember Then
	oFacDoc = MyBAR.FactoryDocument
End If

Dim oAsset As Asset
Dim oLib As AssetLibrary = ThisApplication.AssetLibraries("Andersen Custom 2022")
'oAsset = oLib.AppearanceAssets("Andersen C0001 White")

Dim AssyAsset As Asset
Try
	AssyAsset  = oLib.AppearanceAssets.Item("Andersen C0001 White")
Catch
'	AssyAsset = ThisApplication.ActiveAppearanceLibrary.AppearanceAssets("Andersen C0001 White").CopyTo(oADoc)
	AssyAsset = oLib.AppearanceAssets("Andersen C0001 White").CopyTo(oADoc)
End Try

Dim GBGdef As PartComponentDefinition = oFacDoc.ComponentDefinition
	
Dim SbProxy As SurfaceBodyProxy

For Each Sb As SurfaceBody In GBGdef.SurfaceBodies
	If Sb.Name = "FINE" Then
		'Create the Proxy Body.
'		GBGdef.CreateGeometryProxy(Sb, SbProxy)
'		SbProxy.Appearance = AssyAsset

		MsgBox(Sb.Appearance.Name)
		Sb.Appearance = AssyAsset
	End If
NextS 

 

0 Likes