Script: TypeScript example of OCCT wires to JSCAD

TypeScript example of OCCT wires to JSCAD picture
Type
Typescript logo indicatortypescript
Date Created
Feb 26, 2024, 3:11:30 PM
Last Edit Date
Mar 1, 2024, 8:15:38 AM

Project Information

In the latest release we introduce the methods to transform wires of the shape into collections of points based on OCCT tangential deflection. This enables to rebuild 2D JSCAD paths and save them to DXF file format together with other 2D entities of JSCAD such as path text.

View Full Project

Script Code

const start = async () => {

    const compoundWires = await createCompoundShape();
    const transformedCompound = await bitbybit.occt.transforms.rotate({ shape: compoundWires, angle: 90, axis: [-1, 0, 0] });

    const wiresToPointsOpt = new Bit.Inputs.OCCT.WiresToPointsDto<Bit.Inputs.OCCT.TopoDSShapePointer>();
    wiresToPointsOpt.shape = transformedCompound;
    wiresToPointsOpt.angularDeflection = 0.3;
    const pointsOfWiresInCompound = await bitbybit.occt.shapes.wire.wiresToPoints(wiresToPointsOpt);

    const jscadPaths = await bitbybit.jscad.path.createPathsFromPoints({
        pointsLists: pointsOfWiresInCompound
    });

    const vectorTextOpt = new Bit.Inputs.JSCAD.TextDto();
    vectorTextOpt.xOffset = 10;
    vectorTextOpt.text = "DXF Export"
    const textVectors = await bitbybit.jscad.text.createVectorText(vectorTextOpt);
    const textPaths = await bitbybit.jscad.path.createPathsFromPoints({
        pointsLists: textVectors
    });

    const allPaths = [...jscadPaths, ...textPaths];

    bitbybit.jscad.downloadGeometryDxf({
        geometry: allPaths,
        fileName: "bitbybit-dxf"
    });

   const textOffset = bitbybit.jscad.expansions.offset({ geometry: textPaths, delta: 0.3, segments: 32, corners: Bit.Inputs.JSCAD.solidCornerTypeEnum.round });

    bitbybit.draw.drawAnyAsync({
        entity: textOffset
    });

    bitbybit.draw.drawAnyAsync({
        entity: transformedCompound
    });

    bitbybit.draw.drawAnyAsync({
        entity: pointsOfWiresInCompound.flat()
    });
}

async function createCompoundShape() {
    const filletWireDiff = await createWireWithCutouts();
    const ellipse = await createWireEllipse();
    const interpWire = await createWireInterpolated();
    const circleWire = await createWireCircle();
    return bitbybit.occt.shapes.compound.makeCompound({
        shapes: [filletWireDiff, ellipse, interpWire, circleWire]
    });
}

async function createWireInterpolated(): Promise<Bit.Inputs.OCCT.TopoDSWirePointer> {
    return bitbybit.occt.shapes.wire.interpolatePoints({
        points: [
            [5, 0, 0],
            [6, 0, 0],
            [6, 0, 6],
            [8, 0, 3],
        ],
        periodic: false,
        tolerance: 0.1,
    });
}

async function createWireCircle(): Promise<Bit.Inputs.OCCT.TopoDSWirePointer> {
    return bitbybit.occt.shapes.wire.createCircleWire({
        center: [0, 0, 8],
        direction: [0, 1, 0],
        radius: 2,
    })
}

async function createWireEllipse(): Promise<Bit.Inputs.OCCT.TopoDSWirePointer> {
    const ellipseOpt = new Bit.Inputs.OCCT.EllipseDto();
    ellipseOpt.radiusMajor = 4;
    return bitbybit.occt.shapes.wire.createEllipseWire(ellipseOpt)
}

async function createWireWithCutouts(): Promise<Bit.Inputs.OCCT.TopoDSWirePointer> {
    const rectOpt = new Bit.Inputs.OCCT.RectangleDto();
    rectOpt.width = 5;
    rectOpt.length = 10;
    const rect = await bitbybit.occt.shapes.face.createRectangleFace(rectOpt);
    const rectEdge1 = await bitbybit.occt.shapes.edge.getEdge({
        shape: rect,
        index: 2
    });
    const rectEdge2 = await bitbybit.occt.shapes.edge.getEdge({ shape: rect, index: 4 });
    const divOpt = new Bit.Inputs.OCCT.DivideDto<Bit.Inputs.OCCT.TopoDSEdgePointer>(rectEdge1);
    divOpt.nrOfDivisions = 5;
    const points1 = await bitbybit.occt.shapes.edge.divideEdgeByEqualDistanceToPoints(divOpt);
    divOpt.shape = rectEdge2;
    const points2 = await bitbybit.occt.shapes.edge.divideEdgeByEqualDistanceToPoints(divOpt);
    const allPoints = [...points1, ...points2];

    const circleOpt = new Bit.Inputs.OCCT.CircleDto();
    circleOpt.direction = [0, 1, 0];
    circleOpt.radius = 0.5;
    const circles = await Promise.all(allPoints.map(pt => {
        circleOpt.center = pt;
        return bitbybit.occt.shapes.face.createCircleFace(circleOpt);
    }));

    const diff = await bitbybit.occt.booleans.difference({
        shape: rect,
        shapes: circles,
        keepEdges: false
    });

    const wireDiff = await bitbybit.occt.shapes.wire.getWire({ shape: diff, index: 0 });
    return bitbybit.occt.fillets.fillet2d({ shape: wireDiff, radius: 0.2 });
}

start();
Plans & Pricing

Choose Your Plan

Editor plans for 3D development, API keys for server-side CAD algorithms

B2B

ENTERPRISE

Custom pricing

Custom software development, dedicated servers & CAD automation at scale.

CAD Automation & Software
  • Custom software development
  • Cloud CAD automation pipelines
  • 3D configurators (STEP & GLTF)
  • Batch export jobs
  • Custom algorithms & deployment
Infrastructure & Support
  • Custom compute allocation
  • Dedicated / VPS server tenants
  • Long-running computation jobs
  • Custom upload limits & overage
  • SLA & premium support