Skip to main content

Swept profile box

Let's say you want to start designing with a box. Simple extrusion is good if you do not have a complex profile. A way to work with a more complex shape is to draw the profile in 2D, and then sweep it along a base sketch.

const { makeSolid, makeFace, assembleWire, EdgeFinder, genericSweep, Plane } =
replicad;

function profileBox(inputProfile, base) {
const start = inputProfile.blueprint.firstPoint;
const profile = inputProfile.translate(-start[0], -start[1]);

const end = profile.blueprint.lastPoint;

const baseSketch = base.sketchOnPlane();

// We create the side of the box
const side = baseSketch.clone().sweepSketch(
(plane) => {
return profile.sketchOnPlane(plane);
},
{
withContact: true,
}
);

// We put all the pieces together
return makeSolid([
side,
// The face generated by sweeping the end of the profile
makeFace(assembleWire(new EdgeFinder().inPlane("XY", end[1]).find(side))),
// The face generated by the base
baseSketch.face(),
]);
}

This code assumes some things about its input:

  • the input profile is a single open line
  • the base is a single closed line
  • there is only one profile point at the coordinate of the end of the profile

The box will have its base in the XY plane.

Let's build an example

const { makeSolid, makeFace, assembleWire, EdgeFinder, genericSweep, Plane } =
replicad;

function profileBox(inputProfile, base) {
const start = inputProfile.blueprint.firstPoint;
const profile = inputProfile.translate(-start[0], -start[1]);

const end = profile.blueprint.lastPoint;

const baseSketch = base.sketchOnPlane();

// We create the side of the box
const side = baseSketch.clone().sweepSketch(
(plane) => {
return profile.sketchOnPlane(plane);
},
{
withContact: true,
}
);

// We put all the pieces together
return makeSolid([
side,
// The face generated by sweeping the end of the profile
makeFace(assembleWire(new EdgeFinder().inPlane("XY", end[1]).find(side))),
// The face generated by the base
baseSketch.face(),
]);
}

const { draw, drawRoundedRectangle } = replicad;

function main() {
const base = drawRoundedRectangle(30, 20, 5);

const profile = draw()
.line(5, 5)
.line(2, 3)
.hLine(-2)
.vLine(-1)
.bulgeArcTo([0, 1], 0.2)
.done();

return profileBox(profile, base);
}