Sunteți pe pagina 1din 16

Autodesk WikiHelp Autodesk Inventor 2011 iLogic and Visual Basic

Introduction

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 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++).

Preparation
Please install Visual Basic 2008 Express Edition or Visual Basic 2010 Express. Microsoft offers actually a free download of Visual Basic 2010 Express Edition. ( http://www.microsoft.com/express/download/#2010-Visual-Basic ). Note: You can use Visual Basic Express 2010 together with Autodesk Inventor 2011. If you have any problems, activate (in Visual Basic) Properties > Compile > Advanced Compile Options > Target framework = .NET Framework 3.5.

Autodesk Inventor 2011 iLogic and Visual Basic

Autodesk WikiHelp

The following examples were created with Visual Basic 2008 Express Edition. The dialogs can differ slightly from the functionality in Visual Basic 2010.

You find more information for the first steps with and questions about Visual Basic at Microsoft and other web pages.

Settings in Visual Basic


Start Visual Basic after the installation Click Tools/Options In the Options dialog box, for Projects and Solutions>General, change the path to the iLogicBin directory (e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin)

Autodesk Inventor 2011 iLogic and Visual Basic

Autodesk WikiHelp
Settings in Autodesk Inventor
VB creates DLL files (Dynamic Link Library). iLogic uses these DLL files as external rules. Configure location for external rules and DLL files: - On the ribbon, click Tools tab > Options panel > iLogic Configuration Lists all directories in which iLogic searches for external files containing rules Add the iLogic Addin DLLs Directory (which should be similar to the predefined directory in the Visual Basic settings)

Note: Visual Basic will create for new projects a subdirectory with the name of the current project in the defined project directory, e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin\sample_project\ . So there is no direct access from Autodesk Inventor to the created DLLs in Visual Basic. We will see later the advantages of this configuration.

General Start of a new VB-Project


PLEASE READ THE FOLLOWING INFORMATION CAREFULLY! The following steps will be needed for all examples! Start of a new VB project Start Visual Basic Click File>New Project Click Class Library Define a name e.g. iLogic_VB1 Click OK

Test Storage directory (only one time needed) In the Solution Explorer, right-click on the Project name (e.g. Ilogic_VB1) Click Properties

Note: The main window shows a tab with the options of the current project. The tab Compile shows the Build output path for DLLs. There you will find later the DLL (e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin\iLogic_VB1\iLogic_VB1\bin\release\iLogic_VB1.DLL), which you have to copy manual to the iLogic DLL directory. But why? When you work on a new project, you will test functions in VB and functions in iLogic. Autodesk Inventor 2011 iLogic and Visual Basic 3

Autodesk WikiHelp
After starting a DLL in iLogic, the file access is read-only. So when do any changes in VB and you like to recompile the DLL, it wouldn`t work. Storing the DLL from VB in a separate directory makes your life easier. Just use the following steps: - Compile a new DLL in VB - Close Inventor - Copy the new DLL from the VB DLL directory (e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin\iLogic_VB1\iLogic_VB1\bin\release\iLogic_VB1.DLL) to the Inventor iLogic DLL directory e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin\sample_project\) - Restart Inventor - Start your iLogic program Create dialog boxes in Visual Basic In the Solution Explorer, right-click on the Project name (e.g. Ilogic_VB1) Click Add> New Item Click Dialog If needed, change the name Click Add

Note: The name of the dialog (e.g. Dialog1) will be needed for the program code. Save Project Click File > Save All Note: Create regular backups of these files (If you uninstall Inventor, these files will not be deleted). Create a DLL in Visual Basic and prepare it for the use in iLogic Visual Basic: Click Build > Build (e.g. iLogic_VB1) Open Windows Explorer Copy DLL (e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin\Ilogic_VB1\Ilogic_VB1\obj\Release\iLogic_VB1) to the iLogic DLL directory (e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin\iLogic_VB1.dll)

Visual Basic User Interface


VB offers a lot of different functions in different windows. Sometimes this can confuse the user. - Click Window > Reset Window Layout to get the default user interface again

program code - Important


VB and iLogic program code is case sensitive. Consider this for your program. Autodesk Inventor 2011 iLogic and Visual Basic 4

Autodesk WikiHelp
Don`t use - as a separator in file names. This could cause problems. Use instead _.

Additional settings in VB
The following settings are important for a deeper integration of VB and iLogic. Note: 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 (see below). 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. In the Solution Explorer, right-click on the Project name (e.g. Ilogic_VB1) Click Properties Click tab References Click Add > References

Click tab Browse Browse to the iLogicBin directory (e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin) Click Autodesk.iLogic.Interfaces.DLL Click OK You find the new reference in the list

Example 1 plate with a VB dialog for the length


Goal: Creation of a plate in Inventor Creation of a VB dialog for the length Data transfer parameter length from Inventor to VB Update of plate length in Inventor after value changes in VB dialog

How to: - Create a new empty VB dialog (see chapter Create dialog boxes in Visual Basic) - Click Toolbox >Common Controls>TextBox Position the textbox as shown in the following picture

Autodesk Inventor 2011 iLogic and Visual Basic

Autodesk WikiHelp
The textbox should show later the length value of the plate. A label should show the name of the textbox. Click Toolbox >Common Controls>Label Position the label as shown in the following picture VB shows on the right side the property window specific properties of the selected element. Make sure that the previous inserted label is still selected Change the property parameter text from Label1 to Length The dialog updates automatically

Next step: data transfer program code Double click into the grey area of the dialog

The tab Dialog1.vb shows the current program code. The tabs allow to switch between the different options.

Note: If you double click on any other element in the dialog, you get an entry in the program code (e.g. the label):
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub

This is just an empty shell without any function. You can keep it or erase it without any difference in the program code. To prevent empty shells when you switch between the dialog design and the program code, click on the tabs. Use double click in the dialog only for the first time you want to see the program code. You have to define now the variable for the length. We name it vblength. The data type is double (8 bytes floating point).
-

After the line


insert the new line

Public Class Dialog1 Public vblength As Double Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As Textbox1.Text = vblength

After the line


insert the new line

System.EventArgs) Handles MyBase.Load

