9 Jan 2023

Use custom asset library on Design Automation

 

 

You can create your own Asset (Material or Appearance) library to store your custom assets. In case of the desktop version of Inventor, if you added your library to the active Inventor Project then it seems to be loaded automatically and can access them through InventorServer.AssetLibraries in the API.

Inventor Projects dialog

Inventor Server used on Design Automation is less accommodating and so you have to load the library yourself.  
If the custom asset library is already added to the Inventor Project and on Design Automation you use a zip containing all your files including the *.ipj file then the simplest might be to find them through InventorServer.DesignProjectManager.ActiveDesignProject.AppearanceLibraries

Don't forget you cannot use certain variables like ThisApplication in iLogic rule running on Design Automation - see Prepare iLogic Rules for Design Automation

Here is the code I'm using inside an iLogic rule to switch to a material in my custom appearance library if the Material parameter's value is "Custom"

' see https://aps.autodesk.com/en/docs/design-automation/v3/developers_guide/inventor_specific/ilogic-logging/
iLogicVb.Automation.LogControl.Level = LogLevel.Trace
  
If Material = "Custom" Then
  Logger.Trace("Inside If")
	  
  Dim doc As AssemblyDocument
  doc = ThisDoc.Document 
	  
  designProj = ThisServer.DesignProjectManager.ActiveDesignProject
	
  ' <debug> '  
  ' This does not contain my library on Design Automation '
  Logger.Trace("ThisServer.AssetLibraries")
  For Each l As Inventor.AssetLibrary In ThisServer.AssetLibraries
    Logger.Trace(l.DisplayName + " / " + l.InternalName)
  Next
	  
  ' This does contain my library on Design Automation '
  Logger.Trace("ActiveDesignProject.AssetLibraries")
  For Each l As Inventor.ProjectAssetLibrary In designProj.AppearanceLibraries
    Logger.Trace(l.Name + " / " + l.LibraryFilename)
  Next
  ' </debug> '
	  
  For Each l As Inventor.ProjectAssetLibrary In designProj.AppearanceLibraries
    If l.Name = "mylibrary" Then  
      mylibPath = l.LibraryFilename
      Exit For
    End If
  Next
	
  Logger.Trace("Library path: " + mylibPath)
  mylib = ThisServer.AssetLibraries.Open(mylibPath)
  Logger.Trace("Got library")
  libApp = mylib.AppearanceAssets("Generic")
  Logger.Trace("Got appearance")
	
  localApp = libApp.CopyTo(doc)
  Logger.Trace("Copied appearance to document")
	  
  doc.ComponentDefinition.Occurrences(1).Appearance = localApp
  Logger.Trace("Changed occurrence to custom appearance")
End If

Here is the sample assembly with the custom asset library and iLogic rule: CustomAppearance2023.zip

Contents of the sample project

Note: if you want to test an Inventor model along with a project file (*.ipj) you can use this sample project https://github.com/adamenagy/bucket-model-updater (readme includes link to video showing how to use it)

Related Article