Script: Examle in TypeScript

Examle in TypeScript picture
Type
Typescript logo indicatortypescript
Date Created
Mar 19, 2021, 12:05:46 PM
Last Edit Date
Dec 5, 2023, 8:00:36 PM

Project Information

This example demonstrates how parametric 3D model can be produced by using NURBS framework and CAD features of BitByBit platform. Parametric lamp model can also be saved to STL format and 3D printed.

View Full Project

Script Code

const Inputs = Bit.Inputs;

let start = async () => {

    const isocurveSegments = 70;
    const isocurveSubdivisions = 41;
    const verticalIsocurveRadius = 0.1;
    const downloadStl = false;

    const profiles = [
        bitbybit.verb.curve.ellipse.createEllipse({
            center: [0, 0, 0],
            xAxis: [2, -0.5, 2],
            yAxis: [0, -0.5, 2],
        }),
        bitbybit.verb.curve.ellipse.createEllipse({
            center: [0, 3, 0],
            xAxis: [2, 0, 0],
            yAxis: [0, 0, 4],
        }),
        bitbybit.verb.curve.ellipse.createEllipse({
            center: [0, 6, 0],
            xAxis: [4, 0, 0],
            yAxis: [0, 0, 2],
        }),
        bitbybit.verb.curve.ellipse.createEllipse({
            center: [0, 12, 0],
            xAxis: [1, 0, 0],
            yAxis: [0, 0, 2],
        }),
        bitbybit.verb.curve.ellipse.createEllipse({
            center: [0, 16, 0],
            xAxis: [2, 0.5, 0],
            yAxis: [0, 0.5, 2],
        }),
    ];

    const surface = bitbybit.verb.surface.createSurfaceByLoftingCurves({
        curves: profiles,
        degreeV: 3,
    });

    const allIsocurves = bitbybit.verb.surface.isocurvesSubdivision({
        surface,
        isocurveSegments,
        includeFirst: true,
        includeLast: true,
        useV: false,
    });

    const hullSolids = await Promise.all(allIsocurves.map(async (curve) => {
        const centers = bitbybit.verb.curve.divideByEqualArcLengthToPoints({
            curve,
            subdivision: isocurveSubdivisions,
        });
        const spheres = await bitbybit.jscad.shapes.spheresOnCenterPoints({
            centers,
            radius: verticalIsocurveRadius,
            segments: 4,
        });
        return bitbybit.jscad.hulls.hullChain({
            meshes: spheres,
        });
    }));

    const drawSolidsInput = new Inputs.Draw.DrawBasicGeometryOptions();
    drawSolidsInput.colours = '#ffffff';
    await bitbybit.draw.drawAnyAsync({ entity: hullSolids, options: drawSolidsInput });

    if (downloadStl) {
        await bitbybit.jscad.downloadSolidsSTL({
            meshes: hullSolids,
            fileName: 'bitbybit-model',
        });
    }
}

start();