Helpful Tips and Information

—You can search for all macros containing a certain API call by typing that call name in the search box at the bottom of the page.

—Check out our free PDF, “VBA Debugging Tips for SolidWorks API Programmers” for help with run-time and compile errors you may encounter while programming.

—A public version () of a macro means that the VBA macro may be downloaded and run by any CADSharp.com member, but the source code is only available to premium members.

—Read our FAQ for more information about macros and the API.

Automation / Tools / Utilities

Keep VBA user form on top on 64 bit systems
Create 3D points from data in Excel or text file
Display selected component’s custom properties
Apply all dimension values to all configurations
Get name, owner, type, position, and value of all dimensions on a sheet
Involute spur gear creator
Equation-Triggered Macro Utility (ETMUtil)
Close all documents except drawings
Create configuration for each body
Save out sheets as individual drawings
Save out all part components with specified extension
Error logging using FileSystemObject
Get all SolidWorks models in a folder
Save all parts in a folder as parasolid
Change sketch dimensions via user form
Save Document Utility
Is body a 2D body (manufacturable using only two axes)
Toggle document’s unit system between MMGS and IPS

Assemblies

Delete all missing components
Rename assembly component from feature tree
Add mate to selected entities
Toggle component lightweight / suppressed state
Dissolve all sub-assemblies
Delete specific folder and its components
Save virtual components to external file
Isolate parent sub-assemblies of selected components
Get named entities in a component
Get mate info: mate type, owning component, faces used, faces’ owning feature
Get component mass properties
Get IFeature object from component
Dissolve all component library features in assembly
Assembly automation example – with transforms
Assembly automation example – without transforms
Delete part from sub-assembly
Change configuration of selected component
Edit the alignment of a mate
Get the feature information for a component
Insert components at a specific location using transforms
Demonstration of safe entities
Use mate references for mating automation
Run code on every part in assembly
Get assembly’s maximum depth
Create configurations at component level and reference them at assembly level
Insert a new part in a sub-assembly
Is component from design library
Add width mate
Get belt length and add to belt component’s custom properties

Custom properties

Move all configuration specific custom properties to document custom properties
Copy drawing custom properties to part or assembly
Copy part or assembly custom properties to drawing custom properties
Export model’s custom properties to Excel file
Get all custom property names and values
Demonstration of important custom properties methods
Change BOM quantity in custom properties

Drawings

Get components in selected drawing view
Delete a drawing sheet
Add callouts to all hole wizard holes in selected drawing view
Save selected table as Excel spreadsheet
Get drawing note bounding box
Change all layer colors
Get cell data in active sheet’s table
Search and replace title block notes
Delete all blocks from current sheet
Search for and hide components in drawing views
Flip visibility of all blocks in a drawing
Delete and re-add revision table
Delete balloons attached to fasteners
Move BOM to a different sheet
Get BOM table on specific drawing sheet
Insert top-level only BOM
Change the dimension colors in selected view
Insert broken view and position break lines
Explode the selected block
Get and set dimension text
Get types of display dimension
Get current sheet number
Get the edges used by a dimension
Get the text and custom property link of a note
Get polylines information
Get the position of a view
Get the names of all sheets and drawing views
Get a view’s orientation using transforms
Insert block at specific location
Rename all sheets
Select the edges in a drawing view
Insert hole table using selected vertex
Set the name of a note
IView::GetXform vs IView::GetViewXform
Get center mark’s owning edge
Find text in blocks made from notes
Change layer of selected annotations
Export all drawing sheets to a single PDF
Get selected bend line’s owning flat pattern feature
Get IEntity pointer for entity selected in drawing view
Relocate view to true center after extents are modified

Features

Accessing and releasing feature data
Modify filleted entities using adjacent edges
Modify filleted entities using pre-selection
Modify filleted entities using traversal
Modify fillet size
Get the parent sketches of a selected feature
Create a circular pattern feature
Create an extrude from a sketch
Demonstration of important IFeature members
FeatureManager tree traversal
Get the features used to create a feature
Determine whether feature is suppressed
Mirror part and save out result
Modify feature data of an extrusion
Modify feature data in a component
Create axes through all points normal to a plane

Macro features

Macro feature and PMP skeleton for creating a body
Macro Feature and PMP skeleton for modifying input body

Event notifications

Save component after dropped from task pane
Determine whether new or existing file was opened
Post-rebuild event notification example
Notification when SolidWorks is idle
Drawing sheet change event notification example
Rename solid bodies after rebuild
Cancel save if part wasn’t modified using notifications
Message user upon application exit using notifications
Notify user when a specific feature is created
Listen for mouse events
Kill listener class after event notification fires

Opening and saving documents

Display a SolidWorks-native Open dialog box
Open all SolidWorks parts in a folder
Open document and create new window
Traverse all open documents

Parts Only

