Your First Kernel

Build a parametric 3D model step-by-step with the Replicad kernel, render it, and export to STEP.

Your First Kernel

Build a parametric box with rounded edges using the Replicad kernel. You will learn how to set up a runtime client, write parametric CAD code, render geometry, and export to STEP format.

What You'll Learn

  • How to configure a runtime client with createRuntimeClient
  • How to write parametric CAD models that accept user parameters
  • How to render models and inspect the result
  • How to export models to STEP format for professional CAD workflows

Prerequisites

Step 1: Set Up the Client

Create a runtime client with the Replicad kernel, esbuild bundler, and an in-memory filesystem:

import { createRuntimeClient, fromMemoryFS } from '@taucad/runtime';
import { replicad } from '@taucad/runtime/kernels';
import { esbuild } from '@taucad/runtime/bundler';

const client = createRuntimeClient({
  kernels: [replicad()],
  bundlers: [esbuild()],
  fileSystem: fromMemoryFS(),
});

client.on('log', ({ message }) => console.log('[kernel]', message));

The createRuntimeClient function creates a high-level client that manages worker lifecycle, kernel selection, and middleware. The fromMemoryFS() function provides an in-memory filesystem for passing code to the kernel without touching disk.

Verify: No output yet. The client initializes lazily -- the worker is only created when you call render() or connect().

Step 2: Write a Parametric Model

Define a CAD model that creates a box with configurable dimensions and fillet radius. Parameters make your model flexible -- users can adjust dimensions without editing code.

const modelCode = `
  import { drawRoundedRectangle } from 'replicad';

  const defaultParams = {
    width: 30,
    height: 20,
    depth: 10,
    fillet: 3,
  };

  export default function main(params = defaultParams) {
    const { width, height, depth, fillet } = params;
    return drawRoundedRectangle(width, height, fillet)
      .sketchOnPlane('XY')
      .extrude(depth);
  }
`;

The main function is the entry point that the kernel calls. It receives a params object whose shape is inferred from defaultParams. The kernel extracts these parameters automatically and provides a JSON Schema for UI generation.

Verify: This step assigns a string -- no execution yet. Confirm that modelCode is a valid string with no syntax errors.

Step 3: Render the Model

Pass the code and optional parameter overrides to client.render():

const result = await client.render({
  code: { 'main.ts': modelCode },
  parameters: { width: 50, depth: 15 },
});

if (result.success) {
  console.log(`Rendered ${result.data.length} geometries`);
} else {
  for (const issue of result.issues) {
    console.error(`[${issue.severity}] ${issue.message}`);
  }
}

The success field is the discriminant -- TypeScript narrows the result type so you can safely access result.data in the true branch. If rendering fails, the issues array contains structured errors with messages, locations, and severity levels.

Verify: You should see Rendered 1 geometries in the console. If you see errors instead, check that your model code has correct Replicad imports and a default-exported main function.

Step 4: Export to STEP

Export the rendered geometry to STEP format for use in professional CAD tools:

const exportResult = await client.export('step');

if (exportResult.success) {
  const { name, bytes, mimeType } = exportResult.data;
  console.log(`Exported: ${name} (${bytes.byteLength} bytes, ${mimeType})`);
} else {
  console.error('Export failed:', exportResult.issues);
}

You can also export directly from code without a separate render() call:

const result = await client.export('step', {
  code: { 'main.ts': modelCode },
  parameters: { width: 50, depth: 15 },
});

STEP export is available for the Replicad and OpenCASCADE kernels. Other kernels support different export formats -- see Choose a Kernel for a comparison.

Verify: You should see output like Exported: main.step (12345 bytes, model/step). The byte count varies with model complexity.

Step 5: Clean Up

Terminate the client to shut down workers and release resources:

client.terminate();

Verify: The process should exit cleanly. If it hangs, terminate() was not called or a listener is keeping the event loop alive.

Recap

You built a parametric 3D model using the Replicad kernel:

  1. Created a RuntimeClient with createRuntimeClient
  2. Wrote a parametric model with configurable dimensions
  3. Rendered it to glTF with parameter overrides
  4. Exported the result to STEP format
  5. Used result.success to handle success and error cases

Next Steps