Skip to content

Commit

Permalink
Create a small helper within the studio
Browse files Browse the repository at this point in the history
  • Loading branch information
sgenoud committed Sep 27, 2024
1 parent 1e92bb1 commit 6d44718
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/studio/src/builder.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as replicad from "replicad";
import initOpenCascade from "./initOCSingle.js";
import initOpenCascadeWithExceptions from "./initOCWithExceptions.js";
import normalizeColor from "./utils/normalizeColor";
import { StudioHelper } from "./utils/StudioHelper";
import { runInContext, buildModuleEvaluator } from "./vm";

self.replicad = replicad;
Expand Down Expand Up @@ -168,7 +169,9 @@ const buildShapesFromCode = async (code, params) => {
await replicad.loadFont("/fonts/HKGrotesk-Regular.ttf");

let shapes;
const helper = new StudioHelper();
try {
self.$ = helper;
shapes = await runCode(code, params);
} catch (e) {
let message = "error";
Expand All @@ -194,7 +197,7 @@ const buildShapesFromCode = async (code, params) => {

shapes = organiseReturnValue(shapes);
shapes = normalizeColorAndOpacity(shapes);
console.log(shapes);
shapes = helper.apply(shapes);
SHAPES_MEMORY.defaultShape = shapes;

return shapes
Expand Down
58 changes: 58 additions & 0 deletions packages/studio/src/utils/StudioHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const shapeOrSketch = (shape) => {
if (!(shape instanceof replicad.Sketch)) return shape;
if (shape.wire.isClosed) return shape.face();
return shape.wire;
};

export class StudioHelper {
constructor() {
this._shapes = [];
this._faceFinder = null;
this._edgeFinder = null;
}

debug(shape) {
this._shapes.push(shape);
return shape;
}

d(shape) {
return this.debug(shape);
}

highlightFace(faceFinder) {
this._faceFinder = faceFinder;
return faceFinder;
}

hf(faceFinder) {
return this.highlightFace(faceFinder);
}

highlightEdge(edgeFinder) {
this._edgeFinder = edgeFinder;
return edgeFinder;
}

he(edgeFinder) {
return this.highlightEdge(edgeFinder);
}

apply(config) {
const conf = config.concat(
this._shapes.map((s, i) => ({
shape: shapeOrSketch(s),
name: `Debug ${i}`,
}))
);
conf.forEach((shape) => {
if (this._edgeFinder && !shape.highlightEdge) {
shape.highlightEdge = this._edgeFinder;
}
if (this._faceFinder && !shape.highlightFace) {
shape.highlightFace = this._faceFinder;
}
});
return conf;
}
}

0 comments on commit 6d44718

Please sign in to comment.