Video: Write A Powerful Macro From Scratch

Blog 11.16.2012 1 Comment

The time has come for another free video tutorial! In this tutorial, novice SolidWorks API programmers learn just how easy it is to create a powerful macro from scratch in under a half hour. In this particular example, we write a macro that deletes all of the balloons attached to Toolbox parts in a selected drawing view. The idea came from the SolidWorks API forums, where a gentleman said that such a macro would save him lots of time as he detailed drawings.

Unlike some of my other videos, I don’t explain every single piece of code (though I explain enough to help any beginners), but instead I get right down to business, outlining the steps needed to program the macro and testing the code each step of the way. As you watch this video, you should get a better idea of how I write powerful macros quickly and hopefully glean some insights as a result.

Files

Download file set and completed code (files will open in SolidWorks 2011 and beyond)

Notes:

  • The file set is not the exact same one used in the video tutorial since I needed to quickly create a new file set for compatibility in SolidWorks 2011 and 2012.
  • If you do not have toolbox installed, or do not have it installed at C:\SolidWorks Data, simply create that folder on your C:\, place the hex bolt part file in there, and update your file references accordingly the next time you open the assembly.

I hope you found this tutorial helpful. Feel free to leave feedback!

Keith

Want to keep up with future CADSharp.com content, webinars, and special offers? Sign up for our newsletter.


Leave Comment

What’s New in the SolidWorks 2013 API

Blog 9.25.2012 4 Comments

Unless you’re a die-hard SolidWorks enthusiast that gobbles up the first beta release, chances are you haven’t seen SolidWorks 2013 or are just beginning to explore your freshly downloaded pre-release copy. As with any new releases, SolidWorks 2013 contains some great new “Top Enhancements” to increase design productivity. If you haven’t already, I’d encourage you to watch this short video covering those. Likewise, the SolidWorks API receives its own enhancements each year. Let’s check out some of the most significant.

Microsoft VBA support for 64-bit operating systems

Thank you, Microsoft. This one, my friends, is long overdue. Since VBA in previous versions was a 32-bit process, it could not run in the same thread as 64-bit SolidWorks. This created an annoyance when implementing user forms into one’s VBA macros that resulted in the user form appearing BEHIND the SolidWorks application window rather than in front. Fixing the problem required adding a significant amount of code to your sub-procedure. Say goodbye to this headache in 2013!

This upgrade to VBA is also significant because it proves that VBA is still important to Microsoft. Our SolidWorks VBA macros will remain functional for a long, long time. Indeed, this update to VBA isn’t a mere patching of existing version of VBA but an entirely new version called VBA 7.0. When you use the SolidWorks 2013 VB Editor you’ll also notice a slight facelift to the user interface. Nothing amazing, but at least it looks less like something straight out of 1995.

Notable new API interfaces and calls
  • SolidWorks Costing now includes an API. Use IModelDocExtension::GetCostingManager to return the ICostManager interface.
  • SolidWorks Sustainability now includes an API. Use IModelDocExtension::GetSustainability to return the ISustainabilityApp interface.
  • Insert cosmetic weld beads and access their feature data. See ICosmeticWeldBeadFeatureData, ICosmeticWeldBeadFolder, and IFeatureManager::InsertCosmeticWeldBead.
  • Create a forming tool feature with a point of insertion. See IFeatureManager::CreateFormTool2.
  • Get or set whether to apply the appearance of material. See IMaterialVisualPropertiesData::ApplyAppearance.
  • Copy appearances to the clipboard and apply them to faces, features, bodies, components, and parts. See ISldWorks::CopyAppearance and ISldWorks::PasteAppearance.
  • Create a conic curve in the active sketch. See ISketchManager::CreateConic.
  • More options for background processing large drawings that require significant time to open, including new event notifications for when the processing is complete. See IDrawingDoc::BackgroundProcessingOption, DSldWorksEvents::BackgroundProcessingEndNotifyEventHandler, and DSldWorksEvents::BackgroundProcessingStartNotifyEventHandler.
  • Handle drawing sheet activation events. See DDrawingDocEvents::ActivateSheetPostNotifyEventHandler and DDrawingDocEvents::ActivateSheetPreNotifyEventHandler.
  • Get or set the state of the flyout FeatureManager design tree. See IModelDocExtension::FlyoutFeatureTreeVisibility.
  • Make a component virtual by saving it in an assembly. See IComponent2::MakeVirtual.
  • Vary individual instances of linear and circular feature patterns. See IFeatureManager::FeatureLinearPattern3, IFeatureManager::FeatureCircularPattern4, and IFeatureManager::InsertVaryInstanceIncrement, IFeatureManager::InsertVaryInstanceOverride.
  • Get the unique name of a section view. See IView::GetUniqueName.
  • More control over component visibility in Large Design Review mode. See IAssemblyDoc::LargeDesignReviewTransparencyLevel, IAssemblyDoc::LargeDesignReviewTransparencyLevelDynamic, and IAssemblyDoc::LargeDesignReviewTransparencyLevelEnabled.
  • Display notes as water-marks in drawings. See INote::BehindSheet.
  • Insert a note that contains all of the cut list item properties of a sheet metal part. See IView::InsertCutListPropertyNote.
  • Access the centers of mass in a drawing view. See ICenterOfMass.
  • Insert Center of Mass reference geometry and Center of Mass Reference Points. See IFeatureManager::InsertCenterOfMass and IFeatureManager::InsertCenterOfMassReferencePoint.
  • Create multiple exploded views of an assembly. See IAssemblyDoc::CreateExplodedView.
  • Convert views to sketches and blocks. See IView::InsertViewAsBlock, IView::ReplaceViewWithSketch, and IView::ReplaceViewWithBlock.
  • Get the sheet metal folder feature from sheet metal models created in SolidWorks 2013 or later. See IModelDocExtension::GetTemplateSheetMetal. For SolidWorks 2012 and earlier, continue using ISheetMetalFeatureData.
  • Create a 3D bounding box for a cut list item in a weldment part. See IModelDocExtension::Create3DBoundingBox.

