I am trying to create a VBA macro that will create an ilogic rule and set up event triggers. Does anyone know how to do this?
Thanks, Korey
To actually create the code of a rule or just to go through a bunch of parts,etc.. and set an already created ilogic rule (external rule,etc..) to run on a specific trigger?
Hi koreysd23,
Did you look on the Inventor Customization forum ? :
http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120
Maybe here:
or here:
https://forums.autodesk.com/t5/inventor-customization/how-create-a-ilogic-rule-by-vba/td-p/5634463
or search that forum for similar.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Korey. I don't have the code to hand but I'm sure I've done this before. From memory I saved the rule external then imported it and applied the trigger using vba.
If if I remember when I get in on Monday, I'll dig it out.
Hi,
To give you a kick-start. Have a look at the PDF attached (from page 22), I found it somewhere online.
It is a sample on how to create a new ilogic rule from VB.Net
So you'll have to modify someparts to use it as VBA as you are running within Inventor and not calling it externally.
I hope this helps!
This helps a little bit but I am still having trouble trying to find out how to create an ilogic rule using vba.
If you could find a copy of some code that is similar to creating an ilogic rule that would be much appreciated. Thanks.
Korey. My memory deceived me and i hadn't actually done this, at least i cant find any record of it.
The closest i got was running an external rule.
Private Sub RunILogicRule() RunExternaliLogicRule "NameOfRule" End Sub Sub RunExternaliLogicRule(ByVal ruleName As String) Dim iLogicAuto As Object Set iLogicAuto = GetiLogicAddin(ThisApplication) If (iLogicAuto Is Nothing) Then Exit Sub Dim doc As Document Set doc = ThisApplication.ActiveDocument Dim i As Integer i = iLogicAuto.RunExternalRule(doc, ruleName) End Sub
There are some additional resources here.
http://beinginventive.typepad.com/being-inventive/2011/10/creating-ilogic-rules-automatically.html
I know it's been a while since the original post but I needed to do this yesterday and found that there wasn't a complete, ready-to-go solution online.
I found some code from here that I modified a little bit to work in VBA. It allows iLogic rules to be added to different event triggers. The code to create the rule is modified from several other posts I found and works correctly in VBA. I added a small test sub at the end which creates an ilogic rule called "TestRule" that shows a message box any time a parameter is changed. Hopefully this helps anyone else in the future.
'Option Explicit
Public Sub CreateiLogicRule(RuleName As String, RuleContents As String)
'Written by Josh Smith 10/18/2022
'Creates an iLogic rule with given name and contents
'checks existing iLogic rules to see if an existing rule has the same name (regardless of text case)
'no checking has been added written to make sure names and contents are valid
Dim iLogicAuto As Object
Dim doc As Document
Dim RuleExists As Boolean
Dim iLogic As Object
Dim RuleList As Object
Dim R As Object
Dim RuleText As String
Set iLogicAuto = ThisApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
If (iLogicAuto Is Nothing) Then Exit Sub
Set doc = ThisApplication.ActiveDocument
Set iLogic = iLogicAuto.Automation
Set RuleList = iLogic.rules(doc)
If (Not RuleList Is Nothing) Then
For Each R In RuleList
If UCase(R.Name) = UCase(RuleName) Then Exit Sub
Next
End If
Call iLogic.AddRule(doc, RuleName, RuleContents)
End Sub
Public Sub AddEventTrigger(ByVal cDoc As Document, ByVal rName As String, ByVal trigName As String)
'Original code obtained from
'https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/adding-an-ilogic-rule-to-an-event-trigger-with-vba/m-p/4327544#M45007
'Modified by Josh Smith 10/18/2022
Dim BaseID As Integer
Dim BaseName As String
Dim BaseUse As String
'Each Event Trigger is just a Property in the PropertySet called "iLogicEventsRules"
'You MUST assign a PropertyID to them when you create them,
'AND the property ID, MUST fall withen a certain range, otherwise, the trigger will simply not work.
'
'Below is a list of the Event Triggers for .ipt files (Solid and Sheet Metal) -
'.iam files (Assembly), and .idw/.dwg files (Drawing Files).
'
' CheckList Box Name: : Property Name : : Property ID
' ----------------------------------------------------------------------------------------
' After Open Document : AfterDocOpen : 400
' Close Document : DocClose : 500
' Before Save Document : BeforeDocSave : 700
' After Save Document : AfterDocSave : 800
' Any Model Parameter Change : AfterAnyParamChange : 1000
' Part Geometry Change** : PartBodyChanged : 1200
' Material Change** : AfterMaterialChange : 1400
' Drawing View Change*** : AfterDrawingViewsUpdate : 1500
' iProperty Change : AfterAnyiPropertyChange : 1600
' Feature Suppression Change** : AfterFeatureSuppressionChange : 2000
' Component Suppression Change* : AfterComponentSuppressionChange : 2200
' iPart / iAssembly Change Component* : AfterComponentReplace : 2400
' New Document : AfterDocNew : 2600
'
' ----------------------------------------------------------------------------------------
Select Case trigName
Case "After Open Document"
BaseName = "AfterDocOpen"
BaseID = 400
BaseUse = "All"
Case "Close Document"
BaseName = "DocClose"
BaseID = 500
BaseUse = "All"
Case "Before Save Document"
BaseName = "BeforeDocSave"
BaseID = 700
BaseUse = "All"
Case "After Save Document"
BaseName = "AfterDocSave"
BaseID = 800
BaseUse = "All"
Case "Any Model Parameter Change"
BaseName = "AfterAnyParamChange"
BaseID = 1000
BaseUse = "All"
Case "Part Geometry Change**"
BaseName = "PartBodyChanged"
BaseID = 1200
BaseUse = "Part"
Case "Material Change**"
BaseName = "AfterMaterialChange"
BaseID = 1400
BaseUse = "Part"
Case "Drawing View Change***"
BaseName = "AfterDrawingViewsUpdate"
BaseID = 1500
BaseUse = "Drawing"
Case "iProperty Change"
BaseName = "AfterAnyiPropertyChange"
BaseID = 1600
BaseUse = "All"
Case "Feature Suppression Change**"
BaseName = "AfterFeatureSuppressionChange"
BaseID = 2000
BaseUse = "Part"
Case "Component Suppression Change*"
BaseName = "AfterComponentSuppressionChange*"
BaseID = 2200
BaseUse = "Assembly"
Case "iPart / iAssembly Change Component*"
BaseName = "AfterComponentReplace"
BaseID = 2400
BaseUse = "Assembly"
Case "New Document"
BaseName = "AfterDocNew"
BaseID = 2600
BaseUse = "All"
Case Else
'If the value passed, doesn't match any of the Cases, then we'll throw up a crazy message box!
'It also means that I probably have a spelling error :(
BaseUse = "None"
MsgBox "Trigger Event Error in Selection", vbOKOnly, "Trigger Event Selection Error"
Exit Sub
End Select
'First we check what the BaseUse is set to, then we check that against the current document's type.
'If they match up, we pass the current Document, the rule name, the trigger name, the base trigger name, and
'the base trigger ID to the sub SetEventProperty
If BaseUse = "All" Then
If cDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject _
Or cDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject _
Or cDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Call SetEventProperty(cDoc, rName, trigName, BaseName, BaseID)
Else
Exit Sub
End If
ElseIf BaseUse = "Drawing" Then
If cDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
Call SetEventProperty(cDoc, rName, trigName, BaseName, BaseID)
Else
Exit Sub
End If
ElseIf BaseUse = "Assembly" Then
If cDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
Call SetEventProperty(cDoc, rName, trigName, BaseName, BaseID)
Else
Exit Sub
End If
ElseIf BaseUse = "Part" Then
If cDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Call SetEventProperty(cDoc, rName, trigName, BaseName, BaseID)
Else
Exit Sub
End If
ElseIf BaseUse = "None" Then
Exit Sub
End If
End Sub
Private Sub SetEventProperty(ByVal cDocument As Document, ByVal RuleName As String, ByVal CheckBoxTrigName As String, ByVal TriggerPropName As String, ByVal BaseID As Integer)
'Original code obtained from
'https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/adding-an-ilogic-rule-to-an-event-trigger-with-vba/m-p/4327544#M45007
'Modified by Josh Smith 10/18/2022
Dim propItemCounter As Integer
Dim TriggerID As Integer
Dim CurrentID As Integer
Dim EndHolder As Integer
Dim CurrentEnd As Integer
Dim customIPropSet As PropertySet
'EndHolder will act as the numerical value that will be attached to the end of the property name.
EndHolder = 0
'TriggerID will hold on to the Trigger's base PropertyID value.
TriggerID = BaseID
On Error Resume Next
Set customIPropSet = cDocument.PropertySets.Item("iLogicEventsRules")
If customIPropSet Is Nothing Then
Set customIPropSet = cDocument.PropertySets.Item("_iLogicEventsRules")
End If
If customIPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
Call customIPropSet.Delete
Call cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If Err <> 0 Then
Err.Clear
Call cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
If Err <> 0 Then
MsgBox "Unable to create the Event Triggers property for this file!", vbOKOnly, "Event Triggers Not Set"
Exit Sub
End If
End If
On Error GoTo 0
'Sets customIPropSet to equal the Item that contains the Event Triggers of the open document.
Set customIPropSet = cDocument.PropertySets.Item("iLogicEventsRules")
'If there is an item (Event Trigger) found, then...
If customIPropSet.count > 0 Then
'We'll make a loop! This will go through each Property (Event Trigger), 1 Property at a time
For propItemCounter = 1 To customIPropSet.count Step 1
'This is for testing purposes. Because the individual Properties that are inside of a PropertySet
'are not exposed in VB.Net outright, you can uncomment the two lines of code below to allow you to
'see each Property as it goes through the count, one by one.
'----------------------------
' Dim megatest As Property
' megatest = customIPropSet.Item(propItemCounter)
Dim nameTest As String
nameTest = Strings.Left(customIPropSet.Item(propItemCounter).Name, Len(TriggerPropName))
Dim idTest As Integer
idTest = (CInt(Math.Abs(customIPropSet.Item(propItemCounter).PropId / 100))) * 100
If idTest = BaseID Then
'If the property name is equal, then we need to see if the Value of the property (AKA: The Rule Name) is
'equal to the new rule that was made previously. If it is, Then the trigger already exists
If customIPropSet.Item(propItemCounter).Value = RuleName Then
Exit Sub
Else
'Check to see if the PropertyName equals the TriggerPropName.
If nameTest = TriggerPropName Then
'If that isn't the case, it's alright. But, we'll need to get that numerical value from the end of the current Property
'because we will need it later to determine which numerical value is needed for the end of our PropertyName.
'The below line is simply looking at the last character of the current Property's Name, which should be a number, and making sure that it IS a number
If IsNumeric(Strings.Right(customIPropSet.Item(propItemCounter).Name, Len(customIPropSet.Item(propItemCounter).Name) - Len(TriggerPropName))) = True Then
'If it is a number, then we need to convert that value (because it's still a String), into an Integer, and set CurrentEnd to equal it.
CurrentEnd = CInt(Strings.Right(customIPropSet.Item(propItemCounter).Name, Len(customIPropSet.Item(propItemCounter).Name) - Len(TriggerPropName)))
'We'll also go on and grab the Property ID of the open Property
CurrentID = customIPropSet.Item(propItemCounter).PropId
'If the CurrentEnd is larger than our EndHolder (which was defined at the beginning of this sub), Then we need our EndHolder to
'equal 1 more than what is currently in use. Remember that we can't end up with two 'AfterDocSave0' entries.
If CurrentEnd > EndHolder Then
EndHolder = CurrentEnd + 1
ElseIf CurrentEnd = EndHolder Then
'However, if CurrentEnd is = to EndHolder, then we'll just add 1 to it.
EndHolder = EndHolder + 1
End If
'We do the same thing for the Property ID.
If CurrentID > TriggerID Then
TriggerID = CurrentID + 1
ElseIf CurrentID = TriggerID Then
TriggerID = TriggerID + 1
End If
Else
'If for some reason that last character in the Property Name isn't a numeric value, then we have more work to do...
MsgBox "The end string for Var: CurrentEnd, was not a Numeric Value. If you're reading this, something needs debugging!", vbOKOnly, "Error in SetEventProperty Sub"
Exit Sub
End If
Else
CurrentEnd = (customIPropSet.Item(propItemCounter).PropId - BaseID)
CurrentID = customIPropSet.Item(propItemCounter).PropId
If CurrentEnd > EndHolder Then
EndHolder = CurrentEnd + 1
ElseIf CurrentEnd = EndHolder Then
EndHolder = EndHolder + 1
End If
If CurrentID > TriggerID Then
TriggerID = CurrentID + 1
ElseIf CurrentID = TriggerID Then
TriggerID = TriggerID + 1
End If
End If
End If
End If
Next
End If
Call customIPropSet.Add(RuleName, TriggerPropName & EndHolder, TriggerID)
End Sub
Private Sub test()
'written by Josh Smith 10/18/2022
Dim RuleName As String
Dim RuleContents As String
RuleName = "TestRule"
RuleContents = "msgbox(""You changed a parameter."",MessageBoxButtons.OK,""iLogic Verification Window"")"
Call CreateiLogicRule(RuleName, RuleContents)
Call AddEventTrigger(ThisApplication.ActiveDocument, "TestRule", "Any Model Parameter Change")
End Sub
Hi @bike_josh. That's pretty cool. I have not tested it, but I trust that it works OK for you. I have also worked quite a bit with the Event Triggers hidden property set, but almost all of my related code is in iLogic. I mostly quit using VBA the last couple of years though, due to the security risks, which is why Autodesk has chosen to not include it anymore in the last few releases of Inventor (Link). The iLogic add-in's user interface (rule editor dialog) uses vb.net coding system, which is a newer and more dynamic system than VBA, but based on the same base Visual Basic source coding language.
Anyways, I just wanted to drop a few links here that may help you further develop your automation tool set. I have written a few 'contribution' posts over the past few years that, similarly to your work above, has taken the findings of some of these earlier pioneers who originally found this hidden set, and further developed solutions which take advantage of it, as well as showing some updated event property data in a newer chart. These posts are very limited by a set character count, so they were not able to hold all possible reference Sub/Function type helper routines, but have been fairly condensed to make the main process as simple to follow as possible.
These are certainly not new ideas or processes, but a collection of my personalized codes for them that I have posted publicly, as a quick reference that I can quickly and easily point others too who may have interest/need of these processes or the ideas/thoughts contained within them. The first 3 were updated earlier this year to include my latest findings/techniques.
Add An iLogic Rule To An Event Trigger Using An External iLogic Rule
Copy All iLogic Rules & Event Triggers From One Document To Another Using An External iLogic Rule
How To: "Unhide" the "Hidden" Event Triggers iProperties PropertySet using an iLogic rule
Copy All Rules & Event Triggers Settings From Selected Component To All Other Components - iLogic
External iLogic Rule (or VBA Macro) To Create Local iLogic Rule
Copy All Rules From One Document To Another Using An Exteral iLogic Rule
Wesley Crihfield
(Not an Autodesk Employee)
This is probably worth its own thread.
I am no doubt missing some leap in understanding, but to this day i do not understand how you develop code from scratch using iLogic.
- No IDE.
- No library.
- No intellisense. ( i see it now has this, may have always been there)
- No scope or immediate windows.
- No Reference libraries i think
I've tried a few times over the years to get my head around it but every time i try i'm left thinking it does not even come close to the VBA IDE.
The only thing it does is allow trigger events (which i developed in VBA then ported over).
Happy to be proven wrong on any of these points, i've just never been sold on the whole concept.
Hi @CadUser46. I respect your opinions on the subject, because you may have more years of experience using VBA with Inventor than I do. I may only have around 100 macros in my VBA application project for Inventor, because as I mentioned, I chose to develop most of my automation solutions in iLogic. By comparison I have probably created 30 or more iLogic rules for every VBA module I have created.
As for the IDE thing...most folks I have encoutered that come from a computer/software programming background tend to use Microsoft Visual Studio for their IDE. Then transfer some of their code down into iLogic rules, and some for Inventor AddIns. But all those who don't come from a computer/software programming background do not have any problem at all using the iLogic Rule Editor, even though its design is obviously pretty basic. Most of my programming background is CNC machine related (many different kinds/formats of them, using G & M codes and other systems), but have dabbled with things like VBA for MS Office/Excel, Batch files, and other related stuff on & off over my entire career. So, since I do not have a long history of every day use of one specific IDE, the iLogic Rule Editor dialog is just fine for most of my automation needs.
As for the references & library concerns - there are definitely ways to add references to several other application's API systems & Object Libraries, and ways to reference other external iLogic rules which can contain a whole host of publicly available Modules, Classes, Methods, etc. Just about anything you could imagine doing with vb.net, you can do from an iLogic rule, but of course it is limited because of being within the Inventor process, and a Document must always be open within Inventor. But Inventor's VBA has some similar limitations. You can also write your own DLL's that your iLogic rules can reference and make use of. I admittedly haven't taken full advantage of that DLL tool, but I have taken advantage of referencing several other API's and external resources.
Below are a couple of links to some related content in the online help section.
About iLogic Expansion and Customization
Advanced iLogic Techniques Reference
As for the 'Immediate Window'...you are correct there. Some of the tools available within Inventor's VBA Editor are definitely really nice, and it would be nice if they implemented more of them into the iLogic rule editor interface. Tools like the Object Browser window and the Immediate Window. But as I mentioned above, those who are used to, or dependent on the more advanced tools and debugging systems, often use Visual Studio to create/develop their more advanced solutions that they later transfer to iLogic.
Inventor's iLogic is definitely not very well understood my most folks. It took me a while to figure it out too. It is odd for experienced programmers to use, because of all the built-in helper stuff that it does in the background that they are not used to. For instance, it automatically and invisibly creates a Class named "ThisRule", and the End Class closure for us, and automatically creates the Sub Main and End Sub for us, when we don't put them in ourselves. And there are many unique Objects, Interfaces, Methods, and the like that are just automatically available for us to directly use in our rules, without having to specify any of the 'AddReference or Imports' codes in the header of our rules to be able to access them, because iLogic has already included those references for us in the background. All of that 'automatic' stuff is somewhat 'alien' to experienced programmers, but is extremely helpful for those with less programming background, helping them ease into it until they understand things better. Once I began to understood most of these somewhat hidden, yet powerful aspects of the iLogic AddIn, I came to really appreciate all the custom work that Autodesk has put into it to make automating Inventor so much easier for us.
Wesley Crihfield
(Not an Autodesk Employee)
I'm by no means any kind of expert but nice of you to say 🙂
I'm a mech engineer with little patience for button clicking and repetitive tasks. Certainly didnt come from any kind of CS background which i think is what makes the VBA IDE so great for figuring stuff out.
I dont think i would have achieved a fraction of what i have without being able to walk the scope tree or easily debug things on the fly.
I'm always amazed by professional developers that can just write code in a text editor and have it work correctly.
I realise it's moved on from its early days, but i just dont have the motivation to plough through a bunch of tutorials, trying to figure it out, rewrite all my code, and possibly still not quite reach the level of ease of use the IDE currently has. Not to mention all the windows forms and external app calls that are now in there.
I know it has its security flaws but VBA solves 99% of user level business logic problems, and is common across Windows apps, so unless MS and AD take the nuclear option i may never change.
Can't find what you're looking for? Ask the community or share your knowledge.