Autodesk Inventor 2011 iLogic and Visual Basic

Autodesk WikiHelp
If you change in the running program the value of the length, the model in Inventor should be updated automatically. After the line
insert the new line Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As vblength = Textbox1.Text

System.EventArgs) Handles OK_Button.Click

Note: the complete program code shows all new program code entries fat with grey background color Complete program code:
Imports System.Windows.Forms Public Class Dialog1 Public vblength As Double Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click vblength = 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 = vblength End Sub

End Class

Note: When you type a new code, VB supports you with an intelligent selection of possible functions. You find more about this in the VB help. Autodesk Inventor 2011 iLogic and Visual Basic 7

Autodesk WikiHelp
Save project (see chapter save project) Create the program DLL and copy it ( see chapter Create a DLL in Visual Basic and prepare it for the use in iLogic) Start Autodesk Inventor Create a new part Plate.ipt Create a new sketch with a rectangle and a parameter inv_length (e.g. 100) Extrude(e.g. 10 mm) the rectangle Parameter list (your values could differ) Click Manage > iLogic > Add Rule Name of the rule: Dialog The rule editor starts Enter the following code:

AddReference "iLogic_VB1" Sub Main() Dim dlg as New iLogic_VB1.Dialog1 dlg.vblength = inv_length i = dlg.ShowDialog() If (i = vbOK) Then inv_length = dlg.vblength End If End Sub

Program code note: The program communicates with the VB program iLogic-VB1. The dialog length value will be checked constantly. As soon as i = vbOK (vbOK is a historical VB variable name), Inventor updates the plate with the current dialog length value.

Alternative: You can use instead of also -

If (i = vbOK) Then If i <> vbOK Then Return

Click OK The new dialog starts in Inventor:

Change the value to e.g. 200. Click OK 8

Autodesk Inventor 2011 iLogic and Visual Basic

Autodesk WikiHelp
Click Manage > Update > Update Save the results

Example 2 Automatic rule generation for a rectangle


Goal: The first example showed the communication between Inventor iLogic and VB. Now you will learn how to use the iLogic wizzards to assign program code. d0 and d1 (= d0/2) should drive a rectangle sketch. Start a new project in VB (name: Plate2) Create a dialog with a textbox

