Making Macros in SOLIDWORKS - Engineers Rule PDF
Making Macros in SOLIDWORKS - Engineers Rule PDF
Next Up
JOIN OUR
NEW SLETTER
Signup today for free and be the first
to get notified on new updates.
Enter Email
The API enables users to create custom programs (macros) that can assist them in
automating many of the tasks of the graphical user interface (GUI). This can be as
simple as creating PDFs of several drawings to more complex tasks such as generating
complete parts and assemblies from code. This article will walk through some of the API
basics by creating a simple extrusion.
Getting Started
It will be helpful to have the macro toolbar visible to access the main macro/API
features.
Next Up
JOIN OUR
NEW SLETTER
Signup today for free and be the first
to get notified on new updates.
Enter Email
Right-clicking the menu bar area will bring up the menuJ Oshown
IN OUR
in Figure 1. Selecting
“Macro” from this menu will bring up the toolbarNEW
shown SLETTER
in Figure 2.
Signup today for free and be the first
to get notified on new updates.
Enter Email
(http://www.engineersrule.com/wp-content/uploads/2016/11/image002-6.png)Figure
I agree to receive occasional updates and special offers. 2.
MBD Implementation Dos and Don’ts: Verify and
The Macro toolbar. Validate MBD Data
Subscribe
(https://www.engineersrule.com/mbd-
Recommended Content
implementation-dos-donts-verify-validate-
Below is a legend for the icons shown in the toolbar: Privacy Policy
mbd-data/)
(https://www.engineering.com/Home/PrivacyPolicy/tabid/197/Default.asp
Descending the Grand Canyon in a Wheelchair
Oboe Wu (https://www.engineersrule.com/author/oboe-wu/) •
(https://www.engineersrule.com/descending-the-grand-canyon-in-a-
Nov 17
wheelchair/) No thanks
(http://www.engineersrule.com/wp-content/uploads/2016/11/ping.png)The quickest
way to make a macro is to simply press the record button
(http://www.engineersrule.com/wp-content/uploads/2016/11/image005-5.png)and start
some action, such as creating a sketch. However, in this example, we will rst create a
simple macro from scratch to ensure that the software’s installation is properly
con gured.
From the software’s menu, select Tools > Macro > New, which will create a blank macro
template like the one shown in Figure 3. Tradition would have you print “Hello World”
from the macro and the code in Figure 3 does that.
Next Up
JOIN OUR
NEW SLETTER
Signup today for free and be the first
to get notified on new updates.
(http://www.engineersrule.com/wp-content/uploads/2016/11/image007-3.png)Figure 3.
Enter Email
VBA code for Hello World.
I agree to receive occasional updates and special offers.
MBD Implementation Dos and Don’ts: Verify and
Validate MBD Data
Subscribe
(https://www.engineersrule.com/mbd-
Recommended Content
implementation-dos-donts-verify-validate-
Privacy Policy
mbd-data/)
(https://www.engineering.com/Home/PrivacyPolicy/tabid/197/Default.asp
Descending the Grand Canyon in a Wheelchair
Oboe Wu (https://www.engineersrule.com/author/oboe-wu/) •
(https://www.engineersrule.com/descending-the-grand-canyon-in-a-
Nov 17
wheelchair/) No thanks
(http://www.engineersrule.com/wp-content/uploads/2016/11/image008-3.png)
While this code does not perform any drawing or modeling, it does ensure that all the
support and reference les are in place to continue building more advanced macros.
Also, to ensure that all the API components are installed, go to Tools > Reference and the
appropriate references should appear as shown in Figure 4.
(http://www.engineersrule.com/wp-content/uploads/2016/11/image009-3.png)Figure
4.Visual Basic for Applications reference libraries.
Next API
The most common programming language for interfacing with the Up is Visual Basic
and in this example, we will create a simple macro to give you an overview of this
JOIN OUR
process.
NEW SLETTER
As mentioned, the quickest way to create macros is to for
Signup today record them.
free and be Begin
the firstby pressing
to get notified on new updates.
the record (http://www.engineersrule.com/wp-content/uploads/2016/11/image005-
Enter Email
5.png) button, drawing a centered rectangle and making an extrusion, which will produce
the block shown in Figure 5. I agree to receive occasional updates and special offers.
Enter Email
This code may appear cryptic because it contains much more information than is
necessary to generate the block. This is a consequence of using the macro recorder and
the accompanying machine-generated code.However, it is possible to perform some
limited automation with this generated code. You can identify which lines are creating
the major features of the extrusion and edit this code to create another version of the
Next Up
block. For example, the parameters in the CreateCenterRectangle method (see Figure 6)
can be changed to different (XYZ) values.The macro can
J O Ibe
N Orerun
UR leaving all the other
lines in the code as they are to generate the block
NEWwith the new dimensions that were
SLETTER
entered.
Signup today for free and be the first
to get notified on new updates.
Creating a Macro from Scratch
Enter Email
'(1)
vSkLines = sketchManager.CreateCenterRectangle(0, 0,
J O0,
I N1,
O 1,
U R1)
(http://www.engineersrule.com/wp-content/uploads/2016/11/image012-2.png)Figure 7.
Drop-down menu for object-speci c properties.
Next Up
NEW
The sketching tools can be accessed through the SLETTER
SketchManager interface. The
sketchManager object can be accessed through
Signup the ModelDoc2
today for free andinterface,
be the firstand since Part
to get notified on new updates.
is of type ModelDoc2,it has SketchManager as a property. But how do we know this?
Enter Email
At this point, wewill make a brief introduction to the API help documents. Figure 8 shows
I agree to receive occasional updates and special offers.
an excerpt from the help le for the SketchManager object interface.
MBD Implementation In the
Dos and Accessors
Don’ts: Verify and
Subscribe Validate MBD Data
section, we see that ModelDoc2 has access to the SketchManager interface.
(https://www.engineersrule.com/mbd-
Recommended Content
implementation-dos-donts-verify-validate-
Privacy Policy
mbd-data/)
(https://www.engineering.com/Home/PrivacyPolicy/tabid/197/Default.asp
Descending the Grand Canyon in a Wheelchair
Oboe Wu (https://www.engineersrule.com/author/oboe-wu/) •
(https://www.engineersrule.com/descending-the-grand-canyon-in-a-
Nov 17
wheelchair/) No thanks
(http://www.engineersrule.com/wp-content/uploads/2016/11/image013-2.png)Figure 8.
Help le documentation for the SketchManager interface.
This leads us to the documentation for the ModeDoc2 interface, which is excerpted in
Figure 9. It has a SketchManager property that provides access to the SketchManager
interface. Along with a good grasp of object-oriented programming (OOP), the help le is
the greatest resource for creating advanced macros.
(http://www.engineersrule.com/wp-content/uploads/2016/11/image014-2.png)Figure
Next Up 9.
Help le documentation for the ModelDoc2 interface.
JOIN OUR
Section (4)
This is where the actual block extrusion takes place. FeatureExtrusion3 is a member of
the FeatureManager interface. We have not explicitly de ned a FeatureManager object,
but we can access it by chaining off of the Part object, which does have a
FeatureManager property.
Conclusion
The SOLIDWORKS (http://www.solidworks.com/) API provides access to the software’s
modeling engine through programming methods. It is an e cient method for automating
frequently used modeling functions.
In this article, we created a basic shape by recording a macro to generate code capturing
mouse movements and keystrokes. We then created the code from scratch, which
resulted in a more streamlined and readable program.
Descending the Grand Canyon in a Wheelchair The Top Ten Hits of SOLIDWORKS 2021
(https://www.engineersrule.com/descending- (https://www.engineersrule.com/the-top-ten-
the-grand-canyon-in-a-wheelchair/) hits-of-solidworks-2021/)
Chris Fox (https://www.engineersrule.com/author/chris-fox/) • Stephen Petrock
Dec 08 (https://www.engineersrule.com/author/stephen-petrock/) •
Sep 24
What the Power of the Cloud Brings to Engineering 4 Reasons Why You Should Care About the New
and CAD (https://www.engineersrule.com/what- 3DEXPERIENCE SOLIDWORKS Offers
the-power-of-the-cloud-brings-to-engineering- Next Up
(https://www.engineersrule.com/4-reasons-
and-cad/) why-you-should-care-about-the-new-
Lynn Allen (https://www.engineersrule.com/author/lynn-allen/) 3dexperience-solidworks-offers/)
JOIN OUR
• Sep 17 Lynn Allen (https://www.engineersrule.com/author/lynn-allen/)
NEW SLETTER • Jul 27
Enter Email
JOIN OUR
SHOW MORE (HTTPS://WWW.ENGINEERSRULE.COM/)
NEW SLETTER
Signup today for free and be the first
Tags
to get notified on new updates.
3DEXPERIENCE (https://www.engineersrule.com/tag/3dexperience/)Enter
3DEmail
modeling (https://www.engineersrule.com/tag/3d-modeling/)
Popular Stories
Next Up
JOIN OUR
NEW SLETTER
Signup today for free and be the first
Choosing a Mobile Workstation for
to get notified on new updates.
SOLIDWORKS SOLIDWORKS 2016 Adds Tools to Help
What’s New in
(https://www.engineersrule.com/choosing- Simulations and Validation Template Edit
Enter Email
mobile-workstation-solidworks/) (https://www.engineersrule.com/solidworks-
(https://www
CAD 2016-adds-tools-to-help-simulations-
(HTTPS://WWW.ENGINEERSRULE.COM/CATEGORY/CAD/)
I agree to receive occasional updates and special offers. new-solidwo
and-validation/)
MBD Implementation Dos and Don’ts: Verify and
template-ed
SIMULATION Validate MBD Data
Subscribe
(HTTPS://WWW.ENGINEERSRULE.COM/CATEGORY/SIMULATION/)
CAD
(https://www.engineersrule.com/mbd- (HTTPS://WWW.EN
Recommended Content
implementation-dos-donts-verify-validate-
Privacy Policy
mbd-data/)
(https://www.engineering.com/Home/PrivacyPolicy/tabid/197/Default.asp
Descending the Grand Canyon in a Wheelchair
Oboe Wu (https://www.engineersrule.com/author/oboe-wu/) •
(https://www.engineersrule.com/descending-the-grand-canyon-in-a-
Nov 17
wheelchair/) No thanks
© Copyright 2018 ENGINEERING.com - Sponsored by Dassault Systèmes
Next Up
JOIN OUR
NEW SLETTER
Signup today for free and be the first
to get notified on new updates.
Enter Email