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: 

Inventor Not Loading Material Library with API Launch

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
el_jefe_de_steak
743 Views, 4 Replies

Inventor Not Loading Material Library with API Launch

Hi, I'm using the API to control Inventor with a Windows Forms application I built in VisualStudio. The program simply launches Inventor and uses data from an external source to change parameters in a given Inventor assembly. The Inventor assembly contains an iLogic rule that overrides the display color of all components in an assembly based on a parameter value.

 

This is the code I'm using to change the color. It works just fine when I open the document manually and change the parameter.

 

'get the assembly document
Dim myAssembly As AssemblyDocument = ThisDoc.Document	

'set the desired display color
Dim displayColor As String = "Light Gray" 'I get this value from a parameter. This is simply an example.
'get the correct library from Inventor
Dim oAssetLib As AssetLibrary = ThisApplication.AssetLibraries.Item("Inventor Material Library")	
'choose that display color asset from the library
Dim oColorAsset As Asset = oAssetLib.AppearanceAssets.Item(displayColor)

'get the color asset. Copy it locally as needed
Dim oLocalAsset As Asset
Try 'Copy the asset locally.
	oLocalAsset = oColorAsset.CopyTo(ThisDoc.Document)
Catch 'or just use it if it's already local
	oLocalAsset = ThisDoc.Document.Assets.Item(displayColor)
End Try

'change the color (appearance) of each component occurrence in the assembly
For Each oOcc As ComponentOccurrence In myAssembly.ComponentDefinition.Occurrences
	oOcc.Appearance = oLocalAsset		
Next

 

 

However, I am using the Windows Forms application to open a new instance of Inventor, open the assembly, and change the parameter values. When I do that, the iLogic rule runs into an error at this line:

 

Dim oAssetLib As AssetLibrary = ThisApplication.AssetLibraries.Item("Inventor Material Library")

 

With some more debugging, I was able to discover that Inventor has not yet loaded this library. In other words, the library is not attached to Inventor after the API launch.

I am not sure if it is supposed to load this library after it opens a document or at Inventor launch.

I tried to use a try-catch block with the ThisApplication.AssetLibraries.Add("filename") command inside the catch statement, but that threw an error as well...

 

Is there a way to use the API to wait for Inventor to load this asset library before continuing with the program?

Labels (1)
  • API
4 REPLIES 4
Message 2 of 5
wgraham
in reply to: el_jefe_de_steak

Looked at the code where I'm doing something similar and don't believe the asset library can be accessed by the name directly.  It has to be the selected by the item number.  To bypass this, try cycling through the available AssetLibraries and pass back the library where the display name matches the name you're looking for.  Think there is something similar at play with the various MaterialAssets.  It ends up being a bit of extra work, but should get you what you need.

 

Thanks,

William Graham

Message 3 of 5
el_jefe_de_steak
in reply to: wgraham

Thanks for the suggestion. I tried that but it still didn't work. Spent an hour on a Zoom call with my mentor at Ketiv and we were able to figure out a solution. 

 

The problem resided in the fact that Inventor doesn't "load" all of the asset libraries when it is opened using this code:

 

 

Dim invAppType As Type = GetTypeFromProgID("Inventor.Application")
_invApp = CreateInstance(invAppType)

 

 

 

I was able to get it to work by using a try-catch statement to load the asset library. If the try fails, I load the asset library into Inventor with the file path. I get the file path from the project file to make the code more reusable (in the case the path gets changed). Here is the working code:

 

 

 

Dim oAssetLib As AssetLibrary 
Try 'try to get a pre-loaded asset library
	oAssetLib = ThisApplication.AssetLibraries.Item("Inventor Material Library")
Catch 'assume that the project's default library is correct and use the file path to load the library into Inventor
	Dim libPath As String = ThisApplication.DesignProjectManager.ActiveDesignProject.ActiveAppearanceLibrary.LibraryFilename
	oAssetLib = ThisApplication.AssetLibraries.Open(libPath)
End Try

Dim oColorAsset As Asset = oAssetLib.AppearanceAssets.Item(colorName)

'get the color asset. Copy it locally as needed
Dim oLocalAsset As Asset
Try 'Copy the asset locally.
	oLocalAsset = oColorAsset.CopyTo(ThisDoc.Document)
Catch 'or just use it if it's already local
	oLocalAsset = ThisDoc.Document.Assets.Item(colorName)
End Try

For Each oOcc As ComponentOccurrence In myAssembly.ComponentDefinition.Occurrences
	oOcc.Appearance = oLocalAsset
Next

 

 

Message 4 of 5

@el_jefe_de_steak 

 

Dude, thank you SO MUCH for providing an update to your problem. I was struggling with this for HOURS until I found your post. THANK YOU!!!

Message 5 of 5

Something I want to add here. If somebody copied a library from the original ones then you now have at least two libraries with the same internal name. If you try to load the second one while the other is already loaded then you will get a very cryptic message. Nothing leads directly to this. Dont copy a library. Make a new one.

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

Post to forums  

Autodesk Design & Make Report