Solidworks API Fundamentals Course Content
Solidworks API Fundamentals Course Content
API Fundamentals
Introduction
About This Course . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Course Length. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Course Design Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Using this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
About the Training Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Conventions Used in this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Windows® 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Use of Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Graphics and Graphics Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Color Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
More SOLIDWORKS Training Resources. . . . . . . . . . . . . . . . . . . . . . 6
Local User Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Option Explicit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Choosing Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
API Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
SOLIDWORKS Constants Type Library . . . . . . . . . . . . . . . . . . . . 9
i
Contents SOLIDWORKS
ii
SOLIDWORKS Contents
Lesson 3:
Setting System Options and Document Properties
User Preferences - System Options . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Setting Checkboxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Setting Textboxes with Integers . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Setting Textboxes with Doubles . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Setting Textboxes with String Values . . . . . . . . . . . . . . . . . . . . . 90
Setting Listboxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Setting Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Setting Slider Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
User Preferences - Document Properties . . . . . . . . . . . . . . . . . . . . . . 93
Locating the Correct APIs and Enumeration Values. . . . . . . . . . . . . . 94
UserPreference Tables For System Options,
Document Properties and Menu Items . . . . . . . . . . . . . . . . . . . . . . . . 96
Exercise 6: Change Multiple System Options . . . . . . . . . . . . . . . . . . 97
Exercise 7: Change Multiple Document Properties . . . . . . . . . . . . . . 99
Lesson 4:
Automating Part Design
Case Study: Automation Tool for Parts . . . . . . . . . . . . . . . . . . . . . . 102
Setting Material. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Creating the Sketch Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Adding Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Selection on Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Creating the Sketch Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Creating Extruded Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Enabling Contour Selection for the Extrusion . . . . . . . . . . . . . . 108
Creating Revolved Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Standard Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
View Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Sketch Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Sketch Tools Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Features Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Sketch Relations Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Reference Geometry Commands . . . . . . . . . . . . . . . . . . . . . . . . 113
Exercise 8: Automating the Part Creation Process . . . . . . . . . . . . . . 114
iii
Contents SOLIDWORKS
Lesson 5:
Assembly Automation
Case Study: Automation Tool for Assemblies . . . . . . . . . . . . . . . . . 118
Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Creating MathTransforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The Transformation Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Activating Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Invisible Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Object Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Establishing the Curve and Edge Collections. . . . . . . . . . . . . . . 123
Establishing the Face Collection. . . . . . . . . . . . . . . . . . . . . . . . . 124
Getting Adjacent Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Establishing the Points Collection . . . . . . . . . . . . . . . . . . . . . . . 126
Getting Curve Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Adding and Mating the Knobs to the Chassis. . . . . . . . . . . . . . . 127
Adding Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Adding Mates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Exercise 9: Adding Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Lesson 6:
Drawing Automation
Case Study: Automating Drawing Creation . . . . . . . . . . . . . . . . . . . 136
Getting Configuration Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Creating Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Traversing Drawing Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Inserting Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Saving Drawings in Different Formats . . . . . . . . . . . . . . . . . . . . 146
Drawing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Annotation Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Layer Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Line Format Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Exercise 10: Drawing Automation . . . . . . . . . . . . . . . . . . . . . . . . . . 149
iv
SOLIDWORKS Contents
Lesson 7:
Selection and Traversal Techniques
Case Study: Programming With a Selected Object . . . . . . . . . . . . . 154
SelectionManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Accessing the Selection Manager . . . . . . . . . . . . . . . . . . . . . . . . 155
Counting Selected Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Accessing Selected Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Getting Selected Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Getting Feature Type Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Feature Data Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Accessing the Feature Data Object . . . . . . . . . . . . . . . . . . . . . . . 157
Accessing Selections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Releasing Selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Modifying Feature Data Properties. . . . . . . . . . . . . . . . . . . . . . . 159
Modify the Object Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
The SOLIDWORKS BREP Model. . . . . . . . . . . . . . . . . . . . . . . . . . 160
Traversing Topology and Geometry. . . . . . . . . . . . . . . . . . . . . . 161
Case Study: Body and Face Traversal . . . . . . . . . . . . . . . . . . . . . . . 161
Returning a List of Body Pointers . . . . . . . . . . . . . . . . . . . . . . . 163
Face Material Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Case Study: Feature Manager Traversal . . . . . . . . . . . . . . . . . . . . . . 166
Traversing the FeatureManager Design Tree from the Top . . . . 166
Displaying Feature Names and Types . . . . . . . . . . . . . . . . . . . . 167
Setting Feature Suppression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Setting Feature UI State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Obtaining a Feature by
FeatureManager Design Tree Position . . . . . . . . . . . . . . . . . . . . 171
Exercise 11: Handling Preselection 1 . . . . . . . . . . . . . . . . . . . . . . . . 172
Exercise 12: Handling Preselection 2 . . . . . . . . . . . . . . . . . . . . . . . . 174
Exercise 13: Traversing the FeatureManager Design Tree . . . . . . . 176
v
Contents SOLIDWORKS
Lesson 8:
Adding Custom Properties and Attributes
Case Study: Custom Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Adding Custom Properties to a SOLIDWORKS Document . . . 181
CustomPropertyManager Object . . . . . . . . . . . . . . . . . . . . . . . . 181
Setting and Getting Custom Property Values . . . . . . . . . . . . . . . 183
Getting Custom Property Names . . . . . . . . . . . . . . . . . . . . . . . . 184
Getting the Custom Property Count . . . . . . . . . . . . . . . . . . . . . . 184
Case Study: Configurations With Custom Properties. . . . . . . . . . . . 186
Returning Mass Properties From a SOLIDWORKS Model. . . . 188
Using the API to Return the Mass Properties . . . . . . . . . . . . . . . 188
MassProperty2 Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Case Study: File Summary Information . . . . . . . . . . . . . . . . . . . . . . 191
Adding Summary Information . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Case Study: Document Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Naming Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The Attribute Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
AttributeDef Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Attribute Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Parameter Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Case Study: Face Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Finding the Cylindrical Faces and Attaching Attributes . . . . . . 198
Displaying Callouts in the Model View . . . . . . . . . . . . . . . . . . . 200
Callout Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Creating the CNC Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Types of Attribute Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
A Final Word about Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Exercise 14: Adding Mass Properties as Custom Properties . . . . . . 206
Exercise 15: Adding Attributes to Edges . . . . . . . . . . . . . . . . . . . . . 208
Lesson 9:
The SOLIDWORKS API SDK
The API SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Installing the SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Case Study: Creating a VB.NET Add-In . . . . . . . . . . . . . . . . . . . . . 216
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Comparing Addin DLLs and Stand-Alone Executables. . . . . . . 220
Loading and Running an Add-in Application. . . . . . . . . . . . . . . 221
Case Study: Creating a C# Add-in . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Case Study: C++ Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Compiling a C++ Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Loading the C++ Add-In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Debugging the C++ Add-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Choosing a Programming Language. . . . . . . . . . . . . . . . . . . . . . . . . 236
vi
SOLIDWORKS Contents
Lesson 10:
Customizing the SOLIDWORKS User Interface
Case Study: Customizing the UI With VB.NET. . . . . . . . . . . . . . . . 238
Debugging the DLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Debugger Keyboard Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Understanding The Add-in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Importing Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
The Add-in Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Understanding the GUID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Connecting to SOLIDWORKS. . . . . . . . . . . . . . . . . . . . . . . . . . 247
Bidirectional Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Setting Callback Information . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Custom Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Custom Command Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Command Tabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Command Tab Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Command Tab Box Commands . . . . . . . . . . . . . . . . . . . . . . . . . 255
Creating and Adding Custom Toolbars to an Add-in . . . . . . . . . 258
Creating the Toolbar Bitmaps. . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Adding Toolbar Bitmaps to a VB.NET Solution . . . . . . . . . . . . 259
The Bitmap Handler Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Adding Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Property Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
PropertyPage Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
SldWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
UserPMPage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
SldWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
ppage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
PropertyManager-Page2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
PropertyManager-Page2Handler8 . . . . . . . . . . . . . . . . . . . . . . . 265
Creating a PropertyManager Page . . . . . . . . . . . . . . . . . . . . . . . 266
Property Page Groups and Controls . . . . . . . . . . . . . . . . . . . . . . . . . 268
Adding Group Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Group and control IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Adding Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Adding Picture Labels to Controls . . . . . . . . . . . . . . . . . . . . . . . 272
Removing Menus and Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Other Areas of Customization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Custom Status Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Custom Pop-up Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Custom ModelView Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Exercise 16: Implement a New Menu. . . . . . . . . . . . . . . . . . . . . . . . 279
Exercise 17: Implement Toolbar Buttons . . . . . . . . . . . . . . . . . . . . . 284
Exercise 18: Implement Controls on a Property Manager Page . . . . 289
vii
Contents SOLIDWORKS
Lesson 11:
Notifications
Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Notifications in VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Case Study: Simple Notification. . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
The Class Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Case Study: Using Notifications in .NET. . . . . . . . . . . . . . . . . . . . . 298
The AddHandler Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The AddressOf Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The Event Handler Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
The Document Event Handler Class. . . . . . . . . . . . . . . . . . . . . . 303
Attaching the DocumentEvent Handlers . . . . . . . . . . . . . . . . . . 305
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
The Derived Event Handler Classes . . . . . . . . . . . . . . . . . . . . . . 309
The DocView Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Detaching the Document and Model View Event Handlers. . . . 316
Detaching the SOLIDWORKS Event Handlers . . . . . . . . . . . . . 322
Interfaces That Support Notifications. . . . . . . . . . . . . . . . . . . . . 324
Exercise 19: Handling Events Using the Add-in Wizard . . . . . . . . . 327
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Appendix A:
Examples
Macro Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Batch Conversion 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Batch Conversion 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Assembly Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
viii