Script: manifold to dxf

manifold to dxf picture
Type
Typescript logo indicatortypescript
Date Created
Jan 8, 2025, 6:05:14 AM
Last Edit Date
Feb 18, 2025, 12:43:54 AM

Project Information

Example of creating a DXF file from a slice of Manifold geometry

View Full Project

Script Code

const start = async () => {


    const box = await bitbybit.manifold.manifold.shapes.cube({
        center: true,
        size: 1
    });

    const rectangle = await bitbybit.manifold.manifold.transforms.scale3D({
        manifold: box,
        vector: [10, 1, 10]
    });

    const cylinder = await bitbybit.manifold.manifold.shapes.cylinder({
        height: 2,
        radiusLow: 1,
        radiusHigh: 1,
        circularSegments: 50,
        center: true
    });

    const rotated_cylinder = await bitbybit.manifold.manifold.transforms.rotate({
        manifold: cylinder,
        vector: [90, 0, 0]
    });

    const rectangle_subtraction = await bitbybit.manifold.manifold.booleans.subtract({
        manifold1: rectangle,
        manifold2: rotated_cylinder
    });

    // Rotated into X-Y plane for cross-section 
    const rotated_subtraction = await bitbybit.manifold.manifold.transforms.rotate({
        manifold: rectangle_subtraction,
        vector: [90, 0, 0]
    });

    const plane = await bitbybit.manifold.manifold.operations.slice({
        manifold: rotated_subtraction,
        height: 0
    });

    const polygons = await bitbybit.manifold.crossSection.crossSectionToPolygons({
        crossSection: plane
    });

    // Likely area to improve using vectors rather than nested for loops 
    const jscadPolygonsPromise = polygons.map(polygonPoints => {
        return bitbybit.jscad.polygon.createFromPoints({
            points: polygonPoints.map(point => {
                return [point[0], point[1], 0]
            })
        });
    });

    const jscadPolygons = await Promise.all(jscadPolygonsPromise);


    bitbybit.draw.drawAnyAsync({
        entity: [rotated_subtraction]
    });

    bitbybit.draw.drawAnyAsync({
        entity: [plane]
    });

    const gridOptions = new Bit.Inputs.Draw.SceneDrawGridMeshDto();
    bitbybit.draw.drawGridMesh(gridOptions);

    createGUI(jscadPolygons)
}
async function createGUI(jscadPolygons) {
    var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");

    var panel = new BABYLON.GUI.StackPanel();
    // panel.width = "800px";
    // panel.background = "#00000055";
    // panel.paddingLeftInPixels = 40;
    // panel.paddingRightInPixels = 40;
    panel.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
    panel.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM
    panel.adaptHeightToChildren = true;
    advancedTexture.addControl(panel);

    var button1 = BABYLON.GUI.Button.CreateSimpleButton('download', "Download DXF");
    button1.width = "350px"
    button1.thickness = 0;
    button1.height = "120px";
    button1.paddingTop = 30;
    button1.color = "white";
    button1.fontSize = 30;
    button1.cornerRadius = 10;
    button1.background = "black";
    button1.onPointerUpObservable.add(() => {
        downloadDXF(jscadPolygons);
    });
    panel.addControl(button1);
}

async function downloadDXF(jscadPolygons) {
    bitbybit.jscad.downloadGeometryDxf({
        geometry: jscadPolygons,
        fileName: "dxf_from_manifold_slice",
        options: undefined
    });
}

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