Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding an iLogic Rule to an Event Trigger with VBA

45 REPLIES 45
SOLVED
Reply
Message 1 of 46
ccoomes
12943 Views, 45 Replies

Adding an iLogic Rule to an Event Trigger with VBA

I have done lots of searching and found nurmeous posts about how to get the API to add an iLogic Event Trigger but I cannot find the exact code on how to do it.

 

My knowledge of VBA is limited and I am now stuck.  This is one of the last little pieces of code that we need.

 

Is there a Simple way to add an Ilogic Rule (External or Internal) to an event trigger with a VBA Macro or iLogic Rule?

 

I would like to automate adding an iLogic rule to an Event Trigger when we update our old drawings.

 

The details of the Rule I am trying to call is as follows:

 

Rule Name = Save On Close Commands

Event = Before Close

 

We currently have Inventor 2013 and 2014.  If it is not possible in 2013, is it possible in 2014?

 

If someone could please post the text needed to get the Rule Added to the Event Trigger it would be greatly apprcaited.

 

Thanks in advance.

45 REPLIES 45
Message 41 of 46

I tried some code snippets and I found a solution that works for me. Maybe somebody helps this also. 

Here is my solution. I added to the default.ivb the following code:

 

Sub CreateRule(ByVal RuleName As String)
    
    Dim i As Object
    Set i = GetiLogicAddin(ThisApplication)
    
    Dim oDoc As Inventor.Document
    Set oDoc = ThisApplication.ActiveDocument
    
    'we will create a rule in the current document, that has a name, but doesn't have any text
    'this way the rule will fire with nothing to do, meaning that it does nothing
    Dim exRule As Object
    
    'i will try and open that rule to see if it exists, if it does, then we will overwrite it
    On Error Resume Next
    Set exRule = i.GetRule(oDoc, RuleName)
    If exRule Is Nothing Then
        Call i.AddRule(oDoc, RuleName, "")
        Set exRule = i.GetRule(oDoc, RuleName)
    Else
        exRule.Text = ""
    End If

    'now create the rule text, be careful here with double quotes and line breaks that you use the correct chr()
    Dim ruleText As String
    ruleText = "Sub Main()" & Chr(10) & _
                "iLogicVb.RunExternalRule(" & Chr(34) & RuleName & Chr(34) & ")" & Chr(10) & _
                "End Sub"

    'now we can assign the users rule text to that rule, without it actually running
    exRule.Text = ruleText
    Debug.Print (exRule.Text)

End Sub
Function GetiLogicAddin(ByVal oApplication As Inventor.Application) As Object
        Dim addIns As ApplicationAddIns
        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
            Set GetiLogicAddin = Nothing
            Exit Function
        End If

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

Now I am able to create my own rules. So I create an local rule named "TestRule". An external rule named "TestRule" was created by me manually. 

 

I write to the local rule the following content:

 

    ruleText = "Sub Main()" & Chr(10) & _
                "iLogicVb.RunExternalRule(" & Chr(34) & RuleName & Chr(34) & ")" & Chr(10) & _
                "End Sub"

This code calls the external rule I want to.

 

 

--------------------------------------------------------------

 

Back to the problem again.

The reason for this all was a problem which many developers and engineers will have. Old documents have no iLogic triggers. So if you open an old drawing you have to manually add the triggers. No nice solution.

- I attached an file (vba.txt), which has the complete vba code inside (@MegaJerk: I hope it is ok). Just put it into your vba file

- After that you have to save the external rule "RuleOne.txt" which is attached

- Now run the vba function "AddEventTriggers()"

- If you now save your drawing there should come up an message whith "Bingo". So everything went right.

 

*Please note that the rule name should be the same for internal and external rules.

*Also note that this code works on my system. I give no garantee that it will work on every system

 

PDM/CAD Administrator
Using Inventor/Vault 2022.2
Message 42 of 46
tgirardJ8ULV
in reply to: Anonymous

Hi All,

 

Jumping in this thread, I have a small issue with this, maybe you can help me.

 

Using the same code as DuduHarel, I'm assigning one of my rules to the ''Part Geometry Update'' trigger. I'm able to do that, and my function gets listed in the Trigger dialog window. However, my rule does not execute when the trigger is fired.

 

I've found two workarounds yet: I can either move other rules around in the trigger dialog window, and then my iLogic-created association starts working. So let's say I'm assigning rule1 to the part geometry change with iLogic code, then manually associating Rule2 with let's say ''on document save'' will magically make my rule1 work when my part changes. OR, I can save my file, close it, reopen, and then my trigger starts working like it should.

 

Did anyone run into similar issues or have an idea of how to fix this problem? Maybe some command to tell Inventor to re-compute its trigger list or something like that?  That would be greatly appreciated.

 

Note: the screenshot is what I call the ''trigger dialog window'', just to be clear.

 

Thanks,

Thomas

 

2018-04-25 14_08_40-Clipboard.png

 


@Anonymouswrote:

Hii all,

I've tried this iLogic external rule to add an internal rule (name "Rule1") to Event Trigger and it worked!

 

Dim doc As Document
doc = ThisApplication.ActiveDocument
Dim customPropSets As PropertySets
customPropSets = doc.PropertySets
Dim customPropSet As PropertySet
customPropSet = doc.PropertySets.Item("_iLogicEventsRules")
customPropSet.Add("Rule1", "BeforeDocSave0", 700)

 