Print values of all sketch dimensions
Keep largest body and delete the rest
Insert split feature
Create linear pattern in two directions
Color hole wizard feature after creation
Autosize all reference planes in active part
Get loft pick points
Get face that owns cosmetic thread
Get sketched outline / projection of part
Delete BOM table from part
Add, subtract, or combine two selected bodies
Get body’s owning feature
Traverse body features and make dimensions read-only
Get or set dimension value in a part while in drawing or assembly
Create configuration based on user input (Intro to API webinar)
Run geometry analysis
Create extruded base with user form (SolidWorks World 2012)
Add or find attributes on all faces
Delete global variable and all associated equations
Insert axes concentric to cylindrical faces of selected feature
Select all faces with same color as pre-selected face
Find dimension by name
Update configuration listed in external references of inserted part feature

Appearances and Materials

Change selected body’s material in all configurations
Modify all existing appearances in a part using IRenderMaterial
Remove all part appearances using IRenderMaterial
Remove appearance of selected object using IDisplayStateSetting
Set or remove selected object appearance using that object’s members
Set or remove material from a part or selected body
Set selected object’s color using IRenderMaterial
Set selected object’s appearance using IDisplayStateSetting
Set selected object’s .p2m appearance file using IRenderMaterial
Get material database XML
Get material of selected component
Working with display states

PropertyManager pages

Change face colors with PropertyManager page
Create a cylindrical extrude using PropertyManager page
Simple PropertyManager page example

Selection

Select component body feature using IModelDocExtension::SelectByID2
Sketch contour selection
Determine whether selected faces are parallel
Select an entity by its name
Select the loops adjacent to selected face using co-edges
Get hole wizard feature used by selected hole callout
Select a face using IModelDoc2::SelectByRay
Set selection color

Sketching

Transform sketch points from sketch to model coordinates
Get sketch entity name
Create acute angular dimension
Change driven / driving state of selected dimensions
Get the coordinates of all sketch points in a drawing view
Create points at a curve’s intersections
Get diameter of circle sketch segment used to create selected feature
Create a keyhole profile and then extrude cut
Delete sketch relations and fully define sketch from origin
Find a specified sketch point
Get the parent sketches of a selected feature
Get the number of entities in selected sketch
Get start and end points of sketch segment

Bounding Boxes

Get body bounding box using IBody2::GetBodyBox
Get weldment body bounding box
Get body bounding box using IFace2::GetTessTriangles
Get extreme length of body

Transforms

Align component to assembly origin and default planes
Align components’ origins and default planes
Get the normal data for a face
Get mate information for selected component
Get the normal value in relative to assembly
Rotate a component about an axis using ArrayData
Rotate a component about an axis using IDragOperator
Rotate a component using ArrayData – another example
Rotate component using SetData
Translate component using ArrayData

Traversing and locating objects

Get largest face in a body
Get longest edge in a body
Get longest sketch segment in a sketch
Get diameter of largest circular edge in body
Get distance between two largest planar faces in a body
Assign and find tracking IDs
Get the edges attached to a vertex
Get the length, start point, and end point of an edge
Test whether edges on a body are straight or curved
Mark and select fillets under a certain radius
Re-select entities using persistent IDs
Renaming entities
Select edges adjacent to selected edges use co-edges
Setting and getting faces by name
Setting and getting vertices by name
Traverse components – simple
Traverse components in order
Traverse features in order using recursion
Traverse notes and change their text
Traverse sketch segments while adding dimensions

Weldments and Sheet Metal

Export flat pattern as DXF
Disable automatic cut list updating in all weldment parts in an assembly
Get selected weldment body’s cut list feature
Toggle sheet metal bend notes
Change sheet metal feature k-factor
Toggle sheet metal flat pattern on and off
Export sheet metal flat pattern to specified location
Get weldment profile and cut list custom properties
Get weldment bodies
Delete all bodies except first body in cut list folder
Display custom properties for selected cut list folder

Other

Get SolidWorks version
Isolate selected body
Change system settings
IUserProgressBar demo
Suppress all folders in model with specified name
Get text from BOM table cells
Copy table to clipboard
Change table cell text color
Create columns of text with monospaced fonts
Replace referenced entity in reference plane
Read, add, delete, and modify equations
Create pack and go with specified custom property as prefix
Pack and go top level parts only
Pack and go to folder
Run macro from another macro
Run macro from Design Binder
Run individual SolidWorks commands
Merge temporary bodies
Find intersecting edges between two bodies
Move mouse cursor to selected point
Add and read attributes on document
Is body created at angle / off-plane
Return swSelectType_e name for selected object

VBA Language

Class example using collection
Class example using SolidWorks object
Collection example
Debug.assert example
Enumeration example
Error handling example
Function example
Implements example
Enable command button depending on combo and text box state
Public vs Dim declaration example
User-defined types example
Switch example