api_fundamentals_training_course
api_fundamentals_training_course
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Use of Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Graphics and Graphics Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Color Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
More SOLIDWORKS Training Resources. . . . . . . . . . . . . . . . . . . . . . 5
Local User Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Option Explicit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Choosing Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
API Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
SOLIDWORKS Constants Type Library . . . . . . . . . . . . . . . . . . . . 8
i
Contents SOLIDWORKS
ii
SOLIDWORKS Contents
Lesson 3:
Setting System Options and Document Properties
User Preferences - System Options . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Setting Checkboxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Setting Textboxes with Integers . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Setting Textboxes with Doubles . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Setting Textboxes with String Values . . . . . . . . . . . . . . . . . . . . . 88
Setting Listboxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Setting Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Setting Slider Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
User Preferences - Document Properties . . . . . . . . . . . . . . . . . . . . . . 91
Locating the Correct APIs and Enumeration Values. . . . . . . . . . . . . . 92
UserPreference Tables For System Options,
Document Properties and Menu Items . . . . . . . . . . . . . . . . . . . . . . . . 94
Exercise 6: Change Multiple System Options . . . . . . . . . . . . . . . . . . 95
Exercise 7: Change Multiple Document Properties . . . . . . . . . . . . . . 97
Lesson 4:
Automating Part Design
Case Study: Automation Tool for Parts . . . . . . . . . . . . . . . . . . . . . . 100
Setting Material. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Creating the Sketch Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Adding Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Selection on Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Creating the Sketch Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Creating Extruded Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Enabling Contour Selection for the Extrusion . . . . . . . . . . . . . . 106
Creating Revolved Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Standard Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
View Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Sketch Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Sketch Tools Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Features Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Sketch Relations Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Reference Geometry Commands . . . . . . . . . . . . . . . . . . . . . . . . 111
Exercise 8: Automating the Part Creation Process . . . . . . . . . . . . . . 112
iii
Contents SOLIDWORKS
Lesson 5:
Assembly Automation
Case Study: Automation Tool for Assemblies . . . . . . . . . . . . . . . . . 116
Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Creating MathTransforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
The Transformation Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Activating Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Invisible Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Object Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Establishing the Curve and Edge Collections. . . . . . . . . . . . . . . 121
Establishing the Face Collection. . . . . . . . . . . . . . . . . . . . . . . . . 122
Getting Adjacent Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Establishing the Points Collection . . . . . . . . . . . . . . . . . . . . . . . 124
Getting Curve Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Adding and Mating the Knobs to the Chassis. . . . . . . . . . . . . . . 125
Adding Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Adding Mates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Exercise 9: Adding Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Lesson 6:
Drawing Automation
Case Study: Automating Drawing Creation . . . . . . . . . . . . . . . . . . . 134
Getting Configuration Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Creating Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Traversing Drawing Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Inserting Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Saving Drawings in Different Formats . . . . . . . . . . . . . . . . . . . . 144
Drawing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Annotation Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Layer Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Line Format Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Exercise 10: Drawing Automation . . . . . . . . . . . . . . . . . . . . . . . . . . 147
iv
SOLIDWORKS Contents
Lesson 7:
Selection and Traversal Techniques
Case Study: Programming With a Selected Object . . . . . . . . . . . . . 152
SelectionManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Accessing the Selection Manager . . . . . . . . . . . . . . . . . . . . . . . . 153
Counting Selected Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Accessing Selected Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Getting Selected Object Types . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Getting Feature Type Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Feature Data Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Accessing the Feature Data Object . . . . . . . . . . . . . . . . . . . . . . . 155
Accessing Selections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Releasing Selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Modifying Feature Data Properties. . . . . . . . . . . . . . . . . . . . . . . 157
Modify the Object Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
The SOLIDWORKS BREP Model. . . . . . . . . . . . . . . . . . . . . . . . . . 158
Traversing Topology and Geometry. . . . . . . . . . . . . . . . . . . . . . 159
Case Study: Body and Face Traversal . . . . . . . . . . . . . . . . . . . . . . . 159
Returning a List of Body Pointers . . . . . . . . . . . . . . . . . . . . . . . 161
Face Material Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Case Study: Feature Manager Traversal . . . . . . . . . . . . . . . . . . . . . . 164
Traversing the FeatureManager Design Tree from the Top . . . . 165
Displaying Feature Names and Types . . . . . . . . . . . . . . . . . . . . 165
Setting Feature Suppression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Setting Feature UI State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Obtaining a Feature by FeatureManager Design Tree Position . 169
Exercise 11: Handling Preselection 1 . . . . . . . . . . . . . . . . . . . . . . . . 170
Exercise 12: Handling Preselection 2 . . . . . . . . . . . . . . . . . . . . . . . . 172
Exercise 13: Traversing the FeatureManager Design Tree . . . . . . . 174
v
Contents SOLIDWORKS
Lesson 8:
Adding Custom Properties and Attributes
Case Study: Custom Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Adding Custom Properties to a SOLIDWORKS Document . . . 179
CustomPropertyManager Object . . . . . . . . . . . . . . . . . . . . . . . . 179
Setting and Getting Custom Property Values . . . . . . . . . . . . . . . 181
Getting Custom Property Names . . . . . . . . . . . . . . . . . . . . . . . . 182
Getting the Custom Property Count . . . . . . . . . . . . . . . . . . . . . . 182
Case Study: Configurations With Custom Properties. . . . . . . . . . . . 184
Returning Mass Properties From a SOLIDWORKS Model. . . . 186
Using the API to Return the Mass Properties . . . . . . . . . . . . . . . 186
MassProperty2 Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Case Study: File Summary Information . . . . . . . . . . . . . . . . . . . . . . 189
Adding Summary Information . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Case Study: Document Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Naming Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
The Attribute Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
AttributeDef Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Attribute Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Parameter Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Case Study: Face Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Finding the Cylindrical Faces and Attaching Attributes . . . . . . 196
Displaying Callouts in the Model View . . . . . . . . . . . . . . . . . . . 198
Callout Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Creating the CNC Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Types of Attribute Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
A Final Word about Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Exercise 14: Adding Mass Properties as Custom Properties . . . . . . 204
Exercise 15: Adding Attributes to Edges . . . . . . . . . . . . . . . . . . . . . 206
Lesson 9:
The SOLIDWORKS API SDK
The API SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Installing the SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Case Study: Creating a VB.NET Add-In . . . . . . . . . . . . . . . . . . . . . 214
References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Comparing Addin DLLs and Stand-Alone Executables. . . . . . . 218
Loading and Running an Add-in Application. . . . . . . . . . . . . . . 219
Case Study: Creating a C# Add-in . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Case Study: C++ Add-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Compiling a C++ Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Loading the C++ Add-In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Debugging the C++ Add-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Choosing a Programming Language. . . . . . . . . . . . . . . . . . . . . . . . . 234
vi
SOLIDWORKS Contents
Lesson 10:
Customizing the SOLIDWORKS User Interface
Case Study: Customizing the UI With VB.NET. . . . . . . . . . . . . . . . 236
Debugging the DLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Debugger Keyboard Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Understanding The Add-in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Importing Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
The Add-in Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Understanding the GUID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Connecting to SOLIDWORKS. . . . . . . . . . . . . . . . . . . . . . . . . . 245
Bidirectional Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Setting Callback Information . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Custom Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Custom Command Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Command Tabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Command Tab Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Command Tab Box Commands . . . . . . . . . . . . . . . . . . . . . . . . . 253
Creating and Adding Custom Toolbars to an Add-in . . . . . . . . . 256
Creating the Toolbar Bitmaps. . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Adding Toolbar Bitmaps to a VB.NET Solution . . . . . . . . . . . . 257
The Bitmap Handler Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Adding Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Property Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
PropertyPage Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
SldWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
UserPMPage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
SldWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
ppage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
PropertyManager-Page2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
PropertyManager-Page2Handler8 . . . . . . . . . . . . . . . . . . . . . . . 263
Creating a PropertyManager Page . . . . . . . . . . . . . . . . . . . . . . . 264
Property Page Groups and Controls . . . . . . . . . . . . . . . . . . . . . . . . . 266
Adding Group Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Group and control IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Adding Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Adding Picture Labels to Controls . . . . . . . . . . . . . . . . . . . . . . . 270
Removing Menus and Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Other Areas of Customization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Custom Status Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Custom Pop-up Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Custom ModelView Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Exercise 16: Implement a New Menu. . . . . . . . . . . . . . . . . . . . . . . . 277
Exercise 17: Implement Toolbar Buttons . . . . . . . . . . . . . . . . . . . . . 282
Exercise 18: Implement Controls on a Property Manager Page . . . . 287
vii
Contents SOLIDWORKS
Lesson 11:
Notifications
Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Notifications in VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Case Study: Simple Notification. . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
The Class Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Case Study: Using Notifications in .NET. . . . . . . . . . . . . . . . . . . . . 296
The AddHandler Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
The AddressOf Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
The Event Handler Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The Document Event Handler Class. . . . . . . . . . . . . . . . . . . . . . 301
Attaching the DocumentEvent Handlers . . . . . . . . . . . . . . . . . . 303
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
The Derived Event Handler Classes . . . . . . . . . . . . . . . . . . . . . . 307
The DocView Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Detaching the Document and Model View Event Handlers. . . . 314
Detaching the SOLIDWORKS Event Handlers . . . . . . . . . . . . . 320
Interfaces That Support Notifications. . . . . . . . . . . . . . . . . . . . . 322
Exercise 19: Handling Events Using the Add-in Wizard . . . . . . . . . 323
Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Appendix A:
Examples
Macro Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Batch Conversion 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Batch Conversion 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Assembly Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
viii