15 Replies Latest reply on Jan 4, 2016 11:08 AM by Yannick Proulx

    Event active doc is an assy

    Yannick Proulx

      Hi,

      I'm actually working on an addin and I Would like to have a button in my taskpane that is only enable when the active document is an assembly.

       

      I would like to know how to get this event trigger.

       

      Thank you

      Yannick

        • Re: Event active doc is an assy
          Simon Turner

          If you used the Visual Studio template from the API SDK, you should have a function like this (in the main addin class):

           

              Function SldWorks_ActiveModelDocChangeNotify() As Integer

                  'TODO: Add your implementation here

              End Function

           

          Replace the TODO line with your code to check what the active document is, and enable/disable your control.

          • Re: Event active doc is an assy
            Amen Allah Jlili

            Expanding on Simon Turner's answer:

             

            SolidWorks provide event handlers (in forms of delegates if you're using C#) for managing events. The following event handler contained in SldWorks class should execute when the active model is changed. Please note this will not work for parts editing from inside an assembly.

            Declaration:

            public delegate System.int DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler()
            

             

            Usage:

            Simply add the method that is going to handle the event to the event handler to your add-in sldworks object:

             

            // Assume that swApp is already instanciated
            swApp.ActiveModelDocChangeNotify += this.MyMethod()
            
            public int MyMethod()
                    {
                        ModelDoc2 swModelDoc = (ModelDoc2)swApp.ActiveDoc;
                        if (swModelDoc != null)
                        {
                            return 0;
                        }
                        if (swModelDoc.GetType == (int)swDocumentTypes_e.swDocASSEMBLY)
                        {
                            // Do stuff here: enable the button
                            return 1;
                        }
                        else
                        {  // Do stuff here: disable the button
                            return 0;
                        }
                       
                    }
            

             

             

            and VOILA! SolidWorks executera cette méthode à chaque activation d'un nouveau modele!

              • Re: Event active doc is an assy
                Yannick Proulx

                Hi Guys,

                I tried to convert this c# code to Vb.net and I got a little problem.

                even if I declare the ActiveModelDocNotify, I'm not able to write the swapp.ActiveModelDocChangeNotify = me.Bom_Compare_button

                the intelisense doesn't give me the choose of ActiveModelDocChangeNotify. Any Idea why?

                 

                (another question, How did you had code snippet with line number like you did...it's way more clear like that?)

                 

                here is my conversion result:

                 

                    Public Delegate Function DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler() As System.Int32

                 

                    ' Public Properties

                    ReadOnly Property SwApp() As SldWorks.SldWorks

                        Get

                            Return iSwApp

                        End Get

                    End Property

                 

                'the problem is down here

                  Function SldWorks_ActiveModelDocChangeNotify() As Integer

                        SwApp.--------------------------------------------------------

                 

                    End Function

                 

                    Public Function Bom_Compare_button() As Integer

                        Dim swModelDoc As ModelDoc2 = DirectCast(SwApp.ActiveDoc, ModelDoc2)

                        If swModelDoc IsNot Nothing Then

                            Return 0

                        End If

                        If swModelDoc.[GetType] = CInt(swDocumentTypes_e.swDocASSEMBLY) Then

                            MsgBox("file is an assy")

                            Return 1

                        Else

                            MsgBox("file is NOT an assy")

                            Return 0

                        End If

                 

                 

                    End Function

                 

                 

                 

                Thanks again for your time

              • Re: Event active doc is an assy
                Yannick Proulx

                finally I still need your help

                 

                i'm not able to access my taskpane button to set the property enable = true

                I get the following message but the class SwTaskPaneHost is public so why i can't access it

                I try to access the button in SwTaskPaneHose from the SwAddin Module

                 

                once again, thank you for your time

                Yannick