solidworks api demystified-1

37
SolidWorks API Demystified Leonard Kikstra Designer / CAD Administrator RITE-HITE Products Corporation Milwaukee, WI Lenny’s SolidWorks Resources http://www.lennyworks.com/solidworks

Upload: lcorao

Post on 02-Jun-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 1/37

SolidWorks APIDemystified

Leonard KikstraDesigner / CAD Administrator

RITE-HITE Products CorporationMilwaukee, WI

Lenny’s SolidWorks Resources http://www.lennyworks.com/solidworks

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 2/37

Who am I?

Product Designer – Engineering since 1982. – Cad user since 1991. – SolidWorks user since 1998.

CAD Administrator – 20+ people on site. –  Advise other sites.

Interested in simplifying my job. – Make my computer do some

of my work for me.

SMART User Group –  Active member since

group started in 1999.

Lenny’s SolidWorksResources website. – Online Sept. 2003. – New home Sept. 2006.

Programmed over 30

macros for use inSolidWorks – Most of these are available

for free on the Internet.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 3/37

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 – Justification

Where to get help.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 4/37

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 ToolBoxFeatureWorks SolidWorks UtilitiesPDMWorks SolidWorks Routing

PhotoWorks

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 5/37

How can the API benefit you?

 Automate common and repetitive tasks. – Increase productivity. – Save time and money. – Standardize procedures.

Consistently retrieve or export data thatalready exists in the SolidWorks documents.

Develop useful tools to meet your specific

needs.

Customize SolidWorks. – Add commands, change settings, etc… 

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 6/37

 Access the SolidWorks API

Stand alone application Executable –  Visual C++ .Net Visual Basic .Net.

 Visual C++ Visual Basic Visual C

Programming environment outside ofSolidWorks.

Must be compiled to use outside ofprogramming environment.

 – Can start SolidWorks. – 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 Some limitations.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 7/37

SolidWorks Macros

The macro recorder. – 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 Macro – Select macro – Macro will automatically run

Stop Record / Pause

Run

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 8/37

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.

New Macro – Create a new macro without using macro recorder. – Enter macro name –  Visual Basic Editor automatically launched.

Edit Macro

New Macro

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 9/37

 Visual Basic 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.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 10/37

 Visual Basic Editor:References

 Access via “Tools | References”menu selections in the VBEditor.

 Attach objects from anotherapplication to your project. – Standard descriptions of object

types, properties and methods.

Selections are specific to eachmacro.

SolidWorks Type Libraries areversion specific.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 11/37

Writing a macro: Variables

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

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 12/37

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.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 13/37

Writing a macro:Objects

Must name an object to access it’s properties  – Meta-data - Info about object – Features - Sub-objects

Multiple tiers – Parent – Child relationships

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 to object from parent.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 14/37

ModelDoc2

SolidWorks

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 15/37

Writing a macro:Routines

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.

Function – Special Procedure – Must pass a variable to

Function – Returns a value to variable in

the calling procedure.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 16/37

Writing a macro:Testing and Running

Testing macro or procedure – F5 Run macro – F8 Step thru code

line-by-line.

 – Start in current procedure.

 Automatic starting point – Last procedure, in last inserted module – Not module name specific. – Not procedure name specific.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 17/37

Writing a macro:Typical Steps - Launch

Launch Macro – Load type libraries – Set global variables and

constants

 –  Attach to SolidWorks

Is a document open inSolidWorks? – No, warn user that you need

one, then end.

Get document type. – Wrong type, warn user and end

Proceed…………… 

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 18/37

Writing a macro:Typical Steps - User Form

User form – Preprocessing.

Set program defaults. Read data from document.

 – Display user form to user.

Wait for input – React to user input.

Read more data fromdocument.

Update user form. Update document.

 –  Action buttons OK / Process Cancel Help Other

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 19/37

Writing a macro:Typical Steps - Processing

Process user requests based onform values.

Inform user of status during

processing.

Inform user of errorsencountered.

Inform user of status uponcompletion. – Wait for user OK.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 20/37

Writing a macro:Typical Steps - Completion

Close forms

Clear out objects and variables.

Close macro

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 21/37

Macro Feature:

Embed macro as a feature in the model.

Overview of Macro Feature – See SolidWorks API Help File

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 22/37

 Assigning a macro to aToolbar Button.

Must have document open inSolidWorks

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

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 23/37

When should I write myown 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.

 – Examples: 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.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 24/37

When should I write myown macro?

Enforce compliance with standard procedures. – Provide an automated method to follow standards. – Consistent document appearance. – Examples:

Preset SolidWorks settings to your companystandards.

Set annotations in document to use documentdefaults.

Copy document settings from “master” document tocurrent document.

Simplifies adding standardized notes to drawings.

Need a special tool to automate a specific task.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 25/37

Justification to write amacro:

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 that’s close

to what I need? – Can I modify something I already have?

 – Examples files: Help file SolidWorks Website Lenny’s SolidWorks Resources 

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 26/37

Justification to write amacro:

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, Timeconsuming, 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.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 27/37

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 28/37

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 “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.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 29/37

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 – 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. 

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 30/37

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.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 31/37

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. – Don’t send complete macro. Provide simple code to demonstrate. 

Don’t expect help with programming techniques. 

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 32/37

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 33/37

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.

 – Presentations

Local VAR / SolidWorks Reseller

 – SolidWorks API Training classes

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 34/37

Resources for learning more?Other Websites - API

Lenny’s SolidWorks Resources  Macros & Tips – http://www.lennyworks.com/solidworks

Mike Wilson’s SolidWorks Modeling Techniques  Macro Feature – http://www.mikejwilson.com/solidworks/solidworks_files.htm

Matt Lombard Macros – http://mysite.verizon.net/mjlombard/

Joe Jones at New Hampshire CAD Tutorials – http://www.nhcad.com/sw_macros/index.html

Stefan Berlitz’s SolidWorks Tools  Macros – http://www.swtools.de website.

SolidWorks Tips & Things API Tips – http://www.solidworktips.com/

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 35/37

Resources for learning more? Visual Basic

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.

Too many to list.

Some provide better explanation and examples.

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 36/37

8/10/2019 SolidWorks API Demystified-1

http://slidepdf.com/reader/full/solidworks-api-demystified-1 37/37

Thank You!

Lenny’s SolidWorks Resources has moved to

http://www.LennyWorks.com/solidworks