How to call Global Rule from a Macro?

How to call Global Rule from a Macro?

max_graubert
Observer Observer
607 Views
3 Replies
Message 1 of 4

How to call Global Rule from a Macro?

max_graubert
Observer
Observer

Hello,

 

I am trying to create a macro for drawings that simply advances or retreats the current drawing by 1 sheet. I found this iLogic code to do that, courtesy of nbonnett-murphy :

 

Prev sheet rule:

Sub main()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheetName As String
oSheetName = oDrawDoc.ActiveSheet.name
Dim oSheet As Sheet
Dim oSheetCount As Double
oSheetCount = oDrawDoc.Sheets.Count
i = 0
For Each oSheet In oDrawDoc.Sheets
    i = i + 1
    If oSheetName = oSheet.name Then
        If i > 1 Then
            oDrawDoc.Sheets.Item(i - 1).Activate
        Else
            MsgBox "Reached First Sheet"
        End If
    End If
Next
End Sub

 

Next sheet rule:

Sub main()
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheetName As String
oSheetName = oDrawDoc.ActiveSheet.Name
Dim oSheet As Sheet
Dim oSheetCount As Double
oSheetCount = oDrawDoc.Sheets.Count
i = 0
For Each oSheet In oDrawDoc.Sheets
    i = i + 1
    If oSheetName = oSheet.Name Then
        If i < oSheetCount Then
            oDrawDoc.Sheets.Item(i + 1).Activate
        Else
            MsgBox ("Reached Last Sheet")
        End If
    End If
Next
End Sub

 

I then found this macro code to call the rules courtesy of MJDeck :

 

Sub RuniLogicRule()

Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveDocument

Dim ruleName As String
ruleName = "Rule0"
Dim rule As Object
Set rule = iLogicAuto.GetRule(doc, "Rule0")
If (rule Is Nothing) Then
  Call MsgBox("No rule named " & ruleName & " was found in the document.")
  Exit Sub
End If

Dim i As Integer
i = iLogicAuto.RunRuleDirect(rule)

End Sub


Function GetiLogicAddin(oApplication As Inventor.Application) As Object
Set addIns = oApplication.ApplicationAddIns

Dim addIn As ApplicationAddIn
On Error GoTo NotFound
Set addIn = oApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")

If (addIn Is Nothing) Then Exit Function

addIn.Activate
Set GetiLogicAddin = addIn.Automation
Exit Function
NotFound:
End Function

 

 

This works great for the document that the rules belong to, but the macro can't find the rules when it is used in another document. I am wondering, how can I change my macro code to search for a global rule stored on my C drive? Is this even possible? I would love to have prev/next sheet hotkeys available in any drawing I happen to open.

 

Your help is appreciated!

0 Likes
608 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor

What you are looking for is IiLogicAutomation.RunExternalRule Method

iLogicAuto.RunExternalRule(doc, "C:\iLogicRule.iLogicVB")

 

0 Likes
Message 3 of 4

petr.meduna
Advocate
Advocate

It's been a while since I used vba, but this macro below I used to run external rules in documents. You can use it as you wish.

Public Sub MYSUB()
Dim addIn As ApplicationAddIn
Dim addIns As ApplicationAddIns
Set addIns = ThisApplication.ApplicationAddIns
    For Each addIn In addIns
        If InStr(addIn.DisplayName, "iLogic") > 0 Then
                        addIn.Activate
            Dim iLogicAuto As Object
            Set iLogicAuto = addIn.Automation
            Exit For
        End If
    Next
 
 
Dim EXTERNALrule As String
EXTERNALrule = "MYRULE"
 
  Dim oDoc As Document
 
  Set oDoc = ThisApplication.ActiveDocument
  If oDoc Is Nothing Then
    MsgBox "Missing Inventor Document"
    Exit Sub
  End If

iLogicAuto.RunExternalRule oDoc, EXTERNALrule

End Sub
0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

Same here.  I hardly ever use VBA anymore, but I do still have some macro buttons in my ribbons.  This is the version of this code that I used to use for running either an internal or external rule.  The ApplicationAddIn.Automation object for the iLogic add-in, is like using iLogicVb.Automation in an iLogic rule, just like @Michael.Navara already pointed out.  I left some comments at the bottom of this code, just in case you may need to send data to the rule, the same way you can when running another rule from an iLogic rule.  There are likely hundreds of versions of these types of codes floating around out there, because it's a very common tool.  Thankfully Inventor 2023 gave us the ability to create buttons for our iLogic rules without needing to go through all that VBA stuff.

Sub RunRule()
    Dim iLogicAddInClassId As String
    iLogicAddInClassId = "{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}"
    Dim iLogicAddIn As ApplicationAddIn
    Set iLogicAddIn = ThisApplication.ApplicationAddIns.ItemById(iLogicAddInClassId)
    If iLogicAddIn.Activated = False Then iLogicAddIn.Activate
    Dim iLocigAuto As Object
    Set iLocigAuto = iLogicAddIn.Automation
    
    'specify document for iLogic Rule to act upon
    Dim oDoc As Inventor.Document
    Set oDoc = ThisApplication.ActiveDocument
    
    'use this next one if the rule is internal (saved within the document)
    'Call iLocigAuto.RunRule(oDoc, "Internal Rule Name")
    'use this next one if the rule is external (not saved within a document)
    Call iLocigAuto.RunExternalRule(oDoc, "External Rule Name")
    
    'To send data to the iLogic rule, first create a NameValueMap, and create some name/value pairs within it,
    'then supply that in place of the 'Arguments' input variable in the RunExternalRuleWithArguments() method
    'Dim oArgs As Inventor.NameValueMap
    'Call oArgs.Add("TargetDocument", oDoc)
    'Call iLocigAuto.RunExternalRuleWithArguments(oDoc, oRuleName, oArgs)
    '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

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes