Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do a add in new materials on a document via the api

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
davidt162003
248 Views, 4 Replies

How do a add in new materials on a document via the api

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 

davidt162003_0-1713444763306.png

 

HII
Labels (3)
4 REPLIES 4
Message 2 of 5
WCrihfield
in reply to: davidt162003

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

EESignature

(Not an Autodesk Employee)

Message 3 of 5
A.Acheson
in reply to: davidt162003

Hi @davidt162003 

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!

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 4 of 5
WCrihfield
in reply to: A.Acheson

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

EESignature

(Not an Autodesk Employee)

Message 5 of 5
davidt162003
in reply to: davidt162003

@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

 

HII

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report