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"];
}