The Syntax of a SOLIDWORKS “IF” Statement in an Equation

Article by Jim Peltier, CSWE updated February 21, 2017


Hypothetical scenario: you have a design for a container wherein you have a length, width, and height. The length and width can be anything, but the height is driven indirectly by the length. If the length is greater than a certain value, then the height increases to a different value.

An IF statement would be great for this – except that I’m not using C++, I’m using SOLIDWORKS. No worries, we can still use that, but more on that in a minute.

Since it’s difficult to describe in the abstract, I’ll assign some numbers. My container is 4″ x 6″, and it’s 2″ tall. If the length drops below 2″, I want the height to drop to 1″ (otherwise, it might be top-heavy and tip over like my Christmas tree). So, if the Length > 2, then I want the height to be = 2, otherwise make the length = 1. While many programming languages form the syntax for this a bit different, the one that is most relevant to us here is how this is expressed in Excel:


That is to say:

if (Length >2, make this 2, otherwise make it 1)

This is all nice and good, but how do we apply this to the height of our container in SOLIDWORKS? If we double-click the Height dimension, then type = on our keyboard, we get access to the Equations dropdown. Under Functions, you will find IF() near the bottom. Here’s a screenshot:



Now, you might be tempted to go ahead and do this, even going so far as to typing Length. However, if you haven’t declared Length as a Global Variable then you won’t get very far doing it this way. An easier way to proceed is while you’re typing the if statement in the Modify Dimension dialogue window, just click the Length dimension. This will automatically populate the correct full name of the dimension in the equation. This is what I get:

=if( "Length@Sketch1">2,2,1)

When I clicked the 6.00 dimension, it automatically wrote “Length@Sketch1” into the equation.

Obviously, I recommend renaming the dimensions and sketches BEFORE you create your equations. In SOLIDWORKS 2017 SP0 it automatically fixes the references in this example, but other examples and versions may vary in their results. If you must rename afterwards, please do so sparingly, test thoroughly, and keep a backup copy of your file on hand.

Related Links

Certified SOLIDWORKS Services available from Javelin

Javelin can help you to:

Find Related Content by TAG:

Jim Peltier, CSWE

Jim has been using SolidWorks since 2001, and has spent most of that time working in the design of industrial automated manufacturing equipment. He has been working as an Applications Expert at Javelin Technologies in Oakville, Ontario since July 2012 and is a Certified SolidWorks Expert (CSWE).