13 Replies Latest reply on Aug 23, 2018 1:20 PM by Jake Bakerin

    Trigger a macro when a custom property changed

    Jake Bakerin

      Is there API function call to trigger a macro each time a certain custom property changed?

        • Re: Trigger a macro when a custom property changed
          Marcos Rodríguez

          How is that custom property going to be changed?

           

          In visual basic that's call 'Events', like moving the mouse or click in some button,

           

          e.x.

           

          2017 SOLIDWORKS API Help - Fire Events for External Output Time Step Changes Example (VBA)

          • Re: Trigger a macro when a custom property changed
            Alex Burnett

            The issue with something like this is that you will need to launch a macro immediately after you start each SW session so that it will know to look for the event you're interested in to be fired. It will essentially be running in the background during your whole session.

             

            I've attached a basic example that will launch a message box whenever the user opens a new file.

            • Re: Trigger a macro when a custom property changed
              Jake Bakerin

              Thank you all. Will give it a try

                • Re: Trigger a macro when a custom property changed
                  Josh Brady

                  Not sure how familiar you are with programming events...  One thing that may trip you up a bit is that the event for custom property add or change is a document-level event.  That means that you may need to keep track of LOTS of documents, depending on how you want your macro to work.  Unless I'm missing something (which is, of course, possible) you will need to have a sldworks.sldworks object that's declared WithEvents so that you can get notified every time a document is opened or made active.  Then, here's the tricky part.  A document will only send you an event if you have an object for it.  Custom properties can be changed at any time.

                   

                  So, for example, if you open an assembly, the active document is the assembly.  However, you can select a component and open the custom property tab and change the component's properties. So if you want a notification for any property change, you're going to have to keep track of all open documents in SW with your code.  Even ones that don't have their own window.

                    • Re: Trigger a macro when a custom property changed
                      Alex Burnett

                      Very well said, Josh. I prefer to code an add-in with Visual Studio in C# due to the exact points you brought up.

                       

                      The active document needs to be kept track of at all times. If the document changes, the program will need to forget the previous object and then update the active document and type to the new one and then subscribe to all of the events you are concerned with. For this reason, I have used Luke Malpass' SolidDna wrapper because his handling of the active document is rather fantastic.

                        • Re: Trigger a macro when a custom property changed
                          Josh Brady

                          Alex Burnett wrote:

                           

                          Very well said, Josh. I prefer to code an add-in with Visual Studio in C# due to the exact points you brought up.

                           

                          The active document needs to be kept track of at all times. If the document changes, the program will need to forget the previous object and then update the active document and type to the new one and then subscribe to all of the events you are concerned with. For this reason, I have used Luke Malpass' SolidDna wrapper because his handling of the active document is rather fantastic.

                          Ah, but herein lies the rub!  This code must go far beyond keeping track of the current active document.  As I mentioned, custom properties of INACTIVE documents may be changed at any time.  Therefore, this code will have to create and retain an object for every single file that's open.  It cannot forget the previous document until the document is actually closed!  And not just when its window is closed, because it may still be open as reference in an assembly, drawing, derived part, etc.....

                            • Re: Trigger a macro when a custom property changed
                              Alex Burnett

                              Josh Brady wrote:

                               

                              Ah, but herein lies the rub! This code must go far beyond keeping track of the current active document. As I mentioned, custom properties of INACTIVE documents may be changed at any time. Therefore, this code will have to create and retain an object for every single file that's open. It cannot forget the previous document until the document is actually closed! And not just when its window is closed, because it may still be open as reference in an assembly, drawing, derived part, etc.....

                              If the OP is concerned with such a situation then I agree. You're correct that it would be a very difficult problem to accurately solve. It would take a LOT of references/lists as you have said in order to get that working as you've described.

                            • Re: Trigger a macro when a custom property changed
                              Jake Bakerin

                              Alex Burnett wrote:

                               

                              Very well said, Josh. I prefer to code an add-in with Visual Studio in C# due to the exact points you brought up.

                               

                              The active document needs to be kept track of at all times. If the document changes, the program will need to forget the previous object and then update the active document and type to the new one and then subscribe to all of the events you are concerned with. For this reason, I have used Luke Malpass' SolidDna wrapper because his handling of the active document is rather fantastic.

                              Alex,

                              How to find Luke's tool?

                            • Re: Trigger a macro when a custom property changed
                              Jake Bakerin

                              @josh

                              I'm using PDM standard so I can't add script or dispatch which is only available for PDM pro

                              What I want to do is when a user changes a document to final state (released), which I set in transition to change a "Current State" in the Custom Properties from "Reviewed" to "Released".  when this happens, it'd trigger a macro for design checks

                              Still learning SW API - so it'd better to do it in add-in?

                                • Re: Trigger a macro when a custom property changed
                                  Josh Brady

                                  Gotcha... I don't use either PDM, so I'm not sure...

                                   

                                  Can PDM transition documents without them being open in SolidWorks?  Because if so, you'll never get the notification.  I'm pretty sure PDM (and also SW Explorer) can change document custom properties without opening the document in SolidWorks....

                                    • Re: Trigger a macro when a custom property changed
                                      Alex Burnett

                                      Josh Brady wrote:

                                       

                                      Gotcha... I don't use either PDM, so I'm not sure...

                                       

                                      Can PDM transition documents without them being open in SolidWorks? Because if so, you'll never get the notification. I'm pretty sure PDM (and also SW Explorer) can change document custom properties without opening the document in SolidWorks....

                                      PDM State Changes don't require the document to be open at all. If a change is made in the file, I believe that it's likely done in the same manner as (or with) the DocumentManager API.

                                      • Re: Trigger a macro when a custom property changed
                                        Jake Bakerin

                                        Josh Brady wrote:

                                         

                                        Gotcha... I don't use either PDM, so I'm not sure...

                                         

                                        Can PDM transition documents without them being open in SolidWorks? Because if so, you'll never get the notification. I'm pretty sure PDM (and also SW Explorer) can change document custom properties without opening the document in SolidWorks....

                                        Alex is right

                                        State change can be done in the windows explorer (document is not open)

                                        However, what I want to do here is to change document's state while it's open