Skip to content

Commit 00a7d03

Browse files
tristan-hubersgenoud
authored andcommitted
Move deserialize methods to be standalones instead of static members
1 parent 0c79ef3 commit 00a7d03

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

packages/replicad/src/draw.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
ApproximationOptions,
33
BoundingBox2d,
4-
Curve2D,
4+
deserializeCurve2D,
55
make2dCircle,
66
make2dEllipse,
77
make2dInerpolatedBSplineCurve,
@@ -84,29 +84,6 @@ export class Drawing implements DrawingInterface {
8484
return JSON.stringify(serializeHelper(this.innerShape));
8585
}
8686

87-
static deserializeDrawing(data: string): Drawing {
88-
function deserializeHelper(json: any): Shape2D {
89-
if (json["type"] === "CompoundBlueprint") {
90-
const blueprints = json["blueprints"].map(deserializeHelper);
91-
return new CompoundBlueprint(blueprints);
92-
} else if (json["type"] === "Blueprints") {
93-
const blueprints = json["blueprints"].map(deserializeHelper);
94-
return new Blueprints(blueprints);
95-
} else if (json["type"] === "Blueprint") {
96-
const curves = json["curves"].map((c: string) =>
97-
Curve2D.deserializeCurve(c)
98-
);
99-
return new Blueprint(curves);
100-
} else {
101-
throw new Error("Unknown shape type for deserialization");
102-
}
103-
}
104-
105-
const json = JSON.parse(data);
106-
const shape = deserializeHelper(json);
107-
return new Drawing(shape);
108-
}
109-
11087
get boundingBox(): BoundingBox2d {
11188
if (!this.innerShape) return new BoundingBox2d();
11289
return this.innerShape.boundingBox;
@@ -304,6 +281,31 @@ export class DrawingPen
304281
}
305282
}
306283

284+
/**
285+
* Deserializes a drawing from a string. String is expected to be in the format
286+
* generated by `Drawing.serialize()`.
287+
*/
288+
export function deserializeDrawing(data: string): Drawing {
289+
function deserializeHelper(json: any): Shape2D {
290+
if (json["type"] === "CompoundBlueprint") {
291+
const blueprints = json["blueprints"].map(deserializeHelper);
292+
return new CompoundBlueprint(blueprints);
293+
} else if (json["type"] === "Blueprints") {
294+
const blueprints = json["blueprints"].map(deserializeHelper);
295+
return new Blueprints(blueprints);
296+
} else if (json["type"] === "Blueprint") {
297+
const curves = json["curves"].map((c: string) => deserializeCurve2D(c));
298+
return new Blueprint(curves);
299+
} else {
300+
throw new Error("Unknown shape type for deserialization");
301+
}
302+
}
303+
304+
const json = JSON.parse(data);
305+
const shape = deserializeHelper(json);
306+
return new Drawing(shape);
307+
}
308+
307309
/**
308310
* Creates a drawing pen to programatically draw in 2D.
309311
*

packages/replicad/src/lib2d/Curve2D.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import { pnt } from "./ocWrapper.js";
1616
import { reprPnt } from "./utils.js";
1717
import { distance2d, samePoint } from "./vectorOperations.js";
1818

19+
export function deserializeCurve2D(data: string): Curve2D {
20+
const oc = getOC();
21+
const handle = oc.GeomToolsWrapper.Read(data);
22+
return new Curve2D(handle);
23+
}
24+
1925
export class Curve2D extends WrappingObj<Handle_Geom2d_Curve> {
2026
_boundingBox: null | BoundingBox2d;
2127
constructor(handle: Handle_Geom2d_Curve) {
@@ -53,12 +59,6 @@ export class Curve2D extends WrappingObj<Handle_Geom2d_Curve> {
5359
return oc.GeomToolsWrapper.Write(this.wrapped);
5460
}
5561

56-
static deserializeCurve(data: string): Curve2D {
57-
const oc = getOC();
58-
const handle = oc.GeomToolsWrapper.Read(data);
59-
return new Curve2D(handle);
60-
}
61-
6262
value(parameter: number): Point2D {
6363
const pnt = this.innerCurve.Value(parameter);
6464
const vec: Point2D = [pnt.X(), pnt.Y()];

packages/replicad/src/shapes.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ export const shapeType = (shape: TopoDS_Shape): TopAbs_ShapeEnum => {
203203
return shape.ShapeType();
204204
};
205205

206+
export function deserializeShape(data: string): Shape<TopoDS_Shape> {
207+
const oc = getOC();
208+
return new Shape(oc.BRepToolsWrapper.Read(data));
209+
}
210+
206211
export class Shape<Type extends TopoDS_Shape> extends WrappingObj<Type> {
207212
constructor(ocShape: Type) {
208213
super(ocShape);
@@ -217,11 +222,6 @@ export class Shape<Type extends TopoDS_Shape> extends WrappingObj<Type> {
217222
return oc.BRepToolsWrapper.Write(this.wrapped);
218223
}
219224

220-
static deserializeShape(data: string): Shape<TopoDS_Shape> {
221-
const oc = getOC();
222-
return new Shape(oc.BRepToolsWrapper.Read(data));
223-
}
224-
225225
get hashCode(): number {
226226
return this.wrapped.HashCode(HASH_CODE_MAX);
227227
}

0 commit comments

Comments
 (0)