Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Who Am I?

n Product Designer n SolidWorks User Group


– Engineering since 1982. – SolidWorks Milwaukee Area
– CAD user since 1991. Resource Team (SMART)
SolidWorks API – SolidWorks user since 1998. – http://www.smart-wi.com
– Active member since 1999.
Demystified n CAD Administrator
– 20+ people on site. n Lenny’s SolidWorks Resources
– Advise other sites. – Online since Sept. 2003.
– Free Macros, Tips & Tricks
Leonard Kikstra – LennyWorks Blog Since 2007
Designer / CAD Administrator n Productivity Gains - I want to:
o Includes Macro Tutorials
RITE-HITE Products Corp. – Simplifying my job.
Milwaukee, WI – Make my computer do more work for
me. n Hobbyist programmer since 1981
– Get the most out of the tools I use to – Programmed over 45 SolidWorks
do my job. macros.
– Most of these are available for free
on the Internet.
n Certified SolidWorks Professional
– Experiment with programming.
(CSWP)
– I rarely get paid for my programs.
Lenny’s SolidWorks Resources:
http://www.LennyWorks.com/SolidWorks
Blog: http://designsmarter.typepad.com/lennyworks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

What is covered here? What is covered here?


n Benefits. n Type libraries.

n Definitions. n Running and testing a macro.

n Programming the API. n Typical macro process.

n Macro Toolbar. n Why won’t a macro work?

n Macro Recorder. n Research.

n Look inside the macro n SolidWorks API Object Model

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

What is covered here? What is NOT covered here?


n Progressive learning track. n Programming basics.

n Tips for learning and exploring. n Basic programming techniques.

n Programming strategies.

n Getting help.

n Available resources.

n Sources of FREE examples

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

1
Benefits of learning and using. Basic Definitions
n Save time and reduce errors. n Program.

n Automate tasks. n Object Oriented Programming.

n Develop useful tools. n Macro.

n Enforce compliance. n Application Programming Interface


(API).
n Customize SolidWorks
n SolidWorks API.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

SolidWorks API Accessing / Programming


n Many SolidWorks Add-Ins have – eDrawings n Stand alone application – Visual Basic
API’s – FeatureWorks – Executable – Visual Basic .Net.
– PDMWorks Workgroup – Visual C++
– PhotoWorks – Visual C++ .Net
– SolidWorks Document Manager – Visual C
n Why does SolidWorks make this
– SolidWorks Routing
available?
– SolidWorks ToolBox – Microsoft Visual Basic for
n SolidWorks Macro
– SolidWorks Utilities Applications.
– .swp file o Installed with SolidWorks

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Accessing / Programming Macro Toolbar


n SolidWorks Help File Syntax n Run Macro
– Tools, Macro, Run.
– Syntax (OLE Automation) – Syntax (COM)
o Visual Basic (VB) o Visual C
n Record / Pause
o Visual Basic.NET o Visual C++
– Tools, Macro, Record. Run New Edit
o Visual Basic for Applications (VBA) o Visual C++.NET
– Macros (.swp) – Stop

n Tools, Macro, Stop


– Save recorded macro.

n New Macro Stop Record / Pause


– Tools, Macro, New.
– Launches macro editor.

n Edit Macro
– Tools, Macro, Edit.
– Launches macro editor.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

2
Macro Recorder A Look Inside a Macro File
n Records Actions n SolidWorks Objects

n Getting Started n Forms

n Discovery Tool n Modules

n Programming Aid n Class Module

Stop Record / Pause

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Visual Basic Editor – References / Type Libraries Visual Basic Editor – References / Type Libraries
n What are they? n What libraries may be needed:

n How to set / change – SolidWorks 2008 Type library

– SolidWorks 2008 Constant type


n Notes:
library
– Set during macro recording.
– Specific to each major SolidWorks
– SolidWorks 2008 Commands type
release.
library
– Must be set for each macro.
– SolidWorks 2008 exposed type
libraries for add-in use

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Running a Macro – Where does the macro start? Testing a Macro


