Вы находитесь на странице: 1из 37

SolidWorks API


Leonard Kikstra
Designer / CAD Administrator
RITE-HITE Products Corporation
Milwaukee, WI

Lennys SolidWorks Resources

Who am I?

Product Designer SMART User Group

Engineering since 1982. Active member since
group started in 1999.
Cad user since 1991.
SolidWorks user since 1998.
Lennys SolidWorks
CAD Administrator Resources website.
Online Sept. 2003.
20+ people on site.
New home Sept. 2006.
Advise other sites.

Interested in simplifying my Programmed over 30

macros for use in
job. SolidWorks
Make my computer do some Most of these are available
of my work for me. for free on the Internet.
What is covered here.

General Information
What is the SolidWorks API?
Benefits of learning/using the API.

Getting Started.
Create and modify macros.
Finding your way.
When and Why.

Program examples

Where to get help.

What is the SolidWorks API?

Application Programming Interface.

Allows other programs to access SolidWorks data and/or control
Examples: SolidWorks Add-Ins
Cosmos, Toolbox, PhotoWorks, PDM Works, PDM Works Enterprise

Allow third party vendors to build applications that access
SolidWorks and SolidWorks data.
Lets SolidWorks focus on their core products.

SolidWorks Add-Ins have APIs.

eDrawings SolidWorks ToolBox
FeatureWorks SolidWorks Utilities
PDMWorks SolidWorks Routing
How can the API benefit you?

Automate common and repetitive tasks.

Increase productivity.
Save time and money.
Standardize procedures.

Consistently retrieve or export data that

already exists in the SolidWorks documents.

Develop useful tools to meet your specific


Customize SolidWorks.
Add commands, change settings, etc
Access the SolidWorks API

Stand alone application Executable

Visual C++ .Net Visual Basic .Net.
Visual C++ Visual Basic
Visual C
Programming environment outside of
Must be compiled to use outside of
programming environment.
Can start SolidWorks.
Required for creating Add-Ins/DLLs.

SolidWorks Macro .swp file

Microsoft Visual Basic for Applications.
SolidWorks Program (.swp) is a single file.
Must be launched from within SolidWorks.
Installed with SolidWorks.
No compiling
Some limitations.
SolidWorks Macros

The macro recorder.

Start recorder. Stop Record / Pause
Complete tasks.
Some settings in dialog boxes are not
Pause if needed.
Stop recorder and save new macro.

Running a macro
Run Macro
Select macro
Macro will automatically run Run
SolidWorks Macros

Edit macro.
Select macro. Edit Macro
Visual Basic Editor automatically launched.
Make changes.
Test Macro
F5 to run macro
F8 to step thru the macro line by line
Save macro.
Exit Visual Basic editor.

New Macro
Create a new macro without using macro recorder. New Macro
Enter macro name
Visual Basic Editor automatically launched.
Visual Basic Editor:
SolidWorks Objects.
Definitions of SolidWorks Objects.

Object based interface for user
Custom code programmed for
each object in form.

Modules that only contain code for
the macro.
Common modules used in many
Visual Basic Editor:
Access via Tools | References
menu selections in the VB

Attach objects from another

application to your project.
Standard descriptions of object
types, properties and methods.

Selections are specific to each


SolidWorks Type Libraries are

version specific.
Writing a macro:
Value retained only while in module or procedure where defined.
Value can change
Same variable name can be defined locally in multiple modules.

Value retained through out all modules and procedures.
Value can change
Can be used set fixed values for common reference later.
Pi = 4 * Atn(1) ' The infamous pi value

Assign a specific value to variable name.
Set once
CAN NOT be changed
CAN NOT use formula to define
Writing a macro:
Variable Types
Boolean - True or False

Integer - Whole numbers only

Long - Any numerical value

String - Any text based value

Variant - Can be any type

Array() - Spreadsheet of data

VariantArray() - Spreadsheet of data of any type.

Writing a macro:
Must name an object to access its properties
Meta-data - Info about object
Features - Sub-objects

Multiple tiers
Parent Child relationships

Command and syntax that provides access to objects properties

Roadmap that shows how to get to object.
Command and syntax how to get to object from parent.

Writing a macro:
Sub (Procedures)
Private - Can only access
from module it resides in
Public - Can be accesses
from any module
Run from top-down until
Can be access many times.
Can launch other routines.
Can pass variables between

Function Special Procedure

Must pass a variable to
Returns a value to variable in
the calling procedure.
Writing a macro:
Testing and Running
Testing macro or procedure
F5 Run macro
F8 Step thru code
Start in current procedure.

Automatic starting point

Last procedure, in last inserted module
Not module name specific.
Not procedure name specific.
Writing a macro:
Typical Steps - Launch
Launch Macro
Load type libraries
Set global variables and
Attach to SolidWorks

Is a document open in
No, warn user that you need
one, then end.

Get document type.

Wrong type, warn user and end

Writing a macro:
Typical Steps - User Form
User form
Set program defaults.
Read data from document.
Display user form to user.

Wait for input

React to user input.
Read more data from
Update user form.
Update document.
Action buttons
OK / Process
Writing a macro:
Typical Steps - Processing
Process user requests based on
form values.

Inform user of status during


Inform user of errors


Inform user of status upon

Wait for user OK.
Writing a macro:
Typical Steps - Completion
Close forms

Clear out objects and variables.

Close macro
Macro Feature:

Embed macro as a feature in the model.

Overview of Macro Feature

See SolidWorks API Help File
Assigning a macro to a
Toolbar Button.
Must have document open in

