2018-07-23 11:44:34 +00:00
|
|
|
import { macroName } from "./utils";
|
2018-08-05 18:19:48 +02:00
|
|
|
import Point from "./point";
|
|
|
|
import Path from "./path";
|
|
|
|
import Snippet from "./snippet";
|
|
|
|
import Attributes from "./attributes";
|
2018-07-23 11:44:34 +00:00
|
|
|
import * as hooklib from "hooks";
|
2018-08-05 18:19:48 +02:00
|
|
|
import { units } from "./utils";
|
|
|
|
import { round } from "./round";
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
function Part() {
|
|
|
|
this.attributes = new Attributes();
|
2018-07-23 11:12:06 +00:00
|
|
|
this.points = {};
|
|
|
|
this.paths = {};
|
|
|
|
this.snippets = {};
|
2018-08-01 14:55:54 +02:00
|
|
|
this.freeId = 0;
|
2018-08-01 18:18:29 +02:00
|
|
|
this.topLeft = false;
|
|
|
|
this.bottomRight = false;
|
2018-08-03 14:20:28 +02:00
|
|
|
this.width = false;
|
|
|
|
this.height = false;
|
2018-08-05 14:04:15 +02:00
|
|
|
this.render = true;
|
2018-08-05 18:19:48 +02:00
|
|
|
this.points.origin = new Point(0, 0);
|
2018-07-23 11:44:34 +00:00
|
|
|
for (let k in hooklib) this[k] = hooklib[k];
|
2018-08-07 13:46:38 +02:00
|
|
|
// Keep track of attached hooks
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-24 14:38:03 +00:00
|
|
|
// Constructors so macros can create objects
|
2018-08-05 18:19:48 +02:00
|
|
|
this.Point = Point;
|
|
|
|
this.Path = Path;
|
|
|
|
this.Snippet = Snippet;
|
2018-07-24 14:38:03 +00:00
|
|
|
|
2018-08-01 14:55:54 +02:00
|
|
|
// Expose round method to plugins
|
|
|
|
this.round = round;
|
|
|
|
|
2018-08-07 15:22:23 +02:00
|
|
|
let self = this;
|
|
|
|
this.hooks.attach("debug", self);
|
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
return this;
|
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
Part.prototype.macroRunner = function(args) {
|
2018-07-23 20:14:32 +02:00
|
|
|
let self = this;
|
|
|
|
let data = args;
|
|
|
|
let method = function(key, data) {
|
|
|
|
let macro = macroName(key);
|
|
|
|
if (typeof self[macro] === "function") {
|
|
|
|
self[macro](data);
|
2018-07-24 14:38:03 +00:00
|
|
|
} else {
|
|
|
|
console.log(`Warning: ${macro} is not registered`);
|
2018-07-23 20:14:32 +02:00
|
|
|
}
|
2018-07-23 11:44:34 +00:00
|
|
|
};
|
2018-07-23 17:35:06 +00:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
return method;
|
|
|
|
};
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-07 13:46:38 +02:00
|
|
|
/** Debug method, exposes debug hook */
|
2018-08-07 15:22:23 +02:00
|
|
|
Part.prototype.debug = function(data) {};
|
2018-08-07 13:46:38 +02:00
|
|
|
|
2018-08-01 14:55:54 +02:00
|
|
|
/** Returns an unused ID */
|
2018-08-05 18:19:48 +02:00
|
|
|
Part.prototype.getUid = function() {
|
2018-08-01 14:55:54 +02:00
|
|
|
this.freeId += 1;
|
|
|
|
|
|
|
|
return "" + this.freeId;
|
|
|
|
};
|
|
|
|
|
2018-08-01 18:18:29 +02:00
|
|
|
/** Returns a value formatted for units provided in settings */
|
2018-08-05 18:19:48 +02:00
|
|
|
Part.prototype.units = function(value) {
|
2018-08-01 14:55:54 +02:00
|
|
|
return units(value, this.context.settings.units);
|
|
|
|
};
|
|
|
|
|
2018-08-01 18:18:29 +02:00
|
|
|
/** Calculates the part's bounding box and sets it */
|
2018-08-05 18:19:48 +02:00
|
|
|
Part.prototype.boundary = function() {
|
2018-08-01 18:18:29 +02:00
|
|
|
if (this.topLeft) return this; // Cached
|
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
let topLeft = new Point(Infinity, Infinity);
|
|
|
|
let bottomRight = new Point(-Infinity, -Infinity);
|
2018-08-01 18:18:29 +02:00
|
|
|
for (let key in this.paths) {
|
|
|
|
let path = this.paths[key].boundary();
|
|
|
|
if (path.render) {
|
|
|
|
if (path.topLeft.x < topLeft.x) topLeft.x = path.topLeft.x;
|
|
|
|
if (path.topLeft.y < topLeft.y) topLeft.y = path.topLeft.y;
|
|
|
|
if (path.bottomRight.x > bottomRight.x)
|
|
|
|
bottomRight.x = path.bottomRight.x;
|
|
|
|
if (path.bottomRight.y > bottomRight.y)
|
|
|
|
bottomRight.y = path.bottomRight.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add 10mm margin
|
2018-08-05 18:19:48 +02:00
|
|
|
this.topLeft = new Point(topLeft.x - 10, topLeft.y - 10);
|
|
|
|
this.bottomRight = new Point(bottomRight.x + 10, bottomRight.y + 10);
|
2018-08-03 14:20:28 +02:00
|
|
|
this.width = this.bottomRight.x - this.topLeft.x;
|
|
|
|
this.height = this.bottomRight.y - this.topLeft.y;
|
2018-08-01 18:18:29 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Stacks part so that its top left corner is in (0,0) */
|
2018-08-05 18:19:48 +02:00
|
|
|
Part.prototype.stack = function() {
|
2018-08-01 18:18:29 +02:00
|
|
|
if (this.topLeft.x === 0 && this.topLeft.y === 0) return this;
|
|
|
|
|
|
|
|
this.boundary().attr(
|
|
|
|
"transform",
|
|
|
|
`translate(${this.topLeft.x * -1}, ${this.topLeft.y * -1})`
|
|
|
|
);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
2018-08-05 18:19:48 +02:00
|
|
|
Part.prototype.attr = function(name, value, overwrite = false) {
|
2018-08-05 15:52:37 +02:00
|
|
|
if (overwrite) this.attributes.set(name, value);
|
|
|
|
else this.attributes.add(name, value);
|
2018-08-01 18:18:29 +02:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-08-05 15:52:37 +02:00
|
|
|
/** Copies point/path/snippet data from part orig into this */
|
2018-08-05 18:19:48 +02:00
|
|
|
Part.prototype.copy = function(orig) {
|
2018-08-05 15:52:37 +02:00
|
|
|
for (let type of ["points", "paths", "snippets"]) {
|
|
|
|
for (let i in orig[type]) {
|
|
|
|
if (typeof this[type][i] === "undefined") {
|
|
|
|
this[type][i] = orig[type][i].clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2018-08-03 14:20:28 +02:00
|
|
|
};
|
2018-08-05 15:52:37 +02:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
export default Part;
|