| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /**
- * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
- *
- * 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/Line/Line.js");
- /**
- * \class LineBisector
- * \brief Line as bisector of two other lines.
- * \ingroup ecma_draw_line
- */
- function LineBisector(guiAction) {
- Line.call(this, guiAction);
- this.len = undefined;
- this.number = undefined;
- this.line1 = undefined;
- this.pos1 = undefined;
- this.line2 = undefined;
- this.pos2 = undefined;
- this.setUiOptions(["../Line.ui", "LineBisector.ui"]);
- }
- LineBisector.prototype = new Line();
- LineBisector.State = {
- ChoosingLine1 : 0,
- ChoosingLine2 : 1
- };
- LineBisector.prototype.beginEvent = function() {
- Line.prototype.beginEvent.call(this);
- this.setState(LineBisector.State.ChoosingLine1);
- };
- LineBisector.prototype.setState = function(state) {
- Line.prototype.setState.call(this, state);
- this.getDocumentInterface().setClickMode(RAction.PickEntity);
- this.setCrosshairCursor();
- var tr;
- var appWin = RMainWindowQt.getMainWindow();
- switch (this.state) {
- case LineBisector.State.ChoosingLine1:
- this.line1 = undefined;
- this.pos1 = undefined;
- this.line2 = undefined;
- this.pos2 = undefined;
- tr = qsTr("First line");
- this.setLeftMouseTip(tr);
- this.setCommandPrompt(tr);
- break;
- case LineBisector.State.ChoosingLine2:
- tr = qsTr("Second line");
- this.setLeftMouseTip(tr);
- this.setCommandPrompt(tr);
- break;
- }
- this.setRightMouseTip(EAction.trCancel);
- };
- LineBisector.prototype.escapeEvent = function() {
- switch (this.state) {
- case LineBisector.State.ChoosingLine1:
- EAction.prototype.escapeEvent.call(this);
- break;
- case LineBisector.State.ChoosingLine2:
- this.setState(LineBisector.State.ChoosingLine1);
- break;
- }
- };
- LineBisector.prototype.finishEvent = function() {
- EAction.prototype.finishEvent.call(this);
- };
- LineBisector.prototype.pickEntity = function(event, preview) {
- var di = this.getDocumentInterface();
- var doc = this.getDocument();
- var pos = event.getModelPosition();
- var entityId = this.getEntityId(event, preview);
- var entity = doc.queryEntity(entityId);
- if (isNull(entity)) {
- return;
- }
- var shape = entity.getClosestSimpleShape(pos);
- if (!isLineBasedShape(shape)) {
- if (!preview) {
- EAction.warnNotLine();
- }
- return;
- }
- if (preview) {
- di.highlightEntity(entityId);
- }
- switch (this.state) {
- case LineBisector.State.ChoosingLine1:
- this.line1 = shape;
- this.pos1 = pos;
- if (!preview) {
- this.setState(LineBisector.State.ChoosingLine2);
- }
- break;
- case LineBisector.State.ChoosingLine2:
- this.line2 = shape;
- this.pos2 = pos;
- if (preview) {
- //di.previewOperation(op);
- this.updatePreview();
- }
- else {
- var op = this.getOperation(false);
- if (!isNull(op)) {
- di.applyOperation(op);
- }
- else {
- EAction.handleUserWarning(qsTr("Lines don't intersect."));
- }
- this.setState(LineBisector.State.ChoosingLine1);
- }
- break;
- }
- };
- LineBisector.prototype.getOperation = function(preview) {
- if (isNull(this.pos1) || isNull(this.pos2) ||
- isNull(this.line1) || isNull(this.line2) ||
- !isNumber(this.len) || !isNumber(this.number)) {
- return undefined;
- }
- // get intersection of the two chosen lines:
- var d;
- if (isFunction(this.line2.data)) {
- d = this.line2.data();
- }
- else {
- d = this.line2;
- }
- var ips = this.line1.getIntersectionPoints(d, false);
- if (ips.length===0) {
- return undefined;
- }
- var doc = this.getDocument();
- var ip = ips[0];
- var angle1 = ip.getAngleTo(this.line1.getClosestPointOnShape(this.pos1));
- var angle2 = ip.getAngleTo(this.line2.getClosestPointOnShape(this.pos2));
- var angleDiff = RMath.getAngleDifference(angle1, angle2);
- if (angleDiff>Math.PI) {
- angleDiff = angleDiff - 2*Math.PI;
- }
- var op = new RAddObjectsOperation();
- op.setText(this.getToolTitle());
- for (var n=1; n<=this.number; ++n) {
- var angle = angle1 + (angleDiff / (this.number+1) * n);
- // create vector from intersection to bisected angle / length:
- var vector = new RVector();
- vector.setPolar(this.len, angle);
- var line = this.createLineEntity(doc, ip, ip.operator_add(vector));
- op.addObject(line);
- }
- return op;
- };
- LineBisector.prototype.update = function() {
- var di = this.getDocumentInterface();
- di.clearPreview();
- var op = this.getOperation(false);
- if (!isNull(op)) {
- di.previewOperation(op);
- }
- di.repaintViews();
- };
- LineBisector.prototype.slotLengthChanged = function(value) {
- this.len = value;
- this.update();
- };
- LineBisector.prototype.slotNumberChanged = function(value) {
- this.number = value;
- this.update();
- };
- LineBisector.prototype.typeChanged = function() {
- var optionsToolBar = EAction.getOptionsToolBar();
- var ws = ["LengthLabel", "Length"];
- for (var i=0; i<ws.length; i++) {
- var w = optionsToolBar.findChild(ws[i]);
- if (!isNull(w)) {
- w.enabled = !this.isRayOrXLine();
- }
- }
- };
|