4 Replies Latest reply on Feb 26, 2016 6:09 AM by Sven Jupp

    Taking a New part and running api code

    Sven Jupp

      I am looking to develop an Add-in that will capture the event when a user takes a new part by pressing the 'new' button in Solidworks. So to clarify, I want the user to open Solidworks (there is no document open) then the user presses the new button (ideally I would add my own button to the toolbar but have no idea how to do that), then when they select a document I capture that event (check its type) and run my code to bring up a userform if it is a part or assembly so that the user can fill in some custom properties. So I have identified the following event that fires when the user clicks a new document (iSwApp_FileNewPreNotify) from the solidworks dialogue box that appears when you press the new button, I then want to intercept that new document and do some operations.

      My preference would be to put my own 'New' button on that top level toolbar but I can work with the event I have found if I can figure out how to intercept the document it creates. I would also be up for advice on other methods to achieve the same thing.

       

      Thank you in advance.

       

       

      Sven

        • Re: Taking a New part and running api code
          Artem Taturevych

          You need to do this via the add-in. Install the SDK from SolidWorks and you will find the default template which already adds some sample button and handles SolidWorks events.

           

          Thanks,

          Artem

            • Re: Taking a New part and running api code
              Sven Jupp

              Artem,

               

              I have the SDK installed and have my addin registered, so I am just starting to figure out how to monitor the events I want to use. So I figured out this event is the one I want to use 'iSwApp.FileNewPreNotify' but I can't figure out how to get it to work in the event handler that is loaded when the add-in connects. I can get it to work when as below (this was the event available at the app level):-

               

              Private Function iSwApp_FileNewPreNotify(DocType As Integer,TemplateName As String) As Integer Handles iSwApp.FileNewPreNotify

               

              'enter code

               

              End Function

              I can get it to fire a msg box when the new part or assembly is selected, I guess I am asking is this the best way to do it? If I use this method can I just interrogate the document and change custom properties as I like and then save the file to a given filename?

              I would like to put a button on the tool bar that has the 'New' button on it that relates to my add-in

               

                • Re: Taking a New part and running api code
                  Artem Taturevych

                  Hi Sven,

                   

                  You may take a look at this example: #CODE|Custom Button for New Document

                   

                  Thanks,

                  Artem

                    • Re: Taking a New part and running api code
                      Sven Jupp

                      Artem,

                       

                      Thanks for the link, I have got my code to open a user form when a new document is taken. The user form is to allow the user to take a new number and update the description of the part etc. Now that I am in the userfrm class (as this is an add-in not a macro recoded in solidworks) how do I pass the new part object in to the userform class so that I can then work on that object when the user presses on the useform 'Ok' button to action the inputs? do I pass the solidworks object to the userfrm class when I call frm.show(). Or should I some how pass the data back to the event handler and then add it to the part?

                      Essentially I want to pass the user inputs from the form back to the part that has just be created from the new event.

                       

                      Thank you

                       

                      Sven