n Run Macro n [ F5 ] - Run macro.
– Last “public” procedure of the last – Start in current procedure.
module inserted into the macro.
n [ F8 ] - Step thru code
n Toolbar Button – Line-by-Line.
– User must set module and routine
as starting point.
n Yellow bar – Current position
– Only “public” routines are listed.

n Red bar – Stop at this line


n Hotkey definition
– User must set module and routine
as starting point.
– Only “public” routines are listed.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

3
Running a Macro – Typical Steps SolidWorks Help File
n Launch Macro. n Common
– See Also
n Attach to SolidWorks. – Example
– Availability
n Document Loaded and Type.
n Objects
– Methods
n User Form Initialization.
– Properties
– Events
n Process User Request. – Accessors

n Clean-Up and End Program.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

SolidWorks Examples
ModelDoc2

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Why won’t some recorded macros work? Macro may not work on other computers?
n Obvious reasons: n Not so obvious reasons: n These SolidWorks settings may
affect macro performance
– SolidWorks Version. – Some selections may not have been
recorded. – Automatic Relationships
– Type Libraries.
– Macro requires preselections. – Automatic Inferencing
– Macro improperly started.
– Selected objects may not exist. – Snap to Points to Grid.

– Work may already be done. – SnapEnable / Snap Options.

– Insert dimension value on creation.

– Use Fully Defined Sketches.

n Other users probably set these


different than what you are used to.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

4
Make your macro work on other computers! Research
n These SolidWorks settings may n How to check for these effects n Every macro you write will require
affect macro performance some research.
– Toggle settings.
– Automatic Relationships n Visual Basic
– Re-test macro. – Online tutorials
– Automatic Inferencing – Help file
– Verify results.
– Snap to Points to Grid.
n SolidWorks
n How to bypass these effects – Learning the SolidWorks API
– SnapEnable / Snap Options.
– Document Structure
– Capture current setting. – SolidWorks API Add-Ins and Help
– Insert dimension value on creation.
File
– Change setting as needed.
– Use Fully Defined Sketches.
n Existing macros
– Change setting back.

n Other users probably set these n KEEP YOUR MACRO USER


different than what you are used to. FRIENDLY.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Visual Basic Programming - Objects User Interface: UserForm vs. PropertyManagerPage


n What are they? n UserForm Beginner
– More flexible with more options.
n Can be accessed or modified by a – Display lots of options to user.
macro or external program. – Large form can get in the way.

n PropertyManagerPage Intermediate / Advanced


– User programmed PageClass and PageHandlerClass.
– Requires “SolidWorks Exposed Type Library for Add-In”.
– Out of the way - Looks like it’s part of SolidWorks.
– User needs to scroll to see all info/inputs.
– See example: “VBA_PropertyManagerPage.swp”

n Programmer must write code for each control.


http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Progressive Learning Track Progressive Learning Track

n Easy ( Relatively speaking ) n Still Easy ( A bit more work )

– Macro Recorder. – Load new model and create new geometry.

– Anything the macro can Search for, Predict or Control. – SolidWorks Settings.

– Examples – Building a UserForm to interface with user.

– Traversals. ( Assembly / Part / Drawing )

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

5
Progressive Learning Track Progressive Learning Track
n Intermediate ( More of a challenge ) n Advanced ( You better know your stuff )

– Unpredictability or Uncontrollability. – External applications that interface with SolidWorks data.

– Refine operation of a recorded macro to work in other existing models. – SolidWorks Add-Ins. ( Run within SolidWorks environment )

– PropertyManagerPages. – Event Monitors and Triggers.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Progressive Learning Track - Exploring Further Tips - Learn by Example


n Other Reference Libraries of Interest – Use Object Browser to see what is n Learn by example
available within library.
– Find a good macro, and study how it
– Common Dialog Control works.
n Use Object Browser to see what is o Access and use common Windows o Use [F8] in VB Editor to step thru
available within each library. controls and dialog boxes. code line-by-line.

– DS: OLE Document Properties – SolidWorks Help File


Object Library o Over 490 Visual Basic examples
o Edit document/file properties without
MS Office or SolidWorks installed.
– VBA_PropertyManagerPage.swp
o Search, download and install
o Best example for implementing
DsoFileSetup….
SolidWorks’ PropertyManagerPage

