Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Adding VB External Rule Dependencies to VB External Rules

Anonymous

Adding VB External Rule Dependencies to VB External Rules

Anonymous
Not applicable

I am currently trying to produce a number of modules as iLogic External Rules with Straight VB Code enabled. My hope is that each module will host a series of functions that can be called from a iLogic rule without instantiating a object of a class. Each of these modules may use functions from a separate module as part of a functions code. Right now I can get the iLogic code able to reach one deep, but it runs into a issue trying to access a function from that module to another module. The test code looks like this:

 

Main iLogic Code:

 

AddVbFile "FunctionTools\foo.vb"

Sub Main()
	Dim oDoc = ThisDoc.Document
	foofunc(oDoc)
End Sub

 

 

The First Level External VB Module:

 

Imports System
Imports System.Math
Imports System.Collections
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports Autodesk.iLogic.Interfaces
Imports Autodesk.iLogic.Runtime
Imports Inventor

AddVbRule "FunctionTools\bar.vb"

Module foo
	Public Function foofunc(oDoc As Document)
		MsgBox("FOO" & oDoc.FullFileName)
		barfunc(oDoc)
	End Function
End Module

 

 

The Second Level External VB Module:

Imports System
Imports System.Math
Imports System.Collections
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports Autodesk.iLogic.Interfaces
Imports Autodesk.iLogic.Runtime
Imports Inventor

Module bar
    Public Function barfunc(oDoc As Document)
        MsgBox(oDoc.FullFileName)
    End Function
End Module 

I am hitting "Error on Line 12 : Declaration expected." on the AddVbRule line which either implies that the syntax is incorrect for how you call other rules in Straight VB, or this kind of chained call is not allowed. I have tried using Imports and AddVbFile to no success.

0 Likes
Reply
694 Views
6 Replies
Replies (6)

WCrihfield
Mentor
Mentor

Try this, just as a proof of concept.

Create an external iLogic rule called GetFullFileName (the actual name is not important other than you have to remember & use it when referencing it in other rules.

Within the iLogic Rule Editor for that Rule, copy & paste the following:

 

Class ThisRule
	Public Function GetFullFileName(ByRef oDoc As Inventor.Document) As String
		If oDoc IsNot Nothing Then
			GetFullFileName = oDoc.FullFileName
		End If
	End Function
End Class

 

No extra 'Header' statements should be needed in the rule above, to be able to use this example.

Now save and close that rule, without running it.

Now create another external iLogic rule and paste the following in it:

In the Header Area:

 

AddVbFile "S:\Engineering\SHARED\External iLogic\REF FUNCTIONS\GetFullFileName.txt"

 

The example above is obviously custom to my machine, and needs to be changed to suit your situation, but I'm basically telling it to reference & use the specific other external iLogic rule.  I have all my external iLogic rules using the .txt extension, for simple & easy editing with Notepad, outside of Inventor, when needed.  (extension doesn't matter, as long as it matches your file)

In the main rule body paste this:

 

Sub Main()
	Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
	MsgBox(GetFullFileName(oDoc), vbOKOnly, " ")
End Sub

 

Then save and run it.

It should show a simple message box showing the FullFileName of the currently active Inventor document.

You'll notice that this has to be getting that info from the other referenced rule, to work.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

Another sometimes highly useful, yet similar way of using another external rule to add functionality to your current rule, is to pass one or more variables to the other rule, then get other variables back from it, using NameValueMap, and RuleArguments.  They are also discussed within one of the links from the last post.

Below is a very basic example of one rule running a second rule and passing a name-value pair to it, that second rule doing something with that data, then returning data to the first rule.

Rule 1:

'Create a NameValueMap
Dim oMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
'Add a Name / Value pair into it.
oMap.Add("TestName", "TestValue")
'Run your second rule here.
iLogicVb.RunExternalRule("NameValueMap Recieve & Send Test", oMap)
'This is where you get the other Value back from the second rule.
MsgBox("First Rule - Value returned from second rule = " & oMap.Value("Return"))

 

Rule 2:

'Retrieve NameValue Pair from calling rule
'Arguments is a NameValueMap
Dim oRecivedValue As String = RuleArguments.Arguments.Value("TestName")
'This next line is just prooving that this rule recieved what you sent to it.
'You can delete it when you've learned the trick.
'MsgBox("Second Rule - Recieved Pair Value = " & oRecivedValue)
'Do something with the oRecieved Value

'Now establish the value you want to send back to the Calling Rule.
Dim oValueToSendBack As String = "It Worked!"
'Return a new Value to the Calling Rule
RuleArguments.Arguments.Value("Return") = oValueToSendBack

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Anonymous
Not applicable

I attempted you top solution but hit similar errors:

Error on Line 5 : 'GetFullFileName' is not declared. It may be inaccessible due to its protection level.
Error on Line 3 : Declaration expected.

My code looked like the following:

Top Level iLogic

AddVbFile "FunctionTools\foo.vb"

Sub Main()
	Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
	MsgBox(GetFullFileName(oDoc), vbOKOnly, " ")
End Sub

 

foo.vb

AddVbFile "FunctionTools\bar.vb"

Module foo
End Module

 

bar.vb

Class ThisRule
	Public Function GetFullFileName(ByRef oDoc As Inventor.Document) As String
		If oDoc IsNot Nothing Then
			GetFullFileName = oDoc.FullFileName
		End If
	End Function
End Class

 It doesn't appear to be a issue of the file path as I can directly run foo.vb fine with my previous code. In this case even if I directly reference bar.vb from my ilogic level I hit the same errors. I am unsure how I would be able to directly access the GetFullFileName method without the creation of the class (unless ThisRule by nature has special properties).

0 Likes

WCrihfield
Mentor
Mentor

ThisRule is a pre-defined Class within Inventor's iLogic, so yes it may have special properteis.

See the Additional Statements section of the following link.

Advanced iLogic Techniques Reference 

And here is a link to one of Luke Davenports posts on Cadlinecommunity which mentions the use of ThisRule.

I haven't tried the second layer deep reference, as you seem to be trying to do, but the example I posted above worked for me.  (using Inventor Pro 2021 & Win 10 Enterprise)

Did you mark both of the reference rules as "Straignt VB code" in their options tab?

Which version of Inventor are you using?  (Not really sure if it matters, but this functionality may have not always been available in previous versions.)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

Here are a couple of screen shots showing my 'options' settings and variable recognition within both of my example rules.

Here's the first rule.  Only Silent operation & Fire dependent rules immediately are turned on.

And you can see that when I hover my mouse over the Function name, it recognizes it.

GetFullFileName - 1st Rule screen capture.png

Here is the second rule.  I have Silent operation, Fire dependent rules immediately, and "Straight VB code" enabled.

And you can see that when I hover my mouse over GetFullFileName (within the If statement), it is recognized.

GetFullFileName - 2nd Rule 1st screen capture.png

And here is another shot of the second rule, where I'm hovering my mouse over "FullFileName" (after oDoc.), and it is recognized.

GetFullFileName - 2nd Rule 2nd screen capture.png

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Anonymous
Not applicable

It is possible that what is making that function for you might be the Inventor difference, my company currently has everyone still on 2017 with maybe a update to 2018 coming this year. As such I do not have access to highlighting or the separate header section. I am also on Windows 10 Pro.

0 Likes