• Informed Design
  • Inventor

Codeblocks - use Math blocks

Use Math blocks to add, subtract, multiply, divide, and round numbers, as well as to find the square root and perform trigonometry functions.


Step-by-step guide

Math code blocks contain mathematical blocks to perform a variety of operations. These include a Number constant block, a block with Operations of arithmetic, a block with Square Root and other common functions, a Trigonometry block, a Rounding block, a Remainder Of block, and a Constrain block to limit a number between a minimum and a maximum value.

  • The Math blocks library expanded to show the available options.

When working with mathematical code blocks, it is important to understand the relationship between block grouping and the order of operations.

An arrangement of blocks is created with an intended result of 36, as shown in the calculation.

  • The intended calculation for the math blocks.

However, each block in the following block set acts as a grouped parenthetical calculation. When the code block is run, the result of the following configuration is 40.

  • Math blocks set up and grouped such that the grouping of 2 minus 2 is calculated prior to being subtracted from the grouping of 4 times 10, producing a result of 40.

The same blocks are rearranged and grouped to achieve the expected result of 36.

  • Math blocks set up and grouped such that 4 times 10 is calculated, then that result is grouped with minus 2 and calculated, finally 2 is subtracted from that result to achieve the intended result of 36.

To set up a rule that calculates the area of a wall, first, add the Area parameter to the form:

  1. In Inventor, add a parameter for Area to hold the calculated square footage.
  2. In the Parameters tab of the Product definition, select Area to add it to the form.
  3. Click the Rules tab to view the added Area parameter.

The product definition shown below has already been set up to display the length and height in form messages as inches.

  1. If needed, to view the contents of a collapsed block, right-click and select Expand Block.
  • On the canvas, code blocks set up to display the length and height in form messages, and Expand Block being selected in the context menu; and below this image, an arrow pointing down to a second image showing the expanded block.
  1. To collapse it again, right-click and select Collapse Block.

To make the Area parameter read-only:

  1. Add a Nominal Wall Length parameter block. Set the parameter to Area and the function to readOnly.
  2. Finish the statement by adding a True False block with a value of true.

To create the Area calculation:

  1. Create another Area block.
  2. Add another Length block, then copy and paste it using CTRL+C and CTRL+V. Change the second parameter to Nominal Wall Length.
  3. Add an Operations math block to the Area block, set the operation to Multiply, and add the Length and Height parameter blocks to create the Area calculation.
  4. Right-click the Area block and select External Inputs to wrap the block.
  • The Area blocks set up to calculate the area and to make the Area calculation read-only.
  1. Click Update form.

The Area calculation displays in the form as a read-only value.

  • The Area calculation displayed in the form as a read-only value, highlighted in red.

Next, to determine the diagonal measurement of the wall from corner to corner, transform the Pythagorean theorem to solve for length c.

  • A diagram that shows the calculation for the diagonal measurement of a wall.
  1. Add a Set to variable block and set it to Diagonal. Add and connect a Square Root Math block to the Diagonal block.
  2. Add two Operations Math blocks. Leave the first set to addition (+), and change the second to exponentiation (^), to raise the value by a power of 2.
  3. Add a Nominal Wall Length parameter to the Squared block. Duplicate this block to create the same block for the Height parameter.
  4. Add the Squared blocks to either side of the Addition block to sum the results of both squared parameter values.
  5. Right-click and select External Inputs to wrap the blocks to fit better in the canvas.
  6. Add the Squared blocks to the Diagonal Square Root block.
  7. To set the Diagonal result to print when the form is updated, below the Diagonal Square Root block, add a Print block and attach a Diagonal variable block.
  • The Diagonal Square root block set up for this example.
  1. Click Update form.

A popup shows the Diagonal result.

  • Update form being clicked, and the result of the Diagonal calculation displayed in a popup.

The angles of the diagonal corners can be calculated using the Inverse Tangent Trigonometric block.

  • The calculation for the angles of the diagonal corners.
  1. Right-click the Diagonal Square Root block set and select Duplicate Connected. Remove the Square Root portion of the equation.
  2. Add a Trigonometry math block and set the function to atan.
  3. From the Area calculation, Duplicate the Length multiplied by Height block, add it to the Atan block, and change the Operation to Division.
  4. Change the Set To and Print variable blocks to a previously created variable, angle A.
  • The code block set to calculate and print the result for Angle A, using the Atan function and dividing the Length by Width parameters.
  1. Right-click and select Duplicate Connected to copy the entire block. Set the drop-down to angle B, and then swap the Height and Length parameters.
  • The Angle A block set duplicated and adjusted to calculate and print the result for Angle B.
  1. Click Update form.

The result for the Diagonal, along with the result for Angle A and Angle B display in the popup.

  • Update form being clicked and the result for the Diagonal, Angle A, and Angle B displayed in the popup.

In summary, Math blocks evaluate as parenthetical groups following the standard order of operations. There are numerous Math blocks that can be combined to perform a variety of mathematical calculations.

Was this information helpful?