Script
TypeScript example of OCCT wires to JSCAD

- Project
- OCCT Wires to JSCAD to DXF
- Author
- bitbybit
- Created
- Feb 26, 2024, 2:11:30 PM
- Last edit
- Mar 1, 2024, 7:15:38 AM
Project
OCCT Wires to JSCAD to DXF
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 projectScript 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();
