Download presentation
1
SolidWorks API Demystified
Leonard Kikstra Designer / CAD Administrator RITE-HITE Products Corporation Milwaukee, WI Lenny’s SolidWorks Resources
2
Who am I? Product Designer CAD & PDM Administrator
Engineering since 1982. CAD user/operator since 1991. SolidWorks user since 1998. CAD & PDM Administrator 20+ people on site. Advise other sites. I want to simplifying my job Make my computer do some of my work for me. SMART User Group SolidWorks user group Milwaukee, WI Active member since 1999. Lenny’s SolidWorks Resources Website online Sept New home Sept Programmed over 30 macros for use in SolidWorks Most of these are available for free on the Internet.
3
What is (un)covered here?
General Information What’s a program? Objects? Macro? API? What is the SolidWorks API? Benefits of learning/using the API. Getting Started Create and modify macros. Variables & Constants. Finding your way. When and Why. Program examples Justification. Where to learn more
4
What is a program? List of instructions.
Completed in the order that they appear in program. Instructions tell computer what to do and how to do it. Display status and query user. Change what to do based on changing conditions. Defines what to do if a problem occurs.
5
What is Object Oriented Programming?
Collection of objects used to define a program or data set. Intelligent: Contain data that you can get access to. Programs: Each object is capable of receiving messages, processing data, and sending messages to other objects. Portion of code is executed when object is accessed. Objects can be controlled or modified within a program. Objects can have attributes or properties. Some objects can contain other objects. SolidWorks: Objects can be controlled or modified by outside program.
6
What is a macro? Same as a program Dependant on “Parent” program(s)
List of instructions. Completed in the order that they appear in program. Instructions tell computer what to do and how to do it. Display status and query user. Change what to do based on changing conditions. Defines what to do if a problem occurs. Dependant on “Parent” program(s) Instructs “Parent” program how to complete specific tasks. Retrieve or modify data. Control “parent” program. Written to work within specific “Parent” program(s). Must be launched from within “Parent” program. “Parent” program requires an API.
7
What is an API? Application Programming Interface
An interface that consist of several sets of related functions or procedures. Allows external programs to interact with, and control, the application and the data sets created by the application. Open Source
8
What is the SolidWorks API?
Application Programming Interface Allows other programs to access SolidWorks data and/or control SolidWorks. Examples: SolidWorks Add-Ins Cosmos, Toolbox, PhotoWorks, PDM Works, PDM Works Enterprise. Why? Allow third party vendors to build applications that access SolidWorks and SolidWorks data. Lets SolidWorks focus on their core products. SolidWorks Add-Ins have API’s. eDrawings SolidWorks ToolBox FeatureWorks SolidWorks Utilities PDMWorks SolidWorks Routing PhotoWorks
9
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 needs Customize SolidWorks Add commands, change settings, etc…
10
Access the SolidWorks API
Stand alone application Executable Visual C++ .Net Visual Basic .Net. Visual C++ Visual Basic Visual C Programming environment outside of SolidWorks. Must be compiled to use outside of programming environment. Can start SolidWorks as background task. Required for creating Add-Ins/DLL’s. 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 necessary. Slower than executable. Some limitations.
11
SolidWorks Macros The macro recorder Running a macro
Start macro recorder. Complete tasks. Some changes to settings in dialog boxes are not recorded. Pause macro recorder if needed. Stop recorder and save new macro. Running a macro Run Macro. Select macro. Macro will automatically run. Stop Record / Pause Run
12
SolidWorks Macros Edit macro New Macro Select 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). Enter macro name. Edit Macro New Macro
13
VB Editor: Environment
Project: SolidWorks Objects. Definitions of SolidWorks Objects. Forms: Object based interface for user interaction. Custom code programmed for each object in form. Modules: Modules that only contain code for the macro. Common modules used in many macros.
14
VB Editor: References Access via “Tools | References” menu selections in the VB Editor Attach objects from another application to your project Standard descriptions of object types, properties and methods. Selections are specific to each macro SolidWorks Type Libraries are version specific
15
Programming: Variables
What is a variable? Temporarily data placeholder that can be used later. Allows programmer to use a word in place of a changing value. Local variable Value retained only while in module or procedure where defined. Value can be changed. Same variable name can be defined locally in multiple modules. Global variable Value retained throughout all modules and procedures. Can be used to set fixed values for easy reference later. Pi = 4 * Atn(1) ' The infamous pi value
16
Programming: Constants
What are constants? Similar to a variable. Assign a specific value to variable name. Set once. CAN NOT be changed. CAN NOT use a formula to define value.
17
Programming: 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() - List or spreadsheet of data. VariantArray() - List or spreadsheet of data of any type.
18
Programming: Objects Must set an object to a variable to access it’s properties Meta-data - Information about object (properties). Features - Sub-objects. Multiple tiers Parent - Child relationships. Dependencies. Methods Command and syntax that provides access to object’s properties. Accessors Roadmap that shows how to get to object. Command and syntax how to get a feature (sub-object) from parent.
19
SolidWorks ModelDoc2
20
Programming: Routines
Sub (Procedures) Private Can only be accessed from module it resides in. Public Can be accessed from any module. Run from top-down until all tasks are completed. Can be access many times. Can launch other routines. Can pass variables between routines.
21
Programming: Functions
Special Routine / Procedure Usually pass a variable to the function. Function returns a value back to the calling procedure. Must be defined within module before subroutines. Function can launch itself.
22
Programming: Testing & Running
Testing macro or procedure F5 Run macro. F8 Step thru code line-by-line. Start in current procedure. Automatic starting point Last procedure of the last module inserted into the macro. NOT module name specific. NOT procedure name specific.
23
Programming: Typical Steps
Launch Macro Load type libraries. Set global variables and constants. Attach to SolidWorks. Is a document open in SolidWorks? No, warn user that you need one, then end. Get document type Wrong type, warn user and end. Proceed……………
24
Programming: User Forms
Initialization Set program defaults. Read data from document. Display data and form to user. Wait for input React to user input. Modify document. Read more data from document. Update user form. Update document. Action buttons OK / Process. Cancel. Help. Other.
25
Programming: Processing
Process user requests based on form values. Inform user of status during processing. Inform user of errors encountered. Inform user of status upon completion. Wait for user OK.
26
Programming: Completion
Close forms. Clear out objects and variables. Close macro.
27
Macro Feature: Embed macro as a feature in the model.
Overview of Macro Feature See SolidWorks API Help File Example by Mike Wilson
28
Assigning to a Toolbar Button.
Must have document open in SolidWorks to add a macro to a toolbar. How To: Right-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 - Keyboard combination. Click OK to complete.
29
When to write your own macro?
Consistent retrieval & exporting of data. Remove manual tasks and retyping errors. Consistent results with consistent formatting. Examples: 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 types. Print all drawings in the current directory. Print all drawings for all components in a current assembly. Retrieve and export a BOM for multiple configurations.
30
When to write your own macro?
Need to enforce compliance with standard procedures. Provide an automated method to follow standards. Consistent document appearance. Examples: Preset SolidWorks settings to your company standards. Set annotations in document to use document defaults. Copy document settings from “master” document to current document. Simplifies adding standardized notes to drawings. Need a special tool to automate a specific task.
31
Justification to write a macro:
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. More use = better justification. Does it already exist? Is there something available that’s close to what I need? Can I modify something I already have? Examples files: Help file. SolidWorks Website. Lenny’s SolidWorks Resources.
32
Justification to write a macro:
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 Repetitive, Time consuming, User input required at all steps! Selecting file type by hand every time. Consistent configuration naming. Did I export all configurations? Estimate time to complete: Hours Time comparison: Time to write: 8 Hours Automated: 1.5 Hours Saved first use: Hours Macro can be reused many, many times.
33
Justification to write a macro:
Example #1 – Modify Macro – Change custom property value. Scope: All documents require change, addition or removal of a custom property. 1000’s of files. Found macro on internet, can modify for use. Problems via manual way 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.
34
Tips: Learning Learn by example Experiment
Find a good macro, and study how it works. Use [F8] in VB Editor to step thru code line-by-line. Experiment Write “do nothing” macros for Visual Basic or SolidWorks API. Learn functions you haven’t 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 portions. Start with a “basic” program, then add more functionality and flexibility later. KISS: Keep It Short and Simple.
35
Tips: Program Code Add comments to your code Use existing code
Helps you and others understand what you did, and why. Helps you remember what you did or why you did it that way. Identify what the routines are doing. Use existing code Don’t 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 - Don’t know how many times.
36
Tips: Distribution Think about other people who may be using your macros. Macro may be used for needs other than initially intended. User customization of existing macro. Not every user will know how to modify a/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.
37
Tips: Getting Help SolidWorks API Help File.
SolidWorks API Help forums. SolidWorks API Support. SolidWorks API functionality only. Not for learning. Be specific! Repeatable problem. Steps leading up to problem. Identify specific conditions that lead to problem. Identify specific problem you are experiencing. Don’t send complete macro. Provide simple code to demonstrate. Don’t expect help with programming techniques.
38
Resources: Tools & Help
Installed with SolidWorks SolidWorks 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.
39
Resources: 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. (SDK) Not required. Contains working sample programs. New SDK for each version of SolidWorks. Presentations. Local VAR / SolidWorks Reseller. SolidWorks API Training classes.
40
Resources: SolidWorks API Examples
Lenny’s SolidWorks Resources Macros, Tips, Blog Mike Wilson’s SolidWorks Modeling Techniques Macro Feature Matt Lombard Macros Joe Jones at New Hampshire CAD Tutorials Stefan Berlitz’s SolidWorks Tools Macros website. SolidWorks Tips & Things API Tips
41
Resources: Visual Basic Programming
Websites “Tutorial” and “Lessons” May be Visual Basic Specific. Do not provide help with specific applications. Forums May not be able to provide help with specific applications. Search - Too many to list. Some provide better explanation and examples.
42
Thank you! SolidWorks User Group Network (SWUGN) Richard Doyle
SolidWorks Community Coordinator SWUGN Committee Chairman Wayne Tiffany President, Kansas City SolidWorks User Group SWUGN Committee Member – Southern USA
43
Your questions? This presentation and handout of slides can be downloaded from: Lenny’s SolidWorks Resources:
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.