– Microsoft Scripting Runtime


o File System Object

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Tips - Learn by Example - Included Examples Tips - Learn by Exploring


n FramedBox macro n CommonNotes n Experiment
– Geometry creation – Read data and settings from an
– PropertyManagerPage external file – Write “do nothing” macros for Visual
– Includes articles on multiple stages – UserForm controls. Basic.
of development.
– Write “do nothing” macros for
SolidWorks API.
n DelRevTriangles
n ChannelPMP – Traversing thru a SolidWorks
drawing – Save your “experiments” for later
– PropertyManagerPage
use.

n QuickSlot / QuickRectangle macros n Toggles ( Quick & Dirty )


– Geometry creation – No interface.
– Setting common options – Simple error checking.

n More…

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

6
Tips - Learn by Exploring Programming Strategies
n Look for visual clues that identify SolidWorks document structure. n Start small and work your way up

– SolidWorks FeatureManager. n Know that there are multiple ways to


complete tasks
– SolidWorks API object model.
n Use subroutines and functions
n SolidWorks Journal File
– <drive>\Documents and Settings\<username>\Application Data\SolidWorks
n Early Binding
– File: swxJRNL.swj

n Use variable/constant/enumeration
names that make sense

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Programming Strategies Tips - Getting Help


n Add comments to your code n SolidWorks API n Visual Basic

n Use existing code – SolidWorks API Help file. – Visual Basic Help file.

– SolidWorks API Forum. – Visual Basic Forum.


n Error trapping.

– SolidWorks API Support. – Online Tutorials


n Plan for use in multiple versions of
SolidWorks. – Be specific! – Be specific!

n Plan for multiple users or – Don’t expect help with programming – Don’t expect help with programming
distribution. techniques. techniques.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

Resources - Installed with SolidWorks Resources - SolidWorks’ website


n SolidWorks Application n API programming tips and updated help files.
Programming Interface.
n SolidWorks Express Newsletter
n SolidWorks API Help file.

n Download projects, sample programs and macros.


n Visual Basic for Applications (VBA)
Version 6.
n SolidWorks Developer Kit. (SDK)
n SolidWorks Help File
– Online Tutorial - SolidWorks API. n SolidWorks World Presentations.

n List of SolidWorks Resellers.

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

7
Resources - Examples (Users) Resources - Books
n Lenny’s SolidWorks Resources Macros, Tips, Blog n Teach Yourself Visual Basic 6 in 24 Hours
– http://www.lennyworks.com/solidworks – Author: Greg M. Perry Publisher: SAMS Publishing
– http://designsmarter.typepad.com/lennyworks/

n SolidWorks API Fundamentals Training Manual


n Matt Lombard Macros, Tips, Blog
– http://mysite.verizon.net/mjlombard/
– SolidWorks Corporation
– Must take an API Training Class
n Stefan Berlitz’s SolidWorks Tools Macros, Tips, Blog
– http://www.swtools.de n Automating SolidWorks 2006 using Macros
– Author: Mike Spens Publisher: Schroff Development Corp.
n Mike Wilson’s Modeling Techniques Macro Feature
– http://www.mikejwilson.com/solidworks/solidworks_files.htm n SolidWorks 2007 Bible ( Not much VB )
– Author: Matt Lombard Publisher: Wiley Publishing, Inc.
n Joe Jones at New Hampshire CAD Tutorials
– http://www.nhcad.com/sw_macros/index.html
n SolidWorks For Dummies ( Not much VB )
n SolidWorks Tips & Things API Tips – Authors: Jankowski/Doyle Publisher: Wiley Publishing, Inc.
– http://www.solidworktips.com/

http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/ http://www.LennyWorks.com/SolidWorks http://designsmarter.typepad.com/LennyWorks/

SolidWorks API
Demystified

Questions & Thanks


Leonard Kikstra
Designer / CAD Administrator
RITE-HITE Products Corporation
Milwaukee, WI

Lenny’s SolidWorks Resources:


http://www.LennyWorks.com/SolidWorks
Blog: http://designsmarter.typepad.com/lennyworks/

You might also like