Script: OCCT Text Wires to JSCAD DXF

OCCT Text Wires to JSCAD DXF picture
Type
Typescript logo indicatortypescript
Date Created
Mar 1, 2024, 8:15:07 AM
Last Edit Date
Jan 22, 2025, 11:04:37 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
    });

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

    // OCCT -> JSCAD Text Paths
    const txt = await createOcctText();
    const toPoints = new Bit.Inputs.OCCT.WiresToPointsDto<Bit.Inputs.OCCT.TopoDSShapePointer>();
    toPoints.shape = txt.compound;
    const txtPoints = await bitbybit.occt.shapes.wire.wiresToPoints(toPoints);
    const jscadOccTextPaths = await bitbybit.jscad.path.createPathsFromPoints({ pointsLists: txtPoints });

    const greenTextPaths = await bitbybit.jscad.colors.colorize({
        geometry: jscadOccTextPaths,
        color: "#00ff00",
    });

    const redJscadPaths = await bitbybit.jscad.colors.colorize({
        geometry: jscadPaths,
        color: "#ff0000",
    });

    const blueJscadPaths = await bitbybit.jscad.colors.colorize({
        geometry: textPaths,
        color: "#0000ff",
    });

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

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

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

    const allPaths = [...greenTextPaths, ...redJscadPaths, ...blueJscadPaths];

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

}

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 createOcctText(): Promise<Bit.Advanced.Text3D.Text3DData<Bit.Inputs.OCCT.TopoDSShapePointer>> {
    const textOpt = new Bit.Advanced.Text3D.Text3DDto();
    textOpt.text = "Text example";
    textOpt.height = 0;
    textOpt.origin = [10, 9, 0];
    textOpt.direction = [0, 0, -1];
    textOpt.rotation = -90;
    const text = bitbybit.advanced.text3d.create(textOpt);
    return text;
}

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