Advanced techniques to write iLogic rules

You can write iLogic rules using only Autodesk Inventor parameter assignment statements, predefined iLogic functions, and simple VB.NET code. However, you are not limited to these techniques. You can use more advanced features of VB.NET in a rule. Use Visual Basic 2005 or 2008 to create external DLL files that you can call from rules. You can also call DLL files written in C# or C++.

Visual Basic 2008 (.NET Framework 3.5) syntax is supported within the rule code.

You can use the following statements at the top of the rule, before the main rule code:

Option Explicit On

Option Strict On

Option Infer Off

AddReference "filename.dll"

Imports ...

AddVbRule "Other Rule"

AddVbFile "filename.vb"

AddResources "filename.resources"

Option and Imports are standard VB.NET statements.

Option Explicit Off is the default. It allows you to use local variables without declaring them.

Option Strict Off is the default. Option Strict On also works for most rules.

Option Infer On is the default.

The main rule code is an "implicit" subroutine. To add more subroutines, functions, properties, or Classes, declare a Sub Main() as follows:

Sub Main()
' your main rule code here
End Sub

Sub Main() can be used in any rule, regardless of whether the rule has additional code. For more detailed information about explicitly declaring your rule class, see “How is a rule processed?”

AddReference

Uses special iLogic syntax. Adding a DLL name to an AddReference statement is like using the AddReference command and browsing for a DLL in Visual Studio or Visual Basic Express.

Only .NET assemblies are supported. The DLL can be a standard Microsoft assembly, as in the Microsoft.NET\Framework folder.

For example, consider the following statement:

AddReference "System.Drawing.dll"

In this statement, the .dll extension is optional.

You can also specify a user-created or third-party class library as a reference. User-created or third-party DLLs must all be in one folder. By default, these DLLs are located under the Autodesk Inventor installation folder, in an iLogicAdd subfolder, such as:

C:\Program Files\Autodesk\Inventor [Version]\Bin\iLogicAdd

You can change this location using the iLogic Configuration command. You can also add references to DLLs in the main Autodesk Inventor Bin folder (for example, C:\Program Files\Autodesk\Inventor [Version]\Bin).

The AddReference statement does not support a full path specification. You can only specify a filename. Add references to assemblies in the Global Assembly Cache (GAC), using a qualified name such as:

AddReference "VsWebSite.Interop, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”

You can also create or access registered COM objects in a rule using CreateObject or GetActiveObject. It is not necessary to use AddReference for a COM DLL.

AddVbRule

Includes the code from the specified rule as VB code, compiled with the main rule. The specified rule cannot be a standard rule. It must have the Straight VB code option enabled in the Options panel of the Edit Rule dialog box. iLogic does not pre-process the code in such a rule. The rule contains Classes or Modules that can be called from other rules. Use this statement to share code between rules, or to keep code modular.

Example

AddVbRule "Other Rule"

AddVbFile

Operates similarly to AddVbRule, except that the VB code is stored in an external file instead of a rule. As with AddVbRule, the code must have the Straight VB code option enabled. This statement is useful for sharing common code between models. The file specification must be a relative path name. iLogic searches the folder of the current Autodesk Inventor document, and in other folders, using the same search order employed for external rules.

Example

AddVbFile "fileName.vb"

AddResources

Adds resources such as strings or bitmaps. The resources must be compiled with Visual Studio.

Example

AddResources "fileName.resources"

Imports

Duplicate Imports statements are not allowed. The following default Imports statements are implicitly used in all rules:

If you use MessageBox in a rule, then iLogic automatically adds the following statement:

Imports System.Windows.Forms

If you use ThisApplication or other Autodesk Inventor objects, then iLogic automatically adds the following:

Imports Inventor

Rule arguments

iLogic provides advanced functionality that allows you to pass context information as rule arguments into the rules you run. This information can be used to modify the behavior of a rule without having to create a duplicate rule.

You pass rule arguments using functions available in the IiLogicAutomation interface (for external clients), and in iLogicVB (for other rule code and internal clients). These arguments are made available within a rule via the RuleArguments property.

For IiLogicAutomation, the functions available include:

Function RunRuleWithArguments(ByVal doc As Inventor.Document, ByVal ruleName As String, ByVal ruleArguments As Inventor.NameValueMap) As Integer

Function RunExternalRuleWithArguments(ByVal doc As Inventor.Document, ByVal ruleName As String, ByVal ruleArguments As Inventor.NameValueMap) As Integer