note thet there is no "Set" or "Let" at  iLogic rules

 

David


 

Message 43 of 46
mr_ensing
in reply to: MegaJerk

Although we're 5 years later now, a reply just to thank you for your solution. 

I developed a more limited macro to apply some triggers. But for some (older) parts it just did not work.

The only differences i found between functioning en non-functioning parts was the internal name of the propertyset.

These lines fixed all my problems:

 

 

        If customIPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
            Call customIPropSet.Delete
            Set customIPropSet = cDocument. _
            PropertySets.Add("iLogicEventsRules", _
            "{2C540830-0723-455E-A8E2-891722EB4C3E}")
        End If

So, thanks!

 

Message 44 of 46
Charlies_3D_T
in reply to: mr_ensing

@mr_ensing 

 

Hello,

 

Could you post the code you use to set a rule to the event triggers? I can not figure out to get a rule working to place a rule to the part geometry change trigger. 

Message 45 of 46
mr_ensing
in reply to: Charlies_3D_T

It really needs a rewrite, but you know, who's got time for that.

 

Essential in my case is that the InternalName of the PropertySet is "{2C540830-0723-455E-A8E2-891722EB4C3E}". 

 

My code:

 

Private Sub Triggers(strTrigger As String)
    ' strTrigger:
    ' "file://*********.iLogic"			-   *********.iLogic.txt
    ' "file://*********.iLogic"			-   *********.iLogic.txt

    ' AfterDocOpen,                     Property-id startkey: 400,  use: ipt/iam/idw/dwg
    ' DocClose,                         Property-id startkey: 500,  use: ipt/iam/idw/dwg
    ' BeforeDocSave,                    Property-id startkey: 700,  use: ipt/iam/idw/dwg
    ' AfterDocSave,                     Property-id startkey: 800,  use: ipt/iam/idw/dwg
    ' AfterAnyParamChange,              Property-id startkey: 1000, use: ipt/iam/idw/dwg
    ' PartBodyChanged,                  Property-id startkey: 1200, use: ipt
    ' AfterMaterialChange,              Property-id startkey: 1400, use: ipt
    ' AfterAnyiPropertyChange,          Property-id startkey: 1600, use: ipt/iam/idw/dwg
    ' AfterDrawingViewsUpdate,          Property-id startkey: 1800, use: idw/dwg
    ' AfterFeatureSuppressionChange,    Property-id startkey: 2000, use: ipt
    ' AfterComponentSuppressionChange,  Property-id startkey: 2200, use: iam
    ' AfterComponentReplace,            Property-id startkey: 2400, use: iam
    ' AfterDocNew,                      Property-id startkey: 2600, use: ipt/iam/idw/dwg

    Dim oTriggerSet As PropertySet
       
    On Error Resume Next
        Set oTriggerSet = oPropSets.Item("_iLogicEventsRules")
        If Err <> 0 Then
            Call Err.Clear
            Set oTriggerSet = oPropSets.Item("iLogicEventsRules")
        End If
        If Err <> 0 Then
            Call oPropSets.Add("_iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
            Set oTriggerSet = oPropSets.Item("_iLogicEventsRules")
        End If
        If oTriggerSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
            Call oTriggerSet.Delete
            Call oPropSets.Add("_iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
            Set oTriggerSet = oPropSets.Item("_iLogicEventsRules")
        End If
    On Error GoTo 0
    
    Dim bTrigger1 As Boolean: bTrigger1 = False
    Dim bTrigger2 As Boolean: bTrigger2 = False
    Dim oTrigger As Property
	
    For Each oTrigger In oTriggerSet
        If oTrigger.Name Like "AfterAnyParamChange*" Then
            If oTrigger.Expression = strTrigger Then
                'great
                bTrigger1 = True
            Else
		' Removed some very specific code here
            End If
        ElseIf oTrigger.Name Like "AfterMaterialChange*" Then
            If oTrigger.Expression = strTrigger Then
                'great
                bTrigger2 = True
            Else
		' Removed some very specific code here
            End If
        Else
            oTrigger.Delete
        End If
    Next oTrigger
	
    Dim i As Integer
    i = 0
    Do While bTrigger1 = False
        On Error Resume Next
		Call oTriggerSet.Add(strTrigger, "AfterAnyParamChange" & i, 1000 + i)
		If Err.Number <> 0 Then
			Err.Clear
			i = i + 1
		Else
			bTrigger1 = True
		End If
        On Error GoTo 0
    Loop
    i = 0
    Do While bTrigger2 = False
        On Error Resume Next
		Call oTriggerSet.Add(strTrigger, "AfterMaterialChange" & i, 1400 + i)
		If Err.Number <> 0 Then
			Err.Clear
			i = i + 1
		Else
			bTrigger2 = True
		End If
        On Error GoTo 0
    Loop
End Sub 

 

Message 46 of 46
NeoLitik
in reply to: Anonymous

Hi there !

The solution from DuduHarel not working for me 😞

i mean : sometime work, sometime not.

 

Indeed, sometime i have this error :

Error in rule: add_trigger, in document: Part15

Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

 

Do you know how we can fix this problem ?
I'm on v2013 
thank you soooo much and sorry for my dirty english...

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report