Creating a Parametric Cube in TypeScript
In this short tutorial, we'll create a 3D cube using the powerful OpenCascade Technology (OCCT) kernel, which is integrated into Bitbybit. The cool part? We'll control its size using a variable in TypeScript. This simple example demonstrates how you can use code to create dynamic, parametric relationships with 3D geometry.
What You'll Learn
- How to use the Bitbybit TypeScript editor.
- How to define and use variables to control geometry.
- The basics of creating a solid shape (a cube) using OCCT functions.
- How to draw your 3D shapes onto the screen.
1. Start The Application
First, let's open the Bitbybit online application.
Navigate to: https://bitbybit.dev/app?editor=typescript
This will open a new, empty TypeScript workspace. For this tutorial, you can also directly use the embedded editor example provided below.
2. Understanding the Script
The script shown in the editor below is fairly straightforward, but let's break it down step-by-step. Feel free to click the Run button (usually a play icon ▶️) in the embedded editor to see the result before we dive deeper.
const start = async () => {
const size = 10;
const cubeOptions = new Bit.Inputs.OCCT.CubeDto();
cubeOptions.size = size;
const cube = await bitbybit.occt.shapes.solid.createCube(cubeOptions);
bitbybit.draw.drawAnyAsync({
entity: cube
});
}
start();
Let's go through the code:
-
The
async startFunction:const start = async () => {
// ... code inside the function ...
};
start(); // Call the function to execute itThe script begins by defining an
asyncfunction calledstart. This is a common pattern in Bitbybit scripts.async: This keyword allows us to use theawaitkeyword inside the function.awaitis used for operations that might take some time to complete (like drawing complex 3D objects) without freezing the rest of the application.const start = () => { ... };: This defines an arrow function and assigns it to a constant namedstart.start();: After defining the function, we immediately call it to execute the code within its body.
-
Defining the Cube's Size:
// Inside the start function:
const size = 10; // Try changing this value!Inside the
startfunction, we declare a constant namedsizeand set its value to10. This variable will control the dimensions of our cube. This is where the "parametric" aspect comes in – you can change this one value to change the cube. -
Setting Cube Options:
// Inside the start function:
const cubeOptions = new Bit.Inputs.OCCT.CubeDto();
cubeOptions.size = size;Here, we prepare the options for creating our cube:
- We create a new instance of
Bit.Inputs.OCCT.CubeDto(). This object holds various settings for creating OCCT solid shapes. - We then set the
sizeproperty ofcubeOptionsto the value of oursizevariable.
- We create a new instance of
-
Creating the Cube:
// Inside the start function:
const cube = await bitbybit.occt.shapes.solid.createCube(cubeOptions);This line actually creates the 3D cube geometry:
- We call the
bitbybit.occt.shapes.solid.createCube()function. - We pass our
cubeOptionsobject to it, so it knows what size the cube should be. - The
awaitkeyword is used because creating 3D geometry can be an asynchronous operation. The result (the cube geometry) is stored in thecubeconstant.
- We call the
-
Drawing the Cube:
// Inside the start function:
await bitbybit.draw.drawAnyAsync({ entity: cube });Finally, we make the cube visible on the screen:
- We call
bitbybit.draw.drawAnyAsync(). This is a versatile function in Bitbybit that can draw various types of entities. - We pass an object with an
entityproperty (ourcube). - Again,
awaitis used as drawing is an asynchronous process. This will render a BabylonJS Mesh (the visual representation) of our cube in the 3D view.
- We call
That's it! When you run the script:
- The
sizeis defined. - The cube options are configured with this
size. - The OCCT kernel generates the cube geometry.
- The geometry is drawn to the 3D canvas.
After running the script, click the Swap Canvas button (usually marked with two arrows 🔁) in the editor to switch to the 3D scene and inspect your cube. Try rotating and zooming to see it from different angles.
Experiment!
Go back to the script in the embedded editor (or your own Bitbybit workspace).
Change the value of the size constant:
const size = 3; // Or any other number
Then, click Run again. You'll see the cube instantly update to its new size! This is the power of parametric modeling.
Congratulations! You've successfully created a parametric cube using TypeScript and the OpenCascade Technology kernel within Bitbybit. You've learned how to define variables, use them to control geometric properties, and visualize the results. This foundational knowledge can be expanded to create much more complex parametric designs!

