So I want to add in a material into a documents based on a sql database so i dont have to worry about keeping the inventor material library up to date. the only 2 properties i care about are density and name. the apperance and anything else i dont really care about.
Friend Sub SetMaterial(PartDoc As PartDocument, UNS_ID As Integer, Driver As InventorDriver)
Dim InvMaterial As Material
InvMaterial = PartDoc.Materials.Add("Tester", 78.9)
PartDoc.ActiveMaterial = InvMaterial
End Sub
I tried using this code but it didnt work beacuse the material i create apprently isnt an asset but it dose show up in the material menu
Solved! Go to Solution.
Solved by davidt162003. Go to Solution.
Hi @davidt162003. If you want to add a new material (a MaterialAsset) into a part, then you must either copy one into the part from a global material library (an AssetLibrary) (AssetLibrary.MaterialAssets) (Asset.CopyTo) ; or you must copy an existing material asset that is already within the part, to create another (Asset.Duplicate). If you copied one that is not the Density that you want, then you would have to modify the AssetValue within it to set its Density the way you want it. AssetValue is a base type for a bunch of derived asset value types with more specific properties.
You will find the existing 'local' ones in the PartDocument.MaterialAssets collection. That returns an AssetEnumerator, so it does not have an 'Add' method. And we can get/set the parts active material with the PartDocument.ActiveMaterial property (has an Asset object as its value).
Wesley Crihfield
(Not an Autodesk Employee)
I'm not certain where you found PartDoc.Materials but that doesn't exist now in the API help page for properties of part document. Material assets is there and what you need. Maybe materials existed in old documentation and might be still active but not documented.
Syntax
PartDocument.MaterialAssets() As AssetsEnumerator
Here is a post that attempted to do the same, I am not sure if they succeeded or if we even have access to set the asset properties. There isn't alot of documentation on the process.
Maybe some more experts with expierience can chime in!
Here is the online help page I usually point people to in these situations:
https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-2912C0FB-885F-47ED-81C3-AF19584EA9C1
It explains when the Inventor API changed from using a 'Material' object and 'RenderStyle' object, to using 'Asset' type objects around 2013-2014 versions of Inventor. It seems like they have keept 'supporting' those old objects in the background (to keep old solutions working), but hid them, so that no new development would attempt to work with them.
Wesley Crihfield
(Not an Autodesk Employee)
@WCrihfield @A.Acheson your both right it dose seem i was using old API, it was a suggestion from intelisense in visual studio. thank you both for the help and pointing me in the right direction.
this is the code i settled on
Friend Sub SetMaterial(PartDoc As PartDocument, UNS_ID As Integer, Driver As InventorDriver)
Dim DBMaterial As Object = GetUNSMaterial(UNS_ID, Driver)' Gets an 'object which holds data from a sql database
If DBMaterial Is Nothing Then DBMaterial = New TempMaterial("Tester", 72000)
Dim InvNewMaterial As MaterialAsset = PartDoc.ActiveMaterial.Duplicate(DBMaterial.DisplayText)
For Each MaterialProp As AssetValue In InvNewMaterial.PhysicalPropertiesAsset
If MaterialProp.Name.Contains("ensity") Then
MaterialProp.Value = DBMaterial.Density
Exit For
End If
Next
PartDoc.ActiveMaterial = InvNewMaterial
End Sub
Friend Class TempMaterial ' this is just a holder class i used for testing
Property DisplayText As String
Property Density As Double
Sub New(_DisplayName As String, _Density As Double)
DisplayText = _DisplayName
Density = _Density
End Sub
End Class
Can't find what you're looking for? Ask the community or share your knowledge.