You can view the entire list of new functionality here.

Easter eggs
  • The swSelectType_e enumeration page now has the corresponding integer values in the Description column, making it much easier to figure out what object type ISelectionMgr::GetSelectedObject6 is returning.

The new appearance-related functionality and event notifications are welcome additions. What new functionality stands out to you?

Onward into a 64-bit world,
Keith

Want to keep up with future CADSharp.com content, webinars, and special offers? Sign up for our newsletter.


Leave Comment

Interview With API Expert Jeff Sweeney

Blog 9.12.2012 No Comments


How do long-time SolidWorks API experts use their skills within the engineering industry? In an effort to answer that question, I am pleased to introduce Jeff Sweeney of 3DVision Technologies, a Great Lakes region SolidWorks reseller, where he proudly wears the title “Engineering Data Specialist”. Though much of his work involves Enterprise PDM, he has extensive experience with the SolidWorks API as well. I greatly appreciate him giving us some of his time to share his opinion of the SolidWorks API, his most notable current and past projects, and his advice for aspiring API programmers.

Jeff, you have told me in the past how much you enjoy working with the SolidWorks API. What do you enjoy most about it?

Jeff: I’ve been programming in one form or another for over thirty years. I love the feeling you get when you finally get your program to work after being stuck for several hours, then when you finally get it working, the program can do something faster than anyone has ever been able to do before. I’ve written several programs that have over a year’s worth of man hours of programming in them and it is very satisfying to know that even years later people are still making money from them.

What are some of the more interesting projects you have worked on involving the API?

Jeff: The biggest project I am currently working on is an add-in for SolidWorks Enterprise PDM. This program isn’t too far from being able to make coffee for the owner first thing in the morning. It interfaces not only with the PDM data but external databases, SolidWorks, Visio, Excel, Eagle, Word, .rar files and Adobe files. Passing data back and for to each of these applications in their native tongues has been pretty cool to make happen.

Like many of us, you’re a self-taught programmer. When did you begin learning CAD APIs and how did you learn them?

Jeff: My first experience with API was with LSP inside of AutoCAD. I worked at a company that already had many custom LSP routines. I spent many hours studying them, and taking them apart to learn how they work. My first experience with Object Oriented programming was with Microsoft Office’s API. Microsoft Office has a fantastic macro recorder and I used it along with other macros I found.

Have you ever used the APIs of any other CAD packages? If yes, How does the SolidWorks API compare to them in terms of power or ease of use?

Jeff: I’ve used APIs for many applications, honestly after you learn the basics they are all pretty much the same. When I first started with the SolidWorks API, I had been using AutoCAD’s API for several years, back then I would have told you the SolidWorks API was way more complicated than it needed to be. However I was comparing manipulating 2D geometry to manipulating 3D geometry. I have since learned that pesky third dimension has a way to make everything more complicated.

Complete this sentence: If the SolidWorks API were a vehicle, it would be a ________

Jeff: “Smooth, clean Shelby Cobra with a GPS that can only see a limited number of satellites.” As long as you stay on the beaten path, you’ll go fast, smooth and look good while you are doing it. If you start doing things that few people have ever done before, there are fewer examples, fewer people to help you, the help file is skimpier and some of the properties and methods can get you lost deep in the forest.

That is a really great analogy and certainly reflects my own experience as well. Finally, do you have any advice for aspiring API programmers?

Jeff: The typical answers to this question certainly apply: Be patient and persistent, read everything you can, wash your hands before eating and keep a good snippet library. Specifically, my favorite technical tip is to keep your main routine as small you can. An ideal main routine only calls subroutines and functions. Along those same lines, keep your subroutines and functions as simple as possible. My ideal subroutine is one that I can pull out of the program, and test its input/outputs as a simple routine that can stand on its own. You should also always look for opportunities to create classes to simplify your subroutines even further. Simple building blocks of code are much easier to maintain and debug. Lastly, remember that a program is never as simple as you think it is going to be before you start. If it was easy everyone would be doing it.

That is some sage advice, Jeff, and I couldn’t agree more. Thanks again for your time and good luck with your current projects.

Jeff: Thanks, Keith. Thanks for your web site, as well. I really enjoy its content and it is a huge time saver.

Want to keep up with future CADSharp.com content, webinars, and special offers? Sign up for our newsletter.


Leave Comment