Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Change material of component in assembly

J_Dumont
Advocate

Change material of component in assembly

J_Dumont
Advocate
Advocate

Hello,

 

I could use some help with changing the material of certain parts in my assembly using iLogic.

 

I've seen various threads that can change all parts within an assembly, but I need to change parts using a specific Display Name. I need to use the Display Name because the file names can change when the assembly is copied using Vault's Copy Design.

 

I have a multi-value parameter at the assembly level, and when the user selects a new value, I need to change the corresponding component's material.

 

The materials are all local at the part level so I think this will simplify the coding.

 

 

 

0 Likes
Reply
Accepted solutions (1)
452 Views
5 Replies
Replies (5)

Michael.Navara
Advisor
Advisor

You can use something like this. You need to modify the conditions, parameter name

Sub Main
	Dim materialName As String = MaterialName ' Parameter("MaterialName") ' 
	Dim asm As AssemblyDocument = ThisDoc.Document

	'Iterate all parts
	For Each refDoc As Document In asm.AllReferencedDocuments
		
		'Skip non-part documents
		If refDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
			
		'Skip documents which display name doesn't match your criteria
		If Not DisplayNameMatch(refDoc.DisplayName) Then Continue For
			
		'Change the material
		ChangeMaterial(refDoc, materialName)
	Next
	asm.Update()
End Sub

Private Sub ChangeMaterial(part As PartDocument, materialName As String)
	part.ActiveMaterial = part.MaterialAssets(materialName)
End Sub

Private Function DisplayNameMatch(displayName As String) As Boolean
	'Implement your display name validation
	Return displayName.StartsWith("ExpectedDisplayNameHere")
End Function

J_Dumont
Advocate
Advocate

Hi Michael,

 

Thank you for your quick response.

The only issue I  have with your code is that 

refDoc.DisplayName

displays the file name and not the Display Name shown in the Model Browser.

 

0 Likes

Michael.Navara
Advisor
Advisor

You mention you want to use Document.DisplayName.

It is different from Document.FullFileNameDocument.FullDocumentNameComponentOccurrence.Name, iLogic function FileName, etc.

 

Which "display name" you want to use?

WCrihfield
Mentor
Mentor
Accepted solution

If ComponentOccurrence.Name is the appropriate 'display name' here, then we may be able to use the rather simplistic iLogic shortcut snippet here, like the example below:

iProperties.Material("Part1:1") = "MaterialName"

I do not recall if you can use the MakePath type specifications with that though, for accessing components down within sub assemblies.  I almost never use this myself.

iProperties.Material(MakePath("SubAssem1:1", "Part2:1")) = "MaterialName"

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

J_Dumont
Advocate
Advocate

Hi Wesley,

 

Thank you. This did the trick. I only need to change the material of two components that are not listed under a sub assembly, so the first suggestion worked perfectly.

 

0 Likes