TypeScript Example

TypeScript Example script details
Type
Typescript logo image
typescript
App Version
0.19.9
Visibility
public
Date Created
Jan 29, 2022, 12:34:02 PM
Last Edit Date
Apr 2, 2025, 11:46:51 AM

Script Details

The Code
let myWidth = 5;
let myHeight = 8;
let myThickness = 3;

const ptLightOpt = new Bit.Inputs.BabylonScene.PointLightDto();
ptLightOpt.position = [myThickness * 2, myHeight * 0.5, myWidth];
ptLightOpt.intensity = 1000;
ptLightOpt.radius = 0.1;
ptLightOpt.shadowMinZ = 0.1;

bitbybit.babylon.scene.drawPointLight(ptLightOpt);
ptLightOpt.position = [-myThickness * 2, myHeight, -myWidth];
bitbybit.babylon.scene.drawPointLight(ptLightOpt);

start(myWidth, myHeight, myThickness);

async function start(width: number, height: number, thickness: number) {
    type Pt3 = Bit.Inputs.Base.Point3;
    type Vec3 = Bit.Inputs.Base.Point3;
    type Pt2 = Bit.Inputs.Base.Point2;
    type Vec2 = Bit.Inputs.Base.Point2;

    const aPnt1: Pt3 = [-width / 2, 0, 0];
    const aPnt2: Pt3 = [-width / 2, 0, -thickness / 4];
    const aPnt3: Pt3 = [0, 0, -thickness / 2];
    const aPnt4: Pt3 = [width / 2, 0, -thickness / 4];
    const aPnt5: Pt3 = [width / 2, 0, 0];

    const anArc = await bitbybit.occt.shapes.edge.arcThroughThreePoints({ start: aPnt2, middle: aPnt3, end: aPnt4 });
    const edge1 = await bitbybit.occt.shapes.edge.line({ start: aPnt1, end: aPnt2 });
    const edge2 = await bitbybit.occt.shapes.edge.line({ start: aPnt4, end: aPnt5 });

    const firstHalfWire = await bitbybit.occt.shapes.wire.combineEdgesAndWiresIntoAWire({ shapes: [edge1, anArc, edge2] });

    const direction: Vec3 = [1, 0, 0];
    const origin: Pt3 = [0, 0, 0];
    const secondHalfWire = await bitbybit.occt.transforms.mirror({ direction, origin, shape: firstHalfWire });

    const wire = await bitbybit.occt.shapes.wire.combineEdgesAndWiresIntoAWire({ shapes: [firstHalfWire, secondHalfWire] });

    const aPrismVec: Vec3 = [0, height, 0];
    const face = await bitbybit.occt.shapes.face.createFaceFromWire({ shape: wire, planar: true });
    const extruded = await bitbybit.occt.operations.extrude({ shape: face, direction: aPrismVec });
    const appliedFillets = await bitbybit.occt.fillets.filletEdges({ shape: extruded, radius: thickness / 12 });

    const neckLocation: Pt3 = [0, height, 0];

    const neckRadius = thickness / 4.;
    const neckHeight = height / 10.;
    const neckAxis: Vec3 = [0, 1, 0];

    const neck = await bitbybit.occt.shapes.solid.createCylinder({ radius: neckRadius, height: neckHeight, center: neckLocation, direction: neckAxis })

    const unioned = await bitbybit.occt.booleans.union({ shapes: [appliedFillets, neck], keepEdges: false });

    const faceToRemove = await bitbybit.occt.shapes.face.getFace({ shape: unioned, index: 27 });

    const thickOptions = new Bit.Inputs.OCCT.ThickSolidByJoinDto(unioned, [faceToRemove], -thickness / 50)
    const thick = await bitbybit.occt.operations.makeThickSolidByJoin(thickOptions);

    const geom = bitbybit.occt.geom;

    // Threading : Create Surfaces
    const aCyl1 = await geom.surfaces.cylindricalSurface({ direction: neckAxis, radius: neckRadius * 0.99, center: neckLocation });
    const aCyl2 = await geom.surfaces.cylindricalSurface({ direction: neckAxis, radius: neckRadius * 1.05, center: neckLocation });

    const aPnt: Pt2 = [2. * Math.PI, neckHeight / 2.];
    const aDir: Vec2 = [2. * Math.PI, neckHeight / 4.];
    const aMajor = 2. * Math.PI;
    const aMinor = neckHeight / 10.;

    const anEllipse1 = await geom.curves.geom2dEllipse({ center: aPnt, direction: aDir, radiusMinor: aMinor, radiusMajor: aMajor, sense: true });
    const anEllipse2 = await geom.curves.geom2dEllipse({ center: aPnt, direction: aDir, radiusMinor: aMinor / 4, radiusMajor: aMajor, sense: true });

    const anArc1 = await geom.curves.geom2dTrimmedCurve({ shape: anEllipse1, u1: 0, u2: Math.PI, sense: true, adjustPeriodic: true });
    const anArc2 = await geom.curves.geom2dTrimmedCurve({ shape: anEllipse2, u1: 0, u2: Math.PI, sense: true, adjustPeriodic: true });

    const anEllipsePnt1 = await geom.curves.get2dPointFrom2dCurveOnParam({ shape: anEllipse1, param: 0 });
    const anEllipsePnt2 = await geom.curves.get2dPointFrom2dCurveOnParam({ shape: anEllipse1, param: Math.PI });

    const aSegment = await geom.curves.geom2dSegment({ start: anEllipsePnt1, end: anEllipsePnt2 });

    const anEdge1OnSurf1 = await bitbybit.occt.shapes.edge.makeEdgeFromGeom2dCurveAndSurface({ curve: anArc1, surface: aCyl1 });
    const anEdge2OnSurf1 = await bitbybit.occt.shapes.edge.makeEdgeFromGeom2dCurveAndSurface({ curve: aSegment, surface: aCyl1 });
    const anEdge1OnSurf2 = await bitbybit.occt.shapes.edge.makeEdgeFromGeom2dCurveAndSurface({ curve: anArc2, surface: aCyl2 });
    const anEdge2OnSurf2 = await bitbybit.occt.shapes.edge.makeEdgeFromGeom2dCurveAndSurface({ curve: aSegment, surface: aCyl2 });

    const threadingWire1 = await bitbybit.occt.shapes.wire.combineEdgesAndWiresIntoAWire({ shapes: [anEdge1OnSurf1, anEdge2OnSurf1] });
    const threadingWire2 = await bitbybit.occt.shapes.wire.combineEdgesAndWiresIntoAWire({ shapes: [anEdge1OnSurf2, anEdge2OnSurf2] })

    const loft = await bitbybit.occt.operations.loft({ shapes: [threadingWire1, threadingWire2], makeSolid: true })

    const union = await bitbybit.occt.booleans.union({ shapes: [loft, thick], keepEdges: false });

    const di = new Bit.Inputs.Draw.DrawOcctShapeOptions();
    di.edgeColour = '#000000';
    di.edgeOpacity = 0.5;
    di.faceColour = '#ff00ff';
    di.precision = 0.001;
    di.faceOpacity = 1;
    di.edgeWidth = 1;
    await bitbybit.draw.drawAnyAsync({ entity: union, options: di });
}