Hi @CattabianiI. Correct. Even though I sometimes suggest solutions that include executing a command (ControlDefinition), or one that uses the SendKeys functionality, I pretty much always warn folks against solutions like that these days. They are more of a desperate last resort, when you can not figure out any other way of doing something that you need to quickly automate, to save yourself a lot of time. Any time there is a way to do something using built-in API or iLogic methods, I always try to suggest those first. If I can not think of any API or iLogic way to do something, but think that I may be able to automate it by executing commands and/or using SendKeys, depending on the scenario, I will usually only use that (or suggest that) sparingly and/or suggest caution. Executing commands requires that the document that the commend is to act upon be actively visible on your screen at the time, not just some referenced document within an active assembly, or something like that. Plus, they usually require some sort of object(s) be in the Document.SelectSet just before or after executing the command, and sometimes are complicated to get back out of the command after the primary task is completed. Using SendKeys can also be pretty dangerous, because there is not a very good way of telling it where you want those keystrokes to be sent to. And if they get sent to the wrong place, it could potentially cause lots of problems. The 'AppActivate' method is probably the most basic way I had ever seen to help accomplish what I was attempting to do at the time. There are some other, more advanced/complex methods that are more accurate to use than that method that I picked up on later, but I still do not like to use SendKeys, if I can avoid it any other way.
I was wrong about the timing of the Material & RenderStyle change-over in my previous post. That happened back in 2013, not 2016. Below is a link to the online help documentation about that.
https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-2912C0FB-885F-47ED-81C3-AF19584EA9C1
(if that Link does not work, just click on the ? [help] in Inventor, then navigate to Programming Interface > Inventor API User's Manual > General Concepts > Materials & Appearances)
So, according to that documentation, we should not be continuing to count on PartDocument.ComponentDefinition.Material property for these types of tasks, because it is long outdated, and they have only maintained them for a while, due to some folks having some pre-existing automation solutions that were relying on them, to support their functionality for a while, until they could get their stuff updated or changed to the new way of doing things.
But I must have updated my code since I posted those text files here back in July 2023, because when I compared that code (the last text file - purge & update using iLogic) to my latest code, it is different. I have expanded the portion of the Function where it is attempting to obtain the library version of the Asset, to first check the Asset.AssetType property, where its Value is a variation of the AssetTypeEnum, to check if it is for a material, appearance, or physical, then use that knowledge to choose which library to get the matching library Asset object from, instead of just blindly trying each library one at a time, in a specific order. You could also incorporate a document type check into either the 'Main' or Function portion of it if you wanted, to avoid it trying to run on something like a drawing, but that is up to you. You could also specify just one, or multiple custom AssetLibraries, instead of the built-in ones, if needed. Just keep in mind that a material library can contain both materials and appearances, not just materials. As you can see when you inspect the properties of a MaterialAsset object (a Type derived from the regular Asset Type), it has a reference to an appearance asset, and a physical asset, so most likely, the same library can hold assets used for all 3 purposes. We do have one custom material library, with a small number of materials in it, but we use the built-in material libraries for most stuff. I have attached that updated iLogic code as a text file again, just for reference.
That code could be converted to purely Inventor API (removing iLogic dependency), with a few changes, if needed. One thing would be to get rid of the RuleArguments portion of the code, and the 'ThisDoc.Document' phrase, which was included to optionally support the functionality of running this rule from another rule (or not), where that other rule used a NameValueMap with a Document object put into it (as the rule arguments to send), as a means of telling this rule which document to focus its attention on when it runs. Another thing would be to compensate for use of the 'ThisApplication' veriable, which is defined for us in both iLogic and VBA, behind the scenes. Then you would need to either delete or replace the 'Logger' functionality, on the Catch side of the Try...Catch statement, because 'Logger' is also a 'Rule Object' (variable defined by iLogic, behind the scenes of every rule we create).
Wesley Crihfield

(Not an Autodesk Employee)