Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sww 2008 Automating Your Designs Excel, Vba And Beyond


Published on

This SolidWorks World 2008 presentation from Paul Gimbel of Razorleaf Corporation focuses on the use of Microsoft Excel as a tool to power SolidWorks design automation.

Published in: Technology
  • Be the first to comment

Sww 2008 Automating Your Designs Excel, Vba And Beyond

  1. 1. Automating Your Designs Excel, VBA, and Beyond Paul Gimbel Razorleaf Corporation
  2. 2. OK, Fine. Here’s What We’ll Do <ul><li>Obligatory Introductions </li></ul><ul><li>Overview – Automation: Why? How Much? Who? </li></ul><ul><li>Background For Our Example </li></ul><ul><li>Automation Choices </li></ul><ul><li>Excel Can Do That? </li></ul><ul><li>Gimme a V! Gimme a B! Gimme an A! </li></ul><ul><li>Commercially Available Solutions </li></ul><ul><li>How To Choose </li></ul><ul><li>Questions and (if you’re lucky) Answers </li></ul>
  3. 3. Not Sure If You Should Stay Here or Go See Michael Craffey’s “Drawings For The Masses”? <ul><li>Free Code!!! </li></ul><ul><li>New ways of approaching automation with the same tools </li></ul><ul><li>What can you do with SolidWorks? </li></ul><ul><li>What can you do with SolidWorks and Excel? </li></ul><ul><li>What can you do with SolidWorks, Excel and VBA? </li></ul><ul><li>What can you do if you have money? </li></ul><ul><li>(Michael’s talking about DXF’s, PDF’s and eDrawings) </li></ul>
  4. 4. DISCLAIMER!!! More Information Than Time <ul><li>There is so much that you can do with this stuff!! </li></ul><ul><li>Liz wouldn’t give me a 14 hour session </li></ul><ul><li>I may have to talk faster than the guy at the end of a car commercial </li></ul><ul><li>Everything is documented in the PowerPoint </li></ul><ul><ul><li>Download it from the SolidWorks World Site </li></ul></ul><ul><ul><li>Download it from the Razorleaf Site (link on the handout) </li></ul></ul><ul><ul><li>Give me a business card and I’ll email it to you </li></ul></ul><ul><ul><li>Read the Presenter Notes </li></ul></ul><ul><li>Come on up after the session with any questions </li></ul><ul><li>Take my business card...take a few and hand them around </li></ul>
  5. 5. Paul Gimbel, Business Process Sherpa <ul><li>Yes, that is my official title </li></ul><ul><li>Certified SolidWorks Professional (since program inception) </li></ul><ul><li>Certified SolidWorks Trainer/Support Technician (10 years) </li></ul><ul><li>Only Certified DriveWorks Enterprise Implementer </li></ul><ul><li>RuleStream Implementer (Certification Program Pending) </li></ul><ul><li>VB and SolidWorks API developer as needed </li></ul><ul><li>Business Development Leader – Design Automation Group </li></ul><ul><li>Business Development Leader – Business Process Group </li></ul><ul><li>Plays Well With Others…most of the time </li></ul>
  6. 6. Where To Start <ul><li>“It’s All About The Process” – The Sherpa </li></ul><ul><li>The most important thing to understand is the design process </li></ul><ul><li>Immediately followed by the notion that it might change </li></ul><ul><li>Map out your current process </li></ul><ul><ul><li>AS IT IS PERFORMED! Not how the ISO document says it should be performed. </li></ul></ul><ul><ul><li>Ask around to the people that actually do the work </li></ul></ul><ul><li>Understand and prioritize your goals </li></ul><ul><ul><li>Time to market? Customizability? Standardization? Just lazy? </li></ul></ul><ul><li>Plan your vision – What will life look like? </li></ul>
  7. 7. Yup. This Slide AGAIN Process Technology From “Technology Induced Change And The Line Manager”, Dr. Eliot Levinson Mutual Adaptation Pure Automation Flexible Technical Implementation Non-Implementation Rigid Flexible Rigid
  8. 8. “It’s All About The Process” <ul><li>Don’t just write a macro to push the same buttons </li></ul><ul><li>Re-evaluate what SolidWorks can really do for you </li></ul><ul><li>See the two previous (hidden) slides </li></ul><ul><ul><li>Download the presentation and open it in PowerPoint </li></ul></ul><ul><li>See my two previous SolidWorks World presentations </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>Go see Matthew Cummins “Planning for Change: Tips and Tricks for Design Automation” @ 4:30 today </li></ul>
  9. 9. Reality Check <ul><li>A couple automation realities you should know about: </li></ul><ul><li>100% automation is generally unachievable </li></ul><ul><li>Typically, the last 10% of automation will not be worth it </li></ul><ul><li>Customers will always request something you didn’t plan for </li></ul><ul><li>Your drawings are probably going to require clean-up </li></ul><ul><li>People will fight your efforts </li></ul><ul><li>Your end users will be even stupider than you imagined </li></ul><ul><li>Testing will take you far longer than you expect </li></ul><ul><li>Your system will require ongoing maintenance and updates </li></ul>
  10. 10. Your Options <ul><li>Pure SolidWorks functionality </li></ul><ul><li>SolidWorks Macros </li></ul><ul><li>Microsoft Excel (and some VBA, of course) </li></ul><ul><li>Maximized use of Excel and VBA </li></ul><ul><li>Custom developed application </li></ul><ul><li>Commercially available solution partner product </li></ul><ul><li>AutoDesk Inven…nah, who are we kidding. </li></ul>
  11. 11. Pure SolidWorks <ul><li>Configurations </li></ul><ul><li>Design Tables </li></ul><ul><li>Smart Components </li></ul><ul><li>Mate References </li></ul><ul><li>Design Checker </li></ul><ul><li>SolidWorks Task Scheduler </li></ul><ul><li>DriveWorksXpress </li></ul><ul><li>FeatureWorks </li></ul>
  12. 12. What Do You Mean SolidWorks Has Limitations!?!? <ul><li>All of these functions require interaction with the user </li></ul><ul><li>SolidWorks is required </li></ul><ul><ul><li>Great for your reseller, not so great for your budget </li></ul></ul><ul><ul><li>Puts non-engineers in an uncomfortable environment </li></ul></ul><ul><ul><li>Requires at least some SolidWorks training </li></ul></ul><ul><ul><li>Do you really want a salesperson opening your models!?!? </li></ul></ul><ul><li>Data management issues </li></ul><ul><ul><li>One file with tons of configurations? </li></ul></ul><ul><ul><ul><li>How big is that file, eh? </li></ul></ul></ul><ul><ul><ul><li>Multiple people can’t edit one file </li></ul></ul></ul><ul><ul><ul><li>Revision questions </li></ul></ul></ul><ul><ul><li>Save As Copy every time? </li></ul></ul><ul><ul><ul><li>What about updates? </li></ul></ul></ul>
  13. 13. SolidWorks Macros <ul><li>Designed to drive the SolidWorks User Interface </li></ul><ul><ul><li>Not all that automation-friendly </li></ul></ul><ul><li>OK to use for small functions </li></ul><ul><ul><li>Some 5 or 6 step operation that you want to hotkey </li></ul></ul><ul><li>Can be tough to share due to peoples individual settings </li></ul><ul><li>Often requires specific pre-selections </li></ul><ul><li>Everything plays on the screen (video performance aspect) </li></ul>
  14. 14. The Road To Excel <ul><li>Design Tables alone don’t address </li></ul><ul><ul><li>User Interface </li></ul></ul><ul><ul><li>Input values as opposed to output values </li></ul></ul><ul><ul><li>Advanced Excel functionality </li></ul></ul><ul><li>Leverage Excel </li></ul><ul><ul><li>Use Excel User Interface tools </li></ul></ul><ul><ul><li>Add-Ins (Analysis Pack, Solver, etc.) </li></ul></ul><ul><ul><li>Data validation </li></ul></ul><ul><ul><li>User Defined Functions </li></ul></ul><ul><ul><li>Macros </li></ul></ul>
  15. 15. Excel and SolidWorks – So Happy Together <ul><li>Three Methods To Integrate SolidWorks and Excel </li></ul><ul><ul><li>Design Table </li></ul></ul><ul><ul><ul><li>Embedded in the SolidWorks model </li></ul></ul></ul><ul><ul><ul><li>Must edit through SolidWorks </li></ul></ul></ul><ul><ul><ul><li>Edit Table In New Window to gain access to Excel </li></ul></ul></ul><ul><ul><ul><li>MUST BE IN THE DESIGN TABLE SHEET WHEN YOU EXIT!! </li></ul></ul></ul><ul><ul><li>External Spreadsheet with Macro to drive SolidWorks </li></ul></ul><ul><ul><ul><li>Requires SolidWorks API programming </li></ul></ul></ul><ul><ul><ul><li>Separates workbook from SolidWorks model </li></ul></ul></ul><ul><ul><ul><li>Does not require SolidWorks (until macro is run) </li></ul></ul></ul><ul><ul><ul><li>Shareable (through Excel Share Workbook functionality) </li></ul></ul></ul><ul><ul><ul><li>Can be used to drive multiple SolidWorks files </li></ul></ul></ul><ul><ul><ul><li>Can be used to drive other things as well, not just SW </li></ul></ul></ul><ul><ul><li>Spreadsheet with Macro Inserted as OLE Object </li></ul></ul>
  16. 16. What Excel Brings To The Party (a six pack?) <ul><li>Formatting </li></ul><ul><li>Charts </li></ul><ul><li>Design Binder </li></ul><ul><li>Easy User Interfaces </li></ul><ul><li>Advanced equation functionality </li></ul><ul><li>VBA </li></ul><ul><ul><li>Custom functions </li></ul></ul><ul><ul><li>Macros </li></ul></ul><ul><ul><li>Object-Oriented Programming (well, sort of) </li></ul></ul>
  17. 17. Just A Little Bit Of Work Makes A Huge Difference Some People’s Idea of an Excel User Interface Less Than Twenty Minutes Of Work
  18. 18. Building User Interfaces in Excel <ul><li>Data Validation </li></ul><ul><ul><li>Lists, error and warning dialogs, tool tips </li></ul></ul>Input Messages Error Messages Warning Messages In-Cell Dropdowns
  19. 19. Building User Interfaces in Excel <ul><li>Data Validation </li></ul><ul><ul><li>Lists, error and warning dialogs, tool tips </li></ul></ul><ul><li>Conditional Formatting </li></ul><ul><ul><li>Control user’s attention based on input </li></ul></ul>
  20. 20. Building User Interfaces in Excel <ul><li>Data Validation </li></ul><ul><ul><li>Lists, error and warning dialogs, tool tips </li></ul></ul><ul><li>Conditional Formatting </li></ul><ul><ul><li>Control user’s attention based on input </li></ul></ul><ul><li>Tools, Protection </li></ul><ul><ul><li>Unlock/show only cells you want them to be able to change </li></ul></ul>
  21. 21. Building User Interfaces in Excel <ul><li>Control Toolbox </li></ul><ul><ul><li>VB-style input devices that can fill in your cells </li></ul></ul><ul><li>Data Validation </li></ul><ul><ul><li>Lists, error and warning dialogs, tool tips </li></ul></ul><ul><li>Conditional Formatting </li></ul><ul><ul><li>Control user’s attention based on input </li></ul></ul><ul><li>Tools, Protection </li></ul><ul><ul><li>Unlock/show only cells you want them to be able to change </li></ul></ul>
  22. 22. Building User Interfaces in Excel <ul><li>Control Toolbox </li></ul><ul><ul><li>VB-style input devices that can fill in your cells </li></ul></ul><ul><li>Data Validation </li></ul><ul><ul><li>Lists, error and warning dialogs, tool tips </li></ul></ul><ul><li>Conditional Formatting </li></ul><ul><ul><li>Control user’s attention based on input </li></ul></ul><ul><li>Tools, Protection </li></ul><ul><ul><li>Unlock/show only cells you want them to be able to change </li></ul></ul><ul><li>Pictures, Diagrams, Charts </li></ul><ul><ul><li>Help users understand what information they need to input </li></ul></ul>F dMax
  23. 23. What Excel Brings To The Party (a six pack?) <ul><li>Formatting </li></ul><ul><li>Charts </li></ul><ul><li>Design Binder </li></ul><ul><li>Easy User Interfaces </li></ul><ul><li>Advanced equation functionality </li></ul><ul><li>VBA </li></ul><ul><ul><li>Custom functions </li></ul></ul><ul><ul><li>Macros </li></ul></ul><ul><ul><li>Object-Oriented Programming (well, sort of) </li></ul></ul>
  24. 24. Calculations in Excel <ul><li>Name Your Cells and Ranges </li></ul><ul><ul><li>Just as in SolidWorks, it makes equations easier to read </li></ul></ul><ul><li>Add Comments </li></ul><ul><ul><li>In adjoining cells or with the comment tool </li></ul></ul><ul><li>Keep your formulas simple whenever possible </li></ul><ul><ul><li>Multiple calculation steps are easier to troubleshoot </li></ul></ul><ul><li>Intelligent Layouts </li></ul><ul><ul><li>Don’t scatter stuff all over the place </li></ul></ul><ul><ul><li>Consider cells that may need to be adjacent for lookups, etc. </li></ul></ul><ul><ul><li>Give this collection of cells a single name </li></ul></ul>
  25. 25. Working with Ranges in Excel <ul><li>Index: Return the value at row n , column m </li></ul><ul><li>Match: Which row is this value in? </li></ul><ul><ul><li>Specify which order values are in (asc/desc) or exact matches </li></ul></ul><ul><ul><li>Combine with Index to do a lookup on any column </li></ul></ul><ul><li>VLookup/HLookup: Find value in first column/row </li></ul><ul><ul><li>RangeLookup = FALSE: “No, I only want exact matches” </li></ul></ul><ul><ul><li>RangeLookup = TRUE: “Yes, find me the closest match” </li></ul></ul>VLookup(110,VoltageRange,2,FALSE) = 1.73 VLookup(290,VoltageRange,2,FALSE) = N/A# VLookup(359,VoltageRange,2,TRUE) = 3.48 360 2.9 240 1.73 110 Impedance Voltage 2.9 Searches until it finds a value that is “greater than” the search value, then goes back to take the previous one.
  26. 26. Dealing With Unknown Quantities <ul><li>Count: How many numbers are in the range? </li></ul><ul><li>CountA: How many non-blank cells are in the range? </li></ul><ul><li>Columns/Rows: How many columns/rows are in the range? </li></ul><ul><li>DGet: Return any value in a range based on criteria </li></ul>Criteria1 Criteria2 Criteria3 Criteria 4 Range “Holes” DGet(Holes,”Dia”,Criteria1) = 0.250 DGet(Holes,”Number”,Criteria2) = 2 DGet(Holes,”Number”,Criteria3) = #NUM! DGet(Holes,”Dia”,Criteria4) = #VALUE!
  27. 27. More Cool Excel Functions <ul><li>Indirect: Display range/cell based on a formula </li></ul><ul><li>Ceiling/Floor: Rounds up/down to the next multiple </li></ul><ul><li>SumIf: Totals all of the values in a range that meet a criteria </li></ul><ul><ul><li>=SumIf(ThicknessRange,”>0.25”,PriceRange) </li></ul></ul><ul><li>DSum: Add values from records that meet a criteria </li></ul><ul><li>Error Trapping: </li></ul><ul><ul><li>IsErr </li></ul></ul><ul><ul><li>IsError </li></ul></ul><ul><ul><li>Error.Type </li></ul></ul><ul><ul><li>IsNA </li></ul></ul><ul><ul><li>IsBlank </li></ul></ul><ul><ul><li>IsLogical </li></ul></ul><ul><ul><li>IsNumber </li></ul></ul><ul><ul><li>IsText </li></ul></ul><ul><ul><li>IsNonText </li></ul></ul><ul><ul><li>Type </li></ul></ul>
  28. 28. What Excel Brings To The Party (a six pack?) <ul><li>Formatting </li></ul><ul><li>Charts </li></ul><ul><li>Design Binder </li></ul><ul><li>Easy User Interfaces </li></ul><ul><li>Advanced equation functionality </li></ul><ul><li>VBA </li></ul><ul><ul><li>Custom functions </li></ul></ul><ul><ul><li>Macros </li></ul></ul><ul><ul><li>Object-Oriented Programming (well, sort of) </li></ul></ul>
  29. 29. OK, Enough Easy Stuff…On To The VBA <ul><li>Use VBA to AUGMENT Excel </li></ul><ul><ul><li>Custom functions </li></ul></ul><ul><ul><li>Macros </li></ul></ul><ul><ul><li>Connections to other programs (like SolidWorks) </li></ul></ul><ul><li>Do As Much As You Can In Excel </li></ul><ul><ul><li>Easier to troubleshoot </li></ul></ul><ul><ul><li>Easier to maintain </li></ul></ul><ul><ul><li>Faster to develop </li></ul></ul>
  30. 30. Custom Functions – Repetitive Calculations <ul><li>Tired of typing in the same equations? </li></ul><ul><li>Public Function CylVolume(Radius As Double, Height As Double) As Double </li></ul><ul><ul><li>Dim Pi As Double </li></ul></ul><ul><ul><li>Pi = 4 * Atn(1) ‘ArcTangent of 1 = pi/4…I think </li></ul></ul><ul><ul><li>CylVolume = Pi * Radius * Raidus * Height </li></ul></ul><ul><li>End Function </li></ul><ul><li>Now you can just use: </li></ul><ul><li>= CylVolume(Radius, Height) </li></ul><ul><li>or </li></ul><ul><li>=CylVolume($B$4, C5) </li></ul><ul><li>(if you haven’t been paying attention and you don’t know that you’re supposed to name your cell ranges) </li></ul>
  31. 31. Custom Functions – Fill in Excel Gaps <ul><li>Excel missing a function you want? No problem! </li></ul><ul><li>Public Function Divisible(Value As Double, Divisor As Double) As Boolean </li></ul><ul><li> Dim Compare As Integer </li></ul><ul><ul><li>Compare = CInt(Value / Divisor) </li></ul></ul><ul><ul><li>If Compare = (Value / Divisor) Then </li></ul></ul><ul><ul><li>Divisible = True </li></ul></ul><ul><ul><li>Else </li></ul></ul><ul><ul><li>Divisible = False </li></ul></ul><ul><ul><li>End If </li></ul></ul><ul><li>End Function </li></ul><ul><li>Now all I do is put THIS in my cell: </li></ul><ul><li>=If(Divisible(Length,StdSpacing),Length / StdSpacing,0) </li></ul>Note : Because we’re converting a Double to an Integer, this only works for numbers up to 32767. This was not an issue in my application as the model only supported up to 48”. But you may want to put a check in there so you don’t get overflow errors.
  32. 32. Custom Functions - Conversions <ul><li>What about converting units or data types? (or both!) </li></ul><ul><li>Public Function FeetInches(TotalInches As Double) As String </li></ul><ul><ul><li>Dim Feet As Integer </li></ul></ul><ul><ul><li>Dim Inches As Double </li></ul></ul><ul><ul><li>Feet = Application.WorksheetFunction.RoundDown(TotalInches / 12,0) </li></ul></ul><ul><ul><li>FeetInches = Feet & “’ “ </li></ul></ul><ul><ul><li>Inches = (TotalInches – (Feet * 12)) </li></ul></ul><ul><ul><li>If Inches > 0 Then </li></ul></ul><ul><ul><li>FeetInches = FeetInches & “- “ & Inches & chr(34) ’(34 is ASCII for “) </li></ul></ul><ul><ul><li>End If </li></ul></ul><ul><li>End Function </li></ul><ul><li>Now you can use the equation: </li></ul><ul><li>=“LENGTH = “ & FeetInches(Length) </li></ul><ul><li>To get: </li></ul><ul><li>LENGTH = 4’ – 6.125” </li></ul>The example in your handout actually picks up on increments of 1/32”
  33. 33. Custom Functions - Handling Sets of Data <ul><li>Excel Cannot Really Handle Arrays Of Data </li></ul><ul><ul><li>It can do ranges, but ranges need a known number of cells </li></ul></ul><ul><li>Paul’s Solution: </li></ul><ul><ul><li>Store lists of values in a single cell (ex. “1.25|17|21.1375|9”) </li></ul></ul><ul><ul><li>Create a series of functions to manipulate these lists </li></ul></ul><ul><ul><ul><li>ListQty – How many entities are in the list? </li></ul></ul></ul><ul><ul><ul><li>GetListValue – Give me the n th member of the list. </li></ul></ul></ul><ul><ul><ul><li>FindValueInList – Is this value in the list? Where? </li></ul></ul></ul><ul><ul><ul><li>SortList, FlipList, InsertIntoList – Control the order </li></ul></ul></ul><ul><ul><ul><li>RangeToList – Convert a range of cells into a list </li></ul></ul></ul>Don’t like the pipe (|) character? Just change it to whatever you like in the SPLIT/JOIN commands.
  34. 34. How To Program In VBA <ul><li>The Ultimate Process For Non-Programmers: </li></ul><ul><li>Determine what you want your program/code snippet to do </li></ul><ul><li>Think of some “keywords” to describe it (if you know the functions you need to use, you’re ahead of the game) </li></ul><ul><li> (add “VBA” and/or “Excel” to search) </li></ul><ul><li>Ctrl-C </li></ul><ul><li>Ctrl-V </li></ul><ul><li>Tweak </li></ul>Also look to Excel VBA and SW API Help files. They’re actually helpful. I know! I was shocked, too. Also check out
  35. 35. Programming Tips For Excel <ul><li>DOCUMENT THE $#^@ OUT OF EVERYTHING!!! </li></ul><ul><ul><li>Anything after an apostrophe (‘) is a comment…use them!! </li></ul></ul><ul><li>Option Explicit </li></ul><ul><ul><li>It’s the “Require Fully Defined Sketches” of the VBA world </li></ul></ul><ul><li>Intellisense…good. Typing…bad. </li></ul><ul><ul><li>If you don’t see the option that you need, something’s wrong. </li></ul></ul><ul><li>Compile and Test Often </li></ul><ul><ul><li>Programmatic sanity checks </li></ul></ul><ul><ul><li>Insert Test Code </li></ul></ul><ul><li>Use Debug Tools </li></ul><ul><ul><li>Step Into, Step Over, Watches, Breakpoints </li></ul></ul>
  36. 36. The Object Browser
  37. 37. Getting Around In Excel With VBA <ul><li>To access a named cell (you named them all, right!!?!?) </li></ul><ul><li>Thickness = Worksheets(“Calcs”).Range(“TestCell”).Value </li></ul><ul><li>Running through the cells in a named range </li></ul><ul><li>Dim MyCell As Excel.Range ‘Even a single cell is treated as a range </li></ul><ul><li>For Each MyCell in Worksheets(“Calcs”).Range(“TestRange”) </li></ul><ul><li>Total = Total + MyCell.Value </li></ul><ul><li>Next MyCell ‘Looping through the TestRange </li></ul><ul><li>What if you have rows (i.e. a multi-column range)? </li></ul><ul><li>Dim MyRow As Excel.Range ‘We’re going to cycle through the rows </li></ul><ul><li>For Each MyRow in Worksheets(“Calcs”).Range(“TestRange”).Rows </li></ul><ul><li>Total = Total + MyRow.Cells(1,3).Value ‘Row 1, Column 3 of the range </li></ul><ul><li>Next MyRow ‘Looping through the TestRange </li></ul>
  38. 38. Absolute Row and Column References <ul><li>Can I just put in Row and Column references, like: </li></ul><ul><li>Thickness = Worksheets(“Sheet1”).Range(“A1:A1”).Value </li></ul><ul><li>Thickness = Worksheets(“Sheet1”).Cells(4,9).Value </li></ul><ul><li>Sure, but I would have to slap you silly </li></ul><ul><ul><li>Readability…Minus 10 style points </li></ul></ul><ul><ul><li>What if the range moves…Minus 10 style points </li></ul></ul><ul><ul><li>Debugging…Minus 10 style points </li></ul></ul><ul><ul><li>Scalability…Minus 10 style points </li></ul></ul><ul><ul><li>Portability…Minus 10 style points </li></ul></ul><ul><ul><li>Chances of anyone else understanding your code…Minus 30! </li></ul></ul>
  39. 39. Object-Oriented Programming (or some facsimile thereof) <ul><li>One of the hardest, most confusing concepts in programming (Or so I’m told) </li></ul><ul><li>Class – A definition of something like a window or bolt </li></ul><ul><li>Property – A parameter value that an object has </li></ul><ul><li>Method – Something that an object can do </li></ul><ul><li>Collection – A bunch of objects, like an array, just of objects </li></ul><ul><li>Dot (.) – Something you will type a lot of </li></ul><ul><ul><li>Separates an object from its property or method </li></ul></ul><ul><ul><li>Ex. Bolt.Length or Window.Pane.item(“top”).Glass.Thickness </li></ul></ul><ul><li>Instantiate – To make a real thing as defined by a class </li></ul><ul><ul><li>Ex. Upper Flange Bolt #3 or Kitchen Window Over Sink </li></ul></ul><ul><ul><ul><li>Those are then called “instances” and “objects” </li></ul></ul></ul>
  40. 40. The SolidWorks API is Sort Of Object-Oriented <ul><li>Example: </li></ul><ul><li>Dim swApp As SldWorks.SldWorks </li></ul><ul><li>Dim swModel As SldWorks.ModelDoc2 </li></ul><ul><li>Dim swFeatMgr As SldWorks.FeatureManager </li></ul><ul><li>Dim swFeat As SldWorks.Feature </li></ul><ul><li>Dim swWzdHole As WizardHoleFeatureData2 </li></ul><ul><li>Set swApp = Application.SldWorks </li></ul><ul><li>Set swModel = swApp.ActiveDoc </li></ul><ul><li>Set swFeatMgr = swModel.FeatureManager </li></ul><ul><li>… </li></ul><ul><li>Set swWzdHole = swFeatMgr.CreateDefinition(swFmHoleWzd) </li></ul><ul><li>swWzdHole.HoleDepth = 0.15 </li></ul><ul><li>swWzdHole.HoleDiameter = 0.0555 </li></ul><ul><li>… </li></ul><ul><li>Set swFeat = swFeatMgr.CreateFeature(swWzdHole) </li></ul><ul><li>… </li></ul>Early Bound Object Declarations aka “Fully Qualified” (+5 Style Points) Object assignment (instantiation) requires the keyword Set HoleDepth and HoleDiameter are Properties of the WizardHoleFeatureData2 object CreateDefinition and CreateFeature are Methods (functions)
  41. 41. Object-Oriented Programming and You (an example) <ul><li>Windows are made up of components </li></ul><ul><ul><li>Glass Panes </li></ul></ul><ul><ul><ul><li>How many panes? </li></ul></ul></ul><ul><ul><ul><ul><li>Dunno. 1, 2, 3, maybe 4 </li></ul></ul></ul></ul><ul><ul><ul><li>How thick are they? </li></ul></ul></ul><ul><ul><ul><ul><li>Each one could be different. </li></ul></ul></ul></ul><ul><ul><ul><li>What kind of glass? </li></ul></ul></ul><ul><ul><ul><ul><li>Each one could be different. </li></ul></ul></ul></ul><ul><ul><ul><li>Complex algorithms required </li></ul></ul></ul><ul><ul><li>Other parts, too </li></ul></ul><ul><ul><ul><li>But we’ll just focus on the glass </li></ul></ul></ul><ul><ul><li>Product is ever evolving </li></ul></ul>
  42. 42. Option 1: Loooooong Lists <ul><li>Assume the MAXIMUM number of components </li></ul><ul><li>Brute force all of the values out </li></ul>Imagine this with a dozen instances, each with a dozen parameters! That’s … um … carry the three … plus eleven … over e to the x … That’s a LOT of cells! (yes, I know it’s 144, unless you’re counting the unit and label cells in which case it’s 432, but if you don’t count the blank cells, then it’s an even 300, plus another 12 for the component title cells, which technically are one because they’re merged, but you can count them as three if you want making 336.)
  43. 43. Option 1: Macro Code <ul><li>Your macro code has a limitation built in </li></ul><ul><ul><li>For Counter = 1 to 4 Do… </li></ul></ul><ul><li>You constantly have to test to see how many panes there are </li></ul><ul><ul><li>If Worksheets(“Calculations”).range(“Thickness3”).Value = “” Then… </li></ul></ul><ul><li>If you add the ability for another pane, you’re searching all through the code to replace the 4’s with 5’s </li></ul><ul><li>Your programming is in one long lump o’code </li></ul><ul><li>Lots of variables, or constant read/write to worksheets </li></ul><ul><li>Debugging is a lot harder, more variables to watch </li></ul>
  44. 44. Option 2: Object-Oriented Approach <ul><li>Window Object has GlassPane objects </li></ul><ul><ul><li>How many? An unlimited number!!! </li></ul></ul>Each window has a Window object, an object that holds all of the GlassPanes and an object for each GlassPane Table can expand as much as it needs to
  45. 45. Option 2: Macro Code <ul><li>No quantity limits!! </li></ul><ul><ul><li>For Counter = 1 to Window.GlassPanes.Count Do </li></ul></ul><ul><li>No guessing how many panes there are </li></ul><ul><li>You already have the ability for an unlimited number of panes, no code upgrades needed </li></ul><ul><li>Programming is nice an organized </li></ul><ul><ul><li>Write it once, use it as many times as you want </li></ul></ul><ul><li>No extra variables, no need to constantly write to the worksheets </li></ul><ul><ul><li>Window.GlassPanes.Item(n).Thickness </li></ul></ul><ul><li>Watching an object shows all of its properties (easy debug) </li></ul>
  46. 46. OOP Excel Style: The Basics <ul><li>Think of your class structure like your assy structure </li></ul><ul><ul><li>In most cases </li></ul></ul><ul><li>Right-Select, Insert, Class Module </li></ul><ul><li>Define your properties and methods </li></ul><ul><li>Instantiate as needed </li></ul><ul><li>Always use fully qualified names </li></ul><ul><ul><li>ex. Window.GlassPanes.Item(n).GlassPane.Thickness </li></ul></ul><ul><ul><li>Easier to read and debug (yes, it’s longer) </li></ul></ul><ul><ul><li>Intellisense keeps you from having to type </li></ul></ul>
  47. 47. Defining a Class <ul><li>Standard methods to put in: </li></ul><ul><ul><li>Class_Initialize: Gets run each time this class is instantiated </li></ul></ul><ul><ul><li>Public Sub Class_Initialize() </li></ul></ul><ul><ul><li>Anything you want to happen automatically </li></ul></ul><ul><ul><li>End Sub </li></ul></ul><ul><ul><li>Name Property: Can be used to identify an item in a collection </li></ul></ul><ul><ul><ul><li>Have a variable in the class called sName (indicates it’s a string) </li></ul></ul></ul><ul><ul><li>Private sName As String </li></ul></ul><ul><ul><li>Public Property Get Name () As String </li></ul></ul><ul><ul><li>Name = sName </li></ul></ul><ul><ul><li>End Property </li></ul></ul><ul><ul><li>Public Property Let Name (uName As String) </li></ul></ul><ul><ul><li>sName = uName </li></ul></ul><ul><ul><li>End Property </li></ul></ul>Get allows you to get the value Let allows you to set the value
  48. 48. Collection Classes <ul><li>Class_Initialize creates the collection </li></ul><ul><li>Private mcolGlassPanes As Collection </li></ul><ul><li>Private Sub Class_Initialize () </li></ul><ul><ul><li>Set mcolGlassPanes = New Collection </li></ul></ul><ul><li>End Sub </li></ul><ul><li>Add </li></ul><ul><li>Private Sub Add(uGlassPane As GlassPane) </li></ul><ul><li>mcolGlassPanes.Add uGlassPane </li></ul><ul><li>End Sub </li></ul><ul><li>Remove </li></ul><ul><li>Private Sub Remove (ByVal varID As Variant) </li></ul><ul><li>mcolGlassPanes.remove varID </li></ul><ul><li>End Sub </li></ul>You can pass this an index number or the value of the Name property
  49. 49. Alternate Add Method <ul><li>Instead of passing an object, you can just pass values </li></ul><ul><li>Private mGlassPane As GlassPane </li></ul><ul><li>Public Sub Add(uThickness As Double) </li></ul><ul><li>Set mGlassPane = New GlassPane </li></ul><ul><li>mGlassPane.Thickness = uThickness </li></ul><ul><li>mcolGlassPanes.Add mGlassPane </li></ul><ul><li>End Sub </li></ul>
  50. 50. Collection Classes (Continued) <ul><li>Item: allows you to refer to a single element of the collection </li></ul><ul><li>Public Function Item(ByVal varID As Variant) As GlassPane </li></ul><ul><li>Set item = mcolGlassPanes(varID) </li></ul><ul><li>End Function </li></ul><ul><li>Count: Returns the number of elements in the collection </li></ul><ul><li>Public Get Count() As Long </li></ul><ul><li>Count = mcolGlassPanes.Count </li></ul><ul><li>End Property </li></ul>You can pass this an index number or the value of the Name property
  51. 51. Ready? Set? … Instantiate!! <ul><li>To add another panel (Using a nice User Interface) </li></ul><ul><li>Private Sub cmdAddPane_Click() </li></ul><ul><li>Dim iName As String ‘The input name </li></ul><ul><li>Dim iType As String ‘The input type </li></ul><ul><li>Dim iThk As Double ‘The input thickness </li></ul><ul><li>iName = Worksheets(“Input”).Range(“InputName”).Value </li></ul><ul><li>iType = Worksheets(“Input”).Range(“InputType”).Value </li></ul><ul><li>iThk = Worksheets(“Input”).Range(“InputThk”).Value </li></ul><ul><li>cPanes.Add iName, iThk, iType </li></ul><ul><li>Worksheets(“Input”).Range(“InputName”).Value = “” </li></ul><ul><li>Worksheets(“Input”).Range(“InputType”).Value = “” </li></ul><ul><li>Worksheets(“Input”).Range(“InputThk”).Value = 0 </li></ul><ul><li>Msgbox(“You have successfully added a pane! Aren’t you just so cool?”) </li></ul><ul><li>End Sub </li></ul>*Coating property removed from code due to space, but it’s done the same way. *
  52. 52. Auto-Instantiation <ul><li>Take a whole list of cell values and create the panes: </li></ul><ul><li>Dim cPanes As GlassPanes </li></ul><ul><li>Dim MyRow As Excel.Range ‘Excel range object </li></ul><ul><li>Dim iName, iType, iCoating As String ‘The input values </li></ul><ul><li>Dim iThk As Double ‘The input thickness </li></ul><ul><li>For Each MyRow in Worksheets(“Input”).Range(“Glass”).Rows </li></ul><ul><li>iName = MyRow.Cells(1,1).Value </li></ul><ul><li>iThk = MyRow.Cells(1,2).Value </li></ul><ul><li>iType = MyRow.Cells(1,3).Value </li></ul><ul><li>iCoating = MyRow.Cells(1,4).Value </li></ul><ul><li>cPanes.Add iName, iThks, iType, iCoating </li></ul><ul><li>Next MyRow </li></ul>
  53. 53. Accessing Your Instances <ul><li>Use Me when referring to the object that you’re in </li></ul><ul><ul><li>Ex. In the code for the Window class, you would use: </li></ul></ul><ul><ul><ul><li>Me.ModelNumber (property of the Window class) </li></ul></ul></ul><ul><ul><ul><li>Me.CalculateRValue (method of the Window class) </li></ul></ul></ul><ul><ul><ul><li>Me.GlassPanes.Count (method of a child of the Window class) </li></ul></ul></ul><ul><li>Use the Item method by passing an index or a Name </li></ul><ul><li>dThk = Me.GlassPanes.Item(3).Thickness </li></ul><ul><li>dThk = Me.GlassPanels.Item(“Outside”).Thickness </li></ul>Real Life Example: Me.Walls.Item(“Front”).Panels.item(Me.Walls.item(“Front”).Panels.Count).CeilingLocks.item(1).LocalLocation
  54. 54. VERY Quick Lesson In Debugging <ul><li>Run the code to see if it works. If it does, great! Otherwise, it will show you where the problem is or just stare at you. </li></ul><ul><li>Use Breakpoints at each major stage to see where it trips </li></ul>Click in this column to add a breakpoint
  55. 55. VERY Quick Lesson In Debugging <ul><li>Run the code to see if it works. If it does, great! Otherwise, it will show you where the problem is or just stare at you. </li></ul><ul><li>Use Breakpoints at each major stage to see where it trips </li></ul><ul><li>Break at the last working area and Step (Shift+F8) through </li></ul><ul><li>If it trips in a function or subroutine, use F8 to step into it </li></ul><ul><li>Still can’t figure it out? Watch the values as you approach the problem spot by hovering over variables or “add watch” </li></ul>
  56. 56. Error Trapping (If It Can Happen, It Will) <ul><li>Test for conditions to ensure that they don’t happen </li></ul><ul><li>Anywhere that something could go wrong </li></ul><ul><ul><li>Bad Input is #1 source of problems </li></ul></ul><ul><ul><li>Calculations Gone Wrong is a close second </li></ul></ul><ul><ul><li>Collection.Count = 0 / Nothing Selected rounds out the trifecta </li></ul></ul><ul><li>On Error GoTo (just in case) </li></ul><ul><li>Public Sub RunMe() </li></ul><ul><li>On Error GoTo errhandler </li></ul><ul><li>… </li></ul><ul><li>errhandler: </li></ul><ul><li>If err.Number > 0 Then msgBox(“Dude! “ & err.Description) </li></ul><ul><li>End Sub </li></ul>Check it out! The errors in VBA are an object.
  57. 57. Where To Go From Here? <ul><li>Build some test code to test portions of your code </li></ul><ul><ul><li>Comment out lines </li></ul></ul><ul><ul><li>Hard code values </li></ul></ul><ul><ul><li>Use msgbox() </li></ul></ul><ul><li>Use a macro to kick everything off </li></ul><ul><ul><li>Right-Select, Add Module to house the macro </li></ul></ul><ul><ul><li>Have the macro be on an event (ex. cmdButton_Click) </li></ul></ul><ul><li>The macro will instantiate your top-level </li></ul><ul><ul><li>Your top-level can instantiate everything else or </li></ul></ul><ul><ul><li>User-interaction can instantiate components as required </li></ul></ul>
  58. 58. Connecting to SolidWorks <ul><li>Add the SolidWorks library to your project </li></ul><ul><ul><li>Tools, Macros, Visual Basic Editor </li></ul></ul><ul><ul><li>Tools, References, SolidWorks 2008 Type Library </li></ul></ul><ul><ul><li>Now you have SW Intellisense </li></ul></ul><ul><li>Define your SolidWorks App </li></ul><ul><li>Dim swApp As SldWorks.SldWorks </li></ul><ul><li>Establish a SolidWorks object </li></ul><ul><ul><li>Attaches to an existing session </li></ul></ul><ul><ul><li>Launches SW if it is not open </li></ul></ul><ul><li>Set swApp = CreateObject(&quot;SldWorks.Application&quot;) </li></ul>
  59. 59. Things To Note About SolidWorks API Programming <ul><li>Fairly well documented in the API Help File </li></ul><ul><li>Macro Record, not really as helpful as everyone thinks </li></ul><ul><li>All units are in METERS!! </li></ul><ul><li>ERROR TRAP EVERYTHING!!! </li></ul><ul><ul><li>Check EVERY object  If Not(Object Is Nothing) Then… </li></ul></ul><ul><ul><li>Check your document type </li></ul></ul><ul><ul><li>Check any preselects </li></ul></ul><ul><ul><li>Verify data types before passing anything to SolidWorks </li></ul></ul><ul><ul><li>Check data types of anything you receive from SolidWorks </li></ul></ul><ul><ul><li>Beware of anything that may require user interaction </li></ul></ul><ul><li>Close everything out when you are done </li></ul>(Not enough time to go in depth into SolidWorks API programming, sorry)
  60. 60. Automation Approaches <ul><li>Use SolidWorks to solve complex geometry calculations </li></ul><ul><ul><li>Mass properties </li></ul></ul><ul><ul><li>Interferences/Clearances </li></ul></ul><ul><ul><li>Multi-dimensional sketches </li></ul></ul><ul><li>Do as much OUTSIDE of SolidWorks as possible </li></ul><ul><ul><li>SolidWorks is sloooooow (comparatively speaking, of course) </li></ul></ul>
  61. 61. So What About VB or .NET? <ul><li>Yeah, I promised I would address it. </li></ul><ul><li>Mostly the same techniques as Excel. </li></ul><ul><li>Slightly better performance </li></ul><ul><ul><li>But SolidWorks is almost always the slowest link </li></ul></ul><ul><li>A little more secure (user’s can’t muck about in the code) </li></ul><ul><li>Extensible to the web ASP.NET </li></ul><ul><li>More difficult to maintain </li></ul><ul><ul><li>Recompiling code </li></ul></ul><ul><ul><li>Requires Visual Studio (or equivalent) and knowledge thereof </li></ul></ul><ul><ul><li>Managing versions of the .NET Framework </li></ul></ul>
  62. 62. Even Better Than Excel VBA? (Maybe) <ul><li>SolidWorks Solutions Partners </li></ul><ul><ul><li>Help with the creation of user interfaces </li></ul></ul><ul><ul><li>Help with the capture and storage of rules </li></ul></ul><ul><ul><li>Help with the driving of SolidWorks models </li></ul></ul><ul><ul><li>Help with the creation of derivative SolidWorks drawings </li></ul></ul><ul><ul><li>Roll out solutions to non-SolidWorks users </li></ul></ul><ul><ul><li>Provide server components to generate the SolidWorks data </li></ul></ul>
  63. 63. What To Look For In A SolidWorks Solution Partner <ul><li>How much programming do you want to do? </li></ul><ul><ul><li>Rules can be expressed in VB.NET, Excel, Proprietary languages </li></ul></ul><ul><ul><li>Some allow programming to supplement the application </li></ul></ul><ul><li>How do you want the users to interact? </li></ul><ul><ul><li>Some run inside of SolidWorks (requires seats, some SW knowhow) </li></ul></ul><ul><li>Do you need multiple people to specify a new unit simultaneously? </li></ul><ul><ul><li>Some run inside of a SolidWorks model, and cannot do this </li></ul></ul><ul><ul><li>Some use SQL Server back ends to help support this </li></ul></ul><ul><li>Where are the models going to be generated? </li></ul><ul><ul><li>Server components free up your machine/require fewer SW seats </li></ul></ul><ul><li>Do you want/need a Internet/intranet browser-based interface? </li></ul>
  64. 64. Thanks for doing your part, you sure are smart! <ul><li>Got Questions? </li></ul><ul><li>Thank you! </li></ul><ul><li>FILL OUT YOUR EVALUATION FORMS </li></ul><ul><ul><li>I get $5 for each one turned in! </li></ul></ul>Paul Gimbel Business Process Sherpa [email_address]