How do I change appearance with use of Ilogic? I need a code for both IPT file and Iam file
Solved! Go to Solution.
Solved by Darkforce_the_ilogic_guy. Go to Solution.
Hi, here I send you two codes, the first changes the appearance of each part from assembly, at the level of the part (as if you open the part and change the color)
Dim oAsset As Asset Dim oAsset_Array As New ArrayList For Each oAsset_Array_X In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets oAsset_Array.Add(oAsset_Array_X.DisplayName) oAsset_Array.Sort() Next 'present the user with the list to choose from 100: oAsset_Array_Selected = InputListBox("CHOOSE TEXTURE FROM ABOVE LIST", oAsset_Array, oAsset_Array.Item(0), "TEXTURE SELECTION", "LIST OF TEXTURES") If oAsset_Array_Selected = "" Then GoTo 100 : Dim comps As ObjectCollection Dim comp As Object comps = ThisApplication.TransientObjects.CreateObjectCollection While True comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select a component") ' If nothing gets selected then we're done If IsNothing(comp) Then Exit While comps.Add(comp) End While ' If there are selected components we can do something For Each comp In comps Dim oDef As PartDocument oDef = comp.Definition.Document Dim oRenderStyle As RenderStyle oRenderStyle = oDef.RenderStyles.Item(oAsset_Array_Selected) oDef.ActiveRenderStyle = oRenderStyle iLogicVb.UpdateWhenDone = True Next
the second changes the appearance of the part or assembly at the level of assembly (the part maintains the color of origin)
Dim oAsset As Asset Dim oAsset_Array As New ArrayList For Each oAsset_Array_X In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets oAsset_Array.Add(oAsset_Array_X.DisplayName) oAsset_Array.Sort() Next 'present the user with the list to choose from 100: oAsset_Array_Selected = InputListBox("CHOOSE TEXTURE FROM ABOVE LIST", oAsset_Array, oAsset_Array.Item(0), "TEXTURE SELECTION", "LIST OF TEXTURES") If oAsset_Array_Selected = "" Then GoTo 100 : Dim comps As ObjectCollection Dim comp As Object comps = ThisApplication.TransientObjects.CreateObjectCollection While True comp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a component") 'kAssemblyOccurrenceFilter. kAssemblyLeafOccurrenceFilter ' If nothing gets selected then we're done If IsNothing(comp) Then Exit While comps.Add(comp) End While ' If there are selected components we can do something For Each comp In comps Component.Color(comp.name)=oAsset_Array_Selected iLogicVb.UpdateWhenDone = True Next
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
Thanks for the code... it is not what I needed
I need a code that I can add to I if stement that I already have ...
when I force the user to select betwine Colour 1 and Colour 2 and Galz
in the if statment
If you selece Colour 1
Change appearance of this part or assambly to "Ral 7035"
If you selece Colour 2
Change appearance of this part or assambly to "Ral 7037"
If you selece Galz.
Change appearance of this part or assambly to "Galvanized"
I only want to change the top level ... not the assambly or part in the assambly
I use this in a before save ilogic code
This is a code that just probes and works for me, it is by way of example. Need to add select an occurrence and then create a list of the two colors
Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition Dim oPart As ComponentOccurrence 'Pick part occurrence oPart = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Seleccione Pieza") If oPart Is Nothing Then Exit Sub Else Dim oValueList As New ArrayList oValueList.Add("Colour 1") oValueList.Add("Colour 2") Dim oValue As String 'get user input oValue = InputListBox("Select colour", oValueList, "Colour 1", "Customizar Plantilla", "Selecciones Disponibles") If oValue = "Colour 1" Then Component.Color(oPart.Name)="Magenta" iLogicVb.UpdateWhenDone = True End If If oValue = "Colour 2" Then Component.Color(oPart.Name)="Negro" iLogicVb.UpdateWhenDone = True End If end if
,you do not need to copy all this code, you should copy a line like this inside the "If then end if"
Component.Color("Occurrence")="chosen colour"
iLogicVb.UpdateWhenDone = True
You must make sure that the chosen color belongs to the current appearance library, otherwise you must specify it
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
Can´t get it to work .. how can it be so hard just to change color of a part
and for the same when it goes for assembly.
both at top level...
no seletect ingen ,... no list ...
just change change the appearance on the part or Assambl that you are saving right now .. it may need 2 illogic code .. one for part and one for assembly ... it is just 2 click with the mouse .. so it most be simple but I do not know the code for it
Try below iLogic code for assembly document.
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("Steel") Dim localAsset As Asset Try localAsset = oAssyDoc.Assets.Item("Steel") Catch localAsset = libAsset.CopyTo(oAssyDoc) End Try
oAssyDoc.ActiveAppearance = localAsset
For Part Document
Dim oPartDoc As PartDocument oPartDoc = 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("Steel") Dim localAsset As Asset Try localAsset = oPartDoc.Assets.Item("Steel") Catch localAsset = libAsset.CopyTo(oPartDoc) End Try oPartDoc.ActiveAppearance = localAsset
Thanks and regards,
I have only test the part function so far ... it is working as i want it to... have not have time to test the assambly jet
if it is working for PartDocument, it should work for Assembly as well.
Please let me know if it is not working.
Thanks and regards,
I can´t get it to work.. it seems that you have to change colour on en asammebly componemt(hope it is the right word) not on some the hole assembly as you can with the part.. think it might have to you a while loop to go throungt all of them one by one... hope it can be not on only the toplevel.. not all the way down in the sub assembly.
You know how to do that ?
It seems that appearance of assembly is not allowed to change through UI. Can you please confirm the same?
Thanks and regards,
Hi.
I also found it is not working. I moved steel from library to favorites. then it worked. Only for the part, anyway.
@Sergio.D.Suárez- I can't thank you enough. I've searched far and wide to find a method to update a parts appearance from within an assembly based iLogic rule - and you've given the perfect example of how to do so.
ActiveRenderStyle
ActiveRenderStyle! Not Color, or PartColor, or Appearance, or iProperty.Color.Appearance. ... no, it was "ActiveRenderStyle" all along!!! No wonder I'm losing my hair...
Thank you kindly again! :hot_beverage:
This is a very old tread .. but I have develope something like I wanted today . I will try add some of the code seens I have add a lot more to the code today .. hope you can use it ... If need more of help to use it for your project .. write here again and I will try to make a code that work for you
Sub AddsurfaceAssembly()
Dim NameList As New ArrayList()
NameList.Add("Raw")
'NameList.Add("------------------------------------------------------------------------------")
NameList.Add("Colour 1 Basic colour for chassis, etc.")'Colour Ral 7035 OR RAL 9010
NameList.Add("Colour 2 Basic colour for moving parts.")'Colour Ral 7037 OR RAL 5003
NameList.Add("RAL 1028 Melon yellow")
NameList.Add("RAL 5003 Sapphire blue")
NameList.Add("RAL 9005 Jet black")
NameList.Add("RAL 9010 Pure white")
'NameList.Add("------------------------------------------------------------------------------")
NameList.Add("Electro galvanized Preferable/Cheapest")'Eletro Galvanizes
NameList.Add("Hot dip galvanized Outdoor Use")'Hot Dip galvanized
'NameList.Add("------------------------------------------------------------------------------")
NameList.Add("Hardened Set specifications on Drawing")
NameList.Add("Glassblasted")
NameList.Add("Sandblasted")
'NameList.Add("Shot blasted")
'NameList.Add("Pickled")
'NameList.Add("------------------------------------------------------------------------------")
'NameList.Add("Do not select")
Dim msg As String
msg = ("Surface needed - Select Surface " & vbLf & "Part Numner: "+iProperties.Value("Project", "Part Number")& vbLf & "Description: "+iProperties.Value("Project", "Description")& vbLf & "Title: "+ iProperties.Value("Summary", "Title")& vbLf).ToString
d0 = InputListBox(msg, NameList, d0, Title := "Info - Surface needed", ListName := "Surface")
' iProperties.Value("Custom", "Surface") = d0
'Tilføj farve
If d0 = "Do not select" Then
Else
TextToSurface(d0)
'iProperties.Value("Custom", "Surface") = d0
End If
'find Document type
Dim doc = ThisDoc.Document
Dim sDocumentSubType As String = doc.SubType
If sDocumentSubType <> "{4D29B490-49B2-11D0-93C3-7E0706000000}" And sDocumentSubType <>"{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then ' = "Part"
'------------------
debug("Colour Assambly")
Try
Dim oAsmCompDef 'As AssemblyComponentDefinition
debug("Loading Variable Finish 0,5")
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
debug("Loading Variable Finish 1")
'define view rep collection
Dim oViewReps As DesignViewRepresentations
oViewReps = oAsmCompDef.RepresentationsManager.DesignViewRepresentations
debug("Loading Variable Finish 2")
'define view rep
Dim oViewRep As DesignViewRepresentation
debug("Loading Variable Finish 3")
Dim NumberOfView = oViewReps.Count
' get Manager of Representations
debug("Loading Variable Finish 4")
Dim dViewRepMgr As RepresentationsManager
dViewRepMgr = oAsmCompDef.RepresentationsManager
debug("Loading Variable Finish 5")
'Get active Representation View
Dim dViewRep As DesignViewRepresentation
dViewRep = dViewRepMgr.ActiveDesignViewRepresentation
debug("Loading Variable Finish 6")
debug("Loading Variable Finish")
Try
For i = 1 To NumberOfView
If iProperties.Value("Custom", "Surface") = "Raw" Or iProperties.Value("Custom", "Surface") = "Galv." Or iProperties.Value("Custom", "Surface") = "Colour 1" Or iProperties.Value("Custom", "Surface") = "Colour 2" Or iProperties.Value("Custom", "Surface") = "RAL 5003" Or iProperties.Value("Custom", "Surface") = "RAL 9010" Or iProperties.Value("Custom", "Surface") = "RAL 1028" Or iProperties.Value("Custom", "Surface") = "RAL 9005" Or iProperties.Value("Custom", "Surface") = "EG-Galv" Or iProperties.Value("Custom", "Surface") = "HDG-Galv" Then
debug("chance view")
oViewReps.Item(i).Activate
End if
iLogicVb.RunExternalRule("SurfaceColour")
iLogicVb.UpdateWhenDone = True
Next
If iProperties.Value("Custom", "Surface") = "RAL 9010"
End If
If iProperties.Value("Custom", "Surface") = "RAL 5003"
End If
Catch
debug("Run Error Code 7")
dViewRepMgr.DesignViewRepresentations.Item(1).Activate
End Try
Try
dViewRep.Activate
'Home View
ThisApplication.CommandManager.ControlDefinitions.Item _
("AppViewCubeHomeCmd").Execute
'Home View
Catch
debug("Run Error Code 8")
iLogicVb.RunExternalRule("SurfaceColour")
iLogicVb.UpdateWhenDone = True
End Try
Catch
debug("Run Error Code 9")
debug("Add Colour to Assambly")
iLogicVb.RunExternalRule("SurfaceColour")
End Try
'---------------
'iLogicVb.RunExternalRule("SurfaceColour")
End If
If sDocumentSubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Or sDocumentSubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then ' = "Part"
debug("Colour Part")
If d0 = "Colour 1 Basic colour for chassis, etc." Then
debug("Colour 1 ")
'iProperties.Value("Custom", "Surface") = d0
ChangeView
End If
If d0 = "Colour 2 Basic colour for moving parts." Then
'iProperties.Value("Custom", "Surface") = d0
ChangeView
End If
If d0 = "Galv." Or d0 = "Electro galvanized Preferable/Cheapest" Or d0 = "Hot dip galvanized Outdoor Use" Then
'iProperties.Value("Custom", "Surface") = d0
ChangeView
End If
If d0 = "RAL 1028 Melon yellow" Or d0 = "RAL 9005 Jet black" Or d0 = "RAL 9010 Pure white" Or d0 = "RAL 5003 Sapphire blue" Then
'iProperties.Value("Custom", "Surface") = d0
ChangeView
End If
If d0 = "RAL 5003 Sapphire blue" Then
'iProperties.Value("Custom", "Surface") = d0
ChangeView
End If
'gør intet
End If
End Sub
Sub Colour1
debug("Loading Colour 1")
Dim oPartDoc As PartDocument
Try
oPartDoc = ThisApplication.ActiveDocument
Catch
oPartDoc = ThisDoc.Document
End Try
'define the appearance library
Dim oLib As AssetLibrary
oLib = ThisApplication.AssetLibraries("KallesoeMaterialLibrary")
'make sure colors are in assembly doc
Dim libAsset As Asset
'libAsset = oLib.AppearanceAssets.Item("RAL 9010 Pure white")
libAsset = oLib.AppearanceAssets.Item("RAL 7035 Light grey")
Dim localAsset As Asset
Try
localAsset = oPartDoc.Assets.Item("RAL 7035 Light grey")
Catch
debug("Run Error Code 10")
localAsset = libAsset.CopyTo(oPartDoc)
End Try
oPartDoc.ActiveAppearance = localAsset
Debug("Loading Colour 1 END")
End Sub
Surfacecolour(ilogic code)
Farvelægge samlinger Sub Main() Debug("Starting Surfacecolour") If iProperties.Value("Custom", "Surface")="Colour 1" Then Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="RAL 7035 Light grey" iLogicVb.UpdateWhenDone = True Next End If If iProperties.Value("Custom", "Surface")="Colour 2" Then Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="RAL 7037 Dusty grey" iLogicVb.UpdateWhenDone = True Next End If If iProperties.Value("Custom", "Surface")="HDG-Galv" Then Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="Galvanized (texture)" iLogicVb.UpdateWhenDone = True Next End If If iProperties.Value("Custom", "Surface")="Galv." Or iProperties.Value("Custom", "Surface")="EG-Galv" Then Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="Galvanized" iLogicVb.UpdateWhenDone = True Next End If If iProperties.Value("Custom", "Surface")="RAL 1028" Then Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="RAL 1028 Melon yellow" iLogicVb.UpdateWhenDone = True Next End If If iProperties.Value("Custom", "Surface")="RAL 9005" Then debug("Colour 1") Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="RAL 9005 Jet black" iLogicVb.UpdateWhenDone = True Next End If If iProperties.Value("Custom", "Surface")="RAL 9010" Then Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="RAL 9010 Pure white" iLogicVb.UpdateWhenDone = True Next End If If iProperties.Value("Custom", "Surface")="RAL 5003" Then Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisDoc.Document Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = oAsmDoc.ComponentDefinition Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Component.Color(oOccurrence.name)="RAL 5003 Sapphire blue" iLogicVb.UpdateWhenDone = True Next End If End Sub Public Sub debug(txt As String) Trace.WriteLine("NTI : Colour" & txt) End Sub
Thank you everyone for the examples in this post.
I would like to do something slightly different than I have seen elsewhere, but I figure there is a good start in this thread, so I decided to post it here.
Occasionally we will append "REF" or "MOCK" or some other descriptors to the file name of a part. The part number will not have this, only the file name.
I would like to have some code that runs prior to save via a trigger where if the file name ends in REF or MOCK (also Ref, ref, Mock, mock, maybe create a list of "triggers" and if the filename contains a trigger then set a bool to true?), it will prompt the user with a message ("it seems like you have a "REF" or "MOCK" part, would you like to override the color?") and if the user selects "Yes", then the color will be set to "Pink".
Catch: This rule should work for parts, sheetmetal parts, assemblies, weldments, etc.
Thank you in advance.
Do you also have a part of code for clearing the override on the appearance?
Clear Override cannot be chosen as name
thanks.
I have this code.. that will remove all Color(will clear override the color) . on all View in the file.. I have try to remove some code that are unit to where i use it .. so I hope i did it right so I did not break the code 😀
For assembly
Dim oAsmCompDef 'As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oViewReps As DesignViewRepresentations
oViewReps = oAsmCompDef.RepresentationsManager.DesignViewRepresentations
'define view rep
Dim oViewRep As DesignViewRepresentation
Dim NumberOfView = oViewReps.Count
' get Manager of Representations
Dim dViewRepMgr As RepresentationsManager
dViewRepMgr = oAsmCompDef.RepresentationsManager
'Get active Representation View
Dim dViewRep As DesignViewRepresentation
dViewRep = dViewRepMgr.ActiveDesignViewRepresentation
For i = 1 To NumberOfView
oViewReps.Item(i).Activate
'---------------------
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisDoc.Document
Dim objColl As ObjectCollection
objColl = ThisApplication.TransientObjects.CreateObjectCollection
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
Call objColl.Add(oOccurrence)
iLogicVb.UpdateWhenDone = True
Next
oAsmCompDef.ClearAppearanceOverrides(objColl)
'-------------------------
iLogicVb.UpdateWhenDone = True
Next
For Part
Sub Main
Dim doc = ThisDoc.Document
Dim sDocumentSubType As String = doc.SubType
doc.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance
If sDocumentSubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Or sDocumentSubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then ' = "Sheet Metal and Part"
ChangeView
End If
End Sub
Public Sub debug(txt As String)
Trace.WriteLine("NTI : " & txt)
End Sub
Sub ChangeView
'''New Code Begun
''' Here
debug("Start Run Function Change View")
Dim doc As PartDocument
doc = ThisDoc.Document
debug("Load This Document Variabel")
' get AssemblyComponentDefinition
Dim partDef As PartComponentDefinition
partDef = doc.ComponentDefinition
debug("Loaded Variabel 2")
' get Manager of Representations
Dim dViewRepMgr As RepresentationsManager
dViewRepMgr = partDef.RepresentationsManager
debug("Loaded Variabel 3")
Dim dViewRep As DesignViewRepresentation
dViewRep = dViewRepMgr.ActiveDesignViewRepresentation
debug("Loaded Variabel 4")
Dim oName As String
oName = dViewRep.Name
debug("Loaded Variabel 5")
' get the first Representation view
dViewRep = dViewRepMgr.DesignViewRepresentations.Item(oName)
'New code Ende
'set a reference to the part component definintion.
'Dim oPartCompDef As PartComponentDefinition
'oPartCompDef = ThisDoc.Document.ComponentDefinition
debug("Loaded Variabel 6")
'define view rep collection
Dim oViewReps As DesignViewRepresentations
oViewReps = partDef.RepresentationsManager.DesignViewRepresentations'oPartCompDef.RepresentationsManager.DesignViewRepresentations
debug("Loaded Variabel 7")
Dim NumberOfView = oViewReps.Count
debug("Loading Variable Change view")
Try
For i = 1 To NumberOfView
debug("Run For LOOP")
oViewReps.Item(i).Activate
debug("Run For LOOP2")
ClearOverride
iLogicVb.UpdateWhenDone = True
Next
Catch
debug("Run Error Code 13")
End Try
Try
dViewRep.Activate
Catch
debug("Run Error Code 14")
dViewRepMgr.DesignViewRepresentations.Item(1).Activate
End Try
End Sub
Sub ClearOverride
Dim doc = ThisDoc.Document
doc.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance
End Sub
Thanks for your reply.
I found the solution to my problem:
'clear override material appearence
'Found at: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/clear-all-appearance-overrides-in-part-with-ilogic/m-p/8226065/highlight/true#M88140
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
oDoc.AppearanceSourceType = AppearanceSourceTypeEnum.kMaterialAppearance
This will do the trick.
Can't find what you're looking for? Ask the community or share your knowledge.