mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-12 03:13:24 +01:00
implement arc storing
This commit is contained in:
parent
c0bf2e64ff
commit
b237610249
4 changed files with 75 additions and 28 deletions
|
|
@ -12,15 +12,7 @@ TCAD.App2D = function() {
|
|||
var boundary = null;
|
||||
if (sketchData != null) {
|
||||
var sketch = JSON.parse(sketchData);
|
||||
try {
|
||||
boundary = this.loadSketch(sketch, layer);
|
||||
} catch(e) {
|
||||
if (e == "CAN'T READ SKETCH") {
|
||||
console.error(e);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
boundary = this.loadSketch(sketch, layer);
|
||||
}
|
||||
|
||||
this.viewer.repaint();
|
||||
|
|
@ -95,6 +87,7 @@ TCAD.App2D = function() {
|
|||
if (obj._class === 'TCAD.TWO.Segment') {
|
||||
to.points = [point(obj.a), point(obj.b)];
|
||||
} else if (obj._class === 'TCAD.TWO.Arc') {
|
||||
to.points = [point(obj.a), point(obj.b), point(obj.c)];
|
||||
} else if (obj._class === 'TCAD.TWO.Circle') {
|
||||
} else if (obj._class === 'TCAD.TWO.Dimension' || obj._class === 'TCAD.TWO.HDimension' || obj._class === 'TCAD.TWO.VDimension') {
|
||||
to.a = obj.a.id;
|
||||
|
|
@ -107,7 +100,9 @@ TCAD.App2D = function() {
|
|||
var constrs = sketch.constraints = [];
|
||||
var sys = app.viewer.parametricManager.system;
|
||||
for (var i = 0; i < sys.length; ++i) {
|
||||
constrs.push(app.serializeConstr(sys[i]));
|
||||
if (!sys[i].aux) {
|
||||
constrs.push(app.serializeConstr(sys[i]));
|
||||
}
|
||||
}
|
||||
var sketchData = JSON.stringify(sketch);
|
||||
console.log(sketchData);
|
||||
|
|
@ -223,6 +218,11 @@ TCAD.App2D.prototype.loadSketch = function(sketch, defaultLayer) {
|
|||
var b = endPoint(obj.points[1]);
|
||||
skobj = new TCAD.TWO.Segment(a, b);
|
||||
} else if (obj._class === 'TCAD.TWO.Arc') {
|
||||
var a = endPoint(obj.points[0]);
|
||||
var b = endPoint(obj.points[1]);
|
||||
var c = endPoint(obj.points[2]);
|
||||
skobj = new TCAD.TWO.Arc(a, b, c);
|
||||
skobj.stabilize(this.viewer);
|
||||
} else if (obj._class === 'TCAD.TWO.Circle') {
|
||||
} else if (obj._class === 'TCAD.TWO.HDimension') {
|
||||
skobj = new TCAD.TWO.HDimension(obj.a, obj.b);
|
||||
|
|
@ -331,7 +331,7 @@ TCAD.App2D.prototype.parseConstr = function (c, index) {
|
|||
var name = c[0];
|
||||
var ps = c[1];
|
||||
var constrCreate = TCAD.TWO.Constraints.Factory[name];
|
||||
if (!constrCreate) {
|
||||
if (constrCreate === undefined) {
|
||||
throw "CAN'T READ SKETCH. Constraint " + name + " hasn't been registered.";
|
||||
}
|
||||
return constrCreate(find, ps);
|
||||
|
|
|
|||
|
|
@ -166,7 +166,7 @@ TCAD.TWO.ParametricManager.prototype.radius = function(objs, promptCallback) {
|
|||
promptDistance = Number(promptDistance);
|
||||
if (promptDistance == promptDistance) { // check for NaN
|
||||
for (var i = 0; i < arcs.length; ++i) {
|
||||
this.system.push(new TCAD.TWO.Constraints.EqualsTo(arcs[i].r, promptDistance));
|
||||
this.system.push(new TCAD.TWO.Constraints.Radius(arcs[i], promptDistance));
|
||||
}
|
||||
this.solve();
|
||||
this.notify();
|
||||
|
|
@ -440,6 +440,8 @@ TCAD.TWO.ParametricManager.prototype.prepare = function(locked, alg) {
|
|||
|
||||
TCAD.TWO.Constraints.Factory = {};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------ //
|
||||
|
||||
TCAD.TWO.Constraints.Coincident = function(a, b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
|
|
@ -462,6 +464,8 @@ TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Coincident.prototype.NAME] = f
|
|||
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;
|
||||
|
|
@ -484,6 +488,8 @@ TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Lock.prototype.NAME] = functio
|
|||
return new TCAD.TWO.Constraints.Lock(refs(data[0]), data[1]);
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------ //
|
||||
|
||||
TCAD.TWO.Constraints.Parallel = function(l1, l2) {
|
||||
this.l1 = l1;
|
||||
this.l2 = l2;
|
||||
|
|
@ -506,6 +512,8 @@ TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Parallel.prototype.NAME] = fun
|
|||
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;
|
||||
|
|
@ -528,6 +536,8 @@ TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Perpendicular.prototype.NAME]
|
|||
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;
|
||||
|
|
@ -551,10 +561,13 @@ TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.P2LDistance.prototype.NAME] =
|
|||
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;
|
||||
this.l = l;
|
||||
this.d = d;
|
||||
this.aux = true;
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.P2LDistanceV.prototype.NAME = 'P2LDistanceV';
|
||||
|
|
@ -567,14 +580,17 @@ 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]));
|
||||
};
|
||||
// We don't serialize auxiliary constraints
|
||||
//
|
||||
//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;
|
||||
|
|
@ -599,11 +615,13 @@ TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.P2PDistance.prototype.NAME] =
|
|||
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;
|
||||
this.d = d;
|
||||
this.aux = true;
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.P2PDistanceV.prototype.NAME = 'P2PDistanceV';
|
||||
|
|
@ -616,10 +634,35 @@ TCAD.TWO.Constraints.P2PDistanceV.prototype.getSolveData = function() {
|
|||
return [[this.NAME, params]];
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.P2PDistanceV.prototype.serialize = function() {
|
||||
return [this.NAME, [this.p1.id, this.p2.id, this.d.id]];
|
||||
// We don't serialize auxiliary constraints
|
||||
//
|
||||
//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]));
|
||||
//};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------ //
|
||||
|
||||
TCAD.TWO.Constraints.Radius = function(arc, d) {
|
||||
this.arc = arc;
|
||||
this.d = d;
|
||||
};
|
||||
|
||||
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]));
|
||||
};
|
||||
TCAD.TWO.Constraints.Radius.prototype.NAME = 'Radius';
|
||||
|
||||
TCAD.TWO.Constraints.Radius.prototype.getSolveData = function() {
|
||||
return [['equalsTo', [this.arc.r], [this.d]]];
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.Radius.prototype.serialize = function() {
|
||||
return [this.NAME, [this.arc.id, this.d]];
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.Factory[TCAD.TWO.Constraints.Radius.prototype.NAME] = function(refs, data) {
|
||||
return new TCAD.TWO.Constraints.Radius(refs(data[0]), data[1]);
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------ //
|
||||
|
|
@ -65,6 +65,11 @@ TCAD.TWO.Arc.prototype.normalDistance = function(aim) {
|
|||
return Math.abs(TCAD.math.distance(aim.x, aim.y, this.c.x, this.c.y) - this.radiusForDrawing());
|
||||
};
|
||||
|
||||
TCAD.TWO.Arc.prototype.stabilize = function(viewer) {
|
||||
this.r.set(this.distanceA());
|
||||
viewer.parametricManager.system.push(new TCAD.TWO.Constraints.P2PDistanceV(this.b, this.c, this.r));
|
||||
viewer.parametricManager.system.push(new TCAD.TWO.Constraints.P2PDistanceV(this.a, this.c, this.r));
|
||||
};
|
||||
|
||||
TCAD.TWO.AddArcTool = function(viewer, layer) {
|
||||
this.viewer = viewer;
|
||||
|
|
@ -120,9 +125,7 @@ TCAD.TWO.AddArcTool.prototype.mouseup = function(e) {
|
|||
} else if (this.point.id === this.arc.a.id) {
|
||||
this.point = this.arc.b;
|
||||
} else {
|
||||
var arc = this.arc;
|
||||
this.viewer.parametricManager.system.push(new TCAD.TWO.Constraints.P2PDistanceV(arc.b, arc.c, arc.r));
|
||||
this.viewer.parametricManager.system.push(new TCAD.TWO.Constraints.P2PDistanceV(arc.a, arc.c, arc.r));
|
||||
this.arc.stabilize(this.viewer);
|
||||
this.viewer.toolManager.releaseControl();
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -236,7 +236,8 @@
|
|||
pm.remove(item.constr);
|
||||
},
|
||||
|
||||
hover : function() {}
|
||||
hover : function(item) {
|
||||
}
|
||||
|
||||
});
|
||||
app.viewer.parametricManager.listeners.push(function() {constrList.refresh()});
|
||||
|
|
|
|||
Loading…
Reference in a new issue