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.
Solved! Go to Solution.
Solved by MegaJerk. Go to Solution.
Solved by jdkriek. Go to Solution.
It's possible and some good folks did all the work for us:
I use this tool quite a lot:
Injecting iLogic code and iLogic event triggers automatically
Using that nifty link jdk posted you should be able to not only grab the executable, but the source code as well. Though I commented the heck out of it (if you consider occasional gibbering to be comment worthy), feel free to ask me about any of the particulars about code that you may have questions on, if you still plan on pursuing your own project.
Megajerk,
I looked at your excellent Code Injector and I tried to use the code contained within it to add an Event Trigger.
Unfortunutley, my knowledge of VBA is limited and I was not able to work out what i needed, in what order etc.
Would it be possible for you to either post, attach or send me the code required to add an event trigger from a VBA macro?
Any notes on what and how to do it would be very greatly appreciated.
Because of strange nature of Event Trigger handling in Inventor, I am going to prod you for a little more information.
Question 1: Do you want to add ANY rule (new, old, hasn't been made yet) to an event trigger, or do you only want to pick from a small set of rules that you have running across all documents that just happen to not be set up correctly?
Question 2: Where do the rules exist? Are they already inside of the ipt/iam/idw files? Internal, or external?
Question 3: What sort of workflow are you looking for? The iLogic Injector tool was created to do a large batch of files, and because you wanting to do this programmatically, I am assuming that you would like to achieve similar results. Is this the case, or are you doing something smaller?
Last but not least, because Event Triggers are essentially iProperties with benefits, I will post the entire sub that handles the set up of the Event iProperty, as well as the sub that actually does the iteration of the ID, and the creation of the trigger. Both are heavily commented and could technically be used as is so long as you provided the VerifyTrigger sub with the correct data which is ---
(cDoc As Document :: The document that you want to apply the event trigger in)
(rName As String :: The name of the rule that you want the trigger to activate)
(trigName As String :: The name of the event trigger that you want set up) **
** Note : Event Trigger Names have been listed in that little commented out table, but you should look to that Select Case statement for what your trigName should equal. You could of course rewrite this so that it uses the ACTUAL correct trigger name (hell.. I guess I should rewrite it) but there are things to be done and this is simply a primer for the party to come.
Instead of posting the code in this blob of text, I am attaching it as a text file. Remember that if you're porting anything over into VBA, you will need to use the Set command before you start making objects equal things. To make things easier you could probably get this working faster inside of the iLogic (which understands VB.net code), then inside of VBA.
We will discus this more once you have had a look at this post!
Hi Megajerk,
Many thanks for the post. Please find below the answers to your questions:
Q1 = We would like to be able to add any External rule that is already created and saved on our server.
Q2 = The rules will already exsit on our server so any inventor user will be able to get to them easily.
Q3 = We would like to be able to click a button on a form and update the active drawing. This will then add the required event trigger. We have a lot of drawings that may never get used again, so we don't want to update everything, just the ones we need when we open them (Hence the need for a form button)
I have opened your attached file, copied the text and posted it in an iLogic Rule.
When I click save I get the following error message:
Error in rule program format:
All other Sub's or Functions must be after Sub Main()
I have put a Sub Main() at the very beginning of the Rule, but I then got lots more error messagesas follows:
Rule Compile Errors in Add Event Trigger 3, in WW Event Trigger 1.idw
Error on Line 3 : Type 'Document' is not defined.
Error on Line 158 : 'DocumentTypeEnum' is not declared. It may be inaccessible due to its protection level.
Error on Line 159 : 'DocumentTypeEnum' is not declared. It may be inaccessible due to its protection level.
Error on Line 160 : 'DocumentTypeEnum' is not declared. It may be inaccessible due to its protection level.
Error on Line 166 : 'DocumentTypeEnum' is not declared. It may be inaccessible due to its protection level.
Error on Line 172 : 'DocumentTypeEnum' is not declared. It may be inaccessible due to its protection level.
Error on Line 178 : 'DocumentTypeEnum' is not declared. It may be inaccessible due to its protection level.
Error on Line 191 : Type 'Document' is not defined.
Error on Line 200 : Type 'PropertySet' is not defined.
Error on Line 237 : Type 'Property' is not defined
It will be me who is doing something wrong. If you can let me knowwhat I am doing wrong I can then adjst the code.
Many thanks in advance.
Right after my reply to you about this, two other people have hit up my inbox asking roughly for the same thing. I am directing them to this thread with this post, so that a more thorough dialog can occur, and because you all would like to see examples in VBA.
Right now the general issues seem to be around the creation of the iProperty set that controls Trigger Events if one does not already exist.
Any who, I just wanted to give everyone a heads up so that we were all on the same page! I am working on creating some examples (and will hopefully be able to track down the mystery of the Property Set errors), but must tend to my regular work first and foremost, so do not become crestfallen if it takes a little longer than expected!
Remember that if you have any files that you’ve found something to not work on (but have been successful with other files), please attach it here so that it can be experimented on!
Hi everyone,
I'm one of the guys who is also working on a VBA program with Event Triggers in it.
One question I have for now:
If you have a part that allready contains iLogic with Event Triggers, should this propertyset 'iLogicEventRules' always be present? I have tried the following code on a part that has iLogic and Event Triggers, but only the first four propertysets were found. Also the delete function and add function were not working, therefore if excluded them.
Private Sub CheckPropertysets()
Dim InvDoc As PartDocument
Set InvDoc = ThisApplication.ActiveDocument
Dim invInfo As PropertySets
Dim invInfo2 As PropertySet
Dim TEMP1 As String
Dim TEMP2 As String
TEMP1 = ""
TEMP2 = ""
Set invInfo = InvDoc.PropertySets
MsgBox invInfo.Count
For Each invInfo2 In invInfo
TEMP1 = invInfo2.Name
TEMP2 = invInfo2.InternalName
'Call invInfo2.Delete
'Set invInfo2 = invInfo.Add(TEMP1, TEMP2)
Next
End Sub
If you have an event trigger attached to a document, and the event trigger is working correctly, as far as I know, yes there should be an additional property set that contains all of the event trigger data. When I started working on that injector tool, I ran into an issue where I could create an event trigger, but because it was set up incorrectly, though it looked as if everything had worked once I opened the document (would show the rules associated to the event triggers with the little checkbox), they wouldn’t do anything.
If you can confirm that your event trigger is working on your part, and you do not see that 5th Property Set, would it be possible for you to upload your part so that I could experiment on it this weekend?
You’re using Inventor 2012 correct?
Also, could you make a copy of your part and try to run the latest iLogic Injector Tool (link in my signature) on it, using the tool to set up an event trigger for a new rule? If it’s unable to create the event trigger correctly, then I need to figure out why. If it does manage to create the event trigger / 5th property set, then we need to figure out how that process is breaking down for people when they attempt to do the same via VBA.
Thank you!
I did the suggested test this afternoon. The part (i've included) contains a simple ILogic rule that changes a dimension, also I added the Event Trickers: Any Model Parameter Change and Part Geometry Change.
First test was to run my own code on this document. The program found 4 propertysets, all belonging to the iProperties.
The second test was to run the lastest version of the code injector on this file to add some extra iLogic. The iLogic rule was get created. The Event Trickers where not created. I got the error message: 'unable to create event trickers'. I got the message a couple of times, equal to the number of Event Trickers set in the code injector program.
Not sure what you mean with your question: you are using inventor 2012 correct?
Some other thoughts: Could it be something to do with the references in VBA or some other DLL missing. Because didn't you also tested your program on 2012 version?
Thanks in advance for all the effort.
A little while back I ran into a situation where I needed to add an iLogic rule and event trigger and VBA was my only option. I based it off of your iLogic injector tool and most of it is straight up converted with most of your comments still in tact. I have this code available to post if you'd like.
I also ran into situations where the event trigger would be added incorrectly and show up in the document, but not function. I also remember some weirdness where event triggers created normally through the Inventor UI would not be visible through code. This was not an issue for me in my unique case so I never dug into it deep enough to figure out what was going on.
That would actually be pretty wonderful! If you already have some VBA converted code, it would save a world of time for the weekend, and could probably help some of these cats get started on their projects! It’ll also save me some time trying to hunt down this oddity of a bug.
Post away sir!
Let it be said that this is just another reason these forums are awesome!
See the attached text file for the code.
Currently the iLogic rule text and event trigger details are hard coded. It could be modified to read this from a userform or a text file, but for my purpose it wasn't necessary.
I’ve tested the attached code in two different ways. First I started a new document without any iLogic and Event Triggers and run the code. All’s fine until the event triggers are set. The problems start in the following lines of code. First ‘set customIPropSet’ statement gives value ‘nothing’, then if function gets triggered, the ‘.delete’ function doesn’t do anything I suspect. Also the second ‘set customIPropSet’ function doesn’t do anything. Also the third ‘set customIPropSet’ function doesn’t work. Finally I ended up with the warning message saying it’s unable to create event triggers.
Offcourse it’s logic that the code doesn’t stop when it founds a bug, because of the ‘on error resume next’ statement. When I do the same test without the ‘on error resume next’ statement I found a different result. Without this statement the code returns to main ‘createrule’ sub right after the first ‘set customIPropSet’ statement. I can’t see the reason why.
On Error Resume Next
Set customIPropSet = cDocument.PropertySets.Item("iLogicEventsRules")
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
If customIPropSet Is Nothing Then
Set customIPropSet = cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If customIPropSet Is Nothing Then
MsgBox "Unable to create the Event Triggers property for this file!", , "Event Triggers Not Set"
Exit Sub
End If
I also did a second test on a part that does contain iLogic and Event triggers. Tried your code, but the same results as described above.
I will keep searching for a good solution.
@ MegaJerk: I’m looking forward to hear if your code injector does manage to create some event triggers on the ‘simpleilogic.ipt’ I’ve uploaded in this tread. I think that the result tells us if the problem could be in the template or maybe other settings for Autodesk.
Thanks is advance for the effort
Well well well... It seems that I have finally figured out this mysterious problem. I have been dreading coming back to this because of work being super busy, and the notion that it was going to be a real pain in the neck to track down, but after trying something out of frustration we all arrive at what I hope will be the solution for everyone.
So far I have tested it on both NL-Laurens' file (Found as an attachment in this thread), as well as the file found here, with successful results (the exception being that you'll have to remove the non-existing external rule for the file found using the above link if you want to also test it and succeed)!
Alright. So. For those that want the short and sweet answer I will post the snippet of original code (provided by robert.f), with the fix, and those who wanna know how I found it (for whatever reason) can stay. The modified code of robert's will be posted as an attachment!
This is the original code snippet that would handle the iPropertySet creation (for the iLogicEvent), which would not get set on certain problem files...
Original :
On Error Resume Next Set customIPropSet = cDocument.PropertySets.Item("iLogicEventsRules") 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 If customIPropSet Is Nothing Then Set customIPropSet = cDocument.PropertySets.Add("iLogicEventsRules", _ "{2C540830-0723-455E-A8E2-891722EB4C3E}") End If If customIPropSet Is Nothing Then MsgBox "Unable to create the Event Triggers property for this file!", _ , "Event Triggers Not Set" Exit Sub End If
Modified (Note - I totally didn't format this code, so if you paste it, you'll need to fix the ends) :
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 Set customIPropSet = cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") End If If customIPropSet Is Nothing Then Set customIPropSet = cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") End If If customIPropSet Is Nothing Then MsgBox "Unable to create the Event Triggers property for this file!", , "Event Triggers Not Set" Exit Sub End If
What got added?
This line :
Set customIPropSet = cDocument.PropertySets.Item("_iLogicEventsRules")
For whatever reason, there is a hidden set of PropertySet for the iProperties that control event triggers. This is why it was impossible to create a new set using the necessary Internal Name ID! Technically it was already in use, and couldn't replicate! By checking for the hidden PropertySet in addition to the typically visible one, we are able to make the correct decision before attempting to create a new PropertySet.
-----------------------------
How I found it, and possibly a better way to move forward
-----------------------------
The below is just about the junk I did to figure this out, and will not be of much importance to most people. It's really just if you happen to also be in the position of wanting to know more about this topic, are interested in iProperties, or have a compulsive need to read absolutely everything that is in this post. If the above answered your question and you're alright with that, then simply download the modified code below and get to work on making awesome stuff!
---------
After numerous attempts to create a new iLogicEventsRules PropertySet which all failed, a few frustrated attempts to delete every PropertSet (out of spite mostly), and the creation of a non-correct Internally Named iLogicEventsRules PropertySet, I tried something silly.
Option Explicit Public Sub ReadProperty() Dim doc As Document Set doc = ThisApplication.ActiveDocument Dim customPropSets As PropertySets Set customPropSets = doc.PropertySets Dim customPropSet As PropertySet Set customPropSet = customPropSets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}") End Sub
For some reason it never occurred to me to call the PropertySet by its Internal Name rather than using the typical Display Name string, despite that it's clearly listed in the API reference guide.
From there, once I could see that object was no longer Nothing, I used the following to call it up again :
Set customPropSet = customPropSets.Item("_iLogicEventsRules")
Of course this leads to some other questions that perhaps someone more experienced can come in here and answer later... What sort of iProperties are hidden? Was the underscore used to denote iProperties back in the day, or is it way of marking up special iProperties that didn't exist before the file was migrated to a new release? Is it something that perhaps the Apprentice Server interacts with?
Someone smarter than me should answer that.
Needless to say, I'll have to make some more time in the near future and update the iLogic Injector so that it can be used by, hopefully, by more people!
Thanks for sticking around, and remember that if you still have any more questions, or if this doesn't seem to work for you, post post post!
Thank you MegaJerk for all the effort. I'm going to work on my program and upload it here once I'm finished.
Laurens
Dear All,
Thank you so much for posting this and sorry it has taken me so long to get round to trying the code (Holiday & Work being busy!)...
I have managed to get the code to add an event trigger to a Drawing that already has rules (Internal & External) and did NOT have any event triggers already set-up.
I will try different sernarios with no rules, event triggers already exsisting etc and let you know.
The intention of the is to add event triggers to IV2010 & IV2013 (and Older) Drawings so if we have any problems I will post them here.
Another quesiton, is it possible to get the VBA code to add an external rule (That Already Exsists!) to an event trigger?
If this is not possible I will use the code to create an internal rule that fires off the external rules as required.
Thanks again for all your assistance with this it has helped me out a lot.
MegaJerk,
Firstly, you are awesome and I thank you for your code.
Secondly, the Drawing View Change event trigger does not seem to work for me at the moment.
I am running Inventor 2013. I am writing some code in C# that requires the addition of rules/event triggers automatically.
Before Save, After Save, & iProperty Change event triggers have worked - aside from Drawing View Change, I have yet to test the rest.
I am typically dealing with .dwg files, but I have tested it on both .dwg and .idw.
I have tested numerous variations of this to no avail:
customIPropSet.Add("Scale", "AfterDrawingViewsUpdate0", 1500);
Any ideas? I'll gladly throw some code up if necessary.
Thanks,
Bennywise