Sei sulla pagina 1di 5

21/01/2014 SolidWorks API Video Tutorials 7 Mistakes New SolidWorks API Programmers Make

www.cadsharp.com/blog/7-mistakes-new-solidworks-api-programmers-make/ 1/5
7 Mistakes New SolidWorks API Programmers
You are here: Home Blog 7 Mistakes New SolidWorks API Programmers Make
Bl og 5.10.2012 4 Comments
For those who dont know, SolidSmack.com, the largest CAD/CAM blog in the world, recently ran an article on
CADSharp.com in which I answered three questions about the SolidWorks API. The third question was perhaps
the most insightul: What advice do you have for aspiring API progammers? This question is so important that I
decided to expand upon my answer in more depth in this blog post.
Even though this post is geared toward beginners, those who have many macros under their belt but still dont
quite feel like theyve got it should find great insight. So without further adieu, here are 5 pieces of advice I offer
to every engineer who wants to program seriously with the SolidWorks API.
1. Not learning the SolidWorks API with Visual Basic for Applications
(VBA)
VB.NET, C#, and C++ are very powerful languages. However, for macros of low to medium complexity, that
Recent
SolidWorks World 2014 : API Presentations
Video: Write A Powerful Macro from Scratch,
Episode 2
What's New in the SolidWorks 2014 API
Embed and Auto-Run Macros Using Equations
Using Microsoft Excel with the SolidWorks API
Links
3DVision Technologies
Engineer vs Designer
SolidProfessor
SolidSmack
SolidWize
SolidWorks API Forums
HOME HOME VIDEOS VIDEOS MACRO LIBRARY MACRO LIBRARY SERVICES SERVICES CONTACT CONTACT ABOUT ABOUT BLOG BLOG
21/01/2014 SolidWorks API Video Tutorials 7 Mistakes New SolidWorks API Programmers Make
www.cadsharp.com/blog/7-mistakes-new-solidworks-api-programmers-make/ 2/5
power is completely unnecessary. If youre an aspiring API programmer, you probably care about just one thing:
automating your work. Then why fight the steeper learning curve of those other languages? Learn the API with
VBA so you can spend less time learning the programming language itself and more time learning the
SolidWorks API. The API works roughly the same in each language, so once youre ready to move on to a more
complex language, you already have the important stuff under your belt.
Ready to learn VBA programming basics? Check out the free lessons from Unit 1 of our VBA course.
2. Relying On the Macro Recorder
For an API novice, it makes perfect sense to use the macro recorder. Heres a tool, it would seem, that gives
you all of the API calls you need to create a macro. Right? Wrong. For starters, the macro recorder cant record
certain tasks at all. Working with custom properties is an example. Second, relying on the macro recorder is
dangerous because keeps you in the mindset that the API performs tasks the same way you would perform
them manually. Again, this couldnt be further from the truth. The steps required to create a section view or
change a faces color, for example, are entirely different when using the API versus using SolidWorks normally.
Third, the code the macro recorder produces is incredibly sloppy. Its formatted poorly and typically contains
arguments you may or may not want. A much better practice is to use the macro recorder to discover an API
call when you know it might take a while to find in the API Help. Nevertheless, the API Help, not the macro
recorder, should be your go-to resource, as well discuss in the next mistake.
Want to learn more about the uses and limitations of the macro recorder? Check out Lesson 2.1 in our VBA
course.
3. Not understanding how to use the API Help
If you think the API Help is just a stuffy reference for hardcore developers, think again. If you dont know how to
navigate your way around the API Help, youre macros will never move beyond what you create with the macro
recorder or what you copy and paste from someone elses code. Using the Index tab of the local API Help, you
can search topically until you find the API call or interface you need. Every single API call and interface has its
own page describing how to use it properlyfrom arguments to return values to examples to other useful tidbits
of info that you need to know.
Ready to learn how to use the API Help? Check out Lesson 2.2 from our VBA course.
4. Not understanding the SolidWorks API object model
If you really want to go to the next level as an API programmer, you MUST understand the SolidWorks API
Object Model. What is this object model, you ask? Basically, everything you interact with in SolidWorks is
considered an object by the SolidWorks APIa face, an edge, a drawing view, a component, the
FeatureManager tree, a part document, the SolidWorks application itself, and so on. Each of these objects has
a corresponding interface that lets the API talk with that object. Now heres the important part: these objects
are arranged in a hierarchy. So, for example, before you can change the color of a face, you need access to that
faces body. Before you have access to the body, however, you need access to the part document. Before you
have access to the part document, you need access to the SolidWorks application. Get the idea?
21/01/2014 SolidWorks API Video Tutorials 7 Mistakes New SolidWorks API Programmers Make
www.cadsharp.com/blog/7-mistakes-new-solidworks-api-programmers-make/ 3/5
To access different interfaces you need to use special API calls known as accessors. Heres the great thing:
every interface page in the API Help has a list of the accessors that can be used to access that interface. Hence
the importance of knowing how to use the API Help well.
Ready to learn the SolidWorks API Object Model in conjunction with the API Help? Check out Lesson 2.3 from
our VBA course.
5. Not modularizing your code
Modularizing code allows you to easily re-use code. It also makes you a faster programmer and makes your
code less error-prone. Let me explain. Say that you write lots of macros that require you to pull out the value of a
custom property called PartNo. Instead of re-writing that bit of code over and over again, you should create a
separate function (called something like GetPartNo) that is called by your main code. Once youve written
GetPartNo once, you dont need to keep writing it. Instead you can place that function in its own module that
you can import easily to other macros. Since you know it works, you dont have to worry about debugging it in
the future.
Ready to learn how to modularize your code? Check out Lesson 1.7 from our VBA course.
6. Not documenting or formatting your code properly
This isnt just a mistake committed by API programmers but programmers in general. However the mistake is so
serious that I have to mention it again, even if youve heard it before: take the time to format and document your
code properly. Formatting code properly means using appropriate indentation when you nest layers of code
within sub-procedures, conditional statements, loops, and so on. By not doing this you are making your code
very difficult to read.
Documenting your code means using comments to explain the purpose of each section of code. For example, if
you check out any of the macros in our Macro Library, youll notice three things: 1) at the top, an explanation of
what the code does (including preconditions necessary for running the code), 2) comments throughout the macro
explaining what role each portion of code plays, and 3) often times at the time youll see additional notes giving
more insight into a certain API call. The result is code that is much easier for the author and others to
understand. If you dont believe me, just wait until you have to edit a macro that you wrote two years ago. Youll
have no clue what the variables do and will have to spend a large portion of time re-familiarizing yourself with the
code. Its even worse if you werent the one who wrote the code originally.
Ready to learn more about formatting and documenting code? Check out the first four lessons of our VBA
course, or check out our one-hour intro to the API, Taking Macros to the People.
7. Giving up too quickly
As with anything else in life thats worth doing, becoming a good SolidWorks API programmer requires
perseverance. As a programmer, youre going to hit obstacles constantly. With every obstacle, however, is the
opportunity for another victory. Learn to love these small victories. Keep your eye fixed on prizeautomating
your project, impressing your co-workers, expanding your professional repertoire, or whatever it might be. Again,
as with anything else thats challenging, programming with the API gets easier as you practice it more. Once
21/01/2014 SolidWorks API Video Tutorials 7 Mistakes New SolidWorks API Programmers Make
www.cadsharp.com/blog/7-mistakes-new-solidworks-api-programmers-make/ 4/5
jsweeney
Keith
4 Responses To 7 Mistakes New SolidWorks
API Programmers Make
June 9, 2012 at 12:13 AM
Not a mistake, but one that burns me reading code by someone that uses variable names
that have no meaning.
Another one on the same lineand SW help files are bad at this. Using different variable
names from one program to another. Sometimes theyll use swView, others oView, etc. It
makes it hard to reuse code. If you always use the same variable name for a view, cutting and
pasting from one program to another is much faster.
Log in to Reply
June 10, 2012 at 7:36 PM
Thats a good point. Perhaps I should add something on variables to point 6.
In CADSharps macro library, we use the same variable names consistently for exactly the
reason you described.
Log in to Reply
you understand the API Help and Object Model, youll truly be amazed at how quickly you can write macros.
Youll love it! So stick with it, and enjoy the results.
Whether youre a novice presently committing these mistakes or an expert that has long moved past them, Id
love to hear your insight! Share in the comments below.
Keith
Want to stay up-to-date with new CADSharp.com content? Sign up for our newsletter.

SIGN UP SIGN UP
21/01/2014 SolidWorks API Video Tutorials 7 Mistakes New SolidWorks API Programmers Make
www.cadsharp.com/blog/7-mistakes-new-solidworks-api-programmers-make/ 5/5
SolidWorks
API Advice
for
Beginners |
SolidWorks
API
Programming
4ubarkoIIi
September 20, 2013 at 6:42 PM
[] Without the proper foundation, learning the SolidWorks API can be nothing short of frustrating. As a
self-taught SolidWorks API programmer, heres 7 pieces of advice I want to offer to help you on your
learning journey. These guidelines are adapted from 7 Mistakes New SolidWorks API Programmers Make.
[]
Log in to Reply
December 27, 2013 at 2:04 PM
When I started to learn SolidWorks API (6 months ago) I read this article.
However, I didnt put much attention to some points. I ask anyone who wants to start his own
way to keep in mind my mistake! It definetely will safe a lot of hours for you!
Eventually, after a lot of hard working the seven point will mean a lot to you!
Log in to Reply
Questions And Comments
You must be logged in to post a comment.
Search
Search here...
2012 CADSharp LLC

Potrebbero piacerti anche