Macro Library

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 member, but the source code is only available to premium members.

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

Automation / Tools

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
Copy table to clipboard
Involute spur gear creator
Equation-Triggered Macro Utility (ETMUtil)

Assemblies

Dissolve all sub-assemblies
Delete specific folder and its components
Save virtual components to external file
Changed selected component’s color
Isolate parent sub-assemblies of selected components
Get named entities in a component
Get the faces used by mates
Get a component’s mass properties
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
Remove component appearances at assembly level in 2011 and earlier
Remove component appearances at assembly level in 2012 and later

Custom properties

Add custom properties to every component
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
Get material custom properties
Export model’s custom properties to Excel file
Get all custom property names and values
Demonstration of important custom properties methods
Add part appearance custom property
Change BOM quantity in custom properties

Drawings

Save out sheets as individual drawings
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
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

Features

Accessing and releasing data
Modify filleted entities using adjacent edges
Modify filleted entities using pre-selection
Modify filleted entities using traversal
Modify fillet size
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
Get weldment profile and cut list custom properties
Change feature appearance
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
Notification when SolidWorks is idle
Rename solid bodies using OnIdleNotify
Rename solid bodies with RegenPostNotify
Cancel save if part wasn’t modified using notifications
Message user upon application exit using notifications
Notify user when a specific feature is created

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

Other / Parts Only

Create pack and go with specified custom property as prefix
Save all parts in a folder as parasolid
Change part appearance file
Delete BOM table from part
Traverse body features and make dimensions read-only
Get or set dimension value in a part while in drawing or assembly
Change sheet metal feature k-factor
Toggle sheet metal flat pattern on and off
Toggle sheet metal bend notes
Remove appearances from all parts in a folder
Run macro from another macro
Run macro from Design Binder
Create configuration based on user input (Intro to API webinar)
Run individual SolidWorks commands
Run geometry analysis
Export sheet metal flat pattern to specified location
Merge temporary bodies
Get the minimum bounding box of a model
Find intersecting edges between two bodies
Create extruded base with user form (SolidWorks World 2012)
Move mouse cursor to selected point
Add and read attributes on document
Add or find attributes on all faces
Change color of selected faces

PropertyManager pages

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

Selection

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

Sketching

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

System and document settings

Change system settings

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 about axis using CreateTransformRotateAxis
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

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
Locate blue faces in a part
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 and features using recursion
Traverse notes and change their text
Traverse sketch segments while adding dimensions

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
Types example

Lessons

Lesson 1.2 homework solution - variables
Lesson 1.3 homework solution
Lesson 1.3 code - arrays
Lesson 1.4 homework solution
Lesson 1.4 – select case code
Lesson 1.5 – for – next loop code
Lesson 1.5 – loop examples code
Lesson 1.5 homework solution
Lesson 1.6 – data conversion functions
Lesson 1.6 – date and time functions
Lesson 1.6 – InputBox function
Lesson 1.6 – MsgBox function
Lesson 1.6 – replace function
Lesson 1.6 – split function
Lesson 1.6 – string functions
Lesson 1.6 homework part A solution - InputBox
Lesson 1.6 homework part B solution - string functions
Lesson 1.6 homework part C solution - string and split functions
Lesson 1.7 – ByVal vs Byref
Lesson 1.7 code - modularizing code with functions, sub procedures, modules, and classes
Lesson 1.7 homework part A solution
Lesson 1.7 homework part B solution
Lesson 1.7 homework part C solution
Lesson 1.7 code - modularizing code
Lesson 1.8 code - user forms and controls
Lesson 1.8 homework solution - user forms and controls
Lesson 1.9 code - debugging and error handling
Lesson 1.9 homework solution - debugging and error handling
Lesson 1.10 code - working with files (batch operations, copying, moving, and deleting files and folders)
Lesson 1.10 homework solution - working with files
Lesson 2.2 code - creating a macro from scratch
Lesson 2.2 homework part A solution
Lesson 2.2 homework part B solution
Lesson 2.3 – API object model
Lesson 2.3 homework part A solution
Lesson 2.3 homework part B solution
Lesson 3.1 code – opening and saving documents
Lesson 3.1 homework part A solution
Lesson 3.1 homework part B solution
Lesson 3.1 homework part C solution
Lesson 3.2 code - configurations
Lesson 3.2 homework part A solution
Lesson 3.2 homework part B solution
Lesson 3.3 code – with error handling - custom properties
Lesson 3.3 code - custom properties
Lesson 3.3 homework solution

Lesson 3.4 select methods code
Lesson 3.4 SelectionManager code
Lesson 3.4 homework part A solution
Lesson 3.4 homework part B solution
Lesson 3.5 code - system and document settings
Lesson 3.5 homework solution

Lesson 4.1 sketch creation code
Lesson 4.1 fully-define sketch code
Lesson 4.2a code - feature creation
Lesson 4.2b code - feature modification
Lesson 4.2c – create plate with traversal - feature traversal
Lesson 4.2c – modify features with traversal
Lesson 4.2c code - feature traversal
Lesson 4.3 find largest face code
Lesson 4.3 selected edge length code
Lesson 5.1 – drawer - assembly automation
Lesson 5.1 – crank - assembly automation, edit mate alignment
Lesson 5.2 code - assembly automation, bounding box
Lesson 5.3 – align component origins
Lesson 5.3 – align pin origin to specific point on block
Lesson 5.3 – align specific pt on pin to specific pt on block
Lesson 5.3 – Rotate using CreateTransformRotateAxis
Lesson 5.3 – modify transform using ArrayData
Lesson 6.1 code - create drawing (sheets, views, annotations, bill of materials)
Lesson 6.2 – advanced drawings - create additional views and dimensions
Lesson 6.2 – find faces
Lesson 6.3 code - batch operations on drawings
Lesson 7.1 code - working with Excel
Lesson 7.2 – cancel save using S_FALSE - notifications
Lesson 7.2 – alternative to DoEvents - notifications
Lesson 7.2 – file open notification
Lesson 7.3 – create spike
Lesson 7.4 code
Lesson 7.4 – create spike
Lesson 7.5 code – Part A
Lesson 7.5 code – Part B
Lesson 7.5 code – Part C
Lesson 7.5 code – Part D
Lesson 7.5 code – Part E
Lesson 7.5 code – Part F
Lesson 7.5 code – Part G
Lesson 7.5 code – Part H
Lesson 7.5 code – Part I (spike creator)
Lesson 7.5 – Update custom properties with macro feature