- 'precondition: Adjust line 11 below to point to the default part document of your version of SolidWorks
- Dim swApp As Object
- Dim part As Object
- Dim boolstatus As Boolean
- Sub main()
- Set swApp = Application.SldWorks
- Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\Part.prtdot", 0, 0, 0) 'This line opens up SolidWorks default part document. If you dont have SW 2017, or you've installed SolidWorks to a different location, this line will need to be changed.
- 'Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2016\templates\Part.prtdot", 0, 0, 0)
- 'Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2013\templates\Part.prtdot", 0, 0, 0)
- Set part = swApp.ActiveDoc
- boolstatus = part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
- part.SketchManager.InsertSketch True
- part.SetPickMode
- part.ClearSelection2 True
- End Sub
This macro will create a new part document, select the Top Plane, then insert a sketch. You'll have to point this macro to
the default part document file. If you are using Solidworks 2017 you may not have to adjust the code at all. But if you have any other version of Solidworks, or
you have Solidworks installed in a different directory than I do, you'll need to change line 11 of the macro code.
If you want this macro to create a sketch on the Front or Right plane simply change line 15 in the code. Change the word Top in "Top Plane" to Front or Right.
See also Create Sketch for a macro which requires you to
have a part document open already. For more simple macro examples check out the DeleteRelations
and DeleteRelations2 macros. They demonstrate how to remove sketch constraints
from inside and outside a sketch respectively.
Return to the SolidWorks VBA Macros page.