Download the Onshape Mobile App

IOS Android
Onshape GUI with an overlay with a Linear pattern options open by a 3D-modeled toolbox.
READ TIME:
01:40

Have you ever wondered if you can control the number of instances in a pattern using variables and expressions? You can!

This Tech Tip shows you how to use the “floor” math function to keep patterns confined within part geometry.

Using Math Functions to Keep Patterns in Bounds

One reason to control the instance count of a pattern is to prevent it from spilling off the part, causing one or more pattern instances to fail. Consider the toolbox model below:

The number of slots must increase or decrease with changes in the toolbox's height. How is this done? A simple equation dividing the toolbox height by the slot spacing could be the solution, as long as the height is always a multiple of the slot spacing. What if it isn’t? Pattern instance counts must be integers.

This is where an expression, combined with Onshape’s math functions, provides the answer. In this example, we use the “floor” function to ignore any remainder from dividing the toolbox height by the slot spacing. With the floor function, we always get an integer result, regardless of the toolbox height.

We’ll also need a unitless variable to represent the toolbox height in the expression.

Creating a Unitless Variable

Create an “any” type variable (“wallHeight“ in this example). Be sure it appears in the feature list before the toolbox geometry:

Assign the variable to the toolbox height:

Edit the pattern, and type the expression in the Instance count field as shown in the image below (the “-1” ensures that no instance lands on the edge of the part):

The pattern Instance count now adjusts with changes to the height of the toolbox.

Unlocking the Power of Onshape Variables

This Tech Tip helped you learn how to control pattern instance counts with an expression using a variable and one of Onshape’s built-in math functions. For more information about Onshape’s math functions, visit the Help documentation on Numeric Fields.

Visit the Onshape Learning Center to go deeper with a self-paced course on Variables and Expressions.

The Onshape Learning Center

Take self-paced courses, read articles, or sign up for an instructor-led training session.

Latest Content