Learn SOLIDWORKS-API with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Create a New Part Document
Dim swApp As SldWorks.SldWorks
Dim swPart As ModelDoc2
Set swApp = Application.SldWorks
Set swPart = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2023\templates\Part.prtdot", 0, 0, 0)
Opens a new part document in SolidWorks via API.
2
Add a Sketch on Front Plane
Dim swSketchMgr As SketchManager
Set swSketchMgr = swPart.SketchManager
swPart.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
swSketchMgr.InsertSketch True
Creates a new sketch on the front plane.
3
Draw a Line in Sketch
Dim line As SketchSegment
Set line = swSketchMgr.CreateLine(0, 0, 0, 0.1, 0.1, 0)
Draws a line between two points in the active sketch.
4
Draw a Circle in Sketch
Dim circle As SketchSegment
Set circle = swSketchMgr.CreateCircleByRadius(0, 0, 0, 0.05)
Creates a circle at a specified center with a given radius.
5
Extrude a Sketch
swPart.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.1, 0, False, False, False, False, 0, 0, False, False, False, False, True, False, False
Extrudes the selected sketch to create a 3D solid.
6
Create a Rectangle Sketch
Dim p1, p2, p3, p4 As SketchSegment
Set p1 = swSketchMgr.CreateLine(0, 0, 0, 0.1, 0, 0)
Set p2 = swSketchMgr.CreateLine(0.1, 0, 0, 0.1, 0.05, 0)
Set p3 = swSketchMgr.CreateLine(0.1, 0.05, 0, 0, 0.05, 0)
Set p4 = swSketchMgr.CreateLine(0, 0.05, 0, 0, 0, 0)
Draws a rectangle using four lines in a sketch.
7
Apply Fillet to Edge
swPart.Extension.SelectByID2 "Edge1", "EDGE", 0, 0, 0, False, 0, Nothing, 0
swPart.FeatureManager.InsertFeatureFillet 5, 0.01, 0, 0, 0, 0, 0, 0
Applies a fillet to a selected edge in a part.
8
Add Material to Part
swPart.SetMaterialPropertyName2 "Default", "Steel"
Assigns a material to the active part document.
9
Create a Circular Pattern
swPart.Extension.SelectByID2 "Boss-Extrude1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0
swPart.Extension.SelectByID2 "Axis1", "AXIS", 0, 0, 0, True, 0, Nothing, 0
swPart.FeatureManager.FeatureCircularPattern3 6, 360, False, True, True, True, False, 0, 0, False
Creates a circular pattern of a feature around an axis.
10
Save Part Document
swPart.SaveAs "C:\Users\Public\Documents\SolidWorks\part1.sldprt"
Saves the active part document to a specified path.