1
0
Fork 0

🚧 Part cloning

This commit is contained in:
Joost De Cock 2018-08-03 14:20:28 +02:00
parent 9a60dc5756
commit 18cc30fe74
7 changed files with 90 additions and 3 deletions

View file

@ -48,4 +48,12 @@ attributes.prototype.renderIfPrefixIs = function(prefix = "") {
return svg; return svg;
}; };
/** Returns a deep copy of this */
attributes.prototype.clone = function() {
let clone = new attributes();
clone.list = JSON.parse(JSON.stringify(this.list));
return clone;
};
export default attributes; export default attributes;

View file

@ -15,6 +15,8 @@ function part(id) {
this.freeId = 0; this.freeId = 0;
this.topLeft = false; this.topLeft = false;
this.bottomRight = false; this.bottomRight = false;
this.width = false;
this.height = false;
this.render = id.substr(0, 1) === "_" ? false : true; this.render = id.substr(0, 1) === "_" ? false : true;
this.points.origin = new point(0, 0); this.points.origin = new point(0, 0);
for (let k in hooklib) this[k] = hooklib[k]; for (let k in hooklib) this[k] = hooklib[k];
@ -77,6 +79,8 @@ part.prototype.boundary = function() {
// Add 10mm margin // Add 10mm margin
this.topLeft = new point(topLeft.x - 10, topLeft.y - 10); this.topLeft = new point(topLeft.x - 10, topLeft.y - 10);
this.bottomRight = new point(bottomRight.x + 10, bottomRight.y + 10); this.bottomRight = new point(bottomRight.x + 10, bottomRight.y + 10);
this.width = this.bottomRight.x - this.topLeft.x;
this.height = this.bottomRight.y - this.topLeft.y;
return this; return this;
}; };
@ -100,4 +104,39 @@ part.prototype.attr = function(name, value) {
return this; return this;
}; };
/** Returns a (deep) clone of this part object */
part.prototype.clone = function() {};
/** Returns a deep copy of this */
part.prototype.clone = function(id = false) {
let clone = new part(id);
clone.freeId = 0;
clone.width = this.width;
clone.height = this.height;
clone.attributes = this.attributes.clone();
clone.render = this.render;
if (!id) clone.id = this.id;
if (this.topLeft) clone.topLeft = this.topLeft.clone();
else clone.topLeft = false;
if (this.bottomRight) clone.bottomRight = this.bottomRight.clone();
else clone.bottomRight = false;
clone.points = {};
clone.paths = {};
clone.snippets = {};
for (let i in this.points) clone.points[i] = this.points[i].clone();
for (let i in this.paths) clone.paths[i] = this.paths[i].clone();
for (let i in this.snippets) clone.snippets[i] = this.snippets[i].clone();
for (let k in hooklib) clone[k] = hooklib[k];
clone.point = point;
clone.path = path;
clone.snippet = snippet;
clone.round = round;
clone.context = this.context;
return clone;
};
export default part; export default part;

View file

@ -152,4 +152,30 @@ path.prototype.boundary = function() {
return this; return this;
}; };
/** Returns a deep copy of this */
path.prototype.clone = function() {
let clone = new path();
clone.render = this.render = true;
if (this.topLeft) clone.topLeft = this.topLeft.clone();
else clone.topLeft = false;
if (this.bottomRight) clone.bottomRight = this.bottomRight.clone();
else clone.bottomRight = false;
clone.attributes = this.attributes.clone();
clone.ops = [];
for (let i in this.ops) {
let op = this.ops[i];
clone.ops[i] = { type: op.type };
if (op.type === "move" || op.type === "line") {
clone.ops[i].to = op.to.clone();
} else if (op.type === "curve") {
clone.ops[i].to = op.to.clone();
clone.ops[i].cp1 = op.cp1.clone();
clone.ops[i].cp2 = op.cp2.clone();
}
}
return clone;
};
export default path; export default path;

View file

@ -85,7 +85,7 @@ pattern.prototype.render = function() {
this.hooks.attach("postRenderSvg", this.svg); this.hooks.attach("postRenderSvg", this.svg);
//this.hooks.attach('insertText', this.svg); //this.hooks.attach('insertText', this.svg);
return this.svg.render(this); return this.pack().svg.render(this);
}; };
pattern.prototype.on = function(hook, method) { pattern.prototype.on = function(hook, method) {

View file

@ -108,4 +108,12 @@ point.prototype.shiftOutwards = function(that, distance) {
return this.shiftTowards(that, this.dist(that) + distance); return this.shiftTowards(that, this.dist(that) + distance);
}; };
/** Returns a deep copy of this */
point.prototype.clone = function() {
let clone = new point(this.x, this.y);
clone.attributes = this.attributes.clone();
return clone;
};
export default point; export default point;

View file

@ -9,4 +9,12 @@ function snippet(def, anchor, description = "") {
return this; return this;
} }
/** Returns a deep copy of this */
snippet.prototype.clone = function() {
let clone = new snippet(this.def, this.anchor.clone(), this.description);
clone.attributes = this.attributes.clone();
return clone;
};
export default snippet; export default snippet;

View file

@ -41,8 +41,6 @@ svg.prototype.insertText = function() {};
/** Renders a draft object as SVG */ /** Renders a draft object as SVG */
svg.prototype.render = function(pattern) { svg.prototype.render = function(pattern) {
this.preRenderSvg(); this.preRenderSvg();
// this needs to run after the preSvgRender hook as it might add stuff
pattern.pack();
this.attributes.add("width", pattern.width + "mm"); this.attributes.add("width", pattern.width + "mm");
this.attributes.add("height", pattern.height + "mm"); this.attributes.add("height", pattern.height + "mm");
this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`); this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`);