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 2024.3