SolidWorks API Demystified - LennyWorks

Transcription

Who Am I?SolidWorks APIDemystifiedLeonard KikstraDesigner / CAD AdministratorRITE-HITE Products Corp.Milwaukee, WIn Product Designer– Engineering since 1982.– CAD user since 1991.– SolidWorks user since 1998.n CAD Administrator– 20 people on site.– Advise other sites.n Productivity Gains - I want to:– Simplifying my job.– Make my computer do more work forme.– Get the most out of the tools I use todo my job.n Certified SolidWorks Professional(CSWP)Lenny’s SolidWorks //www.LennyWorks.com/SolidWorksWhat 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 macron SolidWorks API Object ttp://www.LennyWorks.com/SolidWorksn Lenny’s SolidWorks Resources– Online since Sept. 2003.– Free Macros, Tips & Tricks– LennyWorks Blog Since 2007oIncludes Macro Tutorialsn Hobbyist programmer since 1981– Programmed over 45 SolidWorksmacros.– Most of these are available for freeon the Internet.– Experiment with programming.– I rarely get paid for my ks/What is covered here?http://www.LennyWorks.com/SolidWorksn SolidWorks User Group– SolidWorks Milwaukee AreaResource Team (SMART)– http://www.smart-wi.com– Active member since hat 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 ad.com/LennyWorks/1

Benefits of learning and using.Basic Definitionsn 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 SolidWorksn SolidWorks ignsmarter.typepad.com/LennyWorks/SolidWorks APIAPI’sn Why does SolidWorks make tureWorksPDMWorks WorkgroupPhotoWorksSolidWorks Document ManagerSolidWorks RoutingSolidWorks ToolBoxSolidWorks ks/Accessing / Programming– Syntax (OLE Automation)ooVisual Basic (VB)Visual Basic.NETVisual Basic for Applications (VBA)n Stand alone application– Executable–––––n SolidWorks Macro– .swp file– Microsoft Visual Basic ksVisual BasicVisual Basic .Net.Visual C Visual C .NetVisual CoInstalled with rks/Macro Toolbarn SolidWorks Help File /Accessing / Programmingn Many SolidWorks Add-Ins .LennyWorks.com/SolidWorksn Run Macro– Tools, Macro, Run.– Syntax (COM)oooVisual CVisual C Visual C .NET– Macros (.swp)n Record / Pause– Tools, Macro, Record.– StopRunNewEditn Tools, Macro, Stop– Save recorded macro.n New Macro– Tools, Macro, New.– Launches macro editor.StopRecord / Pausen Edit Macro– Tools, Macro, Edit.– Launches macro d.com/LennyWorks/2

Macro RecorderA Look Inside a Macro Filen Records Actionsn SolidWorks Objectsn Getting Startedn Formsn Discovery Tooln Modulesn Programming Aidn Class rd / arter.typepad.com/LennyWorks/Visual Basic Editor – References / Type LibrariesVisual Basic Editor – References / Type Librariesn What are they?n What libraries may be needed:n How to set / change– SolidWorks 2008 Type libraryn Notes:– Set during macro recording.– Specific to each major SolidWorksrelease.– Must be set for each macro.– SolidWorks 2008 Constant typelibrary– SolidWorks 2008 Commands typelibrary– SolidWorks 2008 exposed typelibraries for add-in m/LennyWorks/Running a Macro – Where does the macro start?Testing a Macron Run Macro– Last “public” procedure of the lastmodule inserted into the macro.n [ F5 ] - Run macro.– Start in current procedure.n Toolbar Button– User must set module and routineas starting point.– Only “public” routines are listed.n Hotkey definition– User must set module and routineas starting point.– Only “public” routines are designsmarter.typepad.com/LennyWorks/n [ F8 ] - Step thru code– Line-by-Line.n Yellow bar – Current positionn Red bar – Stop at this ignsmarter.typepad.com/LennyWorks/3

Running a Macro – Typical StepsSolidWorks Help Filen Launch Macro.n Common– See Also– Example– Availabilityn Attach to SolidWorks.n Document Loaded and Type.n Objects– Methods– Properties– Events– Accessorsn User Form Initialization.n Process User Request.n Clean-Up and End orks/Why won’t some recorded macros work?Macro may not work on other computers?n Obvious reasons:n These SolidWorks settings mayn Not so obvious reasons:affect macro performance– SolidWorks Version.– Some selections may not have beenrecorded.– Automatic Relationships– Macro requires preselections.– Automatic Inferencing– Selected objects may not exist.– Snap to Points to Grid.– Work may already be done.– SnapEnable / Snap Options.– Type Libraries.– Macro improperly started.– Insert dimension value on creation.– Use Fully Defined Sketches.n Other users probably set thesedifferent than what you are used m/LennyWorks/4

Make your macro work on other computers!Researchn These SolidWorks settings mayn Every macro you write will requiren How to check for these effectsaffect macro performancesome research.– Toggle settings.– Automatic Relationships– Re-test macro.– Automatic Inferencingn Visual Basic– Online tutorials– Help file– Verify results.– Snap to Points to Grid.n How to bypass these effects– SnapEnable / Snap Options.– Capture current setting.– Insert dimension value on creation.n SolidWorks– Learning the SolidWorks API– Document Structure– SolidWorks API Add-Ins and HelpFile– Change setting as needed.– Use Fully Defined Sketches.– Change setting back.n Other users probably set thesedifferent than what you are used to.http://www.LennyWorks.com/SolidWorksn Existing macrosn KEEP YOUR MACRO designsmarter.typepad.com/LennyWorks/Visual Basic Programming - ObjectsUser Interface: UserForm vs. PropertyManagerPagen What are they?n UserFormBeginner– More flexible with more options.– Display lots of options to user.– Large form can get in the way.n Can be accessed or modified by amacro or external program.n PropertyManagerPageIntermediate / 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 ad.com/LennyWorks/Progressive Learning TrackProgressive Learning Trackn 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 LennyWorks/5

