• Informed Design
  • Inventor

Codeblocks - use Function blocks

Create reusable code modules to perform repeatable tasks with inputs and outputs using the Functions blocks in the Informed Design Codeblocks Editor.


Step-by-step guide

The Function code blocks include two primary Function block types, as well as a Function Conditional block. Functions are self-contained modules of code that are set up to accomplish a specific task. Functions are called on demand from a Calling block. When called, a function is often given data from the Calling block to perform the specified task. The function can be called throughout the rule as many times as needed. It can even be given different data each time it is called.

In this example, create a function to calculate an area parameter value.

  1. From the Functions library, add the first Functions block to the canvas, and name it "Set Area".
  • In the Informed Design interface, Rules tab, code blocks setting parameter max, min, and increment values for Height and Length are on the canvas. The Functions library is expanded, and the first Functions block is being selected.

When a Function block is added to the canvas, a Calling block is automatically created and added to the Functions library.

  • The Functions library expanded to show the newly added Set Area Calling block, which is highlighted.
  1. Add the Calling block where the function should be called; in this case, below the Area read-only block.
  2. In the Function, click plus (+) to add a variable, and name it "Len", for Length.

This variable is automatically added to the Calling block as well; in effect, connecting the two blocks.

  1. Click plus (+) to add a second variable named "Hgt". Add the Nominal Wall Length and Height parameters to the Calling block.

The Length and Height values will be passed to the Function through the variables.

  1. Add an Area parameter to the Function, snap a Math block to it, and set the operation to multiplication for the Area calculation.
  2. From the Variables library, note the newly added Len and Hgt variables, and add them to the Math block.
  • On the canvas, the Calling block and Functions block set up for this example.
  1. In the form, use the up and down arrows to test different Length and Height values. The Area updates accordingly.
  • In the form, arrows in the Length input field being clicked to adjust the value, with the Area calculation updated accordingly.

Next, create a function using the Function Return block, which is set to return an output.

  • The Function Return block highlighted in the expanded Functions library.
  1. Right-click the existing Function blocks and select Collapse Block to make space in the canvas.
  • The right-click menu for the existing Function block, with Collapse Block selected.
  1. Add the Function Return block to the canvas, and name it "Convert to inches".
  2. From the Functions library, add the corresponding Calling block, Convert to inches, to the canvas.
  • On the canvas, the Return Function block, and the Convert to inches calling block.

This type of Function block expects a parameter to call the Function and receive the Function output.

  1. Add a Nominal Wall Length value equals parameter to the canvas and add the Calling block to it. Set the parameter drop-down to message.
  2. Add two variables to the Function, and name them "My Measurement" and "My Label".

Note that these variables are automatically added to the Calling block.

  • On the canvas, the Calling block and added variables attached to the Nominal Wall Length message block, and the Function block, with My Measurement and My Label variables added.
  1. In the Variables library, click Create variable. In the popup, type the name "My_Message", then click OK.
  • The expanded Variables library, with Create variable highlighted, and the popup dialog, with the name My_Message entered.

This adds a new variable that acts as a container for the Function information before passing it to the Calling block.

  1. Add the My_Message variable block to the Function, along with a Create Text With block.
  • The Convert to Inches Function block, with the My Measurement and My Label variables, the newly created My_Message Variable block, and a Create Text With block.

Set the Create Text With block to display a form message with the Length in inches.

  1. Add the My_Label variable and a Text block set to read "in inches:".
  2. Add a Math block with the My_Measurement variable, the operation set to multiplication, and a Number block set to 12.
  3. Add the My_Message variable, to be returned from the Function back up to the Calling block.
  4. Add Nominal Wall Length value and label parameters to the Calling block, to pass information of the correct data type to the Function.
  • The Calling block with added Nominal Wall Length value and label parameters, and the Function block set to convert the Length parameter to inches and display it in a form message.
  1. Click Update form, then place the pointer over the message to view the Length in inches.
  • The updated form showing a message that displays the Length parameter value in inches.

To create the same message for the Height parameter:

  1. Duplicate the Calling block, update the parameters to Nominal Wall Height, and snap the two Calling blocks together.
  • The Calling block for the Length parameter duplicated and set up for the Height Parameter.
  1. Click Update form, then test different Length and Height values to verify that the Function displays the inch conversion for both parameters.

In summary, functions are self-contained, reusable modules of code that are set up to accomplish a task. Functions are called from a Calling block and run on demand when called. Functions are often given data from the Calling block to perform the specified task.