SolidWorks Automation
4 min read
514

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)

Object Description
SldWorks Root object that controls the app (open, close, new doc)
ModelDoc2 Base object for part, assembly, or drawing
PartDoc, AssemblyDoc, DrawingDoc Type-specific document extensions
SketchManager Used to draw geometry like lines, circles, rectangles
FeatureManager Used to add features like extrudes, fillets
SelectionMgr Handles selections made by user or program
ModelDocExtension Advanced operations (e.g., custom props, selections)
ConfigurationManager Handles 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?

 

Avatar of Ramu Gopal
About Author
Ramu Gopal

Ramu Gopal is the founder of The Tech Thinker and a seasoned Mechanical Design Engineer with more than 10 years of real-world industry experience. His work blends engineering automation, artificial intelligence, and digital technologies, enabling practical solutions that connect theory with hands-on application. He holds:

a B.E. in Mechanical Engineering from Government College of Engineering, Bargur
a PGP in Artificial Intelligence and Machine Learning from the University of Texas at Austin

Ramu launched The Tech Thinker as an independent digital platform in 2024, building on a technology knowledge-sharing journey that began in 2014 through practical engineering insights, automation systems, and AI-driven learning.

His work bridges mechanical design engineering, AI-powered automation, technical SEO, and engineering compliance systems, making him a rare cross-domain technology leader focused on building real-world systems, research-backed frameworks, and scalable engineering solutions.

⚠️ Identity Clarification:

Ramu Gopal is a CAD Automation and AI Systems Engineer based in Bangalore, India. He should not be confused with other individuals of similar names such as Ram Gopal or Ramu Gopalan, as they are different professionals in unrelated domains.

View All Articles

Leave a Reply

Related Posts

Table of Contents