So I'm editing this piece of code to switch materials in my parts. and I get this Error message (its in my language i'm afraid)
Code:
'Actieve assembly selecteren (selecting Assembly)
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
'Materiaal naar parts (Material to parts)
Dim strMaterialName As String
strMaterialName = "_SS 316L" & "_FRP"
Dim oMaterial As Material
On Error Resume Next
'Materiaal pakken (take material)
oMaterial = oAsmDoc.Materials.Item(strMaterialName)
On Error GoTo 0
'door alle parts gaan (go through all parts)
Dim oDoc As Document
For Each oDoc In oAsmDoc.AllReferencedDocuments
' kijken of het een part is (check if part)
If oDoc.DocumentType = kPartDocumentObject Then
Dim oPartDoc As PartDocument
oPartDoc = oDoc
' materiaal instellen
oPartDoc.ComponentDefinition.Material = oMaterial
End If
Next
and the message I get is this:
If someone can help me identifying what the error is then I'll give you a imaginary case of beers 😉
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
1) An assembly doesn't have a set 'material', only parts do.
2) An AssemblyDocumet object doesn't have a 'Materials' property that can be accessed. It does however have a 'MaterialAssets' property. Maybe try changing the variable Type to MaterialAsset, then setting its value like oAsmDoc.MaterialAssets.Item(strMaterialName).
3) You interrupt error checking just long enough to try to get the material asset, then resume normal error checking right afterwards, but what if it doesn't find that material asset...It will then throw an error anyways. Try enclosing that line of code that gets the material asset within a Try...Catch...End Try block of code, then in the Catch portion of it, put a MsgBox and/or Exit Sub or Return.
4) Neither the PartDocument, nor the PartComponentDefinition have a Property called 'Material'. The PartDocument has a MaterialAssets Property, similar to the AssemblyDocument, however, the PartDocument does have an 'ActiveMaterial' Property, that you can set an Asset (or a MaterialAsset) as its value.
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield
(Not an Autodesk Employee)
@Anonymous
Try this version of the code:
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim strMaterialName As String = "_SS 316L_FRP"
Dim oMaterial As MaterialAsset
Try
oMaterial = oAsmDoc.MaterialAssets.Item(strMaterialName)
Catch
MsgBox("Didn't find a Material Asset with that name. Exiting.", , "")
Exit Sub
End Try
'door alle parts gaan (go through all parts)
Dim oDoc As Document
For Each oDoc In oAsmDoc.AllReferencedDocuments
' kijken of het een part is (check if part)
If oDoc.DocumentType = kPartDocumentObject Then
Dim oPartDoc As PartDocument = oDoc
Try
oPartDoc.ActiveMaterial = oMaterial
Catch
MsgBox("Cound't change material for the following part:" & vbCrLf & _
oPartDoc.FullDocumentName,,"")
End Try
End If
Next
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield
(Not an Autodesk Employee)
Hello
Am I right that your code assumes that the material asset is in the assembly document and all part documents or it fails? Wouldn't it be better to only check if material asset is in the actual material setting part and if not try to copy it from the actual material library?
If so, I would suggest tthe following, else correct me please. 🙂
'Actieve assembly seleceren (selecting Assembly)
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
'Materiaal naar parts (Material to parts)
Dim strMaterialName As String
strMaterialName = "_SS 316L" & "_FRP"
Dim oMaterial As MaterialAsset
Try
'Materiaal pakken (take material)
oMaterial = ThisApplication.ActiveMaterialLibrary.MaterialAssets.Item(strMaterialName)
Catch
End Try
'door alle parts gaan (go through all parts)
Dim oDoc As Document
Dim oPartDoc As PartDocument
Dim oMatAsset As MaterialAsset
For Each oDoc In oAsmDoc.AllReferencedDocuments
' kijken of het een part is (check if part)
If oDoc.DocumentType = kPartDocumentObject Then
oPartDoc=oDoc
Try
oMatAsset = oPartDoc.MaterialAssets.Item(strMaterialName)
Catch
If oMaterial Is Nothing Then
MsgBox("Material " & strMaterialName & " not found. Exit",,"iLogic")
Exit Sub
End If
oMatAsset = oMaterial.CopyTo(oPartDoc, False)
End Try
oPartDoc.ActiveMaterial = oMatAsset '.ComponentDefinition.Material = oMaterial
End If
Next
Can't find what you're looking for? Ask the community or share your knowledge.