intersect with a Plane

intersect with a Plane script details
Type
Typescript logo image
typescript
App Version
0.19.2
Visibility
public
Date Created
Jan 3, 2025, 4:22:26 PM
Last Edit Date
Feb 3, 2025, 9:34:26 AM

Script Details

The Code
const start = async () => { const url = "https://ik.imagekit.io/bitbybit/prod/ik-seo/o/users%2FyVqNAFXmieX0iAkdP6wAz5BJ4X82%2Fprojects%2Fh3XNAbbSL5n6mYgPaGmk%2Fassets%2Fmultiaxis-xyz/multiaxis-xyz.step?alt=media&token=43232172-fef4-4eca-a31d-787f295761c7"; const stepFile = await bitbybit.asset.fetchFile({ url }); const box1 = await bitbybit.occt.shapes.solid.createCube({ size: 5, center: [0, 0, 0], }); const parsedStep = await bitbybit.occt.io.loadSTEPorIGES({ assetFile: stepFile, adjustZtoY: true, }); const scaledShape = await bitbybit.occt.transforms.scale({ shape: parsedStep, factor: 0.01, }); const plane = await bitbybit.occt.shapes.face.createSquareFace({ size: 10, center: [0, 2.5, 4], direction: [0, 1, 1] }); const scaledPlane = await bitbybit.occt.transforms.scale({ shape: plane, factor: 1, }); const plane2 = await bitbybit.occt.shapes.face.createSquareFace({ size: 10, center: [0, 2.5, 4], direction: [0, 1, 1] }); const scaledPlane2 = await bitbybit.occt.transforms.scale({ shape: plane2, factor: 1, }); let center = [0, 2.5, 4]; let direction = [0, 1, 1]; const step = 1; let finalcuts = []; let z = 4; for (let i = 0; i < 4; i++) { z = z - step; center = [0, 2.5, z]; finalcuts[i] = await slice(scaledShape, center, direction) } const compound = await bitbybit.occt.shapes.compound.makeCompound({ shapes: finalcuts }); const box = await bitbybit.occt.shapes.solid.createBox({ width: 7, height: 7, length: 4, center: [0, 0, -3] }); const faceCut = await bitbybit.occt.shapes.face.createSquareFace({ size: 10, direction: [direction[0], direction[1], direction[2]], center: [center[0], center[1], center[2]] }); const extrudedFace = await bitbybit.occt.operations.extrude({ shape: faceCut, direction: [0, 4, 1], }); const drawOpt = new Bit.Inputs.Draw.DrawOcctShapeOptions(); drawOpt.faceColour = "#ff00ff"; bitbybit.draw.drawAnyAsync({ entity: finalcuts, options: drawOpt, }); // bitbybit.draw.drawAnyAsync({ // entity: extrudedFace, // options: drawOpt, // }); const solids = await bitbybit.occt.shapes.solid.getSolids({ shape: scaledShape }); let i = 0; const leftoverPromises = solids.map(s => { return bitbybit.occt.booleans.difference({ shape: s, shapes: [extrudedFace], keepEdges: false }); }) const leftOverObject = await Promise.all(leftoverPromises); const compoundLefOver = await bitbybit.occt.shapes.compound.makeCompound({ shapes: leftOverObject }); bitbybit.draw.drawAnyAsync({ entity: leftOverObject }); const data = bitbybit.occt.io.saveShapeSTEPAndReturn({ shape: compoundLefOver, fileName: "leftover.step", tryDownload: true, adjustYtoZ: true, }); data.then(function (d) { console.log(d); }) // const data =bitbybit.occt.io.saveShapeSTEPAndReturn({ // shape: compound, // fileName: "sliced-result.step", // tryDownload: false, // adjustYtoZ: true, // }); // console.log("downloaded file is:"); // console.log(data); // data.then(function (d) { // console.log("here....."); // console.log(d); // }) // console.log("downloaded file is:"); // console.log(data); // data.then(function (d) { // console.log("here....."); // console.log(d); // }) // const cut2 = await bitbybit.occt.booleans.difference({ // shape: scaledShape, // shapes: [box], // keepEdges: false, // }); // drawOpt.faceColour = "#0000ff"; // bitbybit.draw.drawAnyAsync({ // entity: box, // options: drawOpt // }); drawOpt.faceColour = "#0000ff"; // bitbybit.draw.drawAnyAsync({ // entity: cut2, // options: drawOpt // }); } async function slice(scaledShape, center, direction) { const plane = await bitbybit.occt.shapes.face.createSquareFace({ size: 10, center: center, direction: direction }); const scaledPlane = await bitbybit.occt.transforms.scale({ shape: plane, factor: 1, }); const cut = await bitbybit.occt.booleans.intersection({ shapes: [scaledShape, scaledPlane], keepEdges: false, }); console.log(cut); return cut; } start();