How To:
Rt. Click toolbar, Customize
Commands tab, Category: Macro
Drag icon and drop on toolbar.
Select an image to use on toolbar.
Fill out fields in dialog box:
Tooltip - Balloon tip at arrow.
Prompt - Help info in
status bar.
Macro - Name and full path.
Method - Module and procedure.
Shortcut - Key combo to
launch macro.
Click OK to complete
When should I write my
own macro?
Consistent retrieval & exporting of data.
Remove manual tasks and retyping errors.
Consistent results with consistent formatting.
Retrieve and export a BOM from the assembly
No drawing required.
Create a list of all components in a directory.

Automating common or repetitive tasks.

Save valuable time and money.
Exports SolidWorks documents to different file
Print all drawings in the current directory.
Print all drawings for all components in a
current assembly.
Retrieve and export a BOM for multiple
When should I write my
own macro?
Enforce compliance with standard procedures.
Provide an automated method to follow standards.
Consistent document appearance.
Preset SolidWorks settings to your company
Set annotations in document to use document
Copy document settings from master document to
current document.
Simplifies adding standardized notes to drawings.

Need a special tool to automate a specific task.

Justification to write a
Define your needs.
What is the purpose of macro?
How often will you use it?
The more you use it, the more time you
will save

Does it already exist?

Is there something available thats close
to what I need?
Can I modify something I already have?
Examples files:
Help file
SolidWorks Website
Lennys SolidWorks Resources
Justification to write a
Example #1 - New Macro - Document Export
Scope: * Export documents to different file types.
* Drawings, Models, Configurations
* 20 models to create 300 exported files

Problems via manual way - Manual process, Repetitive, Time

consuming, user input required at all steps!
Did I export all configurations?
Selecting file type by hand every time.
Consistent configuration naming.
Estimate time to complete: 12-14 Hours

Time comparison:
Time to write: 8 Hours
Automated: 1.5 Hours
Saved first use: 10-12 Hours

Macro can be reused many, many times.

Justification to write a
Example #1 Modify Macro Change custom property value.
Scope: * All documents require change, addition or
removal of a custom property.
* 1000s of files.
* Found macro on internet, can modify for use.

Problems via manual way - Manual process, Repetitive, Time

consuming, user input required at all steps!
Did I change all documents?
Did I make the right changes?
Estimate time to complete: 20+ Hours

Time comparison:
Time to modify macro: 15 Minutes
Automated: 2.5 Hours
Saved first use: 17+ Hours

Macro can be customized and reused many, many times.

Tips: Learning

Learn by example.
Find a good macro, and study how it works.
Use [F8] in VB Editor to step thru code line-by-line.

Write do nothing macros for Visual Basic or SolidWorks API
Learn functions you havent used before.
Develop your own functions or common routines.
Test What If scenarios.
Save your code for later use.

Start small and work your way up.

Write and test small portions of code before continuing to larger
Start with a basic program, then add more functionality and
flexibility later.
Tips: Sample Code

Add comments to your code

Helps you and others understand what you did, and why.
Helps you remember what you did.
Identify what routines are doing.

Use existing code

Dont re-invent the wheel.
Saves time and your effort.
Keep sample code on hand for later use.

Know that there are multiple ways to complete tasks.

Conditional statements
If Then, Else, End If - True/False
Select Case ., End Case - Response based on value.
For . Next - Set number of times to do.
Do While . End Loop - Dont know how many times.
Tips: Distribution

Think about other people who may be using your macros.

Used for other needs than initially intended.

User customization of existing macro.

Not ever user will know how to modify your macro.
External settings file for modifiable default settings.
Macro defaults if settings file is not found.

Different companies have standards, procedures and needs.

Capable of running on different versions of SolidWorks

API can check what version of SolidWorks is in use.
Tips: Getting Help

SolidWorks API Help File

Help forums.

SolidWorks API Support.

SolidWorks API functionality only.

Be specific!
Repeatable problem.
Steps leading up to problem
Specific conditions lead to problem
Identify specific conditions.
Identify specific problem you are experiencing.
Dont send complete macro. Provide simple code to demonstrate.

Dont expect help with programming techniques.

Resources for learning more:

Installed with SolidWorks

SolidWorks API
Application Programming Interface

SolidWorks API and Add-Ins help file

Explains object structure and command syntax.
Many ready to use examples.
Best help file I have seen for writing macros.

Visual Basic for Applications version 6

SolidWorks 2001Plus and later
Visual Basic for Applications Help File.

Online Tutorial - SolidWorks API

Record, Save, Edit a macro.
Resources for learning more?

SolidWorks web site:

API programming tips and updated help files.

SolidWorks Express Newsletter - API Examples

Download projects, sample programs and macros.

SolidWorks Developer Kit.

Not required
Contains working sample programs.


Local VAR / SolidWorks Reseller

SolidWorks API Training classes

Resources for learning more?
Other Websites - API
Lennys SolidWorks Resources Macros & Tips

Mike Wilsons SolidWorks Modeling Techniques Macro Feature


Matt Lombard Macros


Joe Jones at New Hampshire CAD Tutorials


Stefan Berlitzs SolidWorks Tools Macros

http://www.swtools.de website.

SolidWorks Tips & Things API Tips

Resources for learning more?
Visual Basic
Tutorial and Lessons
May be Visual Basic Specific.
Do not provide help with specific applications.

May be Visual Basic Specific.
Do not be able to provide help with specific applications.

Too many to list.

Some provide better explanation and examples.

SolidWorks API
Thank You!

Lennys SolidWorks Resources

has moved to