What are rules?
A rule is a small Visual Basic (VB.NET) program that can monitor and control other Inventor parameters, features, or components.
How do rules work?
iLogic embeds rules as objects directly into part, assembly, and drawing documents. The rules determine and drive the design parameter and attribute values. By controlling these values, you can define the behavior of model attributes, features, and components. Knowledge is saved and stored directly in the documents, like the way in which geometric design elements are stored.
Parameters in rules
You can use standard Inventor parameter names in rules as Visual Basic variables. When you run the rule, the value of the parameter is assigned to the variable. For a numeric parameter, the value is a real number expressed in the document units specified in Tools Document Settings. Text parameters become String variables in the rule. True/false parameters become Boolean variables.
Parameters can appear in two different ways:
When a rule starts running, iLogic reads the values for these parameters from the model into the rule. When the rule has finished running, iLogic saves the changed values back to the model.
To save the values before the rule has finished running, use the
RuleParametersOutput
function. This function is useful if you want to change the model immediately.Parameter
function. For example:When you assign values using this method, the values are saved to the model immediately, before the next line in the rule runs. When you read a value using the Parameter function, it is immediately read from the Inventor model.
Change default entity names in rules
Before you use one of the following Autodesk Inventor entity names in a rule, change its name if it was assigned a default name when it was created:
Use a meaningful name. The name change makes it easier for yourself and others using the model to see what the rule is doing.
Another reason to change default names is to make them more portable. In localized versions of Autodesk Inventor, default item names are translated to the language of that version. The names are automatically changed in the model browser, but not in an iLogic rule. Therefore, the rule may be unable to find items using the default name. If someone else uses the model in a different language version of Inventor, the default names must be changed. This recommendation applies only to items that are called out by name in the rule. For example, the Measure functions can use work feature names. If you have work features, but you do not use them in Measure functions, then you do not have to rename them.One exception to this recommendation is embedded Excel spreadsheets. Do not rename them.
On non-English versions of Autodesk Inventor, use an English name in the rule. For example:
"3rd Party:Embedding 1"
Note: Some entities such as iMates allow you to specify a name when you create them.
When does a rule run?
Rules usually run automatically in response to changes in parameter values, if the parameters are used directly as variables in the rule. You can also trigger rules by events.
A rule runs immediately after you edit it, unless it is suppressed. When rules run, changes in the parameters do not update the model until after the rule runs. If necessary, you can force parameter changes to take effect immediately using either:
Parameter
functionRuleParametersOutput()
function in the ruleRule order
iLogicVb.RunRule("ruleName")
function.How is a rule processed?
When you create or edit a rule, the rule text is converted into valid VB.NET code and then compiled. Some Inventor parameter names are not valid as VB.NET variable names. To allow for the full range of Inventor parameter names, iLogic internally substitutes new variable names for the Inventor parameters.
Keywords used as parameter names
Although you can use Visual Basic keywords as parameter names, it is not recommended for new documents. However, when you add rules to existing documents, you usually do not have to change any parameter names. If a name is the same as a keyword, and you require that keyword in the rule, you can:
Class
if the Inventor parameter is namedclass
. Inventor parameter names are case-sensitive, but VB is not. In our example,class
is replaced with another name before VB sees the code, butClass
remains intact.Internal conversion of rules
Rule text is converted internally to valid VB.NET code, which is not visible and is stored with the rule. This type of storage eliminates the need for iLogic to generate the code whenever the rule is run. The code only changes when you edit the rule.
The VB compiler error messages actually refer to this intermediate code. In rare cases with some complex code, you can receive an error that does not correspond to the rule code. When this situation occurs, simplify your code. If necessary, verify that you have a
Sub Main()
andEnd Sub
statement.When iLogic converts the rule to valid VB.NET code for compilation, it replaces units with conversion factors.
Units in rules
You can use Inventor units in rules in the same manner as they are used in standard Inventor parameter equations.
Example - document units
In this example, a part file uses document units of inches:
d0 = 4.5 in
d18 = 7.2 in
sum = d0 + d18
MessageBox.Show(sum,"Document Units - Inches")
Unless otherwise specified, iLogic assumes the units of the numbers are defined as the document units. In this case, the sum equals
11.7
. A message box displays the value and indicates that the document units are expressed in inches.To assign a value of
7.2 mm
to the parameterd18
, instead of7.2 in
, specify thatd18
is expressed in millimeters:d0 = 4.5 in
d18 = 7.2 mm
sum = d0 + d18
MessageBox.Show(sum,"Document Units - Inches")
In this case, the sum equals
4.78346456692913
. A message box displays the value and indicates that the Document Units are expressed in inchesTo perform the previous calculation for a part file with document units expressed in millimeters, include the inch unit for any parameter expressed in inches (such as
d0 = 4.5 in
).Finally, if you omit the unit of length from a statement, iLogic uses the Document Units value.
Example - mixed document units
In this example, a part file uses document units of inches. In the iLogic Parameter Editor:
x
is set to1 in
y
is set to12.5 mm
A rule has been written as:
result = x + y
MessageBox.Show(result,"Mixed Units Defined in Parameter Editor")
When performing the addition, iLogic converts the value of parameter
y
from12.5 mm
to.4921
inches. A message box displays the value of1.49212598425197
and indicates that mixed units are defined in the Parameter Editor.Conversely, if the document units are defined as millimeters, then iLogic converts the value of parameter
x
from1 in
to25.4 mm
. A message box displays the value of37.9
and indicates that mixed units are defined in the Parameter Editor.Unit names
The unit specification is applied before any other mathematical operation is performed and has precedence over all other operators. Unit names:
The following are examples of valid use of unit names:
12 in
144 in
144 in^2
125.2 mm
60 mi/hr
You can use compound unit specifications in rules. Compound unit specifications follow the Inventor format. To incorporate a complicated unit specification into a rule, create an equation for it in the Parameters dialog box. Then, use Capture Current State on that parameter in the Rule Editor.
External rules
External rules can be used as iLogic rules, or as small programs that are run manually (like VBA macros). They are stored in text or VB files, outside of any Inventor document (part, assembly, or drawing). You can run them manually or from other rules.
When you view the list of external rules in the Rule Browser, the list is the same for whatever document you have open. It is not tied to each individual document.
You can also run external rules as event-driven rules.
What is the Rule Browser?
The Rule Browser shows all iLogic rules for the current document. This view is also available in the standard Inventor browser, but not when you are editing a component in an assembly.
In the Rule Browser, rules are arranged under the two tabs:
A context menu is available for items in the Rule Browser. The commands included in the menu vary depending on the item selected:
Once the Rule Browser is shown, it remains displayed until closed manually. This setting is retained across sessions.
The visibility of the Rule Browser can be toggled on and off by activating or deactivating the Rule Browser check box. This check box can be found in the User Interface drop-down list located on the Windows panel of the View tab.
Additionally, you can dock the Rule Browser on either side of the Autodesk Inventor window.
Wizards
iLogic provides several wizards you can use to create sophisticated rules for certain tasks. These wizards are available on the Wizards tab of the Edit Rule dialog box:
Work with rules
Create a rule
Before you create a rule, first create any parameters that you want to include in the rule.
In an assembly document, select the Model tab to display the assembly components, or select the File Tree tab to display the subassembly and part files. Expand a part file item to display the parameters and features within that file.
Note: When you click a non-parameter item in the Model tree, its name is listed under the Names tab (next to the Parameters tab). You can double-click an item in this list to insert the name into the rule. Many of the Drawing snippets require a view name. Double-clicking on the appropriate item in the list can provide you with a view name from the current drawing for inclusion into the rule.
For example, capture the current state of a parameter to insert the parameter and its current value, such as
d0 = 4.0 in
.Parameters are always captured with their units, but units are not required when you write new equations. You can right-click on a parameter group to capture the state of all parameters of a specific type. You can also right-click on a feature or component to capture its current
IsActive
state (suppressed or unsuppressed).Edit a rule
Rename a rule
To rename a rule, click twice (two discrete clicks) on the name in the Rule Browser to enable editing, and then change the existing name.
Insert rule syntax
Use the drop-down lists on the toolbar above the rule text area to insert syntax strings into a rule, instead of entering the syntax manually. Once inserted, you can customize the strings as necessary. Choices are available for:
To insert a string:
Insert a code snippet
Use the choices in the Snippets area of the Edit Rule dialog box to insert code snippets in a rule. You can then modify the inserted snippet as necessary.
Work with collapsible code regions
Create collapsible code regions and comments
Define collapsible code regions and comments to reduce clutter in the rule text area.
Code region
Comment
Manipulate outlining of collapsible code regions and comments
Use the following context menu choices in the rule text area to work with collapsible code regions and comments:
Prevent a rule from running automatically
Note: You can run a suppressed rule manually. You can also suppress a rule in the Rule Browser.
Run a rule without displaying Inventor dialog boxes
Run dependent rules immediately when referenced parameters change value
Note: By running dependent rules immediately, you can include explicit model updates after you set the parameters. At the time of update, the parameters have changed and the dependent rules have run.
Prevent a rule from running when parameters change
Note: If this option is selected, you can only run the rule manually, or with event triggers, or with
iLogicVB.RunRule
.Share code between rules
iLogic does not pre-process the code in a rule identified as Straight VB code; therefore, Inventor parameter names are not recognized in the rule. The rule should contain Classes or Modules that can be called from other rules. Use this option to share code between rules, or as a way to keep code modular. However, note that shared code is more difficult to write.
Choose alternative font style and size for Rule Editor
Select syntax for Capture Current State
This option affects only assemblies, when Capture Current State is used on a parameter in a component within the assembly:
Parameter("PartA:1", "d0") = 5.6 in
) as part of the syntax. This choice is recommended for portability.Alternatively, clear the checkbox to exclude the component names (for example,
PartA.ipt.d0 = 5.6 in
).Note: You can insert a parameter into a rule by clicking its name while creating rules at the assembly level. If you later change the component name to something other than its original filename, using parameter syntax makes it easier to find the component.
Run rules
To run a rule manually, right-click on the rule in the Rule Browser, and select Run Rule from the context menu.
To ensure that the parameters in the rule are synchronized with the model, select Regenerate Rule from the context menu before running the rule.
To run another rule explicitly within a rule, use the
iLogicVb.RunRule
function.Reorder the tree in the Rule Browser
Drag and drop any rule in the Rule Browser to change its order in the tree. Any rule at or below the rule you drop moves down in the tree.
Regenerate a rule
To synchronize the parameters in a specific rule with your model.
This command reconnects the rule to Inventor parameters, recompiles the rule, and then runs it.
Regenerate all rules in the current document
Use this command to synchronize the parameters in all rules with your model.
Alternatively, you can use the Regenerate All Rules command on the ribbon. Select Manage tab iLogic panel
Regenerate All Rules
.
Delete a rule
Delete all rules in a document
Use this command when you have finished configuring the model and want to save or export a sample configuration. All rules are removed.
Note: If you run this command in an assembly, all rules in the subassemblies and parts are also deleted.
Work with external rules
Create an external rule
The Rule Browser includes a tab for external rules. From this tab, you can load an external rule or create a new one.
To make an existing rule external:
Save external rules as .iLogicVb files (this extension is recommended), or as .vb or .txt files. Save external rules to a folder that can be found when running them from another rule. iLogic looks for external rule files in the following folders, in the order listed:
The Rule Browser provides a context menu for the top node (External Rules in Files) and for each rule in the tree.
Create an external rule icon
You can include an icon for an external rule that displays in the Rule Browser. Create a .bmp file (size 16 X 16 is recommended) with the same name as the rule file, in the same folder.
For example, if your external rule is named Custom Checks Rule.iLogicVb, name the icon file as Custom Checks Rule.bmp.
After you create the bitmap image, restart Autodesk Inventor to display the icon. You cannot edit the icon while Autodesk Inventor is running.
Edit an external rule
To edit an external rule, open the Edit Rule dialog box, and double-click the rule in the tree to edit it.
You can also use an external program such as Notepad to edit a rule. After you save it, use Reload From File from the context menu in the tree to bring the changes into Inventor.
External rules and parameters
External rules do not run automatically in response to particular parameter changes. Unlike internal rules, parameter names are not automatically available as rule variables.
For example, the following rule statement does not work:
d1 = d0 * 0.5
Instead, use the following:
Parameter("d1") = Parameter("d0") * 0.5
This statement works on any part or assembly with parameters named d0 and d1. It can be used to operate on related parts that share something in common.
Use wizards to create special rules
Control a dialog box based on model parameters
The Open DLL File dialog box displays the contents of the iLogicAdd folder, which contains previously created DLL files.
Capture the current display view
Generate a message box
Use this wizard to create a rule that generates a message box.
For example, if you have chosen YesNoCancel as your button scheme, and your default button is No, select 2.
Display diagnostic information when parameter values fall outside specified range
Use a rule to display a dialog box
You can use a rule to display a dialog box for user input. The dialog box can be contained in the rule; however, it is easier and more flexible to include it in an external DLL (class library).
Use Visual Studio version 2005 or 2008. Visual Basic Express is available as a free download from Microsoft.
Get started with Visual Basic 2008
A new window displays, with the Solution Explorer in the top right corner.
A graphical view of your dialog box displays in the Windows Form Designer. You can use this interface to add controls to your dialog box.
The ToolBox on the left side of the screen lists the controls that you can add to the dialog box. For this project, you add a TextBox to show the value of an Inventor parameter and allow you to edit that parameter.
Public Class Dialog1
Public d0 As Double
OK_Button_Click
, in front of theMe.DialogResult...
statement, and press Enter to add a new line.d0 = TextBox1.Text
Your code looks like -
You are almost ready to build the project. When you perform the build operation, Visual Basic creates a DLL file. You can specify where Visual Basic creates this file by modifying the output path for the build operation.
Program Files\Autodesk\Inventor 2011\Bin\iLogicAdd\
Form Rule
that consists of the following -.AddReference "ClassLibrary1"
Sub Main()
dim dlg as new ClassLibrary1.Dialog1
dlg.d0 = d0
i = dlg.ShowDialog()
if (i = vbOK)then
d0 = dlg.d0
end if
End Sub
Note: You can also use Create Rule for a Dialog on the Wizards tab in the Edit Rule dialog box to create this code.
This rule sends the current value of the Inventor parameter
d0
to the dialog box, then shows the dialog box. The person using the dialog box can edit the value in the dialog. When this person clicks OK, the value from the dialog is assigned back to the parameter.Create a dialog box
To create a dialog box that enables you to display and modify some parameter values:
A graphical view of your dialog box displays in the Windows Form Designer. You can use this interface to add controls to your dialog box.
The code editor displays in a new subroutine named
Dialog1_Load
.TextBox1.Text = d0
Public Class Dialog1
Public d0 As Double
OK_Button_Click
, add this line -d0 = TextBox1.Text
Your code now looks like -
Imports System.Windows.Forms
Public Class Dialog1
Public d0 As Double
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click d0 = TextBox1.Text Me.DialogResult = System.Windows.Forms.DialogResult.OK Me.Close()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click Me.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.Close()
End Sub
Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Text = d0
End Sub
End Class
A DLL is created when the project is built. You can either:
The following sample rule uses the dialog box:
AddReference "ClassLibrary1"
Sub Main()
dim dlg as new ClassLibrary1.Dialog1
dlg.d0 = d0
i = dlg.ShowDialog()
if (i = vbOK) then
d0= dlg.d0
end if
End Sub
This rule sends the current value of the Inventor parameter d0 to the dialog box, and then shows the dialog box. The person using the dialog box can edit the value in the dialog box. If that person clicks OK, the value from the dialog box is assigned back to the parameter.
You can generate similar code for a dialog box automatically. On the Edit Rule dialog box, select the Wizards tab, and then select Create Rule for a Dialog.
Add Rule
Use the Edit Rule dialog box to define a new rule or change an existing rule.
Access: | Ribbon: Manage tab ![]() ![]() ![]() |
Snippets | Lists reusable pieces of code. Double-click a snippet to insert it in the rule. | |
System tab | Lists iLogic snippets provided with the installation.
| |
Custom tab | Lists snippets you create or copy from the System tab.
| |
Model | Presents a model view of a part or drawing, or a component view of an assembly. | |
Parameters | Lists the parameters based on the Parameter Type selected in the tree. | |
Names | Cumulatively lists items from the tree as they are selected. Click Clear List to remove the items from the list and start over. | |
File Tree | Presents a file view of an assembly, instead of the component view shown on the Parameters tab. Use this view to find objects quickly under part and assembly names. | |
Files | Lists the names of the part files in an assembly. Use this view to copy file names for use in a rule. | |
Options | Determines the behavior of the rule, along with certain characteristics of the rule appearance. | |
Behavior of this Rule | Suppressed - Prevents a rule from running automatically. Silent Operation - Runs a rule without displaying the Inventor dialog boxes by automatically selecting the default responses. Fire dependent rules immediately - For other rules that run because they refer to a parameter that this rule changes, run them as soon as the parameter value changes. Avoid waiting until this rule finishes. Don't run automatically - Prevents the rule from running in response to parameter changes. Straight VB code - Identifies this rule as code that can be shared between rules. | |
Rule Editor | Select Font - Selects the font for the rule text. | |
Capture Syntax | Use component names - Convert parameters into Parameter syntax automatically, using the component name. | |
Search and Replace | Finds a string in the current rule, and optionally replaces the string with another string. | |
Find what | Enter a string to search for. | |
Replace with | Enter a string to replace the original string with. | |
Match Case | Select to distinguish between lower case and upper case characters. | |
Match whole word only | Select to find only the exact character string. Do not match if it is found within a longer word. | |
Find | Find an occurrence of the string. | |
Find Next | Find the next occurrence of the string. | |
Find Previous | Return to the previous occurrence of the string. | |
Replace | Substitute the found string with the specified replacement string. | |
Replace All in This Rule | Substitute all occurrences of the found string in the current rule with the specified replacement string. | |
Replace All in All Rules | Substitute all occurrences of the found string in all rules with the specified replacement string. | |
Wizards | Lets you choose from several task-based wizards. | |
Create Rule for a Dialog | Creates a rule for a custom dialog box based on model parameters. | |
Message Box | Creates a rule that generates a message box. | |
Capture Current View | Creates a rule that captures the current screen view. | |
Parameter Limits | Creates a rule that displays diagnostic messages when parameter values are outside of a specified range. | |
Editing Toolbar | Includes tools for working with iLogic rules.. | |
| Prints the text of the current rule. | |
Page Setup | Determines the page setup to print rule text. | |
Cut | Removes selected rule text and places a copy on the clipboard. | |
Copy | Places a copy of the selected rule text on the clipboard. | |
Paste | Pastes a copy of the rule text from the clipboard at the cursor location. | |
Redo | Reapplies the effects of the last editing function. | |
Undo | Removes the effects of the last editing function. | |
Increase Indent | Adds space to the left of the current line. | |
Decrease Indent | Removes space to the left of the current line. | |
Comment out the selected lines | Changes the selected line of rule text to a comment. | |
Uncomment the selected lines | Removes the comment tag from a commented line. | |
If...Then...End If | Displays a drop-down list used to insert a selected conditional statement. | |
Keywords | Displays a drop-down list used to insert a selected keyword. | |
Operators | Displays a drop-down list used to insert a selected operator. | |
Help | Displays the Help for this dialog box. | |
OK | ||
Cancel |
Rule for External DLL
Use this dialog box to create a rule for a custom dialog box based on the parameters in the model.
Access: | Ribbon: Manage tab ![]() ![]() ![]() |
Browse for DLL | Displays the Open DLL dialog box, which allows you to select a DLL to populate the dialog box with the class and variable names. |
DLL Name | Displays the name of the selected DLL file. |
Dialog or Class Names | Displays the dialog box or class names for a selected DLL file. You can select or deselect the dialog box or class names. Deselect to omit from the rule. |
Variable Names | Displays the variable names for a selected DLL file. You can select or deselect the variable names. Deselect to omit from the rule. |
OK | Creates the rule. |
Cancel | Cancels the operation. |
Capture View
Use this dialog box to create a rule that captures the current display view based on a selected size option.
Access: | Ribbon: Manage tab ![]() ![]() ![]() |
Save and Restore current view extents | Restores the view to the orientation and zoom settings from when the rule captured the view. |
Don't change view size on restore | Moves the model to the center of the display and sets the orientation, but does not change the zoom. |
Fit to model extents on restore | Moves the model to the middle of the display and fills the display (same as Zoom All). |
OK | Adds text to the rule. |
Cancel | Cancels the operation. |
Message Box Wizard
Use this dialog box to create a rule that generates a message box.
Access: | Ribbon: Manage tab ![]() ![]() ![]() |
Title | The title that appears at the top of the message box. |
Buttons | The combination of buttons that appear on the message box. |
Default Button | The number of the default button, based on its relative placement in the Buttons selection. |
Icon | The status icon to display in the message box. |
Prompt Text | The text to display in the message box, surrounded by quotes. |
Preview | Displays a preview of the message box, based on your current selections. |
OK | Adds text to the rule. |
Cancel | Cancels the operation. |
iLogic Limits Wizard
Use this dialog box to create a rule that displays diagnostic messages when parameter values are outside of a specified range.
Access: | Ribbon: Manage tab ![]() ![]() ![]() |
Name | The name of the parameter. |
Max. Value | The maximum value allowed before triggering the message. |
Min. Value | The minimum value allowed before triggering the message. |
Max. Violation | The text to display when the parameter value exceeds the maximum value allowed. |
Title (for Max. Violation) | The title to display on the message box. |
Min. Violation | The text to display when the parameter value falls below the minimum value allowed. |
Title (for Min. Violation) | The title to display on the message box. |
Default | Resets the messages to their default text. |
OK | Creates the rule. |
Cancel | Cancels the operation. |
Rule Browser
Access: | Ribbon: Manage tab ![]() ![]() ![]() |
Rule context menu
Right-click on a rule or parameter in the Rule Browser to display the context menu for that item.
For a rule, the context menu commands include:
Run Rule
Runs the rule. Use this command to test the rule, or to ensure that the model is up-to-date. You can also use this command to run rules that do not run automatically.
Edit Rule
Opens the Edit Rule dialog box for the selected rule.
You can also double-click the rule in the browser to edit it.
Regenerate Rule
Regenerates the selected rule. This command reconnects the rule to the Inventor parameters that it references, then recompiles and runs the rule.
Suppress Rule (or Unsuppress Rule)
A suppressed rule does not run automatically. You can run a suppressed rule manually.
Delete
Deletes the rule. You can also delete a rule using the Delete key. To delete all rules, use the Delete All Rules command on the ribbon.
Top-level context menu
For the document at the root of the Rule Browser tree, the context menu commands include:
Add Rule
This command performs the same action as the Add Rule command on the ribbon.
Run All Rules
Runs all rules in the document. If the document is an assembly, this command also runs all rules in all subassemblies and parts. You can use this command to verify that the model is up-to-date. Execution stops when the first error is encountered. If you receive an error, edit the offending rule and repeat this command.
You can also use this command if you changed something in the model that is driven by a rule. Rules only run in response to parameter changes. Anything else that a rule can set, such as feature or component suppression, iPart configuration, manual changes, or iProperties, can be changed independently.
Rules are run in the order in which they appear in the browser, unless specified otherwise by inclusion of the
iLogicVb.RunRule("ruleName")
function.Run All Rules ensures that the model is in the state specified by the rules.
Regenerate All Rules
Reconnects the rules to Inventor parameters, recompiles the rules, and then runs them. Like Run All Rules, this command processes rules in subassemblies and parts.
In Document tab
Lists the rules that are defined within the current Inventor document.
External tab
Lists the rules that are defined in external files.
Rule Browser behavior
Advanced rule creation techniques
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. Visual Basic 2005 or 2008 can be used to create external DLL files that can be called 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.
The following statements can be used 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
andImports
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 directory.
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 directory. By default, these DLLs are located under the Autodesk Inventor installation directory, in an iLogicAdd subdirectory, such as:
C:\Program Files\Autodesk\Inventor 2011\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 directory (for example, C:\Program Files\Autodesk\Inventor 2011\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
orGetActiveObject
. It is not necessary to useAddReference
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 directory of the current Autodesk Inventor document, and in other directories, 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:
Imports System
Imports System.Math
Imports System.Collections
Imports Microsoft.VisualBasic
Imports Autodesk.iLogic.Interfaces
Imports Autodesk.iLogic.Runtime
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 theClass...
andEnd Class
statements are not visible, and the statements beforeSub 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 asPrivate 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 callsSub Main
(within it) to run your rule.To include a Module or 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, useAddVbFile
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 directory). 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. UseThisDoc.Document
to access the document that contains the current rule.. You can create and modify features. UseParameter.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 directory, under the Inventor bin directory (usually in C:\Program Files\Autodesk\Inventor 2011\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
.