Custom Asset/Material Library

Custom Asset/Material Library

Anonymous
Not applicable
7,890 Views
20 Replies
Message 1 of 21

Custom Asset/Material Library

Anonymous
Not applicable

Hello Everyone,

 

I've been at this for a couple days now trying to figure how code up a material library. I would like to use a spreadsheet to feed a material properties library. What I know so far is that the material, appearance, and physical properties are 3 different assets. So a copper material contains the material asset, an appearance asset, and a physical property asset each with its own values. For now, I am just focusing on the physical asset.

 

I also know that material asset can not be created or even modified in an asset library, but rather created in the open document and then copied to the asset library. Here is the code I have so far that is able to create a new material and change its physical properties, but I am stuck on how to get it to save to the Asset library after. Any help is appreciated, Thank you for you time.

 

Sub AddMaterialToLibrary()

Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oApp As Application
Set oApp = ThisApplication

Dim AssetLib As AssetLibrary
Set AssetLib = ThisApplication.AssetLibraries.Open("C:\Temp\Asset Lib.adsklib")

'Dim myAssetLib As AssetLibrary
'Set myAssetLib = AssetLib("Asset Lib")

' Create new asset
Dim newAsset As Asset
Set newAsset = oDoc.Assets.Add(kAssetTypeMaterial, "Metal", "New Material", "New Material")

Set localPhysicalPropsAsset = newAsset.PhysicalPropertiesAsset

newAsset.PhysicalPropertiesAsset.Item("structural_Density").value = 9000
newAsset.PhysicalPropertiesAsset.Item("structural_Thermal_conductivity").value = 0.15
newAsset.PhysicalPropertiesAsset.Item("structural_Specific_heat").value = 1.5
newAsset.PhysicalPropertiesAsset.Item("structural_Thermal_expansion_coefficient").value = 150
newAsset.PhysicalPropertiesAsset.Item("structural_Young_modulus").value = 2900000
newAsset.PhysicalPropertiesAsset.Item("structural_Poisson_ratio").value = 0.4
newAsset.PhysicalPropertiesAsset.Item("structural_Shear_modulus").value = 6400
newAsset.PhysicalPropertiesAsset.Item("structural_Minimum_yield_stress").value = 6500
newAsset.PhysicalPropertiesAsset.Item("structural_Minimum_tensile_strength").value = 19000


' Apply newly created material to part
'oDoc.ActiveMaterial = localAsset

' Get reference to part material object
Dim oLocalMat As Material
Set oLocalMat = oDoc.ComponentDefinition.Material

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

Call oDoc.BrowserPanes.ActivePane.TopNode.DoSelect
newAsset.CopyTo (AssetLib)


End Sub

 

 

Accepted solutions (1)
7,891 Views
20 Replies
Replies (20)
Message 2 of 21

JaneFan
Autodesk
Autodesk
Accepted solution

Hello There,

 

Do you mean it fails at the last line in CopyTo method? If so, please try changing it to this line:

Call newAsset.CopyTo (AssetLib)

 

In VB or VBA, we need call method either like this:

newAsset.CopyTo AssetLib

or like this:

Call newAsset.CopyTo(AssetLib)




Jane Fan
Inventor/Fusion QA Engineer
Message 3 of 21

Anonymous
Not applicable

Ok that was really simple, don't know why I was having so much issues with it. Must be frustrations on trying to figure this issue out. Thank you!

 

How does inventor know to link the physical asset to material or does it automatically link the asset to the current active material?

0 Likes
Message 4 of 21

JaneFan
Autodesk
Autodesk

Hello,

 

With Assets.Add(), we can only create MaterialAssets and AppearanceAssets. When a material asset is created, a physical asset is automatically created that is associated with it that you can edit, just as what you are doing here.
What you modified to PhysicalAsset reflects on the material asset you just created directly, which has nothing to do with current activated material.

 

As the code in your post, you get Assets from PartDocument, then the new asset you just created is in document level, that you can get it from ActiveDocument.Assets and can use it in current document. If you want to use it in inventor application level, you need copy it to a existing asset library or a new library which you can created by AssetLibraries.Add().

 




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 5 of 21

Anonymous
Not applicable

Jane,

 

Thanks for the help, that makes sense. For the appearance I am trying to use

 

Dim aaLib As AssetLibrary
Set aaLib = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")
Set newAsset = aaLib.AppearanceAssets.Item("Gold")

 

which doesn't error out, but I assume you now need to apply it to the current material before saving it out?

0 Likes
Message 6 of 21

3DAli
Collaborator
Collaborator

