mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-13 11:54:01 +01:00
second dims approach
This commit is contained in:
parent
00fa2c8f40
commit
de77500902
4 changed files with 181 additions and 14 deletions
|
|
@ -25,6 +25,12 @@ TCAD.TWO.Styles = {
|
|||
lineWidth : 2,
|
||||
strokeStyle : "#00FF00",
|
||||
fillStyle : "#00FF00"
|
||||
},
|
||||
|
||||
DIM : {
|
||||
lineWidth : 1,
|
||||
strokeStyle : "#bcffc1",
|
||||
fillStyle : "#00FF00"
|
||||
}
|
||||
|
||||
};
|
||||
|
|
@ -81,6 +87,9 @@ TCAD.TWO.Viewer = function(canvas) {
|
|||
this.snapped = [];
|
||||
|
||||
this._setupServiceLayer();
|
||||
this.dimLayer = new TCAD.TWO.Layer("_dim", TCAD.TWO.Styles.DIM);
|
||||
this.layers.push(this.dimLayer);
|
||||
|
||||
this.refresh();
|
||||
};
|
||||
|
||||
|
|
@ -115,7 +124,7 @@ TCAD.TWO.Viewer.prototype.remove = function(obj) {
|
|||
}
|
||||
};
|
||||
|
||||
TCAD.TWO.Viewer.prototype.search = function(x, y, buffer, deep, onlyPoints) {
|
||||
TCAD.TWO.Viewer.prototype.search = function(x, y, buffer, deep, onlyPoints, filter) {
|
||||
|
||||
buffer *= 0.5;
|
||||
|
||||
|
|
@ -126,25 +135,33 @@ TCAD.TWO.Viewer.prototype.search = function(x, y, buffer, deep, onlyPoints) {
|
|||
var unreachable = buffer * 2;
|
||||
var heroLength = unreachable; // unreachable
|
||||
|
||||
function isFiltered(o) {
|
||||
for (var i = 0; i < filter.length; ++i) {
|
||||
if (filter[i] === o) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < this.layers.length; i++) {
|
||||
var objs = this.layers[i].objects;
|
||||
for (var j = 0; j < objs.length; j++) {
|
||||
var l = unreachable + 1;
|
||||
var hit = !objs[j].acceptV(true, function(o) {
|
||||
var before = pickResult.length;
|
||||
objs[j].acceptV(true, function(o) {
|
||||
if (onlyPoints && o._class !== 'TCAD.TWO.EndPoint') {
|
||||
return false;
|
||||
}
|
||||
l = o.normalDistance(aim);
|
||||
if (l >= 0 && l <= buffer) {
|
||||
if (l >= 0 && l <= buffer && !isFiltered(o)) {
|
||||
pickResult.push(o);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
var hit = before - pickResult.length != 0;
|
||||
if (hit) {
|
||||
if (!deep && pickResult.length != 0) return pickResult;
|
||||
if (l < heroLength) {
|
||||
if (l >= 0 && l < heroLength) {
|
||||
heroLength = l;
|
||||
heroIdx = pickResult.length - 1;
|
||||
}
|
||||
|
|
@ -168,7 +185,6 @@ TCAD.TWO.Viewer.prototype._setupServiceLayer = function() {
|
|||
layer = new TCAD.TWO.Layer("_selection", TCAD.TWO.Styles.DEFAULT);
|
||||
layer.objects = this.selected;
|
||||
this._serviceLayers.push(layer);
|
||||
|
||||
};
|
||||
|
||||
TCAD.TWO.Viewer.prototype.refresh = function() {
|
||||
|
|
@ -209,14 +225,9 @@ TCAD.TWO.Viewer.prototype.repaint = function() {
|
|||
|
||||
TCAD.TWO.Viewer.prototype.snap = function(x, y, excl) {
|
||||
this.cleanSnap();
|
||||
var snapTo = this.search(x, y, 20 / this.scale, false, true);
|
||||
var snapTo = this.search(x, y, 20 / this.scale, true, true, excl);
|
||||
if (snapTo.length > 0) {
|
||||
snapTo = snapTo[0];
|
||||
for (var i = 0; i < excl.length; i++) {
|
||||
if (excl[i] === snapTo) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.mark(snapTo, TCAD.TWO.Styles.SNAP);
|
||||
this.snapped.push(snapTo);
|
||||
return snapTo;
|
||||
|
|
@ -271,7 +282,7 @@ TCAD.TWO.Viewer.prototype.select = function(objs, exclusive) {
|
|||
|
||||
TCAD.TWO.Viewer.prototype.pick = function(e) {
|
||||
var m = this.screenToModel(e);
|
||||
return this.search(m.x, m.y, 20 / this.scale, true, false);
|
||||
return this.search(m.x, m.y, 20 / this.scale, true, false, []);
|
||||
};
|
||||
|
||||
TCAD.TWO.Viewer.prototype.mark = function(obj, style) {
|
||||
|
|
|
|||
|
|
@ -59,6 +59,10 @@ TCAD.App2D = function() {
|
|||
app.viewer.toolManager.releaseControl();
|
||||
});
|
||||
|
||||
this.registerAction('addDim', "Add Dimension", function () {
|
||||
app.viewer.toolManager.takeControl(new TCAD.TWO.AddDimTool(app.viewer, app.viewer.dimLayer));
|
||||
});
|
||||
|
||||
this.registerAction('save', "Save", function () {
|
||||
var sketch = {};
|
||||
//sketch.boundary = boundary;
|
||||
|
|
|
|||
150
web/app/sketch/dim.js
Normal file
150
web/app/sketch/dim.js
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
|
||||
TCAD.TWO.Dimension = function(a, b) {
|
||||
TCAD.TWO.SketchObject.call(this);
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.flip = false;
|
||||
};
|
||||
|
||||
TCAD.TWO.utils.extend(TCAD.TWO.Dimension, TCAD.TWO.SketchObject);
|
||||
|
||||
TCAD.TWO.Dimension.prototype._class = 'TCAD.TWO.Dimension';
|
||||
|
||||
TCAD.TWO.Dimension.prototype.collectParams = function(params) {
|
||||
};
|
||||
|
||||
TCAD.TWO.Dimension.prototype.getReferencePoint = function() {
|
||||
return this.a;
|
||||
};
|
||||
|
||||
TCAD.TWO.Dimension.prototype.translateImpl = function(dx, dy) {
|
||||
};
|
||||
|
||||
TCAD.TWO.Dimension.prototype.drawImpl = function(ctx, scale) {
|
||||
|
||||
var off = 30;
|
||||
|
||||
var a = this.flip ? this.b : this.a;
|
||||
var b = this.flip ? this.a : this.b;
|
||||
|
||||
var d = TCAD.math.distanceAB(a, b);
|
||||
|
||||
var _vx = - (b.y - a.y);
|
||||
var _vy = b.x - a.x;
|
||||
|
||||
//normalize
|
||||
var _vxn = _vx / d;
|
||||
var _vyn = _vy / d;
|
||||
|
||||
_vx = _vxn * off;
|
||||
_vy = _vyn * off;
|
||||
|
||||
ctx.beginPath();
|
||||
|
||||
var _ax = a.x + _vx;
|
||||
var _ay = a.y + _vy;
|
||||
var _bx = b.x + _vx;
|
||||
var _by = b.y + _vy;
|
||||
|
||||
ctx.moveTo(_ax, _ay);
|
||||
ctx.lineTo(_bx, _by);
|
||||
|
||||
|
||||
function drawRef(a) {
|
||||
ctx.moveTo(a.x, a.y);
|
||||
var off2 = 1.2;
|
||||
ctx.lineTo(a.x + _vx * off2, a.y + _vy * off2);
|
||||
}
|
||||
|
||||
drawRef(a);
|
||||
drawRef(b);
|
||||
|
||||
ctx.closePath();
|
||||
ctx.stroke();
|
||||
|
||||
function drawArrow(x, y) {
|
||||
var s1 = 50;
|
||||
var s2 = 20;
|
||||
ctx.lineCap = 'round';
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, y);
|
||||
ctx.lineTo(x - s1, y - s2);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// drawArrow(_ax, _ay);
|
||||
// drawArrow(_bx, _by);
|
||||
|
||||
ctx.font="12px Arial";
|
||||
var txt = d.toFixed(2);
|
||||
var h = d / 2 - ctx.measureText(txt).width / 2;
|
||||
|
||||
if (h > 0) {
|
||||
var tx = _ax - (- _vyn) * h;
|
||||
var ty = _ay - ( _vxn) * h;
|
||||
ctx.save();
|
||||
ctx.translate(tx, ty);
|
||||
ctx.fillText(txt ,0, 0);
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
TCAD.TWO.Dimension.prototype.normalDistance = function(aim) {
|
||||
return -1;
|
||||
};
|
||||
|
||||
TCAD.TWO.AddDimTool = function(viewer, layer) {
|
||||
this.viewer = viewer;
|
||||
this.layer = layer;
|
||||
this.dim = null;
|
||||
this._v = new TCAD.Vector(0, 0, 0);
|
||||
};
|
||||
|
||||
TCAD.TWO.AddDimTool.prototype.keydown = function(e) {};
|
||||
TCAD.TWO.AddDimTool.prototype.keypress = function(e) {};
|
||||
TCAD.TWO.AddDimTool.prototype.keyup = function(e) {};
|
||||
TCAD.TWO.AddDimTool.prototype.cleanup = function(e) {};
|
||||
|
||||
TCAD.TWO.AddDimTool.prototype.mousemove = function(e) {
|
||||
var p = this.viewer.screenToModel(e);
|
||||
this.viewer.snap(p.x, p.y, []);
|
||||
if (this.dim != null) {
|
||||
this.dim.b.x = p.x;
|
||||
this.dim.b.y = p.y;
|
||||
}
|
||||
this.viewer.refresh();
|
||||
};
|
||||
|
||||
TCAD.TWO.AddDimTool.prototype.mouseup = function(e) {
|
||||
|
||||
if (e.button > 0 && this.dim != null) {
|
||||
this.dim.flip = !this.dim.flip;
|
||||
this.viewer.refresh();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.viewer.snapped.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
var p = this.viewer.snapped.pop();
|
||||
this.viewer.cleanSnap();
|
||||
|
||||
if (this.dim == null) {
|
||||
this.dim = new TCAD.TWO.Dimension(p, new TCAD.TWO.EndPoint(p.x, p.y));
|
||||
this.layer.objects.push(this.dim);
|
||||
this.viewer.refresh();
|
||||
} else {
|
||||
this.dim.b = p;
|
||||
this.viewer.toolManager.releaseControl();
|
||||
this.viewer.refresh();
|
||||
}
|
||||
};
|
||||
|
||||
TCAD.TWO.AddDimTool.prototype.mousedown = function(e) {
|
||||
};
|
||||
|
||||
TCAD.TWO.AddDimTool.prototype.mousewheel = function(e) {
|
||||
};
|
||||
|
||||
|
|
@ -188,6 +188,8 @@
|
|||
<script src="app/sketch/arc.js"></script>
|
||||
<script src="app/sketch/circle.js"></script>
|
||||
<script src="app/sketch/segment.js"></script>
|
||||
<script src="app/sketch/dim.js"></script>
|
||||
|
||||
<script src="app/math/math.js"></script>
|
||||
<script src="app/math/matrix.js"></script>
|
||||
<script src="app/math/optim.js"></script>
|
||||
|
|
@ -257,7 +259,7 @@
|
|||
--><button class="btn tbtn act-addArc sep" style="background-image: url(img/arc.png);" type="submit" value=""></button><!--
|
||||
--><button class="btn tbtn" style="background-image: url(img/hdim.png);" type="submit" value=""></button><!--
|
||||
--><button class="btn tbtn" style="background-image: url(img/vdim.png);" type="submit" value=""></button><!--
|
||||
--><button class="btn tbtn" style="background-image: url(img/dim.png);" type="submit" value=""></button><!--
|
||||
--><button class="btn tbtn act-addDim" style="background-image: url(img/dim.png);" type="submit" value=""></button><!--
|
||||
--><button class="btn tbtn" style="background-image: url(img/ddim.png);" type="submit" value=""></button><!--
|
||||
-->
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in a new issue