Pass Variable between iLogic Subs

Pass Variable between iLogic Subs

e_frissell
Advocate Advocate
968 Views
3 Replies
Message 1 of 4

Pass Variable between iLogic Subs

e_frissell
Advocate
Advocate

Hey guys, got a question for passing a variable between iLogic subs

 

I have an iLogic rule to insert parts that I'd like to be able to re-use based on whether I want to insert 1 part manually via an input box or automatically via an Excel list.  My script to insert parts works great with the input box but, to run it with excel, it now needs to be a separate rule that just accepts the part number as a string and runs on that part number

 

What's confusing is when I created a copy of the rule and added the argument as an input to the sub I get an error.  It clearly has an End Sub so I'm not sure what the correct way to pass variables between iLogic rules are as I'm pretty sure the way I did it is convention for VB.net

 

Any suggestions?

 

e_frissell_0-1714142189695.png

 

0 Likes
Accepted solutions (1)
969 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

You can use two approaces

  1. Call another rule with arguments
  2. Create direct VB.NET code file and include them to the main rule

 

MainRule (external or internal)

 

AddVbFile "DirectVbCode"

'Run external rule with arguments
Dim map As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
map.Add("MessageString", "This is a message string 1")
iLogicVb.RunRule("ShowMessageRule", map)


'Use external file
Dim messanger As New Messenger(ThisApplication)
messanger.ShowMessage("This is a message string 2")

 

 

ShowMessageRule (external is recommended)

 

Dim message As String = RuleArguments("MessageString")
Dim title As String = ThisApplication.Caption
MsgBox(message, Title :=title)

 

 

 

External file DirectVbCode.vb

Must be external rule and placed in default iLogic external rules directory

 

Public Class Messenger
	Private inventor  As Inventor.Application 
	Public Sub New(inventorApp As Inventor.Application)
		inventor = inventorApp
	End Sub
	
	Public Sub ShowMessage(message As String)
		Dim title As String = inventor.Caption
		MsgBox(message, Title :=title)
	End Sub
End Class

 

 

Message 3 of 4

WCrihfield
Mentor
Mentor

Hi @e_frissell.  That issue you are seeing is just an iLogic thing.  The 'Sub Main' block of code can not have any 'input parameters', but other Subs / Functions and such can have them.  It is similar for VBA macros that you want to use for a button in your Inventor ribbon.  Those are all supposed to be a Sub (does not return anything), and they are not supposed to have any input parameters.  Others that you are not going to use that way can have them, just not the ones you will be creating a ribbon button for should not.

 

There are various ways to pass something into the Sub Main block of code, but just not as a true input parameter.  One way is to use the iLogic rule object called RuleArguments.  That is the primary tool used for sending & receiving data between iLogic rules (or for an iLogic rule to receive data from a VBA macro that ran an iLogic rule with arguments).  There is also the iLogic SharedVariable system, which can be used to store data & objects within Inventor 'session memory' temporarily.  One rule can create a SharedVariable and assign a value to it, then another rule can check for the existence of it, then retrieve its value, and so on.  There could also be variables that were declared outside of the Sub Main block of code (such as between it, and other Subs / Functions, or at a higher level, such as in a Class block of code), where one of the other routines sets its value, but the Sub Main block of code can access its value.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 4

e_frissell
Advocate
Advocate

Thanks both of you, that is extremely helpful.  It seems like creating the name value map will be the easiest way to go (for now, at least until I break something)  as my intent is to use this insert script similar to a function that I can either call a single time from a sub with an input box that is ran from a button, or multiple times from an Excel file.  I don't know if there will be any hang ups trying this, but for now it seems like the simplest way to move forward.  Thanks for the help!

 

0 Likes