Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AddIn event iProperty change

6 REPLIES 6
Reply
Message 1 of 7
Jef_E
881 Views, 6 Replies

AddIn event iProperty change

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.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
6 REPLIES 6
Message 2 of 7
Owner2229
in reply to: Jef_E

Hi, here it is, straight from the API Help:

 

DocumentEvents.OnChange( ReasonsForChange As CommandTypesEnum, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, HandlingCode As HandlingCodeEnum )

 

Parameters

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
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 7
Jef_E
in reply to: Owner2229

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!



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 4 of 7
Owner2229
in reply to: Jef_E

Might work. I'd just change the "ConnandTypeEnum" to "CommandTypesEnum". Smiley Happy

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 5 of 7
Jef_E
in reply to: Owner2229


@Owner2229 wrote:

Might work. I'd just change the "ConnandTypeEnum" to "CommandTypesEnum". Smiley Happy


Sure 😄

 

Is it also possible to monitor a specific property for a change ? for example "description"?

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
Message 6 of 7
ejmacchi
in reply to: Jef_E

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

            
        }

 

 

Message 7 of 7
DRoam
in reply to: Jef_E


@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.

Post to forums  

Autodesk Design & Make Report