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?
Solved! Go to Solution.
Solved by el_jefe_de_steak. Go to Solution.
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
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
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!!!
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.