mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-10 02:13:58 +01:00
tools refactoring for dimensions
This commit is contained in:
parent
8945e95fd7
commit
61bf3ec671
3 changed files with 112 additions and 109 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import * as utils from '../../utils/utils'
|
||||
import * as math from '../../math/math'
|
||||
import {SketchObject, EndPoint} from '../viewer2d'
|
||||
import {SketchObject} from '../viewer2d'
|
||||
import Vector from '../../math/vector'
|
||||
|
||||
/** @constructor */
|
||||
|
|
@ -297,106 +297,4 @@ DiameterDimension.prototype.normalDistance = function(aim) {
|
|||
return -1;
|
||||
};
|
||||
|
||||
/** @constructor */
|
||||
function AddDimTool(viewer, layer, dimCreation) {
|
||||
this.viewer = viewer;
|
||||
this.layer = layer;
|
||||
this.dim = null;
|
||||
this._v = new Vector(0, 0, 0);
|
||||
this.dimCreation = dimCreation;
|
||||
}
|
||||
|
||||
AddDimTool.prototype.keydown = function(e) {};
|
||||
AddDimTool.prototype.keypress = function(e) {};
|
||||
AddDimTool.prototype.keyup = function(e) {};
|
||||
AddDimTool.prototype.cleanup = function(e) {};
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
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.viewer.historyManager.checkpoint();
|
||||
this.dim = this.dimCreation(p, new 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();
|
||||
}
|
||||
};
|
||||
|
||||
AddDimTool.prototype.mousedown = function(e) {
|
||||
};
|
||||
|
||||
AddDimTool.prototype.mousewheel = function(e) {
|
||||
};
|
||||
|
||||
/** @constructor */
|
||||
function AddCircleDimTool(viewer, layer) {
|
||||
this.viewer = viewer;
|
||||
this.layer = layer;
|
||||
this.dim = new DiameterDimension(null);
|
||||
this.viewer.add(this.dim, this.layer);
|
||||
}
|
||||
|
||||
AddCircleDimTool.prototype.keydown = function(e) {};
|
||||
AddCircleDimTool.prototype.keypress = function(e) {};
|
||||
AddCircleDimTool.prototype.keyup = function(e) {};
|
||||
AddCircleDimTool.prototype.cleanup = function(e) {};
|
||||
|
||||
AddCircleDimTool.prototype.mousemove = function(e) {
|
||||
var p = this.viewer.screenToModel(e);
|
||||
var objects = this.viewer.search(p.x, p.y, 20 / this.viewer.scale, true, false, []).filter(function(o) {
|
||||
return o._class === 'TCAD.TWO.Circle' || o._class === 'TCAD.TWO.Arc';
|
||||
});
|
||||
|
||||
if (objects.length != 0) {
|
||||
this.dim.obj = objects[0];
|
||||
} else {
|
||||
this.dim.obj = null;
|
||||
}
|
||||
if (this.dim.obj != null) {
|
||||
this.dim.angle = Math.atan2(p.y - this.dim.obj.c.y, p.x - this.dim.obj.c.x);
|
||||
}
|
||||
this.viewer.refresh();
|
||||
};
|
||||
|
||||
AddCircleDimTool.prototype.mouseup = function(e) {
|
||||
if (this.dim.obj !== null) {
|
||||
this.viewer.historyManager.checkpoint();
|
||||
} else {
|
||||
this.viewer.remove(this.dim);
|
||||
}
|
||||
this.viewer.toolManager.releaseControl();
|
||||
};
|
||||
|
||||
AddCircleDimTool.prototype.mousedown = function(e) {
|
||||
};
|
||||
|
||||
AddCircleDimTool.prototype.mousewheel = function(e) {
|
||||
};
|
||||
|
||||
export {AddDimTool, AddCircleDimTool, HDimension, VDimension, Dimension, DiameterDimension}
|
||||
export {HDimension, VDimension, Dimension, DiameterDimension}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import {Viewer} from './viewer2d.js'
|
|||
import * as ui from '../ui/ui'
|
||||
import {Terminal} from '../ui/terminal'
|
||||
import {IO, BBox} from './io'
|
||||
import {AddDimTool, AddCircleDimTool, HDimension, VDimension, Dimension, DiameterDimension} from './shapes/dim'
|
||||
import {AddFreeDimTool, AddHorizontalDimTool, AddVerticalDimTool, AddCircleDimTool} from './tools/dim'
|
||||
import {AddPointTool} from './tools/point'
|
||||
import {AddSegmentTool} from './tools/segment'
|
||||
import {AddArcTool} from './tools/arc'
|
||||
|
|
@ -11,7 +11,6 @@ import {FilletTool} from './helpers'
|
|||
import {ReferencePointTool} from './tools/origin'
|
||||
import {InputManager} from './input-manager'
|
||||
|
||||
/** @constructor */
|
||||
function App2D() {
|
||||
var app = this;
|
||||
|
||||
|
|
@ -144,15 +143,15 @@ function App2D() {
|
|||
});
|
||||
|
||||
this.registerAction('addDim', "Add Dimension", function () {
|
||||
app.viewer.toolManager.takeControl(new AddDimTool(app.viewer, app.viewer.dimLayer, function(a,b) {return new Dimension(a,b)} ));
|
||||
app.viewer.toolManager.takeControl(new AddFreeDimTool(app.viewer, app.viewer.dimLayer));
|
||||
});
|
||||
|
||||
this.registerAction('addHDim', "Add Horizontal Dimension", function () {
|
||||
app.viewer.toolManager.takeControl(new AddDimTool(app.viewer, app.viewer.dimLayer, function(a,b) {return new HDimension(a,b)} ));
|
||||
app.viewer.toolManager.takeControl(new AddHorizontalDimTool(app.viewer, app.viewer.dimLayer));
|
||||
});
|
||||
|
||||
this.registerAction('addVDim', "Add Vertical Dimension", function () {
|
||||
app.viewer.toolManager.takeControl(new AddDimTool(app.viewer, app.viewer.dimLayer, function(a,b) {return new VDimension(a,b)} ));
|
||||
app.viewer.toolManager.takeControl(new AddVerticalDimTool(app.viewer, app.viewer.dimLayer));
|
||||
});
|
||||
|
||||
this.registerAction('addCircleDim', "Add Circle Dimension", function () {
|
||||
|
|
|
|||
106
web/app/sketcher/tools/dim.js
Normal file
106
web/app/sketcher/tools/dim.js
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
import {HDimension, VDimension, Dimension, DiameterDimension} from '../shapes/dim'
|
||||
import Vector from '../../math/vector'
|
||||
import {EndPoint} from '../viewer2d'
|
||||
import {Tool} from './tool'
|
||||
|
||||
export class AddDimTool extends Tool {
|
||||
|
||||
constructor(name, viewer, layer, dimCreation) {
|
||||
super(name, viewer);
|
||||
this.layer = layer;
|
||||
this.dim = null;
|
||||
this._v = new Vector(0, 0, 0);
|
||||
this.dimCreation = dimCreation;
|
||||
}
|
||||
|
||||
mousemove(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();
|
||||
}
|
||||
|
||||
mouseup(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.viewer.historyManager.checkpoint();
|
||||
this.dim = this.dimCreation(p, new 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class AddFreeDimTool extends AddDimTool {
|
||||
constructor(viewer, layer) {
|
||||
super('free dimension', viewer, layer, (a, b) => new Dimension(a, b));
|
||||
}
|
||||
}
|
||||
|
||||
export class AddHorizontalDimTool extends AddDimTool {
|
||||
constructor(viewer, layer) {
|
||||
super('horizontal dimension', viewer, layer, (a, b) => new HDimension(a, b));
|
||||
}
|
||||
}
|
||||
|
||||
export class AddVerticalDimTool extends AddDimTool {
|
||||
constructor(viewer, layer) {
|
||||
super('vertical dimension', viewer, layer, (a, b) => new VDimension(a, b));
|
||||
}
|
||||
}
|
||||
|
||||
export class AddCircleDimTool extends Tool {
|
||||
constructor(viewer, layer) {
|
||||
super(viewer);
|
||||
this.layer = layer;
|
||||
this.dim = new DiameterDimension(null);
|
||||
this.viewer.add(this.dim, this.layer);
|
||||
}
|
||||
|
||||
mousemove(e) {
|
||||
var p = this.viewer.screenToModel(e);
|
||||
var objects = this.viewer.search(p.x, p.y, 20 / this.viewer.scale, true, false, []).filter(function (o) {
|
||||
return o._class === 'TCAD.TWO.Circle' || o._class === 'TCAD.TWO.Arc';
|
||||
});
|
||||
|
||||
if (objects.length != 0) {
|
||||
this.dim.obj = objects[0];
|
||||
} else {
|
||||
this.dim.obj = null;
|
||||
}
|
||||
if (this.dim.obj != null) {
|
||||
this.dim.angle = Math.atan2(p.y - this.dim.obj.c.y, p.x - this.dim.obj.c.x);
|
||||
}
|
||||
this.viewer.refresh();
|
||||
}
|
||||
|
||||
mouseup(e) {
|
||||
if (this.dim.obj !== null) {
|
||||
this.viewer.historyManager.checkpoint();
|
||||
} else {
|
||||
this.viewer.remove(this.dim);
|
||||
}
|
||||
this.viewer.toolManager.releaseControl();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue