Changing Material and Finish in an Assembly - iLogic

Changing Material and Finish in an Assembly - iLogic

GKPByDesign
Advocate Advocate
2,764 Views
6 Replies
Message 1 of 7

Changing Material and Finish in an Assembly - iLogic

GKPByDesign
Advocate
Advocate

Was wondering, if i open an assembly i can run a code to change a material and finish to something else.

 

Example:

 

I have a part that is Steel, Mild and Black and i want it to be Acrylic and White? 

0 Likes
Accepted solutions (2)
2,765 Views
6 Replies
Replies (6)
Message 2 of 7

mcgyvr
Consultant
Consultant

yes thats entirely possible..

 

How will Inventor determine which part you want to change in that assembly?

 

iProperties.Material = "Acrylic"
iProperties.PartColor = "White"

 



-------------------------------------------------------------------------------------------
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
0 Likes
Message 3 of 7

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hi, A little late, put together this code and it served me. Previously I had made one that identified the occurrence and opened a selected file in false mode, but this type of code gave a problem if the created piece had not been saved (for example, a piece created in the frame generator not yet saved). This new code opens in context and makes the changes it is not necessary that the piece is saved

 

change appearance of part

 

 

Sub Main()
		'Change the appearance of part
		'The part may not be saved
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oPart As ComponentOccurrence

Line1 :
'''Pick part occurrence
oPart = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select Part")
	If oPart Is Nothing Then
		Exit Sub
	Else
	    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 oDef As PartDocument
	    oDef = oPart.Definition.Document
	    Dim oRenderStyle As RenderStyle
        oRenderStyle = oDef.RenderStyles.Item(oAsset_Array_Selected)
	    oDef.ActiveRenderStyle = oRenderStyle
        iLogicVb.UpdateWhenDone = True		
	End If
End Sub

 

change material of part

 

Sub Main()
    'Change the material of part 
'The part may not be saved Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition Dim oPart As ComponentOccurrence Line1 : '''Pick part occurrence oPart = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Seleccione Pieza") If oPart Is Nothing Then Exit Sub Else Dim oDef As PartDocument oDef = oPart.Definition.Document Dim oMaterialAssets As AssetsEnumerator oMaterialAssets = oDef.MaterialAssets Dim oAsset As Asset Dim oAsset_Array As New ArrayList For Each oAsset_Array_X In ThisApplication.ActiveMaterialLibrary.MaterialAssets 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: oDef.ComponentDefinition.Material =oDef.Materials.Item(oAsset_Array_Selected ) iLogicVb.UpdateWhenDone = True End If End Sub

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 4 of 7

GosponZ
Collaborator
Collaborator

Hi Sergio,

i tried first code for appearance and is working good, but material is not working. After i pick part it makes error. Would you please revisit code. I m running INV 2021 and soon i'll switch to inv 2022.

 

Thank you 

0 Likes
Message 5 of 7

SharkDesign
Mentor
Mentor

I found material to  be the easy bit, the appearance was a nightmare and only overrode the appearance instead of changing at part colour. 

 

I asked a similar question here:

 

https://forums.autodesk.com/t5/inventor-customization/get-appearance-at-part-level-from-assembly/m-p...

 

 

 

  Inventor Certified Professional
0 Likes
Message 6 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @GosponZ 

 

The previous code was written for use in an assembly and will error out if you use it in a part file... here is a version that works for both assembly and part files. I've included a similar version for setting appearances as well

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

 

 

 

 

 


Dim oDoc As PartDocument

If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	oDoc = ThisApplication.ActiveDocument	
Else
	oPart = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, _
	"Select Part")
	
	If oPart IsNot Nothing Then
		oDoc = oPart.Definition.Document	
	Else
		Return 'exit rule
	End If
End If 

Dim oMaterialAssets As AssetsEnumerator
Dim oList As New ArrayList
For Each oItem In ThisApplication.ActiveMaterialLibrary.MaterialAssets
	oList.Add(oItem.DisplayName)
	oList.Sort()
Next

oCurrent = oDoc.ComponentDefinition.Material.Name
oMaterial = InputListBox("Select One", oList, oCurrent, "iLogic", "List of Materials")
If oMaterial Is Nothing Then return
oDoc.ComponentDefinition.Material = oDoc.Materials.Item(oMaterial)	


iLogicVb.UpdateWhenDone = True

 

 

 

 

 

 

 

 

 


Dim oDoc As PartDocument

If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	oDoc = ThisApplication.ActiveDocument	
Else
	oPart = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, _
	"Select Part")
	
	If oPart IsNot Nothing Then
		oDoc = oPart.Definition.Document	
	Else
		Return 'exit rule
	End If
End If 

Dim oMaterialAssets As AssetsEnumerator
Dim oList As New ArrayList
For Each oItem In ThisApplication.ActiveAppearanceLibrary.AppearanceAssets
	oList.Add(oItem.DisplayName)
	oList.Sort()
Next
		
oCurrent = oDoc.ActiveRenderStyle.name
oAppearance = InputListBox("Select One", oList, oCurrent, "iLogic", "List of Appearances")
If oAppearance is Nothing then Return
oDoc.ActiveRenderStyle = oDoc.RenderStyles.Item(oAppearance)

iLogicVb.UpdateWhenDone = True

 

 

 

 

EESignature

Message 7 of 7

GosponZ
Collaborator
Collaborator

Thank you it is working good

0 Likes