mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-24 17:33:27 +01:00
fix snapping for ellipse / alternate default tool for a shape
This commit is contained in:
parent
3cfff1b863
commit
939b93410e
7 changed files with 49 additions and 24 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import {Ref} from './ref'
|
||||
import {SketchObject} from './sketch-object'
|
||||
import {EllipseTool, STATE_RADIUS} from '../tools/ellipse'
|
||||
|
||||
import * as math from '../../math/math';
|
||||
|
||||
|
|
@ -61,6 +62,17 @@ export class Ellipse extends SketchObject {
|
|||
static findMinorRadius(majorRadius, pntRadius, pntAngle) {
|
||||
return Math.abs( Math.sin(pntAngle) / Math.sqrt(1 / sq(pntRadius) - sq(Math.cos(pntAngle) / majorRadius)) );
|
||||
}
|
||||
|
||||
getDefaultTool(viewer, alternative) {
|
||||
if (alternative) {
|
||||
return super.getDefaultTool(viewer, alternative);
|
||||
} else {
|
||||
const editTool = new EllipseTool(viewer);
|
||||
editTool.ellipse = this;
|
||||
editTool.state = STATE_RADIUS;
|
||||
return editTool;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ellipse.prototype._class = 'TCAD.TWO.Ellipse';
|
||||
|
||||
|
|
|
|||
|
|
@ -73,6 +73,15 @@ export class SketchObject {
|
|||
}
|
||||
this._translate(dx, dy, {});
|
||||
}
|
||||
|
||||
translateImpl(dx, dy) {
|
||||
this.accept(function (obj) {
|
||||
if (obj._class === 'TCAD.TWO.EndPoint') {
|
||||
obj.translate(dx, dy);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
draw(ctx, scale, viewer) {
|
||||
if (!this.visible) return;
|
||||
|
|
|
|||
|
|
@ -84,15 +84,6 @@ export class AddArcTool extends Tool {
|
|||
this.viewer.toolManager.releaseControl();
|
||||
}
|
||||
|
||||
snapIfNeed(p) {
|
||||
if (this.viewer.snapped != null) {
|
||||
var snapWith = this.viewer.snapped;
|
||||
this.viewer.cleanSnap();
|
||||
this.viewer.parametricManager.linkObjects([p, snapWith]);
|
||||
this.viewer.parametricManager.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
demoSecondPoint() {
|
||||
const r = this.radiusOfFirstPoint();
|
||||
let ang = Math.atan2(this.arc.a.y - this.arc.c.y, this.arc.a.x - this.arc.c.x) + (2 * Math.PI - 0.3);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import {Tool} from './tool'
|
|||
import {optim} from '../../math/optim'
|
||||
import * as math from '../../math/math'
|
||||
|
||||
export class DragTool extends Tool{
|
||||
export class DragTool extends Tool {
|
||||
|
||||
constructor(obj, viewer) {
|
||||
super('drag', viewer);
|
||||
|
|
@ -23,7 +23,7 @@ export class DragTool extends Tool{
|
|||
this.lockedValues[i + 1] = this._point.y - this.lockedShifts[i + 1];
|
||||
}
|
||||
this.solver.updateLock(this.lockedValues);
|
||||
if (!e.altKey && !e.ctrlKey) {
|
||||
if (!e.ctrlKey) {
|
||||
this.solveRequest(true);
|
||||
} else {
|
||||
this.obj.translate(dx, dy);
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ import {EndPoint} from '../shapes/point'
|
|||
import {Ellipse} from '../shapes/ellipse'
|
||||
import Vector from '../../math/vector'
|
||||
|
||||
const STATE_POINT1 = 0;
|
||||
const STATE_POINT2 = 1;
|
||||
const RADIUS = 2;
|
||||
export const STATE_POINT1 = 0;
|
||||
export const STATE_POINT2 = 1;
|
||||
export const STATE_RADIUS = 2;
|
||||
|
||||
export class EllipseTool extends Tool {
|
||||
|
||||
|
||||
constructor(viewer) {
|
||||
super('ellipse', viewer);
|
||||
this.ellipse = null;
|
||||
|
|
@ -21,6 +21,10 @@ export class EllipseTool extends Tool {
|
|||
this.sendHint('specify first major axis point')
|
||||
}
|
||||
|
||||
cleanup(e) {
|
||||
this.viewer.cleanSnap();
|
||||
}
|
||||
|
||||
point(e) {
|
||||
return this.viewer.snapped ? this.viewer.snapped : this.viewer.screenToModel(e);
|
||||
}
|
||||
|
|
@ -30,21 +34,23 @@ export class EllipseTool extends Tool {
|
|||
case STATE_POINT1: {
|
||||
const p = this.point(e);
|
||||
this.ellipse = new Ellipse(new EndPoint(p.x, p.y), new EndPoint(p.x, p.y));
|
||||
this.snapIfNeed(this.ellipse.ep1);
|
||||
this.viewer.activeLayer.objects.push(this.ellipse);
|
||||
this.viewer.refresh();
|
||||
this.state = STATE_POINT2;
|
||||
this.sendHint('specify second major axis point')
|
||||
this.sendHint('specify second major axis point');
|
||||
break;
|
||||
}
|
||||
case STATE_POINT2: {
|
||||
const p = this.point(e);
|
||||
this.ellipse.ep2.setFromPoint(p);
|
||||
this.snapIfNeed(this.ellipse.ep2);
|
||||
this.viewer.refresh();
|
||||
this.state = RADIUS;
|
||||
this.sendHint('specify minor axis radius')
|
||||
this.state = STATE_RADIUS;
|
||||
this.sendHint('specify minor axis radius');
|
||||
break;
|
||||
}
|
||||
case RADIUS:
|
||||
case STATE_RADIUS:
|
||||
this.viewer.toolManager.releaseControl();
|
||||
}
|
||||
}
|
||||
|
|
@ -58,9 +64,9 @@ export class EllipseTool extends Tool {
|
|||
case STATE_POINT2:
|
||||
this.ellipse.ep2.setFromPoint(this.viewer.screenToModel(e));
|
||||
this.ellipse.r.value = this.ellipse.radiusX * 0.5;
|
||||
this.viewer.snap(p.x, p.y, [this.ellipse.ep1]);
|
||||
this.viewer.snap(p.x, p.y, [this.ellipse.ep1, this.ellipse.ep2]);
|
||||
break;
|
||||
case RADIUS:
|
||||
case STATE_RADIUS:
|
||||
const polarPoint = this.ellipse.toEllipseCoordinateSystem(p);
|
||||
let minorRadius = Ellipse.findMinorRadius(this.ellipse.radiusX, polarPoint.radius, polarPoint.angle);
|
||||
if (isNaN(minorRadius)) {
|
||||
|
|
@ -74,6 +80,4 @@ export class EllipseTool extends Tool {
|
|||
}
|
||||
this.viewer.refresh();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -57,7 +57,7 @@ export class PanTool extends Tool {
|
|||
}
|
||||
this.viewer.select([toSelect], true);
|
||||
if (!toSelect.isAuxOrLinkedTo()) {
|
||||
var tool = toSelect.getDefaultTool(this.viewer);
|
||||
var tool = toSelect.getDefaultTool(this.viewer, e.altKey);
|
||||
tool.mousedown(e);
|
||||
this.viewer.toolManager.switchTool(tool);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,15 @@ export class Tool {
|
|||
this.viewer.referencePoint.x = x;
|
||||
this.viewer.referencePoint.y = y;
|
||||
};
|
||||
|
||||
snapIfNeed(p) {
|
||||
if (this.viewer.snapped != null) {
|
||||
var snapWith = this.viewer.snapped;
|
||||
this.viewer.cleanSnap();
|
||||
this.viewer.parametricManager.linkObjects([p, snapWith]);
|
||||
this.viewer.parametricManager.refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Tool.ParseNumber = function(str) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue