Inventor 2014 API: Set Part material

by Vladimir Ananyev

With Inventor 2014 new API functionality has been introduced that fully supports consistent materials. The primary object in consistent materials is an Asset object, where Asset objects represent materials, physical properties, and colors (which are now referred to as appearances). An asset is essentially a collection of values. You may consider a material as a combination of its physical properties (density, yield strength, etc.) and how it looks or its appearance.

A material property has some basic information that identifies it – name, description, type, etc., and it references a physical properties asset and an appearance asset.

Assets exist within libraries. Inventor API Help contains several code samples that write out all of the information associated with the material, appearance, and physical property assets.

The following VBA sample set desired material from the Autodesk Material Library to the active part.

 

Private Sub SetMaterialToPart() 

  Dim oDoc As PartDocument

  Set oDoc = ThisApplication.ActiveDocument

 

  Dim Name As String

  Name = "Copper"

    Name = "Steel"

 

  Dim localAsset As Asset

  On Error Resume Next

  Set localAsset = oDoc.Assets.Item(Name)

  If Err Then

    On Error GoTo 0

    ‘ Failed to get the appearance

    ‘ in the document, so import it.

   

    ‘ Get an asset library by name.

    ‘ Either the displayed name (which

    ‘ can changed based on the current language)

    ‘ or the internal name

    ‘ (which is always the same) can be used.

    Dim assetLib As AssetLibrary

    Set assetLib = ThisApplication.AssetLibraries _

        .Item("Autodesk Material Library")

    Set assetLib = ThisApplication.AssetLibraries _

       ‘.Item("AD121259-C03E-4A1D-92D8-59A22B4807AD")

   

    ‘ Get an asset in the library

    Dim libAsset As Asset

    Set libAsset = assetLib.MaterialAssets.Item(Name)

    ‘ Copy the asset locally.

    Set localAsset = libAsset.CopyTo(oDoc)

  End If

  On Error GoTo 0

 

  ‘set material to the part

  oDoc.ActiveMaterial = localAsset

 

  ‘ Select the top browser node of the model pane.

  ‘ This is a workaround to refresh materials info in the UI.

  Call oDoc.BrowserPanes.ActivePane.TopNode.DoSelect

 

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNoSpacing”>End Sub


Comments

4 responses to “Inventor 2014 API: Set Part material”

  1. I had to use oDoc.MaterialAssets.Item(Name) instead of oDoc.Assets.Item(Name) to make it works.

  2. Serje Bulavskiy Avatar
    Serje Bulavskiy

    Hello, Vladimir. Example is incorrect or i’m doing something wrong ?
    I found Asset and MaterialAsset types in documentation but when i’m trying to use it, Visual Studio failing to find this types in API.
    Visual Studio 2013, Inventor 2014, C# 5.0, .NET 4.0

  3. ACEDeSmedt Avatar
    ACEDeSmedt

    This does not seem to work well with custom/migrated libraries in 2014. The old method still works (thank you backward compatibility) and uses much less code witch results in cleaner code:
    Dim M As Material
    Set M = ThisApplication.StylesManager.Materials(MaterialName)
    Doc.ComponentDefinition.Material = M

  4. Hi Serje,
    Did you ever find a solution? I’m facing the same problem, also with C#…

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading