Skip to content

Commit

Permalink
feat: Add support for extra space on top and right sides of panels
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 committed Apr 5, 2024
1 parent 02679e1 commit f371ab0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
16 changes: 10 additions & 6 deletions npm/src/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,15 @@ export interface Point {

export class BoardLayouter {
readonly placements: Rectangle<PartToCut>[] = [];
private readonly paddedStock: Rectangle<Stock>;

constructor(
readonly stock: Rectangle<Stock>,
readonly config: Config,
) {}
) {
const padding = -new Distance(config.extraSpace).m;
this.paddedStock = stock.pad({ right: padding, top: padding });
}

tryAddPart(part: PartToCut): boolean {
if (part.material !== this.stock.data.material) return false;
Expand All @@ -161,7 +165,7 @@ export class BoardLayouter {
const possiblePositions: Point[] =
this.placements.length === 0
? // Always position bottom left when empty
[{ x: this.stock.x, y: this.stock.y }]
[{ x: this.paddedStock.x, y: this.paddedStock.y }]
: // Get possible locations from callback
getPossiblePositions();

Expand All @@ -172,7 +176,7 @@ export class BoardLayouter {
)
.find(
(placement) =>
placement.isInside(this.stock) &&
placement.isInside(this.paddedStock) &&
this.placements.every((p) => !placement.isIntersecting(p)),
);

Expand All @@ -191,9 +195,9 @@ export class BoardLayouter {
const bladeWidth = new Distance(this.config.bladeWidth).m;
return [
// Left of stock and top of existing
{ x: this.stock.x, y: existing.top + bladeWidth },
{ x: this.paddedStock.x, y: existing.top + bladeWidth },
// left of existing, bottom of stock
{ x: existing.right + bladeWidth, y: this.stock.y },
{ x: existing.right + bladeWidth, y: this.paddedStock.y },

// Left of existing, top of other existing
...this.placements.map((existing2) => ({
Expand Down Expand Up @@ -262,7 +266,7 @@ export class BoardLayouter {

reduceStock(allStock: Rectangle<Stock>[]): BoardLayouter {
const validStock = allStock.filter(
(stock) => stock.data.material === this.stock.data.material,
(stock) => stock.data.material === this.paddedStock.data.material,
);
const validLayouts = validStock
.map((stock) => {
Expand Down
13 changes: 7 additions & 6 deletions npm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {
PartToCut,
Stock,
StockMatrix,
import {
type PartToCut,
type Stock,
type StockMatrix,
Config,
BoardLayout,
BoardLayoutLeftover,
type BoardLayout,
type BoardLayoutLeftover,
} from './types';
import consola from 'consola';
import { BoardLayouter, Rectangle } from './geometry';
Expand All @@ -26,6 +26,7 @@ export function generateBoardLayouts(
layouts: BoardLayout[];
leftovers: BoardLayoutLeftover[];
} {
config = Config.parse(config);
consola.info('Generating board layouts...');

// Create geometry for stock and parts
Expand Down
4 changes: 4 additions & 0 deletions npm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export const Config = z.object({
* column, making it easier to cut out.
*/
optimize: z.union([z.literal('space'), z.literal('cuts')]).default('cuts'),
/**
* Extra padding to add to the top and right sides of the boards/stock.
*/
extraSpace: Distance.default('0'),
});
export type Config = z.infer<typeof Config>;

Expand Down
7 changes: 6 additions & 1 deletion npm/src/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ export class Distance {
this.m = Number(v.replace('ft', '')) * 0.3048;
} else if (v.endsWith('in') || v.endsWith('"')) {
this.m = Number(v.replace(/(in|")/, '')) * 0.0254;
} else if (v.endsWith('mm')) {
this.m = Number(v.replace('mm', '')) / 1000;
} else {
throw Error('Could not parse distance: ' + JSON.stringify(v));
this.m = Number(v.replace('m', ''));
}
if (isNaN(this.m)) {
throw Error('Could not convert to meters: ' + v);
}
}

Expand Down

0 comments on commit f371ab0

Please sign in to comment.