4 Replies Latest reply on Dec 31, 2017 11:43 PM by Deepak Gupta

    Insert a sketch for user to edit from event handler

    Rob Edwards

      Hi Forum

       

      I'm using the holidays to invest in a bit of API study, it's great! -  no ones bothering me

      I'm currently looking at using events

       

      Specifically in this case I am trying to create a new sketch when the user adds a configuration.

      I would then like to pass control back to the user so they can create the sketch

      I will then pick up the new sketch has been created and do some more stuff

       

      At first it appears to work fine, however there is something wrong.

      The sketch entities are not displayed and the sketch is empty - eg a rectangle

      It gets added to the tree, but will be deleted if it is again edited

       

      Here's my debug.print that shows somethings working

      swNotifyConfiguration       A

      swNotifyFeature             Sketch3-A     ProfileFeature

      swNotifyConfiguration       B

      swNotifyFeature             Sketch3-B     ProfileFeature

      swNotifyConfiguration       C

      swNotifyFeature             Sketch3-C     ProfileFeature

       

      and heres the code,,, macro attached.  I use 2016

      The macro selects a face called "Blank Face" , so I also attach a simple sample file

       

      'Part_Events_Study1
      Option Explicit
      Public swapp As SldWorks.SldWorks
      Dim swPartEvents As Class1
      
      Sub main()
          Set swapp = Application.SldWorks
          Set swPartEvents = New Class1
          swPartEvents.Init
      End Sub
      

       

      'Class1
      Option Explicit
      
      Public WithEvents swPart As SldWorks.PartDoc
      
      Public Sub Init()
          Set swPart = swapp.ActiveDoc
      End Sub
      
      Public Function swPart_AddItemNotify(ByVal EntityType As Long, ByVal itemName As String) As Long
          
           Select Case EntityType
                  Case swNotifyConfiguration
                      With swPart
                          Debug.Print "swNotifyConfiguration", itemName
                          
                          Dim swFace As SldWorks.Face2
                          Set swFace = .GetEntityByName("Blank Face", swSelFACES)
                          swFace.Select4 False, Nothing
                          
                          '.FeatureManager.EditRollback swMoveRollbackBarToAfterFeature, _
                                                        "Folder1"
                          With .SketchManager
                              .InsertSketch (True)
                              .ActiveSketch.Name = .ActiveSketch.Name & "-" & itemName
                          End With
                      End With
                  
                  Case swNotifyFeature
                      Debug.Print "swNotifyFeature", itemName, _
                                  swPart.Extension.GetLastFeatureAdded().GetTypeName2
                      'TODO catch when sketch is added
                      '.....
           End Select
          
      End Function
      
      Public Function swPart_DestroyNotify2(ByVal DestroyType As Long) As Long
           Set swPart = Nothing
      End Function
      
        • Re: Insert a sketch for user to edit from event handler
          Rob Edwards

          I've managed to get it working   I guessed that it was best not to do anything from within the part event notify directly, so I set up an OnIdleNotify event and called my sub from there.  Happy days for now

          • Re: Insert a sketch for user to edit from event handler
            Deepak Gupta

            Rob, not sure on that mystery but may be you can add a dummy sketch entity and later edit that.

             

            With .SketchManager

                                    .InsertSketch (True)

                                    .CreateCircle 0#, 0#, 0#, 0.003857, -0.009744, 0#

                                    .ActiveSketch.Name = .ActiveSketch.Name & "-" & itemName

                                    .InsertSketch (True)

            End With

            And for this issue, the sketch gets deleted since there is no entity in it.

            It gets added to the tree, but will be deleted if it is again edited

              • Re: Insert a sketch for user to edit from event handler
                Rob Edwards

                Deepak Thanks for looking

                but may be you can add a dummy sketch entity and later edit that

                That's the exact approach I took when I started this macro   I went as far as to add all the features I needed and then opened the sketch for editing, even deleting the circle before I fell out of the macro.

                 

                It worked well for the sketch, but I had a surface trim feature that always failed as a result.  This led me down the route of wanting interaction throughout.

                I'm just messing about really and seeing whats possible - it's certainly testing what brain cells I have left

                 

                And for this issue, the sketch gets deleted since there is no entity in it

                 

                 

                 

                Yes, I was surprised it got added at all, it was quite happy just sitting there.  I guess there were sketch entities in there, but somehow they were transient.

                 

                I have quite a bit of work to do for sure, but I'm happy that I have an approach that looks workable.  The last piece of the jigsaw that Im not sure about is how do I go about attaching this macro to the part.  I presume I will have to insert it as a macro feature or is there another way?

                 

                 

                 

                  • Re: Insert a sketch for user to edit from event handler
                    Deepak Gupta

                    Rob Edwards wrote:

                     

                    The last piece of the jigsaw that I'm not sure about is how do I go about attaching this macro to the part. I presume I will have to insert it as a macro feature or is there another way?

                    Macro feature would be idle but I would suggest to simply keep it running in background (you can run it with SOLIDWORKS automatically) as I do not have good experience with macro features.