0
0
Arm-architectureHow-ToBeginner · 4 min read

How to Use SolidWorks API: Syntax, Example, and Tips

To use the SolidWorks API, start by creating an instance of the SolidWorks application object in your programming environment, then access its methods and properties to automate tasks. Use COM interfaces in languages like VBA, C#, or VB.NET to interact with SolidWorks models and documents programmatically.
📐

Syntax

The basic syntax to start using the SolidWorks API involves creating an application object and accessing its interfaces. For example, in VBA or VB.NET, you use CreateObject("SldWorks.Application") to get the SolidWorks application instance. Then, you can call methods like OpenDoc or NewPart to manipulate documents.

  • CreateObject: Initializes SolidWorks application.
  • Application Object: Main entry point to API functions.
  • Methods: Actions like opening or creating files.
  • Properties: Access document or system info.
vb
Dim swApp As Object
Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True

Dim swModel As Object
Set swModel = swApp.NewPart()
Output
A new SolidWorks part document opens and SolidWorks window becomes visible.
💻

Example

This example shows how to open SolidWorks, create a new part, and add a simple sketch programmatically using VBA. It demonstrates basic API calls to automate model creation.

vb
Sub CreateSimplePart()
    Dim swApp As Object
    Dim swModel As Object
    Dim swSketchMgr As Object

    ' Start SolidWorks
    Set swApp = CreateObject("SldWorks.Application")
    swApp.Visible = True

    ' Create a new part document
    Set swModel = swApp.NewPart()

    ' Access the sketch manager
    Set swSketchMgr = swModel.SketchManager

    ' Insert a new sketch on the front plane
    swModel.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
    swSketchMgr.InsertSketch True

    ' Draw a circle with center at origin and radius 0.05 meters
    swSketchMgr.CreateCircle 0, 0, 0, 0.05, 0, 0

    ' Exit the sketch
    swSketchMgr.InsertSketch False

    MsgBox "Simple part with circle sketch created."
End Sub
Output
SolidWorks opens a new part with a circle sketch on the front plane and shows a message box: "Simple part with circle sketch created."
⚠️

Common Pitfalls

Common mistakes when using the SolidWorks API include:

  • Not setting swApp.Visible = True, so SolidWorks runs hidden and you cannot see changes.
  • Failing to select the correct plane or document before sketching, causing errors.
  • Not releasing COM objects properly, which can cause SolidWorks to stay open in the background.
  • Using incorrect method parameters or forgetting to end sketches.

Always check for errors after API calls and use proper object cleanup.

vb
'' Wrong: Not making SolidWorks visible
Dim swApp As Object
Set swApp = CreateObject("SldWorks.Application")

'' Right: Make SolidWorks visible
swApp.Visible = True
📊

Quick Reference

Here are some quick tips for using the SolidWorks API effectively:

  • Use CreateObject("SldWorks.Application") to start SolidWorks.
  • Set Visible = True to see the UI during automation.
  • Use NewPart(), OpenDoc() to create or open documents.
  • Access SketchManager to create sketches.
  • Always select the correct plane or face before sketching.
  • Release COM objects to avoid memory leaks.

Key Takeaways

Start by creating a SolidWorks application object with CreateObject("SldWorks.Application").
Make SolidWorks visible during automation to see your changes.
Use API methods like NewPart and SketchManager to create and edit models.
Always select the correct plane before sketching to avoid errors.
Release COM objects properly to prevent SolidWorks from hanging in the background.