| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /**
- * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
- * Copyright (c) 2015 by Robert S. (Modification to draw arc with specified height).
- * This file is part of the QCAD project.
- *
- * QCAD is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * QCAD is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with QCAD.
- */
- include("scripts/Draw/Arc/Arc.js");
- /**
- * \class Arc2PH
- * \brief Arc from two points and height.
- * \ingroup ecma_draw_arc
- */
- function Arc2PH(guiAction) {
- Arc.call(this, guiAction);
- this.point1 = undefined;
- this.point2 = undefined;
- this.center = undefined;
- this.height = undefined;
- this.radius = undefined;
- this.reversed = false;
- this.setUiOptions("Arc2PH.ui");
- }
- Arc2PH.prototype = new Arc();
- Arc2PH.State = {
- SettingPoint1 : 0,
- SettingPoint2 : 1
- };
- Arc2PH.prototype.beginEvent = function() {
- Arc.prototype.beginEvent.call(this);
- this.setState(Arc2PH.State.SettingPoint1);
- };
- Arc2PH.prototype.setState = function(state) {
- Arc.prototype.setState.call(this, state);
- this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
- this.setCrosshairCursor();
- var appWin = RMainWindowQt.getMainWindow();
- switch (this.state) {
- case Arc2PH.State.SettingPoint1:
- this.point1 = undefined;
- this.point2 = undefined;
- this.center = undefined;
- var trStartPoint = qsTr("Start point");
- this.setCommandPrompt(trStartPoint);
- this.setLeftMouseTip(trStartPoint);
- this.setRightMouseTip(EAction.trCancel);
- break;
- case Arc2PH.State.SettingPoint2:
- this.point2 = undefined;
- this.center = undefined;
- var trEndPoint = qsTr("End point");
- this.setCommandPrompt(trEndPoint);
- this.setLeftMouseTip(trEndPoint);
- this.setRightMouseTip(EAction.trBack);
- break;
- }
- EAction.showSnapTools();
- };
- Arc2PH.prototype.escapeEvent = function() {
- switch (this.state) {
- case Arc2PH.State.SettingPoint1:
- EAction.prototype.escapeEvent.call(this);
- break;
- case Arc2PH.State.SettingPoint2:
- this.setState(Arc2PH.State.SettingPoint1);
- break;
- }
- };
- Arc2PH.prototype.pickCoordinate = function(event, preview) {
- this.error = "";
- var di = this.getDocumentInterface();
- switch (this.state) {
- case Arc2PH.State.SettingPoint1:
- this.point1 = event.getModelPosition();
- if (!preview) {
- di.setRelativeZero(this.point1);
- this.setState(Arc2PH.State.SettingPoint2);
- }
- break;
- case Arc2PH.State.SettingPoint2:
- this.point2 = event.getModelPosition();
- if (preview) {
- this.updatePreview();
- }
- else {
- var op = this.getOperation(false);
- if (!isNull(op)) {
- di.applyOperation(op);
- di.setRelativeZero(this.point2);
- this.setState(Arc2PH.State.SettingPoint1);
- }
- }
- break;
- }
- if (!preview && this.error.length!==0) {
- EAction.handleUserWarning(this.error);
- }
- };
- Arc2PH.prototype.getOperation = function(preview) {
- var shape = this.getArc2PH(preview);
- if (isNull(shape)) {
- return undefined;
- }
- var doc = this.getDocument();
- var entity = new RArcEntity(doc, new RArcData(shape));
- if (!isEntity(entity)) {
- return undefined;
- }
- return new RAddObjectOperation(entity, this.getToolTitle());
- };
- Arc2PH.prototype.getArc2PH = function(preview) {
- if (isNull(this.point1) || isNull(this.point2) || !isNumber(this.height)) {
- return undefined;
- }
- if (this.height <= 0.0 || this.height > 1.0e6) {
- if (!preview) {
- this.error = qsTr("Invalid height");
- }
- return undefined;
- }
- if (this.point1.equalsFuzzy(this.point2)) {
- if (!preview) {
- this.error = qsTr("The two points are identical");
- }
- return undefined;
- }
- var chordlen = this.point1.getDistanceTo(this.point2);
- var archeight = this.height;
- this.radius = (archeight / 2) + ((chordlen * chordlen) / (8 * archeight));
- var circle1 = new RCircle(this.point1, this.radius);
- var circle2 = new RCircle(this.point2, this.radius);
- var ips = circle1.getIntersectionPoints(circle2, false);
- if (ips.length===2) {
- var ipRight, ipLeft;
- var line = new RLine(this.point1, this.point2);
- if (line.getSideOfPoint(ips[0]) === RS.RightHand) {
- ipRight = ips[0];
- ipLeft = ips[1];
- }
- else {
- ipLeft = ips[0];
- ipRight = ips[1];
- }
- if (this.reversed === false) {
- if (this.height > (chordlen / 2)) {
- this.center = ipRight;
- } else {
- this.center = ipLeft;
- }
- } else {
- if (this.height > (chordlen / 2)) {
- this.center = ipLeft;
- } else {
- this.center = ipRight;
- }
- }
- } else {
- // center in the middle:
- var v = new RVector();
- v.setPolar(this.radius, this.point1.getAngleTo(this.point2));
- this.center = this.point1.operator_add(v);
- this.point2 = this.point1.operator_add(v.operator_multiply(2.0));
- }
- var angle1 = this.center.getAngleTo(this.point1);
- var angle2 = this.center.getAngleTo(this.point2);
- return new RArc(this.center, this.radius, angle1, angle2, this.reversed);
- };
- Arc2PH.prototype.slotHeightChanged = function(value) {
- this.height = value;
- this.updatePreview(true);
- };
- Arc2PH.prototype.slotDirectionChanged = function(button) {
- if (!isQObject(button) || button.checked!=true) {
- return;
- }
- if (button.objectName==="Clockwise") {
- this.reversed = true;
- }
- else {
- this.reversed = false;
- }
- this.updatePreview(true);
- };
- Arc2PH.prototype.getAuxPreview = function() {
- if (isNull(this.center) || isNull(this.point1) || isNull(this.point2)) {
- return undefined;
- }
- var ret = [];
- if (this.state==Arc2PH.State.SettingPoint2) {
- ret.push(new RLine(this.center, this.point1));
- ret.push(new RLine(this.center, this.point2));
- }
- return ret;
- };
|