Function RunRuleDirectWithArguments(ByVal rule As iLogicRule, ByVal ruleArguments As Inventor.NameValueMap) As Integer

For iLogicVB, the functions available include:

Function RunRule(ByVal ruleName As String, ByVal ruleArguments As Inventor.NameValueMap) As Integer

Function RunRule(ByVal compoOrDocName As Object, ByVal ruleName As String, ByVal ruleArguments As Inventor.NameValueMap) As Integer

Function RunExternalRule(ByVal ruleName As String, ByVal ruleArguments As Inventor.NameValueMap) As Integer

Create rule arguments

To create rule arguments, use the Inventor API to create a new NameValueMap object. It is then passed to one of the functions when running the rule.

Access an argument passed to the rule

x = RuleArguments(“myArg”)

Determine if an argument has been passed to the rule

If RuleArguments.Exists(“myArg”) Then...

Pass the set of arguments to another rule using RunRule

iLogicVB.RunRule(“someOtherRule”, RuleArguments.Arguments)

Additional statements

If you use Sub Main(), the rule follows standard VB.NET format for a class, except that the Class... and End Class statements are not visible, and the statements before Sub Main() are outside the class. Therefore, place all assignment statements inside a Sub, Function, or Property. You can include declaration statements for class member variables such as Private temp As Double = 4.5 outside of a subroutine or function.

You can add Sub, Function, Property, and Class definitions after Sub Main()... End Sub. Any Class you add is nested in the main rule Class for that rule and cannot be used from another rule. To add an independent Class or Module, explicitly declare the rule Class using the following:

Class ThisRule ' ...
Sub Main
End Sub
' ...
End Class

You can then add another Class or Module (or more than one) outside of this code. Class ThisRule becomes your main rule Class, and iLogic calls Sub Main (within it) to run your rule.

To include a Moduleor Class that is visible to multiple rules, put it in an external assembly (DLL). You can put more than one in the same DLL. You can also use AddVbRule to put them in a rule identified as "Straight VB code" within the Inventor document (). Or, use AddVbFile to put them in an external VB file.

When you develop advanced VB.NET code, use Visual Studio or Visual Basic Express rather than coding directly in a rule. You can then cut and paste relatively small snippets of code from Visual Studio into a rule. You can even paste in an entire dialog box Class (although resources are not easily supported). For larger units of code, or where required, create an assembly and use it as an external DLL from a rule.

You can store objects that are instances of a user-defined Class using the iLogic rule Shared Variable functions. To store these objects, serialize the Class, or it must be derived from MarshalByRefObject.

External DLLs

You can write your own external DLLs in .NET and call them from a rule. To debug a DLL, set the build output path under Project Properties Compile to iLogicAdd (under the Inventor Bin folder). Then, choose Inventor.exe for Start external program in your project properties in Visual Studio. This method allows you all the benefits of the debugging environment, including Edit and Continue.

External DLLs are useful when using a rule to display a dialog box for user input.

Inventor API

To access the Inventor API from a rule, use the ThisApplication property to access the Inventor application. Use ThisDoc.Document to access the document that contains the current rule.. You can create and modify features. Use Parameter.Param to get direct access to an Inventor.Parameter.

To use the Inventor API from your own DLL, add a reference to Autodesk.Inventor.Interop.dll in your Visual Basic project. It displays in the Add Reference dialog box on the .NET tab. For Inventor 2011, the version is 15.0.0.0.

Use iLogic interfaces in external DLLs

All iLogic functions are grouped under interface objects such as Parameter, iProperties, iPart, and so on. You can pass iLogic interface objects as arguments to functions in external DLLs. To use these objects in your project, add a reference to Autodesk.iLogic.Interfaces.dll. Select the Browse tab in the Add Reference dialog box, and browse to the iLogicBin folder, under the Inventor bin folder (usually in C:\Program Files\Autodesk\Inventor [Version]\Bin\iLogicBin).

Documentation for the iLogic interfaces is provided in Autodesk.iLogic.Interfaces.xml. You can use the Object Browser in Visual Studio to read the interface descriptions. These descriptions include the names of the objects which implement the interfaces in rules. For example, the Feature object in a rule implements the ICadFeature interface.

Parameters in rules

By default, numeric parameters in rules are not declared as Double, but instead as a custom iLogic type called DoubleForEquals.