Many of the features in Onshape use sketches. As a result, sketches account for about 35% of the features that are created every day. How do they work and why do they work this way?

If you've used a professional MCAD system before, you likely already understand how Onshape’s sketching works in general – and you likely will have noticed the differences. However, let’s assume that you haven't, so I can explain the foundations of Onshape sketches.

Sketches in Onshape, for now anyway, live on a plane and could be described as “2D sketches.” It is critical that a plane is defined for a sketch. Without one, Onshape won't know where any of the geometry goes. So what happens when you select a plane or planar face? First, Onshape will ensure all the geometry is on that plane. Second, Onshape defines the directions as horizontal and vertical. It does that in a way so sketching on default planes, or planes at angles to them, ought to result in horizontal and vertical directions that make sense.

Once you have a plane, you can start adding geometry and constraining it. A sketch consists of three different things: Geometry, Constraints and Dimensions.

Geometry is all the things that you can see, including text and images. It really doesn't need much explanation. Lines, arcs, circles, ellipses, splines, points, etc. These are all things that someone will recognize, even if they have never used a computer. And that's it. Every sketch is made up of those simple sketch curves.

But Onshape has a rectangle tool. What does that give us? Four lines with a bunch of constraints that ensure that opposite lines are parallel and that neighboring lines are perpendicular. It's the constraints that make it a rectangle, not some special memory that the rectangle tool was used.

This brings us to the real power of the sketch in Onshape: Constraints and Dimensions (which are constraints with a number!). It is constraints that allow us to capture design intent in a sketch. For example, if you draw a corner rectangle, you get four lines plus four coincident constraints to glue the corners together, two pairs of parallel constraints, a perpendicular constraint and a horizontal constraint.

Together, these define the rectangle. In fact, it’s pretty much the same definition of a rectangle that you might learn at school. All except the horizontal constraint – that’s just for convenience.

Let’s stop right now and reflect on a few things that shed light on some ideas underpinning the sketch in Onshape.

## When is a Rectangle Not a Rectangle?

First, why do we have four lines with pairs of end points coincident? The alternative is to have only four points, with each of the points shared by two lines. That would be cleaner in some ways, but we decided on this approach for two main reasons:

1. We wanted lines to have distinct ends that were identifiable; and
2. We didn’t want users to have a second concept to deal with.

This brings us to the second point: The rectangle isn’t a rectangle, it’s really four constrained lines. Want to make a rectangle at an angle? Delete the horizontal constraint. Want to make a parallelogram? Delete the perpendicular constraint. Want to break two of the lines apart at a corner? No special command is needed to break the lines apart, just delete the coincident constraint. Want to make a square? Add an equal constraint between two adjacent lines.

(An aside: Yes, we know that all the coincident constraints clutter the sketch when looking at all the constraints and we have ideas for how to address that in the future.)

Now let’s take a look at how we satisfy (or solve) the constraints in the sketch. Effectively, Onshape sketches have no history whatsoever. If you draw two circles of different sizes and then add an equal constraint to them, then there is no “master” circle. As a result, when you add that constraint I couldn’t tell you which one will change size. However, when Onshape solves the sketch, it will try its best to move as little as possible. So if one of those circles was constrained to a bunch of other geometry, and the other wasn’t, it is more likely that the one that is less constrained will change size.

Oh, I should point out that all of this solving, the heavy lifting anyway, is done up in the cloud. We have some code on the client to provide smoother user interactions, but none of the intensive work is done on the client. This allows us to provide every feature of the sketch on even the least capable of devices and also means that sketches work consistently no matter what device you are using.

## How Onshape Sketches 'Think'

Bearing all this in mind explains some other behavior that can seem annoying or baffling – like sketch fillets. As with rectangles, a sketch fillet doesn’t remember that it is a fillet. Sketch fillet is a tool that does a bunch of stuff to the sketch: It adds an arc, adds some coincident and tangency constraints and a radius dimension. Then, when it is done, you have some constrained geometry. If you later change the dimension of the fillet, then you may expect the lines to remain where they are and only the arc to move. What is more likely to happen is that one of the lines will move. Why? Because that’s the best solution Onshape found given the change – and because it has no idea that moving the arc would be “better.”