Note: The parameter names in this example are the same in iLogic and VB. Note: Make sure that you use for d0 not the character O but the figure zero. Add the marked program code:
Imports System.Windows.Forms Public Class Dialog1 Public d0 As Double Public d1 As Double Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click d0 = TextBox1.Text d1 = d0 / 2 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

Autodesk Inventor 2011 iLogic and Visual Basic

Autodesk WikiHelp
Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Text = d0 End Sub End Class

Save project (see chapter save project) Create the program DLL and copy it ( see chapter Create a DLL in Visual Basic and prepare it for the use in iLogic) Start Autodesk Inventor Create a new part Plate2.ipt Create a new sketch with a rectangle with dimensions

Finish sketch Click Manage > iLogic > Add Rule Name of the rule: Dialog The rule editor starts Click tab Wizzards >Create rule for a dialog Search the directory where you copied Plate2.DLL (e.g. C:\Program Files\Autodesk\Inventor 2011\Bin\iLogicBin) select Plate2.DLL Click Open

Note: The wizard works only with empty rules The dialog Rule for external DLL starts Click OK to translate the parameters from the VB DLL to the iLogic program code

The following program code will be created in iLogic automatically:


AddReference "Plate2.dll" localTrigger = iTrigger0 Using dlg As New Plate2.Dialog1 dlg.d0 = d0 dlg.d1 = d1

Autodesk Inventor 2011 iLogic and Visual Basic

10

Autodesk WikiHelp
Dim i As Integer = dlg.ShowDialog() If i <> vbOK Then Return d0 = dlg.d0 d1 = dlg.d1 End Using iLogicVb.UpdateWhenDone = True

program code note: There are different ways to create program loops. Here we use the if then return function. The program code localTrigger = iTrigger0 allows the user later to start the program with Manage > iLogic >Trigger. The function iLogicVb.UpdateWhenDone = True updates the Inventor model at the end of the iLogic program
automatically.

Click OK Change the value in the dialog e.g. to 40 Click OK. The sketch will be updated automatically Save the part

Example 3 Plate with a table selection for width and automatic rule generation
Goal: Creation of a plate with the following length parameters: 100, 120 and 150 mm Selection of these parameters by a Combobox in VB Start a new VB project (name: Plate3) Create a Dialog Click Toolbox >Common Controls>Combobox Position the textbox as shown in the following picture

Add the marked program code (read the green comments in the program code for more information):
Imports System.Windows.Forms Imports Autodesk.iLogic.Interfaces ' necessary for the cooperation with specific iLogic functions Public Class Dialog1 Public Parameter As IParamDynamic Public MultiValue As IMultiValueParam Public ILogicVB As ILowLevelSupport ' access additional iLogic functions

Autodesk Inventor 2011 iLogic and Visual Basic

11

Autodesk WikiHelp
Public length As Double Public Length_list As ArrayList ' List definition is needed for the listing of the different length values Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each length_val As String In length_list ComboBox1.Items.Add(length_val) Next ComboBox1.Text = length ' writes length values in combobox End Sub Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click Me.DialogResult = System.Windows.Forms.DialogResult.OK Me.Close() End Sub Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged Parameter("length") = ComboBox1.Text ' current combobox value will be returned to parameter length End Sub End Class

Note: In case of of a DLL error message: the link to Autodesk.iLogic.Interfaces.DLL is missing (see chapter Additional settings in VB) Save project (see chapter save project) Create the program DLL and copy it ( see chapter Create a DLL in Visual Basic and prepare it for the use in iLogic) Start Autodesk Inventor Create a new part Plate3.ipt Create a new sketch with a rectangle with dimensions Define the parameter length Finish sketch Click Manage > Parameters > Parameters Activate key for parameter length Make Multi-Value list for length: 100, 120, 150

Autodesk Inventor 2011 iLogic and Visual Basic

12

Autodesk WikiHelp
Click Done Click Manage > iLogic > Add Rule Name of the rule: Dialog The rule editor starts

Enter the following code (or copy it from this document):


AddReference "Plate3.DLL" Parameter.UpdateAfterChange = True MultiValue.UpdateAfterChange = True iLogicVb.UpdateWhenDone = True localTrigger = iTrigger0 Using dlg As New Plate3.Dialog1 dlg.Parameter = Parameter dlg.MultiValue = MultiValue dlg.length_list = MultiValue.List("length") dlg.length = Parameter("length") dlg.ShowDialog() End Using

