From f9736e349cd3b7aa9e90a3dc55f02aa938fe2bf8 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Mon, 9 Feb 2015 21:09:29 -0800 Subject: [PATCH] saving constrints refactoring --- web/app/main2d.js | 42 ++++------------------- web/app/parametric.js | 80 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 46 deletions(-) diff --git a/web/app/main2d.js b/web/app/main2d.js index 0d5a6aec..083d3bc9 100644 --- a/web/app/main2d.js +++ b/web/app/main2d.js @@ -323,51 +323,21 @@ TCAD.App2D.prototype.parseConstr = function (c, index) { function find(id) { var p = index[id]; if (!p) { - throw "CAN'T READ SKETCH"; + throw "CAN'T READ SKETCH. Object ref is not found."; } return p; } var name = c[0]; var ps = c[1]; - switch (name) { - case "equal": - return new TCAD.TWO.Constraints.Equal(find(ps[0]), find(ps[1])); - case "equalsTo": - return new TCAD.TWO.Constraints.EqualsTo(find(ps[0]), ps[1]); - case "perpendicular": - return new TCAD.TWO.Constraints.Perpendicular(find(ps[0]), find(ps[1])); - case "parallel": - return new TCAD.TWO.Constraints.Parallel(find(ps[0]), find(ps[1])); - case "P2LDistance": - return new TCAD.TWO.Constraints.P2LDistance(find(ps[0]), find(ps[1]), ps[2]); - case "P2LDistanceV": - return new TCAD.TWO.Constraints.P2LDistanceV(find(ps[0]), find(ps[1]), find(ps[2])); - case "P2PDistance": - return new TCAD.TWO.Constraints.P2PDistance(find(ps[0]), find(ps[1]), ps[2]); - case "P2PDistanceV": - return new TCAD.TWO.Constraints.P2PDistanceV(find(ps[0]), find(ps[1]), find(ps[2])); + var constrCreate = TCAD.TWO.Constraints.Factory[name]; + if (!constrCreate) { + throw "CAN'T READ SKETCH. Constraint " + name + " hasn't been registered."; } + return constrCreate(find, ps); }; TCAD.App2D.prototype.serializeConstr = function (c) { - switch (c.NAME) { - case "equal": - return ['equal', [c.p1.id, c.p2.id]]; - case "equalsTo": - return ['equalsTo', [c.p.id, c.v]]; - case "perpendicular": - return ['perpendicular', [c.l1.id, c.l2.id]]; - case "parallel": - return ['parallel', [c.l1.id, c.l2.id]]; - case "P2LDistance": - return ['P2LDistance', [c.p.id, c.l.id, c.d]]; - case "P2LDistanceV": - return ['P2LDistanceV', [c.p.id, c.l.id, c.d.get()]]; - case "P2PDistance": - return ['P2PDistance', [c.p1.id, c.p2.id, c.d]]; - case "P2PDistanceV": - return ['P2PDistanceV', [c.p1.id, c.p2.id, c.d.get()]]; - } + return c.serialize(); }; TCAD.App2D.prototype.getSketchId = function() { diff --git a/web/app/parametric.js b/web/app/parametric.js index 87662f95..7015ad3a 100644 --- a/web/app/parametric.js +++ b/web/app/parametric.js @@ -73,7 +73,7 @@ TCAD.TWO.ParametricManager.prototype.removeConstraintsByParams = function(ownedP TCAD.TWO.ParametricManager.prototype.lock = function(objs) { var p = this._fetchPoints(objs); for (var i = 0; i < p.length; ++i) { - this.system.push(new TCAD.TWO.Constraints.Lock(p[i], p[i])); + this.system.push(new TCAD.TWO.Constraints.Lock(p[i], { x : p[i].x, y : p[i].y} )); } this.solve(); this.notify(); @@ -185,13 +185,8 @@ TCAD.TWO.ParametricManager.prototype.linkObjects = function(objs) { this.system.push(c); } - for (i = 0; i < objs.length; ++i) { - for (var j = 0; j < objs.length; ++j) { - if (objs[i].id !== objs[j].id) { - objs[j].linked.push(objs[i]); - } - } - } + + this.notify(); }; @@ -443,6 +438,7 @@ TCAD.TWO.ParametricManager.prototype.prepare = function(locked, alg) { return solver; }; +TCAD.TWO.Constraints.Factory = {}; TCAD.TWO.Constraints.Coincident = function(a, b) { this.a = a; @@ -458,6 +454,14 @@ TCAD.TWO.Constraints.Coincident.prototype.getSolveData = function() { ]; }; +TCAD.TWO.Constraints.Coincident.prototype.serialize = function() { + return [this.NAME, [this.a.id, this.b.id]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Coincident.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.Coincident(refs(data[0]), refs(data[1])); +}; + TCAD.TWO.Constraints.Lock = function(p, c) { this.p = p; this.c = c; @@ -467,11 +471,19 @@ TCAD.TWO.Constraints.Lock.prototype.NAME = 'lock'; TCAD.TWO.Constraints.Lock.prototype.getSolveData = function() { return [ - ['equalsTo', [this.p._x, this.c.x], []], - ['equalsTo', [this.p._y, this.c.y], []] + ['equalsTo', [this.p._x], [this.c.x]], + ['equalsTo', [this.p._y], [this.c.y]] ]; }; +TCAD.TWO.Constraints.Lock.prototype.serialize = function() { + return [this.NAME, [this.p.id, this.c]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Lock.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.Lock(refs(data[0]), data[1]); +}; + TCAD.TWO.Constraints.Parallel = function(l1, l2) { this.l1 = l1; this.l2 = l2; @@ -486,6 +498,14 @@ TCAD.TWO.Constraints.Parallel.prototype.getSolveData = function() { return [[this.NAME, params, []]]; }; +TCAD.TWO.Constraints.Parallel.prototype.serialize = function() { + return [this.NAME, [this.l1.id, this.l2.id]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Parallel.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.Parallel(refs(data[0]), refs(data[1])); +}; + TCAD.TWO.Constraints.Perpendicular = function(l1, l2) { this.l1 = l1; this.l2 = l2; @@ -500,6 +520,14 @@ TCAD.TWO.Constraints.Perpendicular.prototype.getSolveData = function() { return [[this.NAME, params, []]]; }; +TCAD.TWO.Constraints.Perpendicular.prototype.serialize = function() { + return [this.NAME, [this.l1.id, this.l2.id]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Perpendicular.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.Perpendicular(refs(data[0]), refs(data[1])); +}; + TCAD.TWO.Constraints.P2LDistance = function(p, l, d) { this.p = p; this.l = l; @@ -515,6 +543,13 @@ TCAD.TWO.Constraints.P2LDistance.prototype.getSolveData = function() { return [[this.NAME, params, [this.d]]]; }; +TCAD.TWO.Constraints.P2LDistance.prototype.serialize = function() { + return [this.NAME, [this.p.id, this.l.id, this.d]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.P2LDistance.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.P2LDistance(refs(data[0]), refs(data[1]), data[2]); +}; TCAD.TWO.Constraints.P2LDistanceV = function(p, l, d) { this.p = p; @@ -532,6 +567,14 @@ TCAD.TWO.Constraints.P2LDistanceV.prototype.getSolveData = function() { return [[this.NAME, params]]; }; +TCAD.TWO.Constraints.P2LDistanceV.prototype.serialize = function() { + return [this.NAME, [this.p.id, this.l.id, this.d.id]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.P2LDistanceV.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.P2LDistanceV(refs(data[0]), refs(data[1]), refs(data[2])); +}; + TCAD.TWO.Constraints.P2PDistance = function(p1, p2, d) { this.p1 = p1; @@ -548,6 +591,15 @@ TCAD.TWO.Constraints.P2PDistance.prototype.getSolveData = function() { return [[this.NAME, params, [this.d]]]; }; +TCAD.TWO.Constraints.P2PDistance.prototype.serialize = function() { + return [this.NAME, [this.p1.id, this.p2.id, this.d]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.P2PDistance.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.P2PDistance(refs(data[0]), refs(data[1]), data[2]); +}; + + TCAD.TWO.Constraints.P2PDistanceV = function(p1, p2, d) { this.p1 = p1; this.p2 = p2; @@ -562,4 +614,12 @@ TCAD.TWO.Constraints.P2PDistanceV.prototype.getSolveData = function() { this.p2.collectParams(params); params.push(this.d); return [[this.NAME, params]]; +}; + +TCAD.TWO.Constraints.P2PDistanceV.prototype.serialize = function() { + return [this.NAME, [this.p1.id, this.p2.id, this.d.id]]; +}; + +TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.P2PDistanceV.prototype.NAME] = function(refs, data) { + return new TCAD.TWO.Constraints.P2PDistanceV(refs(data[0]), refs(data[1]), refs(data[2])); }; \ No newline at end of file