We considered adding “extra cleverness” to the sketch for such cases, but in our experience the more we try to get fancy with the constraint system for simple cases, the more likely it is going to come back and bite us with more complex cases. If you don’t like the way your sketch changes – i.e. “this line shouldn’t move” – then it’s probably best to incorporate that design intent with a dimension or constraint.

All this sounds great, right? We can make a bunch of shapes and we can include design intent so that the sketch updates in a useful way. That might be fine for the first sketch. But as anyone who had modeled in Onshape after our first six months of development would tell you, it’s unusable later on.

Why? Let’s assume we’ve got an extruded circle and we want to put a hole in the top of the cylinder, smack bang in the middle of that circular face. In the earliest pre-alpha days of Onshape, a sketch couldn’t refer to anything outside itself. Even making the hole in the first place required the user to eyeball the position and then if they changed the earlier feature so that the cylinder moved, then the hole wouldn’t line up any more. Not good.

Today, it’s easy. You have two options: Project the circular edge into the sketch and make another circle that is concentric to that; or better yet, sketch a circle and make it concentric to the edge. You could then even dimension the distance from that edge. We call these “external references” and they are very powerful.

## The Power of External References

How do external references work? First, let’s consider the simpler case where you select an edge (or vertex) and a sketch entity and add a constraint. When you do this, Onshape takes that part geometry and projects it into the sketch plane. To picture this, set up your viewing direction to be directly perpendicular to the sketch. Now imagine tracing over that edge on your monitor. That resulting geometry is what you see “projected” into the sketch. If the model edge is a circle that is slanted so that it looks like an ellipse, then what the sketch sees IS an ellipse.

Where the power is most evident is on the regeneration of the model. If I change an earlier feature so that the edge moves, then the sketch will re-solve to the newly projected curve. All design intent is maintained and everyone is happy. Usually!

If that change means that the dimensions and constraints cannot be solved, then the sketch will warn you that it can’t be solved and any constraint that it doesn’t like will be highlighted. What to do in those cases is a whole article all by itself, but you can delete constraints to fix conflicts and re-constrain if needed.

What happens if you didn’t just move the other edge, but deleted the feature and the edge no longer exists? Well, then the sketch no longer knows where it should go and will flag a warning on the sketch entity, marking it as dangling. Like a failure to solve, you can discard that if it’s no longer needed.

How about “Use”? This is functionality in Onshape that does almost exactly the same as adding an external reference, but it also creates geometry. If you “Use” an edge, then Onshape will have created the projected geometry for real, not just behind the scenes – and it will add a constraint to maintain that relationship.

“Use” is great when you want that geometry for real, but I would recommend not to use it as purely reference geometry. To explain why, I’ll share another fact about how the sketch works. We never change the type of a geometry. In other words, if you have a circle in the sketch, then it will always be a circle. It will never spontaneously turn into a line or ellipse.

So let’s take the case of a cylinder that points directly at the sketch so that its edge projects as a circle. There are two ways to make a point coincident with the projection of that edge:

1. Select it, and the point, and add a coincident constraint.
2. Use the edge and then put the point on the resulting circle.

Now if you change the earlier feature so that it tilts and the edge projects to an ellipse, the “use edge” will fail. It will be unhappy because it can only be a circle, but what it wants to follow is now an ellipse. So it won’t change and neither will the point. However, the direct constraint WILL work because it doesn’t care what the curve is as long as a point can be coincident to it.

So, if you want to use the projected geometry in more features, then definitely use “Use,” However, if you want to constrain or dimension, just constrain to it directly.

In a nutshell, this is how the sketch works when it updates. If you have external references, then they get evaluated and projected into the sketch – then all the constraints/dimensions are solved simultaneously.

There are lots of other areas we could explore in more detail about the sketch, such as:

• How the more advanced tools like pattern do what they do;
• How we handle over-defined or otherwise unhappy sketches;
• Why I can make two things parallel twice but I can’t dimension them twice.

And so on. But that’s enough for now!

Is there a technical subject you’d like us to tackle in a future “Under the Hood” blog? Please drop us a line at dmurphy(at)onshape.com and let us know what’s on your mind.