Parametric Cube with Blockly
In this short tutorial, we'll create a cube using the OpenCascade (OCCT) geometry kernel and control its size with a variable. This is a simple example, but it effectively demonstrates how Blockly can be used to create variables and establish parametric relationships in your 3D designs.
1. Start The Application
First, open our Blockly editor in your browser by navigating to: https://bitbybit.dev/app?editor=blockly
This will immediately start an empty Blockly workspace. While you can use the embedded example editors directly in this tutorial, it's good practice to try finding the components yourself by following the described paths in the Blockly toolbox.
2. Adding and Connecting Components
Let's start by adding the necessary blocks to our workspace.
Add the Drawing Block
We need a block to draw our 3D shapes onto the canvas.
- In the Blockly toolbox (on the left), find the "draw" category.
- Expand it, then find the "draw async" subcategory.
- Drag the block named
draw any async no return(or similar, representing drawing without expecting a return value) onto your workspace.
Add the Cube Creation Block (OCCT)
Next, we'll add a block to create a cube using the OCCT kernel.
- In the Blockly toolbox, find the "occt" category.
- Expand it, then navigate to "shapes" -> "solid" -> "primitives".
- Drag the block named
cube(or similar) from this category. - Connect this
cubeblock to the input slot labeled "entity" on thedraw any async no returnblock.
At this point, you can click the "Run" button (often a play icon ▶️) in the Blockly interface. You should see a cube with a default size (e.g., 1 unit) appear in the 3D preview area.
Create and Use a Variable for Size
Now, let's make the cube's size parametric using a variable.
-
Create a Variable:
- In the Blockly toolbox, find the "variables" category.
- Click on "Create variable...".
- A dialog will pop up. Enter the name
sizefor your variable and click "Create".
-
Set the Variable's Value:
- After creating the variable, the "variables" category in the toolbox will now contain blocks related to your
sizevariable. - Drag the
set size toblock onto your workspace. - From the "math" category in the toolbox, drag a number block (it usually shows
0by default) and connect it to the empty slot in theset size toblock. You can change this number later to control the cube's size.
- After creating the variable, the "variables" category in the toolbox will now contain blocks related to your
-
Use the Variable in the Cube:
- Go back to the "variables" category in the toolbox.
- Drag the block that simply says
size(this block represents the value of the variable). - Connect this
sizevariable block to the input slot labeled "size" on yourcubeblock.
Your Blockly workspace should now look something like this interactive example:
<xml xmlns="https://developers.google.com/blockly/xml"><variables><variable id="jVWX.QGY%cHn[hzK+n%A">size</variable></variables><block type="variables_set" id=",0sefL+dW}xV5n^$j+AV" x="-689" y="-1055"><field name="VAR" id="jVWX.QGY%cHn[hzK+n%A">size</field><value name="VALUE"><block type="math_number" id="V^t{t:]qg=9tg8,9LcC8"><field name="NUM">3</field></block></value><next><block type="bitbybit.draw.drawAnyAsyncNoReturn" id="$2#tlK1lUC`%0`Yy=Na."><value name="Entity"><block type="bitbybit.occt.shapes.solid.createCube" id="KSbGrF@x?4}%ch#4CC?N"><value name="Size"><block type="variables_get" id="FoIWtg5M|v!ht|=%;aq;"><field name="VAR" id="jVWX.QGY%cHn[hzK+n%A">size</field></block></value><value name="Center"><block type="bitbybit.point.pointXYZ" id="jxe4^~!Dm|=.jsOXT%CQ"><value name="X"><block type="math_number" id="oSJ:g{kkIk/r{Yt$BW:`"><field name="NUM">0</field></block></value><value name="Y"><block type="math_number" id="sFs?IY5vqosq~TV(Inzo"><field name="NUM">0</field></block></value><value name="Z"><block type="math_number" id="_-4Thhl)EZH2~HQVQ%]X"><field name="NUM">0</field></block></value></block></value></block></value></block></next></block></xml>
Experiment and Understand
Now that your setup is complete:
- Try changing the number connected to the
set size toblock (e.g., to5,10, or any other value). - Run the script again.
- Observe how the cube in the 3D preview changes its size based on the value you set for the
sizevariable.
This demonstrates a fundamental programming concept: using variables to store information (the desired size) and then passing that information to other parts of your program (the cube block) to control their behavior. While simple, this is the basis of creating powerful parametric models where a few key variables can drive complex geometric changes.