Click Options > fire dependent rules immediately


'make sure and set "Fire dependent rules immediately" option for this rule.

- Click OK (if you start the program later, you can click Manage > iLogic > iTrigger) You can select now one of the sizes from the combobox. It is also possible to toggle through the values with the key-cursor. The sketch will be updated automatically. - Click OK to use the selected value Save the results

Example 4 A table drives the selection of the next table


You see iLogic offers a lot of possibilties. The last example shows how to use additional constraints. Length parameters come from a table and each length table value drives a group of parameters for the width1. e.g. length 100, width1 50 or 60 Autodesk Inventor 2011 iLogic and Visual Basic 13

Autodesk WikiHelp
llength 120, width1 60 or 70 length 150, width1 70, 80 or 90 It is important that the tables are all the time updated.

Lets enhance Example 3: - Inventor: define the parameter width1 - Activate key for parameter width1 - Make Multi-Value list for width1: 50, 60, 70, 80, 90 Click Manage > iLogic > Add Rule Name of the rule: Values The rule editor starts Enter the following code (or copy it from this document):

'value update control ist aktivated MultiValue.SetValueOptions(True, DefaultIndex := 0) ' Select Case is better than if then for bigger tables. ' Pay attention that the Case name is the same as the name value of the parameter ' e.g. Case 100 for length=100 SelectCaselength Case 100 MultiValue.SetList("width1", 50, 60) Case 102 MultiValue.SetList("width1", 60, 70) Case 150 MultiValue.SetList("width1", 70, 80, 90) EndSelect InventorVb.DocumentUpdate()

- Click OK - Ignore the Error Message Click OK - Click Cancel Note: The last 3 steps are the easiest way in case of problems /unfinished codes to save the current status Enhance the rule Dialog:
AddReference "Plate3.DLL" Parameter.UpdateAfterChange = True

Autodesk Inventor 2011 iLogic and Visual Basic

14

Autodesk WikiHelp
MultiValue.UpdateAfterChange = True iLogicVb.UpdateWhenDone = True localTrigger = iTrigger0 Using dlg As New Plate3.Dialog1 dlg.Parameter = Parameter dlg.MultiValue = MultiValue dlg.length_list = MultiValue.List("length") dlg.length = Parameter("length") dlg.width1_list = MultiValue.List("width1") dlg.width1 = Parameter("width1") dlg.ShowDialog() End Using

Click OK Ignore the Error Message Click OK Click Cancel Save the results Close Inventor (why? see chapter Test Storage directory (only one time needed)) Open the VB project from Example 3 (plate3) Insert an additional combobox

Add the marked program code:


Imports System.Windows.Forms Imports Autodesk.iLogic.Interfaces

Public Class Dialog1 Public Parameter As IParamDynamic Public MultiValue As IMultiValueParam Public ILogicVB As ILowLevelSupport

Public length As Double Public length_list As ArrayList Public width1 As Double Public width1_list As ArrayList Private Sub Dialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each length_val As String In length_list

Autodesk Inventor 2011 iLogic and Visual Basic

15

Autodesk WikiHelp
ComboBox1.Items.Add(length_val) Next ComboBox1.Text = length For Each width1_val As String In width1_list ComboBox2.Items.Add(width1_val) Next ComboBox2.Text = width1 End Sub Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click Me.DialogResult = System.Windows.Forms.DialogResult.OK Me.Close() End Sub Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged Parameter("length") = ComboBox1.Text update_width1_list() ComboBox2.Text = Parameter("width1") End Sub Private Sub ComboBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.TextChanged Parameter("width1") = ComboBox2.Text End Sub Private Sub update_width1_list() width1_list = MultiValue.List("width1") ComboBox2.Items.Clear() For Each width1_val As String In width1_list ComboBox2.Items.Add(width1_val) Next End Sub End Class

Save project (see chapter save project) Create the program DLL and copy it ( see chapter Create a DLL in Visual Basic and prepare it for the use in iLogic) Test your results

Example results

Autodesk Inventor 2011 iLogic and Visual Basic

16

S-ar putea să vă placă și