Hi,
How do you create an event that fires on iProperty change?
Like the iLogic triggers but then create a custom event in the AddIn.
Hi, here it is, straight from the API Help:
DocumentEvents.OnChange( ReasonsForChange As CommandTypesEnum, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, HandlingCode As HandlingCodeEnum )
Name | Description |
ReasonsForChange | This argument indicates the type of change that occurred. The value is from the CommandTypesEnum list, which represents the different categories of changes that can be made. Typically this will be a single value from the list but it can represent multiple values that have been combined together so you need to use bitwise operations to check for a specific change. |
BeforeOrAfter | This argument specifies if the notification is being sent before a change is made (kBefore), after a change is made (kAfter), or when a change has been aborted (kAbort). |
Context | Input NameValueMap object that can be used to determine the context of why the event fired. This argument provides additional information as described below:
Name = "DisplayName", Value = The display name of the command that caused the change. This name will change based on the language currently being used for Inventor. Name = "InternalName", Value = The internal name of the command that caused the change. This name will be consistent regardless of the current language. Name = "InternalNamesList", Value = as follows. When the internal name is "CompositeChange" this value is returned. Some commands result in making several changes but combine or composite them into a single command. The individual changes that were composited into the single command are provided through this value. It is an array of strings that consist of the internal names of the individual changes made. For example, if you drag a sketch point that connects two lines, the display name is "Drag Sketch Inference", the internal name is "CompositeChange" and the internal names list is an array of the following three strings; "Modify Point", "Modify Line", and "Modify Line". Name = "ConsideredDirty", Value = "". If this name appears in the context list then the change made is one that causes the document to be dirty. |
HandlingCode |
Output HandlingCodeEnum that indicates how you are handling the event. This event supports the ability to cancel the change. By setting this argument to kEventCanceled when the BeforeOrAfter argument is kBefore Inventor will abort the change. When the change is cancelled, this event is fired again but the BeforeOrAfter argument will have a value of kAbort. |
So, here is your final code:
Sub AddHandlers() Dim oDoc As Inventor.Document = invApp.ActiveDocument AddHandler oDoc.DocumentEvents.OnChange, AddressOf Me.MyNewSub End Sub Sub MyNewSub(ReasonsForChange As CommandTypesEnum, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, HandlingCode As HandlingCodeEnum) MsgBox("A change has just been commited.") End Sub
So if (correct me if i'm wrong) to catch the iProperty change event I would use your code sample and add IF statements into it like in my code below?
Sub AddHandlers() Dim oDoc As Inventor.Document = invApp.ActiveDocument AddHandler oDoc.DocumentEvents.OnChange, AddressOf Me.MyNewSub End Sub Sub MyNewSub(ReasonsForChange As CommandTypesEnum, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, HandlingCode As HandlingCodeEnum)
If ReasonsForChange = ConnandTypeEnum.kFilePropertyEditCmdType Then
' Do something cool
End If
End Sub
Thanks for the help!
Might work. I'd just change the "ConnandTypeEnum" to "CommandTypesEnum".
@Owner2229 wrote:
Might work. I'd just change the "ConnandTypeEnum" to "CommandTypesEnum".
Sure 😄
Is it also possible to monitor a specific property for a change ? for example "description"?
Hi,
I had this problem too, and unsuccessfully searched all over for an acceptable solution before I found my own. I'll post it in case it helps someone else.
The problem with DocumentEvents.OnChange is that it does not pass the document which is being changed. You can only look at the active document. If the active document is not the document being changed, for example editing iproperties of a part from within a BOM in an assembly, how do you get a reference to the document where the property was changed?
TransactionEvents.OnCommit also fires when a property is changed, and you can get a reference to the offending document from the TransactionObject.
Problem: you can't do anything within the OnCommit handler which creates a new transaction.
Solution: create a new thread, and pass a reference to the TransactionObject to a method which waits for the TA to finish, then does some cool stuff.
Example - first handle TransactionEvents.OnCommit. We want to look for a TA with the displayname "Properties" At that point pass a reference to the TA to a method which does what you want - in this case AfterPropertyChange()
private void TransactionEvents_OnCommit(Transaction TransactionObject, NameValueMap Context, EventTimingEnum BeforeOrAfter, out HandlingCodeEnum HandlingCode) { HandlingCode = HandlingCodeEnum.kEventNotHandled; if (BeforeOrAfter == Inventor.EventTimingEnum.kBefore) { //optional stuff before the commit return; } if (BeforeOrAfter == Inventor.EventTimingEnum.kAfter) { HandlingCode = HandlingCodeEnum.kEventHandled; //We want the TA with DisplayName "Properties" if (TransactionObject.DisplayName == "Properties") { //create a new thread, pass a reference to the TA to your method Thread t = new Thread( () => AfterPropertyChange(ref TransactionObject) ); t.Start(); } return; } return; }
your method looks like this - first get the Document object from the TA for later use. Then loop until the TA state is done. After that you can do what you want to the document where the property was changed:
public static void AfterPropertyChange(ref Transaction TransactionObject) { Document DocumentObject = TransactionObject.Document; //DateTime t1 = System.DateTime.Now; while (true) { if (TransactionObject.State == TransactionStateEnum.kDoneState) break; } //DateTime t2 = System.DateTime.Now; //MessageBox.Show((t2-t1)+""); //DocumentObject.DoCoolStuff(); PropertySet UDP = DocumentObject.PropertySets["Inventor User Defined Properties"]; PropertySet DTP = DocumentObject.PropertySets["Design Tracking Properties"]; }
@Jef_E wrote:Is it also possible to monitor a specific property for a change ? for example "description"?
FYI for Jef and others, I created a request in the Idea Station for a direct way to determine which iProperty caused a document's OnChange event to fire. Please consider voting for it here: API/Add-Ins: OnPropertyChange event.
Can't find what you're looking for? Ask the community or share your knowledge.