Hi Jane, 

 

I have a related issue to the same subject and I was wondering if you can help me.

I wrote a macro that I run in drawing environment, when I right click on a view and run the macro via Marking Menu , it recognises if the the selected  view reference document is part and if so it runs the macro, My macro works fine with changing the material in the drawing environment as long as the correct material library is selected. I have a custom material library and if I pick it once in the part environment my macro works fine, but if its by default on Autodesk Material Library, error message will pop up, is there any way I can change the material library in drawing environment for a referenced document (part) when that part is not open ?

 

Thank you so much for your help in advance, 

 

Regards 

0 Likes
Message 7 of 21

JaneFan
Autodesk
Autodesk

Hey @3DAli

 

If the asset is not copied to a document, it can't be used directly to set to a document.

In such case, we need to find the asset from the relative library, then set it to a document, Like the following code, to find the Asset from "Inventor Material Library" and then set to the view referenced part. Hope it helps. 

Sub Test()
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
   
    Dim oSheet As Sheet
    Set oSheet = oDoc.Sheets(1)
   
    Dim oView As DrawingView
    Set oView = oSheet.DrawingViews(1)
   
    Dim oPart As PartDocument
    Set oPart = oView.ReferencedDocumentDescriptor.ReferencedDocument
    Dim oAsset  As Asset
    Set oAsset = ThisApplication.AssetLibraries.Item("Inventor Material Library").AppearanceAssets(5)
    oPart.ActiveAppearance = oAsset
End Sub




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 8 of 21

3DAli
Collaborator
Collaborator

Hi Jane, 

 

Thank you for your prompt response on this, here is what I'm doing,

 

These  variables are all defined in a module as global across my project:

 

Global oSheet As Sheet
Global invDV As DrawingView
Global oRefFileDesc As ReferencedFileDescriptor
Global oPartDoc As PartDocument
Global oAsmDoc As AssemblyDocument
Global oPresDoc As PresentationDocument
Global oRefDoc As Document
Global oDesc As String
Global oPartNumber As String
Global OriginalDesc As String
Global Template As String
Global TUnit As String
Global UUnit As String
Global SecType As String
Global iConvFac As Double
Global iPrecision As String
Global iRoundIt As Boolean
Option Explicit

 

when my form initialises :

 

 

  Set invDV = ThisApplication.ActiveDocument.SelectSet(1)
Set oSheet = ThisApplication.ActiveDocument.ActiveSheet
Set oRefFileDesc = invDV.ReferencedFile

If (TypeOf oRefFileDesc.ReferencedDocument Is PartDocument) Then

Set oPartDoc = oRefFileDesc.ReferencedDocument

ElseIf (TypeOf oRefFileDesc.ReferencedDocument Is AssemblyDocument) Then

msg = "Selected view reference document is an assembly" & vbCr & _
"Select a view with a part reference!"
MsgBox msg

End

End If

Set oRefDoc = oRefFileDesc.ReferencedDocument
oDesc = oRefDoc.PropertySets("Design Tracking Properties").ItemByPropId(kDescriptionDesignTrackingProperties).Value
If Len(oRefDoc.DisplayName) = 10 Then
oPartNumber = oRefDoc.DisplayName
Else
oPartNumber = oRefDoc.DisplayName
End If

EDiMaterial.Caption = "EDiMaterial : " & oPartNumber

ComboBox1.Enabled = True
ComboBox1.AddItem ("300W")
ComboBox1.AddItem ("350W(A)")
ComboBox1.AddItem ("350W(B)")
ComboBox1.AddItem ("350WT(A)")
ComboBox1.AddItem ("350WT(B)")

 

if the selected view is part the user should be able to select the desired material off the combo list  and press apply

 

Capture.JPG

 

I have this code under my "Apply" button click event :

 

Private Sub CommandButton2_Click()

Dim assetLib As AssetLibrary
'set asset to custom library
Set assetLib = ThisApplication.AssetLibraries.Item("Extended Steel Library")
oPartDoc.ComponentDefinition.Material = oPartDoc.Materials(ComboBox1.Text)
oPartDoc.Update2
oSheet.Update
EDiMaterial.Hide

End Sub

 

 

This worked for me , thanks for the hint 🙂 

 

please let me know if there's something here than can be improved in my method, otherwise I'm good 

 

Cheers!

 

0 Likes
Message 9 of 21

3DAli
Collaborator
Collaborator

It's me again, 

I dont know why , but when I restarted the inventor, 

 

this happens :

 

Set assetLib = ThisApplication.AssetLibraries.Item("Extended Steel Library") ---> "Invalid procedure call or argument" 

 

