6 Replies Latest reply on Mar 15, 2019 7:10 AM by Marco Cruz

    Ask and wait to select a plane with macros

    Marco Cruz

      Hi.

       

      I have created many macros, but I am having some dificulty with a simple easy task.

       

      I want to create a sketch in user choosen plane.

       

      I know I could do it on a pre-defined plane, but I need to be able to choose it.

       

      This isnt enough:

       

      Set Part = swApp.ActiveDoc

      Part.SketchManager.InsertSketch True

      boolstatus = Part.Extension.SelectByID2("Right Plane", "PLANE", 0, 1, 1, False, 0, Nothing, 0)

      Part.ClearSelection2 True

       

      any ideas?

       

      Thanks!

        • Re: Ask and wait to select a plane with macros
          Rob Edwards

          Can the user pre-select the plane before they run the macro?

          If so use the selection manager, check it's a plane and you're good to go

          • Re: Ask and wait to select a plane with macros
            Josh Brady

            Please see:

             

            Beginner API - DASSAULT: SOLIDWORKS 3D Design - Eng-Tips

             

            User "handleman" posted an example of a quick-and-dirty way to prompt the user to select something and then wait for a selection.  As discussed in that thread, WithEvents is a better, more robust way to do it.  However, it's not nearly as straightforward.

            • Re: Ask and wait to select a plane with macros
              Simon Turner

              I would create a Property Manager Page with a selection box in it. Then SolidWorks will handle things like only allowing to select certain types of object.

              • Re: Ask and wait to select a plane with macros
                Marco Cruz

                Hi.

                 

                Thank you all for the replies.

                 

                I have sucessfully made the script I wanted.

                 

                Here are the steps the script makes:

                1) create new part

                2) ask to select a plane, and start a sketch on it

                3) "project" planes to sketch

                 

                Thanks!

                 

                 

                EDIT: Macro on next post

                  • Re: Ask and wait to select a plane with macros
                    Marco Cruz

                    UPDATE:

                     

                    I need to show planes fot easier user selection.

                     

                    I have following code, but it hides planes on selection. Any way to avoid this?

                     

                    EDIT: Fixed. Complete Macro:

                     

                    Sub main()

                        On Error Resume Next

                        Dim swApp               As Object

                        Dim swModel             As SldWorks.ModelDoc2 'As Object

                        'Dim Part                As Object

                        Dim SketchLine          As Object

                        Dim SelMgr              As SldWorks.SelectionMgr 'As Object

                        Dim nContinue           As Integer

                        Dim SelType             As SwConst.swSelectType_e

                        Dim sMsg                As String

                        Dim i                   As Long

                        Dim swDoc               As SldWorks.ModelDoc2

                        Dim boolstatus          As Boolean

                         

                        Set swApp = Application.SldWorks

                         

                        'CREATE NEW FILE

                        Set swModel = swApp.NewDocument("T:\3 - CONFIG\5 - SOLIDWORKS\1 - TEMPLATES\Part.prtdot", 0, 0, 0)

                         

                        'SHOW PLANES

                        boolstatus = swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        swModel.UnBlankRefGeom

                        boolstatus = swModel.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        swModel.UnBlankRefGeom

                        boolstatus = swModel.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        swModel.UnBlankRefGeom

                        swModel.ClearSelection2 True

                         

                        'CREATE SKETCH ON SELECTED PLANE

                        Set SelMgr = swModel.SelectionManager

                        MsgBox "Please select a plane..."

                        While SelMgr.GetSelectedObjectType3(1, -1) <> swSelectType_e.swSelDATUMPLANES

                            swModel.ClearSelection2 True

                            While SelMgr.GetSelectedObjectCount < 1

                                DoEvents

                            Wend

                        Wend

                         

                        'CREATE SKETCH

                        swModel.SketchManager.InsertSketch True

                     

                     

                        'CREATE CENTER LINES

                        Set SketchLine = swModel.CreateLine2(0#, 1, 0#, 0#, -1, 0#)

                        SketchLine.ConstructionGeometry = True

                        SketchLine.MakeInfinite

                        Set SketchLine = swModel.CreateLine2(1, 0#, 0#, -1, 0#, 0#)

                        SketchLine.ConstructionGeometry = True

                        SketchLine.MakeInfinite

                         

                        'SHOW PLANES

                        boolstatus = swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        swModel.BlankRefGeom

                        boolstatus = swModel.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        swModel.BlankRefGeom

                        boolstatus = swModel.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

                        swModel.BlankRefGeom

                        swModel.ClearSelection2 True

                    End Sub