Script: Wire Projection in TypeScript Example

Wire Projection in TypeScript Example picture
Type
Typescript logo indicatortypescript
Author
matas
Date Created
Nov 15, 2023, 12:33:26 PM
Last Edit Date
Oct 2, 2025, 7:57:52 PM

Project Information

Projecting wires onto 3D shapes is a powerful feature that can help in creating various interesting geometries

View Full Project

Script Code

const start = async () => {
    const sphere1 = await createGeometry(0.5, 3.7, 2, 0.2)
    const sphere2 = await createGeometry(1, 3, 1.8, 0.3)
    const sphere3 = await createGeometry(1, 3, 1.5, 0.3)

    const compound = await bitbybit.occt.shapes.compound.makeCompound({
        shapes: [sphere1, sphere2, sphere3],
    });

    const drawOptions = new Bit.Inputs.Draw.DrawOcctShapeOptions();
    drawOptions.faceColour = "#ffffff";
    drawOptions.edgeColour = "#000000";
    drawOptions.edgeWidth = 1;
    drawOptions.precision = 0.005;

    await bitbybit.draw.drawAnyAsync({
        entity: compound,
        options: drawOptions
    });

    const ptLightOpt = new Bit.Inputs.BabylonScene.PointLightDto();
    ptLightOpt.position = [-3, 3, 0];
    ptLightOpt.shadowGeneratorMapSize = 2056;
    ptLightOpt.diffuse = "#ff00ff";
    bitbybit.babylon.scene.drawPointLight(ptLightOpt)
}

start();

async function createGeometry(starInnerRadius: number, starOuterRadius: number, sphereRadius: number, thickness: number) {
    const star1 = await bitbybit.occt.shapes.wire.createStarWire({
        direction: [1, 0, 0],
        center: [5, 0, 0],
        innerRadius: starInnerRadius,
        outerRadius: starOuterRadius,
        numRays: 8,
        half: false
    });

    const sphere1 = await bitbybit.occt.shapes.solid.createSphere({
        radius: sphereRadius,
        center: [0, 0, 0]
    });

    const fillet1 = await bitbybit.occt.fillets.fillet2d({
        shape: star1,
        radius: 0.1
    });

    const projection1 = await bitbybit.occt.shapes.wire.project({
        wire: fillet1,
        shape: sphere1,
        direction: [1, 0, 0]
    });

    const projectionWires = await bitbybit.occt.shapes.wire.getWires({ shape: projection1 });

    const split1 = await bitbybit.occt.operations.splitShapeWithShapes({
        shape: sphere1,
        shapes: projectionWires,
        nonDestructive: true,
        localFuzzyTolerance: 0.00001,
    });

    const faces = await bitbybit.occt.shapes.face.getFaces({ shape: split1[8] });
    const face = faces.shift();

    const thicken = await bitbybit.occt.operations.makeThickSolidSimple({
        shape: face,
        offset: -thickness,
    })

    return thicken;
}