SolidWorks API
SolidWorks API
Leonard Kikstra Designer / CAD Administrator RITE-HITE Products Corporation Milwaukee, WI Lennys SolidWorks Resources http://www.lennyworks.com/solidworks
Who am I?
Product Designer
Engineering since 1982. Cad user since 1991. SolidWorks user since 1998.
CAD Administrator
General Information
Getting Started.
Create and modify macros. Finding your way. When and Why.
Program examples
Justification
Allows other programs to access SolidWorks data and/or control SolidWorks. Examples: SolidWorks Add-Ins
Why?
Allow third party vendors to build applications that access SolidWorks and SolidWorks data. Lets SolidWorks focus on their core products.
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
Executable
Programming environment outside of SolidWorks. Must be compiled to use outside of programming environment.
SolidWorks Macro
.swp file
SolidWorks Program (.swp) is a single file. Must be launched from within SolidWorks. Installed with SolidWorks. No compiling Some limitations.
SolidWorks Macros
Start recorder. Complete tasks. Some settings in dialog boxes are not recorded. Pause if needed. Stop recorder and save new macro.
Running a macro
Run
SolidWorks Macros
Edit 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.
Edit Macro
New Macro
Create a new macro without using macro recorder. Enter macro name Visual Basic Editor automatically launched.
New Macro
Project:
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.
Access via Tools | References menu selections in the VB Editor. Attach objects from another application to your project.
Selections are specific to each macro. SolidWorks Type Libraries are version specific.
Local
Value retained only while in module or procedure where defined. Value can change Same variable name can be defined locally in multiple modules.
Global
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
Constants
Assign a specific value to variable name. Set once CAN NOT be changed CAN NOT use formula to define
- True or False - Whole numbers only - Any numerical value - Any text based value - Can be any type
Array()
VariantArray()
- Spreadsheet of data
- Spreadsheet of data of any type.
Accessors
Roadmap that shows how to get to object. Command and syntax how to get to object from parent.
SolidWorks
ModelDoc2
Sub (Procedures)
Private - Can only access from module it resides in Public - Can be accesses from any module Run from top-down until completed Can be access many times. Can launch other routines. Can pass variables between routines.
Must pass a variable to Function Returns a value to variable in the calling procedure.
Last procedure, in last inserted module Not module name specific. Not procedure name specific.
Launch Macro
Load type libraries Set global variables and constants Attach to SolidWorks
User form
Preprocessing.
Action buttons
Read more data from document. Update user form. Update document. OK / Process Cancel Help Other
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.
Macro Feature:
Click OK to complete
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.
Remove manual tasks and retyping errors. Consistent results with consistent formatting. Examples:
No drawing required.
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.
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.
What is the purpose of macro? How often will you use it?
The more you use it, the more time you will save
Is there something available thats close to what I need? Can I modify something I already have? Examples files:
* 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:
* 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:
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.
Experiment
Write and test small portions of code before continuing to larger portions. Start with a basic program, then add more functionality and flexibility later.
Helps you and others understand what you did, and why. Helps you remember what you did. Identify what routines are doing.
Dont re-invent the wheel. Saves time and your effort. Keep sample code on hand for later use.
If Then, Else, End If Select Case ., End Case For . Next Do While . End Loop
True/False Response based on value. Set number of times to do. 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.
SolidWorks API Help File Help forums. SolidWorks API Support. Be specific!
Repeatable problem.
Identify specific problem you are experiencing. Dont send complete macro. Provide simple code to demonstrate.
Explains object structure and command syntax. Many ready to use examples. Best help file I have seen for writing macros.
SolidWorks 2001Plus and later Visual Basic for Applications Help File.
Presentations
http://www.lennyworks.com/solidworks
Macros & Tips Macro Feature Macros Tutorials Macros API Tips
http://www.mikejwilson.com/solidworks/solidworks_files.htm
Matt Lombard
http://mysite.verizon.net/mjlombard/
Joe Jones at New Hampshire CAD http://www.nhcad.com/sw_macros/index.html Stefan Berlitzs SolidWorks Tools
http://www.swtools.de website.
Websites
Tutorial and Lessons May be Visual Basic Specific. Do not provide help with specific applications.
Forums
May be Visual Basic Specific. Do not be able to provide help with specific applications.
Thank You!