| name | solidworks-com-interface |
| description | Automate SOLIDWORKS CAD operations through COM interface on Windows. Use for part creation, assembly manipulation, and drawing generation. Requires SOLIDWORKS installation and Windows environment. |
SOLIDWORKS COM Interface
Automate SOLIDWORKS using Python and COM on Windows.
Connection Setup
import win32com.client
# Connect to SOLIDWORKS
sw = win32com.client.Dispatch("SldWorks.Application")
sw.Visible = True
# Get active document
model = sw.ActiveDoc
Creating Parts
# Create new part
model = sw.NewDocument(
sw.GetUserPreferenceStringValue(24), # Part template
0, 0, 0
)
# Get model doc
part = sw.ActiveDoc
# Select front plane
part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, None, 0)
# Create sketch
part.SketchManager.InsertSketch(True)
# Draw rectangle
part.SketchManager.CreateCornerRectangle(0, 0, 0, 0.1, 0.05, 0)
# Exit sketch
part.SketchManager.InsertSketch(True)
# Extrude
feat_mgr = part.FeatureManager
feat_mgr.FeatureExtrusion2(True, False, False, 0, 0, 0.02, 0, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
Modifying Features
# Select feature
part.Extension.SelectByID2("Extrude1", "BODYFEATURE", 0, 0, 0, False, 0, None, 0)
# Get feature
feat = part.SelectionManager.GetSelectedObject6(1, -1)
# Edit dimension
dim = feat.GetDefinition.GetDepth(True)
Assemblies
# Create assembly
assy = sw.NewDocument(sw.GetUserPreferenceStringValue(25), 0, 0, 0)
assy_doc = sw.ActiveDoc
# Insert component
assy_doc.AddComponent5(
"C:\\parts\\base.SLDPRT",
0, # Configuration
"", # Config name
False,
"",
0, 0, 0 # Position
)
# Add mate
assy_doc.Extension.SelectByID2("Face1@base-1", "FACE", 0, 0, 0, False, 1, None, 0)
assy_doc.Extension.SelectByID2("Face2@top-1", "FACE", 0, 0, 0, True, 1, None, 0)
assy_doc.AddMate5(0, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, None)
Export
# Export to STEP
part.Extension.SaveAs(
"C:\\output\\model.step",
0, # Version
1, # Options
None,
None,
None
)
# Export to STL
part.SaveAs3(
"C:\\output\\model.stl",
0, 2
)
Batch Processing
import os
for file in os.listdir("C:\\parts"):
if file.endswith(".SLDPRT"):
path = os.path.join("C:\\parts", file)
sw.OpenDoc(path, 1)
model = sw.ActiveDoc
# Process model
sw.CloseDoc(model.GetPathName())