so obviously the function is nit used in a right way.

 

could you please give me a hint to resolve the issue

 

Thank you

0 Likes
Message 10 of 21

JaneFan
Autodesk
Autodesk

Hey @3DAli

 

Please add the library into the libraries list in project settings: 

test.png




Jane Fan
Inventor/Fusion QA Engineer
Message 11 of 21

3DAli
Collaborator
Collaborator

Hi Jane, 

That has been added already.

is there anything else I should check to diagnose this issue ?

 

Thanks 

 

 Capture.JPG

0 Likes
Message 12 of 21

3DAli
Collaborator
Collaborator

just to clarify, 

 

if I open a part and change the active library to my custom library, then close that file, open a drawing, import any other part into my drawing space and run the macro it works totally fine, the only time it doesn't work is when you restart the software or you go inside a part and change the active library to anything other that "Extended Steel". So essentially , where I am failing at is to change the active library via VBA code.

 

Regards

0 Likes
Message 13 of 21

JaneFan
Autodesk
Autodesk

It seems to me not reproducible with the versions newer than inventor 2017, what is the inventor version you are using? 

Sub testlibrary()
    Dim oLib As AssetLibrary
    Set oLib = ThisApplication.AssetLibraries.Item("test")
   
    ThisApplication.ActiveAppearanceLibrary = oLib
End Sub

 




Jane Fan
Inventor/Fusion QA Engineer
Message 14 of 21

3DAli
Collaborator
Collaborator

Hi Jane, 

 

I'm using the latest update 2018.2, factory design suit (inventor professional 2018). I have recorded my experience for you to look at hopefully we can solve this together.

 

Thank you so much,

Ali

 

 
0 Likes
Message 15 of 21

JaneFan
Autodesk
Autodesk

Hey Ali, 

 

I see what the thing is. 

The material API has changed several years ago that we can see PartComponentDefinition.Material is a hidden property now. 

Here are two choices for you: 

1. If you still want to use the hidden property Material: 

Set the active material library to the one you needed in API before the line you met error: 

ThisApplication.ActiveMaterialLibrary = assetLib

2. Try to switch to the new way to set the appearance/material: 

Replace the error line with this one: 

oPartDoc.ActiveMaterial = assetLib.MaterialAssets.Item(ComboBox1.Text)




Jane Fan
Inventor/Fusion QA Engineer
Message 16 of 21

3DAli
Collaborator
Collaborator

Thank you so much, 

worked like a charm, 

I picked the second method which is a new way as per your advise.

Appreciate your help Jane 🙂

 

0 Likes
Message 17 of 21

RoyWickrama_RWEI
Advisor
Advisor

Hi Jane;

 

I have a similar need: need to activate my custom library, DhaMag Material Library, so that I can get a drop-down list of materials from the custom library.

 

 

Sub Main()

Dim assetLib As AssetLibrary
'set asset to custom library
	oCust_Lib_Name = "Dhamag Material Libary"
	oDefault_Lib_Name = "Autodesk Material Library"
assetLib = ThisApplication.AssetLibraries.Item(oCust_Lib_Name)

End Sub

 

It runs for the oDefault_Lib_Name, but not for the oCust_Lib_Name.

oCust_Lib_Name (DhaMag Material Library) is set up in the project.

 

Could you reply. Thanks.

0 Likes
Message 18 of 21

MechMachineMan
Advisor
Advisor

@RoyWickrama_RWEI it ends up making the threads a flurry of old posts with not very much relevance if you revive old threads based on a thread you already have open.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 19 of 21

Anonymous
Not applicable

Hi Jane,

In regards of Material Library, I am trying to write code through vb net (API) to assign the Material and CategoryName using my custom Library, but I can not find the "Asset" nor the "AssetLibrary" the code that it was in this trend I think is for VBA and not for VB which is different, I found one with the use of "Apprentice" which is outside the inventor but I still don't find the object API to modify the part "Material" and "Category"

I am hoping you can describe a little more how to find it.

Thank you for the help I can get.

Sam

0 Likes
Message 20 of 21

JaneFan
Autodesk
Autodesk

Hey @Anonymous , 

 

You must have added reference to Autodesk.Inventor.Interop.dll, right? Then we can use it directly like this: 

Dim oApp As Inventor.Application
Dim assetLibs As Inventor.AssetLibraries
assetLibs = oApp.AssetLibraries

If you ca1.png

 

n't find it, try to check whether this dll version is too old, it should have these interfaces. 




Jane Fan
Inventor/Fusion QA Engineer