SolidWorks Automation
4 min read
20

Overview of the SolidWorks VBA Macro Object Library & Model

May 31, 2025
0
Overview of the SolidWorks VBA Macro Object Library & Model

SolidWorks VBA Macro Object Library

When you write a macro in VBA for SolidWorks, you’re essentially telling the software,
“Hey SolidWorks, here’s what I want you to do.”
To make that possible, SolidWorks exposes a huge set of objects, methods, and properties called the API (Application Programming Interface) — and this is what you access via VBA.

Let’s break down how the SolidWorks Object Model works in VBA, and what objects you’ll use 90% of the time.


🧩 What is the SolidWorks VBA Object Library?

The SolidWorks VBA Object Library is a collection of predefined objects like:

  • Application (SldWorks)

  • Documents (ModelDoc2, PartDoc, AssemblyDoc, DrawingDoc)

  • Sketches (SketchManager, SketchSegment)

  • Features (FeatureManager, Feature)

  • Selections (SelectionMgr)

  • Views & Sheets

  • Export, Config, Dimension Objects

  • …and many more

You declare these objects in your VBA macro and then call methods/functions on them to control SolidWorks.


📘 Core Object Hierarchy (Object Model)

Here’s a simplified object hierarchy of a SolidWorks macro:

SldWorks (application)
│
├── ModelDoc2 (active document - part/asm/drw)
│   ├── SketchManager (to create sketches)
│   ├── FeatureManager (to add features)
│   ├── Extension (advanced functions: selection, property, etc.)
│   └── SelectionMgr (user selection context)
│
└── Documents (collection of open docs)

This means:

  1. You start with SldWorks to connect to the application.

  2. From there, you get the active document using ActiveDoc.

  3. Then you dive deeper—like opening a sketch, drawing a circle, and making a feature.


🔑 Most Common Objects (with Purpose)

ObjectDescription
SldWorksRoot object that controls the app (open, close, new doc)
ModelDoc2Base object for part, assembly, or drawing
PartDoc, AssemblyDoc, DrawingDocType-specific document extensions
SketchManagerUsed to draw geometry like lines, circles, rectangles
FeatureManagerUsed to add features like extrudes, fillets
SelectionMgrHandles selections made by user or program
ModelDocExtensionAdvanced operations (e.g., custom props, selections)
ConfigurationManagerHandles configurations of the model

🔁 Common Object Patterns You’ll Use Often

These are reusable code patterns that appear in almost every macro:

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSketchMgr = swModel.SketchManager
Set swFeatMgr = swModel.FeatureManager
Set swSelMgr = swModel.SelectionManager
Set swExt = swModel.Extension

Each line sets up a different layer of control:

  • swApp connects you to SolidWorks

  • swModel gives access to the active file

  • swSketchMgr lets you draw

  • <strong>swFeatMgr</strong> lets you create extrudes, fillets, etc.

  • <strong>swSelMgr</strong> helps you pick or filter selections

  • swExt gives you access to advanced tools like renaming features, custom properties, and saving files

📌 Additional Object Layer Tips

  • ModelDoc2 is your base: You can typecast it to PartDoc, AssemblyDoc, or DrawingDoc as needed.

  • Use .Extension for extras: Custom properties, feature renaming, and advanced selections.

  • Explore object methods: For example, swModel.GetTitle or swSketchMgr.CreateLine(...)

🧠 You’re now using:

  • SketchManager to draw

  • FeatureManager to extrude

  • ModelDocExtension to select planes


Set swApp = Application.SldWorks
Set swModel = swApp.NewDocument("C:\Path\Part.prtdot", 0, 0, 0)
swModel.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0

Set swSketchMgr = swModel.SketchManager
swSketchMgr.InsertSketch True
swSketchMgr.CreateCircleByRadius 0, 0, 0, 0.05
swSketchMgr.InsertSketch True

Set swFeatMgr = swModel.FeatureManager
swFeatMgr.FeatureExtrusion2 True, False, False, 0, 0, 0.1, 0, False, False, False, False, _
    0, 0, False, False, False, False, True, True, True, 0, 0, False

🧪 Example: Real Macro Showing Object Flow

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swSketchMgr As SketchManager
Dim swFeatMgr As FeatureManager

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    ' Access sketch and feature managers
    Set swSketchMgr = swModel.SketchManager
    Set swFeatMgr = swModel.FeatureManager

    MsgBox "Using VBA Object Model Successfully!"
End Sub

This small macro:

  • Connects to SolidWorks

  • Accesses the open document

  • Loads the two most common managers: sketch and feature


🧠Tips

  • Use swApp.ActiveDoc to always get the currently open model.

  • Always check if the doc is Nothing to avoid errors.

  • Use ObjectBrowser (F2 in VBA Editor) to explore all SolidWorks API objects.

  • Reference the SOLIDWORKS XX Type Library under Tools → References.


📚 Learn More:

Read Also:

Getting Started with SolidWorks VBA Macro: #1 Best Guide

Top 5 Reasons to Use VBA in SolidWorks for Smarter CAD Automation

How to Cut-Extrude from Sketch in SolidWorks using VBA macro?

 

Leave a Reply

Related Posts

Table of Contents