8 Replies Latest reply on Apr 19, 2016 7:35 AM by Marc-Xavier Bilodeau

    Solidworks dimension mode - Event Listener?

    Marc-Xavier Bilodeau

      This is my original post: Solidworks dimension mode

       

      So I have this issue about some view being in Projected mode instead of True without my to notice it.

       

      I found this interresting Add-in Drawing View Tattler addin  but unfortunately it seems it won't run a SolidWorks 2015.

       

      But then I thought that maybe there could but some Event Listener function that would do the trick. Then I'll just make it run on the opening of SolidWorks and this could work foe me.

       

      Anybody can help?

       

      Thanks

        • Re: Solidworks dimension mode - Event Listener?
          Mark Greenwell

          Hi Marcx,

           

          I assume you know why you can either have projected or True view dimensions.

           

          As like you I would use projected views 99% of the time before I create a drawing I do the following.

           

          Select the face that you know is flat / straight and press Ctrl+8 save the part open up drawing and create view.

          I tend to use the view listed as 'Current' and have projected views from this.

           

          To check click on the 1st view and look at left of screen under dimension type there is a box showing if the view is projected or true.

           

          View on a skew is always True

           

           

          Select a face and press Ctrl+8

           

           

          After pressing Ctrl+8

           

          When selecting the current view in the drawing the drawing view is now Projected

           

          Hope this helps

           

          Thanks

           

          Mark (SolidWorks 2016sp2)

            • Re: Solidworks dimension mode - Event Listener?
              Marc-Xavier Bilodeau

              Thanks Mark for the reply.

               

              The problem is that even if I put on a straight view, it does (sometime) ends up being automatically un True mode.

               

              It seems it happends when the body UCS doesn't fit the part, here's an exemple

               

               

              I'm not sure how people build their par, I think it's made on place from a large assembly.

               

              Anyway we receive CAD on .STEP format so we can't do anything about it.

                • Re: Solidworks dimension mode - Event Listener?
                  Amen Allah Jlili

                  You should be looking at this delegate (Event Listener) : DDrawingDocEvents_ViewCreatePreNotifyEventHandler Delegate (SolidWorks.Interop.sldworks)
                  This notifies you when a new view is about to be created.  The subscription to this delegate should use Sheet.GetViews method to get a list of views, presumably the last element of the array should be the last (or the first?) created view. You now have the reference to that view and you can do what you like with it. The drawback of this approach is that the event listener is fired before the view gets created so technically-speaking, the subscription has to wait for the view to be created for the Sheets.GetViews to actually return the last created viewed when you call it. So you might want to make the thread sleep for sec? (which is not quite reliable)
                  This might be interesting too: DDrawingDocEvents_AddItemNotifyEventHandler Delegate (SolidWorks.Interop.sldworks)

                    • Re: Solidworks dimension mode - Event Listener?
                      Marc-Xavier Bilodeau

                      Hi Amen,

                       

                      I also think the Event Listener is my best option, unfortunately, if my understanding of macro isn't quiet good, my understanding of Events is even worst!

                      When I first try to use Event Listener, I found this video tutorial from Javelin: Event Listeners - YouTube

                       

                      Now I'm proud to say that I can make a message box on a new document creation...

                       

                      Seriously, I know there are 2 ways of making a good use of Event Listener, either use it directly, or use it to lauch a macro, right?

                      I might be wrond, but I don't think Event Listener and Macro use the same coding, are they?

                       

                      For some reason I had this wrote down also, the DDrawingDocEvents thing, but I was never able to use it. I don't even know if this is part a the event or a macro...

                       

                      If you could just tell me where I should use your function, that would help me a lot! Thanks you.

                       

                      ---

                       

                      Here are the code from the video:

                       

                      Modules

                           Event_listeners1

                                Option Explicit

                                Dim eventListener As classEvents

                                Sub main()

                       

                                Set eventListener = New classEvents

                                End Sub

                       

                      Class Modules

                           classEvents

                                Option Explicit

                                Private WithEvents swApp As SldWorks.SldWorks

                                Private Sub Class_Initialize()

                                Set swApp = Application.SldWorks

                                End Sub

                       

                                Private Function swApp_FileNewNotify2(ByVal NewDoc As Object, ByVal DocType As Long, ByVal TemplateName As String) As Long

                                swApp.SendMsgToUser "New document as been created"

                                End Function

                • Re: Solidworks dimension mode - Event Listener?
                  Amen Allah Jlili

                  Alright, I think I've figured a way to do this:

                   

                  First of all, we add a module in which we define a new class based on the IDrawingDoc interface that handles event

                   

                  Dim swDrawingDoc As swDrawingDocWithEvents
                  Sub Main()
                  Set swDrawingDoc = New swDrawingDocWithEvents
                  End Sub
                  

                  Then we add the said class module and we name it to swDrawingDocWithEvents. Here below is the class implementation:

                  Private WithEvents swDrawingDocWithEvents As SldWorks.DrawingDoc
                  Private Sub Class_Initialize()
                  Dim swApp As SldWorks.SldWorks
                  Set swApp = Application.SldWorks
                  Dim swModelDoc As ModelDoc2
                  Set swModelDoc = swApp.ActiveDoc
                  ' 3 is the enum for drawing doc
                  If swModelDoc.GetType = 3 Then
                  Set swDrawingDocWithEvents = swModelDoc
                  Else
                  MsgBox ("Unable to instanciat class because doc type is not drawing.")
                  End If
                  End Sub
                  Private Function swDrawingDocWithEvents_AddItemNotify(ByVal EntityType As Long, ByVal itemName As String) As Long
                  ' 6 is the entity type for drawing vies
                  If EntityType = 6 Then
                  Dim swSheet As Sheet
                  Set swSheet = swDrawingDocWithEvents.GetCurrentSheet
                  Dim Views As Variant
                  Views = swSheet.GetViews
                  If Not IsEmpty(Views) Then
                  Dim LastAddedView As View
                  Set LastAddedView = Views(UBound(Views))
                  ''''''''''''''''''''''''''''''''
                  ' Do what you like with view here
                  ' for example, i've changed the display state to 2 which removes hidden lines
                  ' a message box will pop up with the name of the drawing view
                  LastAddedView.SetDisplayMode (2)
                  MsgBox("Last added view's name is : " & LastAddedView.Name)
                  ''''''''''''''''''''''''''''''''
                  End If
                  End If
                  End Function
                  

                   

                  Now, just like in the Javelin tutorial, go to the module and press F5.

                  swDrawingDocWithEvents_AddItemNotify is a subscriber to the event that fired whenever a new item is added to the feature manager.
                  Attached to this post is the swp file.

                   

                  Why would I not recommend  this approach: This is a functional solution but I'm not sure  (I can still be wrong) that it will withstand the effect of time. If you're planning to use this while you do your SolidWorks design, I recommend you develop an add-in which is more stable performance wise.

                   


                  Hope this helps Marc!