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

Assemblies

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 the faces used by mates
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

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

Save selected table as Excel spreadsheet
Get drawing note bounding box
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
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

Keep largest body and delete the rest
Rename solid bodies after rebuild
Get part body bounding box
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

Appearances and Materials

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

PropertyManager pages

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

Selection

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

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

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 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
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 features in order using recursion
Traverse notes and change their text
Traverse sketch segments while adding dimensions

Weldments and Sheet Metal

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

Other

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
Sketch the minimum bounding box of a model
Find intersecting edges between two bodies
Move mouse cursor to selected point
Add and read attributes on document

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

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