How to Make the Opened Assembly the Current Document When Running an External iLogic Rule

How to Make the Opened Assembly the Current Document When Running an External iLogic Rule

leebrg
Contributor Contributor
320 Views
3 Replies
Message 1 of 4

How to Make the Opened Assembly the Current Document When Running an External iLogic Rule

leebrg
Contributor
Contributor

 

Hello,

To make the question easy to understand, let's assume the following file names:

  • Assembly1.iam

  • Assembly2.iam

  • EXRule1.iLogicVb

  • EXRule2.iLogicVb

  1. Assembly1.iam is currently open.

  2. While this document is active, I run the external rule EXRule1.iLogicVb.

  3. Inside EXRule1, it opens Assembly2.iam and runs another external rule called EXRule2.

Here’s the simplified code:

 

Dim oDocName As String
oDocName = "C:\Temp\Assembly2.iam"
Dim openDocument As AssemblyDocument = ThisApplication.Documents.Open(oDocName, True)
openDocument.Activate()
iLogicVb.RunExternalRule("EXRule2")

The Problem:

In EXRule2.iLogicVb, when I try to access parameters or properties using ThisDoc,
it still points to Assembly1.iam, not the newly opened Assembly2.iam.

However, all logic inside EXRule2 is written assuming that the current document is Assembly2.iam,
so most parameter calls are like this:

 

Parameter("MyParam")

If I use ThisApplication.ActiveDocument instead, then I'd need to rewrite all parameter calls like:

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim myParam As String = oDoc.ComponentDefinition.Parameters("MyParam").Value

But the problem is, there are hundreds of parameters used in EXRule2, and rewriting all of them is not realistic.

 

Is there any way to make the newly opened Assembly2.iam be recognized as ThisDoc in EXRule2?
Or any workaround that allows me to keep using short form like Parameter("MyParam") inside the rule
as if Assembly2.iam is the current document?

 

Any ideas or best practices would be greatly appreciated.

Thank you!

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

Curtis_Waguespack
Consultant
Consultant

Hi @leebrg 

 

I think we can do this but we’d need to get the document as an ilogic document object in the 2nd rule.

 

I’m about to hop on a flight, but I’ll ask to get this moved to ilogic forum where one of our ilogic focused community members can have a look at you question.

 

hope that help, 

curtis

EESignature

0 Likes
Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Instead of using this:

iLogicVb.RunExternalRule("EXRule2")

...another option would be to use this:

Dim oArgs As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
oArgs.Value("Document") = openDocument
iLogicVb.Automation.RunExternalRuleWithArguments(openDocument, "EXRule2", oArgs)

Now, when 'EXRule2' runs, its 'ThisDoc.Document' should be pointing to that 'openDocument' that you specified as the first 'input' to that method.

And, if you wanted to, you could use the 'arguments' to 'receive' the Document object that was 'sent' to that second rule, like the following...as an optional.

Dim oDoc As Inventor.Document = Nothing
If RuleArguments.Exists("Document") Then
	oDoc = RuleArguments.Value("Document")
Else
	oDoc = ThisDoc.Document
End If
If oDoc Is Nothing Then Return 'exit if no document

The 'RuleArguments' is unique to the iLogic add-in, and is its way to 'share' data between rules.  Another tool for that purpose that is also unique to the iLogic add-in is the Rule Object named 'SharedVariable'.  The SharedVariable system is essentially a temporary 2-factor (each item/entry has 2 parts) collection of objects that the iLogic add-in creates for us.  The data in it does not survive after Inventor quits, because it is just being held in Inventor's session memory.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 4

leebrg
Contributor
Contributor

Thank you. I tried the code you provided, and it solved the problem. I'm really grateful. 🙂

0 Likes