Run Assembly iLogic Rule (vba)

Run Assembly iLogic Rule (vba)

inulobo
Advocate Advocate
2,502 Views
5 Replies
Message 1 of 6

Run Assembly iLogic Rule (vba)

inulobo
Advocate
Advocate

I am trying to push some values from Excel (vba) to an inventor assembly document. There are 2 iLogic rules within the assembly document that normally run before save. I cannot get the triggers to flip via vba. Is there a way to run the assembly document iLogic rules via Excel vba? These are not external rules.

0 Likes
2,503 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @inulobo.  Yes.  I believe this is possible.  Have you ever interacted with Inventor from an Excel VBA macro before?  If not, you may need to turn on the reference to the "Autodesk Inventor Object Library", before you can do so.  With Excel VBA dialog open, go to Tools tab > References..., then within that pop-up dialog, scroll to the reference I mentioned above, then ensure there is a checkmark in the little checkbox beside it, then click OK on that pop-up dialog.

WCrihfield_0-1643900273208.png

Once you've done that, the next step would of course be to create a new Module to put your macro code into (if you don't already have one prepared).  Next, you will need to create a variable for the Inventor.Application object, then dig into Inventor's ApplicationAddIns to get a variable for its iLogic AddIn, then for its Automation object (as Object).  Then we can use that similarly to how we would use iLogicVb.Automation within an iLogic rule, to access the RunRule type methods.  You will not have any Intellisense type help with those methods though, because it is foreign to Excel at this point.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

WCrihfield
Mentor
Mentor

Here is an example of an Excel VBA macro that will run a local iLogic rule within an Inventor document.  It assumes that Inventor is already running, and that the target document (with the rule in it) is open and active.  You will most likely have to change the name of the iLogic rule I am specifying within.  You can get the document however you want, but to keep this example simple, I'm just getting the 'active' one, for now.  Also, I don't know if you need to 'send' anything to that iLogic rule or not, but if you do need to, you can use one of the other variations of the RunRule type methods (I left a few commented out near the end, for convenience).  Another important thing to notice is how I'm getting the Inventor application.  You can't simply use the term 'ThisApplication' here, because that would be trying to get the Excel application, instead of the Inventor application.  If Inventor is not running, you could try using CreateObject() instead, or use a Function that tries both ways, then returns the appropriate application object.

Here is the VBA code for it:

 

Sub RunInventorLocaliLogicRule()
    Dim oInv As Inventor.Application
    Set oInv = GetObject(, "Inventor.Application")
    'get the iLogic Add-in, and its Automation object
    'so we can use the 'RunRule' type methods defined within it
    Dim oAddin As Inventor.ApplicationAddIn
    Dim iLogic As Inventor.ApplicationAddIn
    For Each oAddin In oInv.ApplicationAddIns
        If VBA.Strings.InStr(oAddin.DisplayName, "iLogic") > 0 Then
            Set iLogic = oAddin
            Exit For
        End If
    Next
    If iLogic Is Nothing Then Exit Sub
    If Not iLogic.Activated Then iLogic.Activate
    Dim oAuto As Object
    Set oAuto = iLogic.Automation
    
    'specify target document for external rule to effect
    Dim oDoc As Inventor.Document
    Set oDoc = oInv.ActiveDocument
    'Set oDoc = oInv.Documents.Open("C:\Temp\MyAssembly.iam", True) 'False = open invisibly
    
    'specify the name of the rule
    Dim oRuleName As String
    oRuleName = "LocalRuleName"
    
    Call oAuto.RunRule(oDoc, oRuleName)
    'Call oAuto.RunRuleWithArguments(oDoc, oRuleName, oRuleArguments)
    'Call oAuto.RunExternalRule(oDoc, oRuleName)
    
    'To send data to the iLogic rule, first create a NameValueMap,
    'create some name/value pairs within it,
    'then supply that in place of the 'Arguments' input variable in the RunRuleWithArguments() method
    'Dim oRuleArguments As Inventor.NameValueMap
    'Call oRuleArguments.Add("TargetDocument", oDoc)
    'Call oAuto.RunExternalRuleWithArguments(oDoc, oRuleName, oRuleArguments)
    'the iLogic rule will need to be set-up to retrieve that NameValueMap using the RuleArguments interface.
    'once the iLogic rule has retrieved it, it can get/set values or pairs within it
    'once the iLogic rule finishes, it will return control to this VBA macro,
    'then the macro can simply retrieve any resulting values from the NameValueMap it created earlier
End Sub

 

P.S.:  Be aware that if the rule contains any pop-up dialogs, like MessageBox.Show(), or MsgBox(), or similar that you may need to interact with, Inventor may not currently have top system focus, so the pop-up dialog might possibly be hidden behind an application window or something like that.  If that happens, it may appear like Excel is taking a really long time to run the macro, but it may in fact be waiting on you to interact with the dialog from the rule, before it will continue.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 6

Ralf_Krieg
Advisor
Advisor

Hello

 


@WCrihfield  schrieb:
'the iLogic rule will need to be set-up to retrieve that NameValueMap using the RuleArguments interface.
    'once the iLogic rule has retrieved it, it can get/set values or pairs within it
    'once the iLogic rule finishes, it will return control to this VBA macro,

Can you please explain in detail how you make the NameValueMap retrieved by the IRuleArguments Interface writeable? And how is this NameValueMap returned back to VBA when iLogic rule finishes?


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor

Hi @Ralf_Krieg.  Long time, no see. 😀  Within the iLogic rule, we can 'retrieve' the data sent to it from the RuleArguments object in two different ways.  Either by getting the whole NameValueMap from the RuleArguments.Arguments property, or by getting the individual 'entry' values directly from within the NameValueMap (oValue = RuleArguments.Value("ArgumentName")).  If there are only a few, then I generally opt for the individual route, using the RuleArguments.Exists("ArgumentName") property first, to make sure it is present, then using the Value property, but if there are lots of arguments, then I will just get the whole NameValueMap at once.

 

However, one the 'send' (or 'return') side of this transaction, we can not set a new or modified NameValueMap object as the value of RuleArguments.Arguments directly, because that 'Property' is ReadOnly.  But just because that property is ReadOnly, that does not mean that we can not modify the properties (entries) of its existing NameValueMap.  So, to 'send/return' data to the 'sender', I simply use something like

RuleArguments.Arguments.Value("Return") = "My Return Value"

As you are likely aware, similar to how the SharedVariable route works, we can use the Value property to 'Add' a new entry, even when one does not already exist, so that is actually a 'safer' route than using the 'Add' method of the NameValueMap, just in case that 'argument entry' already exists.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

Ralf_Krieg
Advisor
Advisor

Hello

 

Thanks a lot for the detailed explanation. I found the mistake i made before. I tried to use RuleArguments as NameValueMap, which of course failed. I totally missed that there is an RuleArguments.Arguments property. 🙈

Now it works as expected.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes