Hello Everyone,
i have a code, adding a material to a (for now) temporary Material Library. It all works fine, but I need to set the Material Category to the new material.
I tried to add a material category but it didn't work with something like this:
AssetLib.MaterialAssetCategories.Add("","")
Has someone a clue or an idea?
'option explicit on
' PROGRAMMABLAUF
Sub Main()
AddMaterialToLibrary
End Sub
Sub AddMaterialToLibrary()
' Konfiguration -----------------------------------------------------------------------------------
Dim stNameMaterialdatenbank As String = "XXX"
' -------------------------------------------------------------------------------------------------
' Vorbedingungen prüfen
If ThisDoc.Document.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then ' prüfen, ob Bauteil
Dim oDoc As PartDocument
oDoc = ThisDoc.Document
Dim oApp As Inventor.Application
oApp = ThisApplication
Dim AssetLib As AssetLibrary
AssetLib = oApp.AssetLibraries.Open("C:\temp_Material\Asset_Lib_abc.adsklib")
oDoc.ActiveMaterial = oApp.AssetLibraries.Item(stNameMaterialdatenbank).MaterialAssets.Item("---")
' Lösche alle lokalen, unbenutzte Materialien
Dim i As Integer
i = 1
While i <= oDoc.MaterialAssets.Count
If oDoc.MaterialAssets.Item(i).IsUsed = False Then
oDoc.MaterialAssets.Item(i).Delete
Else
i = i + 1
End If
End While
' Create new asset
Dim oNewMatAsset As Asset
oNewMatAsset = oDoc.Assets.Add( _ ' Material anlegen
kAssetTypeMaterial, _ ' AssetTypeEnum: kAssetTypeAppearance, kAssetTypeMaterial, kAssetTypePhysicalProperties, kAssetTypeUnknown
"Metall_abc", _ ' Gruppe
"Optional_abc", _ ' Optional
"NeuesMaterial_abc" _ ' Angezeigter Name
)
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Density").value = 9000
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Thermal_conductivity").value = 0.15
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Specific_heat").value = 1.5
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Thermal_expansion_coefficient").value = 150
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Young_modulus").value = 2900000
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Poisson_ratio").value = 0.4
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Shear_modulus").value = 6400
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Minimum_yield_stress").value = 6500
oNewMatAsset.PhysicalPropertiesAsset.Item("structural_Minimum_tensile_strength").value = 19000
' Get reference to part material object
Dim oLocalMat As Material
oLocalMat = oDoc.ComponentDefinition.Material
'This is a workaround to refresh materials info in the UI
Call oDoc.BrowserPanes.ActivePane.TopNode.DoSelect
oNewMatAsset.CopyTo(AssetLib, True)
End If
End Sub
Solved! Go to Solution.
Solved by d_eckel. Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
Hi @d_eckel . Make sure you choose the correct .Add(DisplayName As Strsng, Asset As Asset) variables when adding a category.
Also your code doesn't define local material correctly (line 60), you need to do it like this:
Dim oLocalMat As Asset oLocalMat = oDoc.ActiveMaterial
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Hi @Andrii_Humeniuk, thanks for your help.
I know that the line with .Add("","") wasn't correct. But what am I supposed to write as an Asset? I've tried things like this:
AssetLib.MaterialAssetCategories.Add("ABC", oNewMatAsset)
The first value is the Name of the Category I want to add. But whats the second value?
That's right, in order to create a new category, you must add one asset to it. Next, you need to use this rule for the ready-made category:
AssetLib.MaterialAssetCategories("ABC").AddAsset(oOfherMatAsset)
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
so even with this code and an empty Library (just to make sure the Material isn't already in the Library) this code does't work:
' Vorbedingungen prüfen If ThisDoc.Document.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then ' prüfen, ob Bauteil Dim oDoc As Inventor.PartDocument oDoc = ThisDoc.Document Dim oApp As Inventor.Application oApp = ThisApplication Dim AssetLib As AssetLibrary AssetLib = oApp.AssetLibraries.Open("C:\temp_Material\Asset_Lib_XXX.adsklib") ' Get reference to part material object Dim oLocalMat As Asset oLocalMat = oDoc.ActiveMaterial 'This is a workaround to refresh materials info in the UI Call oDoc.BrowserPanes.ActivePane.TopNode.DoSelect 'oLocalMat.CopyTo(AssetLib, True) AssetLib.MaterialAssetCategories.Add("ABC", oLocalMat) 'AssetLib.MaterialAssetCategories("ABC").AddAsset(oNewMatAsset) End If
it is probably a simple error or a wrong assumption, but something isn't working.
I found a solution. The Material needs to be the one out of the Library!
' Vorbedingungen prüfen If ThisDoc.Document.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then ' prüfen, ob Bauteil Dim oDoc As Inventor.PartDocument oDoc = ThisDoc.Document Dim oApp As Inventor.Application oApp = ThisApplication Dim oAssetLib As AssetLibrary oAssetLib = oApp.AssetLibraries.Open("C:\temp_Material\Asset_Lib_Eckel.adsklib") ' Get reference to part material object Dim oLocalMat As Asset oLocalMat = oDoc.ActiveMaterial 'This is a workaround to refresh materials info in the UI Call oDoc.BrowserPanes.ActivePane.TopNode.DoSelect oLocalMat.CopyTo(oAssetLib, True) Try oAssetLib.MaterialAssetCategories("ABC").AddAsset(oAssetLib.MaterialAssets.Item(oLocalMat.DisplayName)) Catch oAssetLib.MaterialAssetCategories.Add("ABC", oAssetLib.MaterialAssets.Item(oLocalMat.DisplayName)) End Try End If
Maybe I missunderstood you, @Andrii_Humeniuk but thanks for your help and "discussion".
Is it possible that your material library set to ReadOnly? Can you do what you are trying to do manually?
Wesley Crihfield
(Not an Autodesk Employee)
Hi @WCrihfield, it's working fine now. The problem was the wrongly linked Asset in
AssetLib.MaterialAssetCategories.Add("ABC", oNewMatAsset)
It needs to be an Asset out of the library, not the oDoc.ActiveMaterial - Asset.
Can't find what you're looking for? Ask the community or share your knowledge.