Hi,
I am trying to assign water as a material for all parts in an assembly and subassembly. I want to do this rapidly and then back to the previous materials (several different materials) .
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Solved by bradeneuropeArthur. Go to Solution.
Via the Bill Of Material?
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Hi @kenneth_paulsen. What would be the purpose of rapidly changing the material of all parts in an assembly to a specific material, then changing them all back? Do you truly need to change their material, or would just changing their appearance be OK. Changing their material will make a change to the individual files that the components are referencing, not just the component objects in the assembly. Plus, that material needs to be 'locally' available within each part, not just a material that is available in the material library. There needs to be a local copy of the material stored in the part, before the part can be set to that material, at least that is how it works by code. When doing things manually, stuff automatically happens behind the scenes that we do not realize, to help things go smoothly.
Here is an example iLogic rule for recursively iterating through all components in an assembly, and changing the material of all of its parts to 'Water'. It uses a 'Transaction', so that you can potentially undo the changes when it is done.
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oInvMatLib As AssetLibrary = ThisApplication.AssetLibraries.Item("Inventor Material Library")
oLibraryWaterMaterial = oInvMatLib.MaterialAssets.Item("Water")
Dim oTrans As Inventor.Transaction = ThisApplication.TransactionManager.StartTransaction(oADoc, "Change Materials - iLogic")
RecurseComponents(oOccs)
oTrans.End
If oADoc.RequiresUpdate Then oADoc.Update2(True)
'If oADoc.Dirty Then oADoc.Save2(True)
End Sub
Dim oLibraryWaterMaterial As MaterialAsset
Sub RecurseComponents(oOccs As ComponentOccurrences)
If oOccs Is Nothing OrElse oOccs.Count = 0 Then Return
For Each oOcc As ComponentOccurrence In oOccs
If oOcc.Suppressed Then Continue For
If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim oOccPDoc As PartDocument = oOcc.Definition.Document
Dim oLocalWaterMaterial As MaterialAsset = Nothing
Try
oLocalWaterMaterial = oOccPDoc.MaterialAssets.Item("Water")
Catch
oLocalWaterMaterial = oLibraryWaterMaterial.CopyTo(oOccPDoc, True)
End Try
Try
oOccPDoc.ActiveMaterial = oLocalWaterMaterial
Catch
'what to do if that fails
End Try
End If
If oOcc.Suppressed = False AndAlso _
oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
RecurseComponents(oOcc.Definition.Occurrences)
'RecurseComponents(oOcc.SubOccurrences)
End If
Next
End Sub
We could have optionally used the iLogic method iProperties.Material(oOcc.Name) = "Water" on those components too, but I am showing the Inventor API way of doing it, instead of the iLogic way.
Wesley Crihfield
(Not an Autodesk Employee)
Hi Arthur,
Yes, that was seemingly a better way of doing it than I did previously (opening each part and manually changing it).
The purpose of changing to water and then back again to the previous material is to calculate/find the center of buoyancy in an assembly.
Can't find what you're looking for? Ask the community or share your knowledge.