Wednesday, May 25, 2016

Using VB based forms in iLogic Trick


I was recently building a form in VB.NET using Visual Studio Express and was faced with the task of wiring it up to Inventor.

In case you've never done it before, you need to use an iLogic rule to pass current parameters to the form class then back again.  This is typically done as follows:

' add a reference to the DLL hosting the form
AddReference "myAutomation"

' make the connection to the form

Using dlg As New myAutomation.MainForm


' shows the dialog
Dim i As Integer = dlg.ShowDialog()

' pass parameter info to matching variables in the form code

dlg.PreOrder_Initials = iProperties.Value("Custom", "PreOrder_Initials")
dlg.Shop_Order_Number = iProperties.Value("Custom", "Shop_Order_Number")

dlg.Customer_Stock_Number = iProperties.Value("Custom", "Customer_Stock_Number")

When the form closes you need to reverse the process and pass the data from the form back to Inventor, so those three sample lines become:

iProperties.Value("Custom", "PreOrder_Initials") = dlg.PreOrder_Initials
iProperties.Value("Custom", "Shop_Order_Number") = dlg.Shop_Order_Number

iProperties.Value("Custom", "Customer_Stock_Number") = dlg.Customer_Stock_Number

Seems simple right?  And, it is, unless you've got a complex form and 300+ parameters to hand back and forth.

Those that follow this blog know that I"m a huge fan of Notepad++ for creating/editing external iLogic rules.

Using a free add-in for Notepad++, called FingerText to create some shortcuts that allowed me to quickly create my initial entries for pushing values to the form.  But, now I was faced with essentially mirroring the string about the "=" sign.  To me that represented a ton of selecting, copying, replacing, etc. and I could tell that somewhere along the line I would get distracted or fat finger an entry then have to track down the bug.

My saving grace was learning that I could use Notepad++ to do the switching for me.  Here's the process I used:

  1. Copy all the dlg.xxx lines and pasted them into the same document after the .ShowDialog() line.  These would become the code that would pass the form values back to Inventor.
  2. I used the Notepad++ Replace function to find and mirror the text for me using a regular expression.

In the Find what: cell, enter:   ^(.*)=(.*)$
In the Replace with: cell, enter: \2 =\1
Set the search mode to Regular expression

After that it's a simple matter of Find Next and Replace.  I caution not to use Replace All as it will also mirror your originals.

dlg.UID = iProperties.Value("Custom", "UID") becomes
   iProperties.Value("Custom", "UID") = dlg.UID at the click of a button.  It doesn't get much simpler than that!

I hope that you find this little trick useful.

Happy Coding,


No comments:

Post a Comment