September 29, 2020

Prepare iLogic Rules for Design Automation

When using Design Automation API for Inventor, you might have to change some of the code in the iLogic Rules of your documents.

1) ThisApplication.ActiveDocument

On the desktop inside Inventor Application, a user can open and activate documents without your add-in's permission, and so when one of its commands is executing, it might need to know which document is currently active - that's what the ActiveDocument property of the Application class will tell us.

On the cloud, using Inventor Server, your application (i.e. the code inside your AppBundle) has full control of things. So, there is not much point in keeping track of which document is active - your application should know what document it opened and needs to work with. And that's why there is no InventorServer.ActiveDocument

Most of the time you could simply replace ThisApplication.ActiveDocument in your iLogic Rules with ThisDoc.Document (the document whose Rule is running) - it depends on the Rule's intent.
However, in certain cases ThisApplication.ActiveDocument might have been a shortcut to the top assembly containing the part whose Rule (in our case "UpdatePart") is running: 

ActiveDocument vs ThisDoc.Document

In that case you would need a function to get to the top assembly. Something like this:

Function GetTopAssembly(doc As Document) As Document
  If doc.ReferencingDocuments.Count = 0 Then
    Set GetTopAssembly = doc
  Else
    Set GetTopAssembly = GetTopAssembly(doc.ReferencingDocuments(1))
  End If
End Function

The above is VBA - in case of iLogic you'd just have to delete the "Set" keywords. 

ThisApplication itself is not supported either. You could use ThisServer instead which is also supported on the desktop. This will only contain properties and functions that Inventor Server supports, so you won't find e.g. ActiveDocument property on it. 

2) iLogic Forms

I already wrote about how you could recreate the same forms for your users on your webpage: 
Get iLogic Form information from Inventor documents

3) Other User Interface components

Since there is no user to interact with, you'll have to remove all the message boxes and other components that the user would have to react to - i.e. calls to MessageBox.Show(), MsgBox(), iLogicForm.Show(), InputBox(), etc

There is a very useful tool called "iLogic Rule Exporter" to help you with this and finding where e.g. ActiveDocument is used. It enables you to extract all the Rules from any Inventor document and its referenced documents into iLogicVb files
https://github.com/ADN-DevTech/iLogicRuleExporter

iLogic Rule Exporter

This makes it much easier to search their source code for certain things: e.g. where a particular parameter is modified, where specific Inventor API's are used, etc 

That should help you find where UI components are used and work around them. 

4) Logs

In case of running things on a server without a user to keep an eye on things, logging what's happening inside your code becomes even more important. 
I already wrote about how to use Trace inside your RulesDebug iLogic code 

Since then, iLogic introduced an object called Logger, which lets you log messages to the "iLogic Log" tab in the Inventor User Interface:

iLogic LoggerHowever, at the moment those logs do not show up in the WorkItem's report. But the Trace logs (e.g. Trace.WriteLine()) do, so use them instead. 

 

Related Posts