Equations galore

Note: I completely rewrote and updated this post. Read the new version here.

SolidWorks equations are underappreciated.

By using equations, you can make your model more adaptive to changes. You can make it smarter.

Furthermore, you can use equations to evaluate values or mathematical functions, suppress features or use them in custom properties. Even nested IF statements are a possibility.

Using equations to drive a model
Create a smart model using equations

Equations viewer

You can access the equations box by right clicking the equations folder in the feature tree and selecting Manage Equations, by going to Tools > Equations or by clicking the Equations icon in the Tools toolbar.

When the equations folder isn’t visible in the feature tree, you can show it by right clicking in the empty space in the feature tree and selecting Hide/Show Tree Items. There you can set the visibility to Show,  Hide or Automatic.

Once opened, you will see a window like this one:

Equations window
Equations window

So there are a lot of buttons and options here. These are the visible ones:

  • Top row: four different ways of viewing the equations, a search/filter bar, undo/redo and the configuration selector
  • Center: main window with Global Variables, Features and Equations
  • Bottom row: rebuild and other settings, linking the equations to a text file
  • Right: OK, cancel and import/export options

While the ones above are always visible, these are the controls that are usually hidden:

  • Right click in the gray area before an equation to suppress or delete an equation
  • A pop-up menu will appear in the value column. It shows you variables that you can use, mathematical functions and model properties.
  • You can select the properties of that equation for different configurations in the value column as well
  • If the syntax of your equation is correct, a green arrow will appear.
    Four views

The goal of the four views isn’t immediately clear, at least not to me. These are their functions:

  1. Equation View: show all global variables and equations
  2. Sketch Equation View: only shows the globals and equations that are used in sketches
  3. Dimension View: show all globals, equations and all model dimensions
  4. Ordered View: globals and equations, shown in the order that they are solved when they are dependent on each other
Three variable groups

I’ll re-post the equations window so you can clearly see the three groups of variables. That’s what I call them at least, for lack of a title in the window.

Equations window
Equations window
First group: Global variables

A global variable is a variable that you can name and use throughout the part/assembly. All you have to do is type in a name and a value or equation. You can use other variable names when you enter them between double quotes: =”height”*12 would be a working equation when you define the variable “height” as well.

You can use the global variables:

  • when entering a sketch or feature dimension
  • when creating an equation
  • when creating a custom property
  • when creating a weldment
Second group: Features

This group will let you determine the suppression state of features, depending on the outcome of equations.

To enter a value in the first column here, you click in an empty cell and then select a feature from the feature tree. The second column can be used to enter a text like =”unsuppressed” or =if(“var6″>3,”suppressed”,”unsuppressed”)

Third group: Equations

I would rather call this group Dimensions than Equations. You can enter a new item by selecting a dimension in a sketch. You can then create an equation to accompany the dimension.

If you have typed a new equation in a dimension dialog box, the equation will also show up here. The same holds for linked values.

Mathematical functions

There’s a limited, but usually sufficient, set of mathematical functions that you can use in your equations. Once you start entering a value, a popup shows you the possibilities:

  • Trigonometric functions like sin, cos, tan, arctan and cosec
  • Square root: sqr
  • Logarithmic functions: log and exp
  • Value properties : integer (int), absolute value (abs) and sign (sgn)
If, then, else

The final entry in the list of mathematical functions is the IF or IFF function from VBA. When you enter an IF statement it will usually replace it with an IFF.

The difference is explained here, but what is comes down to is that every option is evaluated and one of the results is returned. So dividing by zero will result in an error, even when that side of the if statement isn’t applicable.

You can create functions like:

= if ( “variable1” > 100 , 10 , 100 )

This will make the variable before the equal sign equal to ten if variable1 is greater than 100. If not, it will be set to 100.

Creating nested functions is also possible:

= if ( “var1” > 100 , if ( “var1” > 200 , 30 , 20 ) , 10 )

Controlling the suppression state

A very basic version of suppressing features has been in SolidWorks for years. However you could only suppress a feature by creating an equation like “Feature 1″ =”suppressed”. The help files said it should be used for debugging your equations only.

Since 2014 you can really use the suppression state in an if, then, else function. This video by TriMech Solutions shows it very well:

Configurations

Different configurations can have different sets of equations. You can suppress an equation by right clicking the gray first column.

Older versions would just hide the suppressed equation, but I believe more current versions just gray them out. They are much easier to find that way.

Suppressing equations

You can also configure the equation itself, as shown in this video by GoEngineer.

Other possibilities
  • You can use all kinds of units, you can even combine them. Entering =3 inch + 6 mm should work perfectly. It looks horrible though…
  • Entering an equation in a Design Table can be done as well. Just make sure you start the cell value with an apostrophe so Excel treats it as a string. Example: ‘=”D6@Sketch4″*3
  • You can add a calculated value column to a Bill Of Materials (Javelin howto)
  • When you select Measure as a function in an equation, you can actually add a driven dimension to your model. It doesn’t show up in a sketch or feature. It’s called an annotation, but it won’t be listed there.
Measure tool
Using a measured value for var2
Import, export and linking files

The two buttons labelled Import and Export should speak for themselves. You can create a text file by exporting your equations and you can reuse them using the import function.

In addition, if you want multiple parts to use the same text file with equations, you check the box labelled “Link to external file”. This will then keep the equations in sync. Because of the synchronization, you will have to edit the text file to edit the equations.

Using the API

First of all, to add and edit equations using the API, you need to create an Equation Manager.

Adding an equation can be done using either Add2 (faster, no configuration support) or Add3 (newer, using configs).

Note that to use double quotes inside a string in VBA, you need to add them twice.

Open a part (with sketches called Sketch1 and Sketch2) and run this simple macro to add two equations:

Equation driven curves

This is a feature you can include in all sketches, so 2D and 3D. You can add a curve to a 2D sketch using Tools > Sketch Entities > Equation Drive Curve or by expanding the curve icon in the sketch toolbar. This menu isn’t self-explanatory though:

Adding an equation drive curve
Adding an equation drive curve

Fortunately, the official help page will give you a flying start. As it turns out, you can use X here to calculate Y and enter the start and end value for X. They draw half a circle by entering sqrt( 4 – x ^ 2) in the Y-box,-2 for the start value of x and 2 for the end value.

As a result, creating a straight line can be done by entering a linear function like 2*x+10.

The parametric option lets you define formulas for X and Y separately so you can draw other kinds of funky lines.

The great thing about these equation driven curves is that you can drag them after they have been defined. When you unlock the end points, you can even drag and redefine those.

Once you enter a 3D sketch you will get the option of adding a 3D curve using the menu shown below.

Creating a curve in a 3D sketch
Creating a curve in a 3D sketch, these settings will result in a spiral

In conclusion: curves in 3D can be used to create spirals, springs, staircases and zig-zag lines. Just play with them and see what happens. One day you will probably find a good use for a 3D curve.

Summary

So that’s all I know about equations in SolidWorks. This post has become quite a read due to all the option that SolidWorks has to offer, well done if you read everything up to this point 🙂

Now it’s your turn to draw your own smart Lego blocks (just don’t 3D print them because you’ll only learn that they need to be damn accurate).

Let me know what you think is the best use of equations in the comments. I probably still haven’t covered every aspect of them yet.

Finally, be sure to follow me on Twitter to hear a beep every time I post a new blog entry.