Progressive Learning TrackProgressive Learning Trackn 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 pad.com/LennyWorks/Progressive Learning Track - Exploring FurtherTips - Learn by Examplen Other Reference Libraries of Interestn Learn by examplen Use Object Browser to see what isavailable within each library.– Use Object Browser to see what isavailable within library.– Common Dialog ControloAccess and use common Windowscontrols and dialog boxes.– DS: OLE Document PropertiesObject LibraryooEdit document/file properties withoutMS Office or SolidWorks installed.Search, download and installDsoFileSetup .– Find a good macro, and study how itworks.oUse [F8] in VB Editor to step thrucode line-by-line.– SolidWorks Help FileoOver 490 Visual Basic examples– VBA PropertyManagerPage.swpoBest example for implementingSolidWorks’ PropertyManagerPage– Microsoft Scripting Runtimeohttp://www.LennyWorks.com/SolidWorksFile System marter.typepad.com/LennyWorks/Tips - Learn by Example - Included ExamplesTips - Learn by Exploringn FramedBox macro– Geometry creation– PropertyManagerPage– Includes articles on multiple stagesof development.n Experimentn CommonNotes– Read data and settings from anexternal file– UserForm controls.n ChannelPMP– PropertyManagerPagen DelRevTriangles– Traversing thru a SolidWorksdrawingn QuickSlot / QuickRectangle macros– Geometry creation– Setting common optionsn Toggles ( Quick & Dirty )– No interface.– Simple error checking.– Write “do nothing” macros for VisualBasic.– Write “do nothing” macros forSolidWorks API.– Save your “experiments” for lateruse.n More ennyWorks/6

Tips - Learn by ExploringProgramming Strategiesn 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 tocomplete tasks– SolidWorks API object model.n SolidWorks Journal File– drive \Documents and Settings\ username \Application Data\SolidWorks– File: swxJRNL.swjn Use subroutines and functionsn Early Bindingn Use variable/constant/enumerationnames that make com/LennyWorks/Programming StrategiesTips - Getting Helpn Add comments to your coden SolidWorks APIn Visual Basicn Use existing code– SolidWorks API Help file.– Visual Basic Help file.n Error trapping.– SolidWorks API Forum.– Visual Basic Forum.– SolidWorks API Support.– Online Tutorials– Be specific!– Be specific!– Don’t expect help with programmingtechniques.– Don’t expect help with programmingtechniques.n Plan for use in multiple versions ofSolidWorks.n Plan for multiple users r.typepad.com/LennyWorks/Resources - Installed with SolidWorksResources - SolidWorks’ websiten SolidWorks Applicationn API programming tips and updated help files.Programming Interface.n SolidWorks API Help file.n Visual Basic for Applications (VBA)n SolidWorks Express Newslettern Download projects, sample programs and macros.Version 6.n SolidWorks Developer Kit. (SDK)n SolidWorks Help File– Online Tutorial - SolidWorks API.n SolidWorks World Presentations.n List of SolidWorks epad.com/LennyWorks/7

Resources - Examples (Users)Resources - Booksn Lenny’s SolidWorks Resources– http://www.lennyworks.com/solidworks– , Tips, Blogn Matt Lombard– http://mysite.verizon.net/mjlombard/Macros, Tips, Blogn Stefan Berlitz’s SolidWorks Tools– http://www.swtools.deMacros, Tips, Blogn SolidWorks API Fundamentals Training Manual– SolidWorks Corporation– Must take an API Training Classn Automating SolidWorks 2006 using Macros– Author: Mike Spens Publisher: Schroff Development Corp.n Mike Wilson’s Modeling TechniquesMacro Feature– http://www.mikejwilson.com/solidworks/solidworks files.htmn Joe Jones at New Hampshire CAD– http://www.nhcad.com/sw macros/index.htmlTutorialsn SolidWorks Tips & Things– http://www.solidworktips.com/API Tipshttp://www.LennyWorks.com/SolidWorksn Teach Yourself Visual Basic 6 in 24 Hours– Author: Greg M. Perry Publisher: SAMS rks/n SolidWorks 2007 Bible ( Not much VB )– Author: Matt Lombard Publisher: Wiley Publishing, Inc.n SolidWorks For Dummies ( Not much VB )– Authors: Jankowski/Doyle Publisher: Wiley Publishing, ignsmarter.typepad.com/LennyWorks/SolidWorks APIDemystifiedQuestions & ThanksLeonard KikstraDesigner / CAD AdministratorRITE-HITE Products CorporationMilwaukee, WILenny’s SolidWorks :http://designsmarter.typepad.com/lennyworks/8

- Visual Basic - Visual Basic .Net. - Visual C - Visual C .Net - Visual C . n Visual Basic - Online tutorials - Help file n SolidWorks - Learning the SolidWorks API - Document Structure . - Microsoft Scripting Runtime o File System Object