LineBisector.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/Draw/Line/Line.js");
  20. /**
  21. * \class LineBisector
  22. * \brief Line as bisector of two other lines.
  23. * \ingroup ecma_draw_line
  24. */
  25. function LineBisector(guiAction) {
  26. Line.call(this, guiAction);
  27. this.len = undefined;
  28. this.number = undefined;
  29. this.line1 = undefined;
  30. this.pos1 = undefined;
  31. this.line2 = undefined;
  32. this.pos2 = undefined;
  33. this.setUiOptions(["../Line.ui", "LineBisector.ui"]);
  34. }
  35. LineBisector.prototype = new Line();
  36. LineBisector.State = {
  37. ChoosingLine1 : 0,
  38. ChoosingLine2 : 1
  39. };
  40. LineBisector.prototype.beginEvent = function() {
  41. Line.prototype.beginEvent.call(this);
  42. this.setState(LineBisector.State.ChoosingLine1);
  43. };
  44. LineBisector.prototype.setState = function(state) {
  45. Line.prototype.setState.call(this, state);
  46. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  47. this.setCrosshairCursor();
  48. var tr;
  49. var appWin = RMainWindowQt.getMainWindow();
  50. switch (this.state) {
  51. case LineBisector.State.ChoosingLine1:
  52. this.line1 = undefined;
  53. this.pos1 = undefined;
  54. this.line2 = undefined;
  55. this.pos2 = undefined;
  56. tr = qsTr("First line");
  57. this.setLeftMouseTip(tr);
  58. this.setCommandPrompt(tr);
  59. break;
  60. case LineBisector.State.ChoosingLine2:
  61. tr = qsTr("Second line");
  62. this.setLeftMouseTip(tr);
  63. this.setCommandPrompt(tr);
  64. break;
  65. }
  66. this.setRightMouseTip(EAction.trCancel);
  67. };
  68. LineBisector.prototype.escapeEvent = function() {
  69. switch (this.state) {
  70. case LineBisector.State.ChoosingLine1:
  71. EAction.prototype.escapeEvent.call(this);
  72. break;
  73. case LineBisector.State.ChoosingLine2:
  74. this.setState(LineBisector.State.ChoosingLine1);
  75. break;
  76. }
  77. };
  78. LineBisector.prototype.finishEvent = function() {
  79. EAction.prototype.finishEvent.call(this);
  80. };
  81. LineBisector.prototype.pickEntity = function(event, preview) {
  82. var di = this.getDocumentInterface();
  83. var doc = this.getDocument();
  84. var pos = event.getModelPosition();
  85. var entityId = this.getEntityId(event, preview);
  86. var entity = doc.queryEntity(entityId);
  87. if (isNull(entity)) {
  88. return;
  89. }
  90. var shape = entity.getClosestSimpleShape(pos);
  91. if (!isLineBasedShape(shape)) {
  92. if (!preview) {
  93. EAction.warnNotLine();
  94. }
  95. return;
  96. }
  97. if (preview) {
  98. di.highlightEntity(entityId);
  99. }
  100. switch (this.state) {
  101. case LineBisector.State.ChoosingLine1:
  102. this.line1 = shape;
  103. this.pos1 = pos;
  104. if (!preview) {
  105. this.setState(LineBisector.State.ChoosingLine2);
  106. }
  107. break;
  108. case LineBisector.State.ChoosingLine2:
  109. this.line2 = shape;
  110. this.pos2 = pos;
  111. if (preview) {
  112. //di.previewOperation(op);
  113. this.updatePreview();
  114. }
  115. else {
  116. var op = this.getOperation(false);
  117. if (!isNull(op)) {
  118. di.applyOperation(op);
  119. }
  120. else {
  121. EAction.handleUserWarning(qsTr("Lines don't intersect."));
  122. }
  123. this.setState(LineBisector.State.ChoosingLine1);
  124. }
  125. break;
  126. }
  127. };
  128. LineBisector.prototype.getOperation = function(preview) {
  129. if (isNull(this.pos1) || isNull(this.pos2) ||
  130. isNull(this.line1) || isNull(this.line2) ||
  131. !isNumber(this.len) || !isNumber(this.number)) {
  132. return undefined;
  133. }
  134. // get intersection of the two chosen lines:
  135. var d;
  136. if (isFunction(this.line2.data)) {
  137. d = this.line2.data();
  138. }
  139. else {
  140. d = this.line2;
  141. }
  142. var ips = this.line1.getIntersectionPoints(d, false);
  143. if (ips.length===0) {
  144. return undefined;
  145. }
  146. var doc = this.getDocument();
  147. var ip = ips[0];
  148. var angle1 = ip.getAngleTo(this.line1.getClosestPointOnShape(this.pos1));
  149. var angle2 = ip.getAngleTo(this.line2.getClosestPointOnShape(this.pos2));
  150. var angleDiff = RMath.getAngleDifference(angle1, angle2);
  151. if (angleDiff>Math.PI) {
  152. angleDiff = angleDiff - 2*Math.PI;
  153. }
  154. var op = new RAddObjectsOperation();
  155. op.setText(this.getToolTitle());
  156. for (var n=1; n<=this.number; ++n) {
  157. var angle = angle1 + (angleDiff / (this.number+1) * n);
  158. // create vector from intersection to bisected angle / length:
  159. var vector = new RVector();
  160. vector.setPolar(this.len, angle);
  161. var line = this.createLineEntity(doc, ip, ip.operator_add(vector));
  162. op.addObject(line);
  163. }
  164. return op;
  165. };
  166. LineBisector.prototype.update = function() {
  167. var di = this.getDocumentInterface();
  168. di.clearPreview();
  169. var op = this.getOperation(false);
  170. if (!isNull(op)) {
  171. di.previewOperation(op);
  172. }
  173. di.repaintViews();
  174. };
  175. LineBisector.prototype.slotLengthChanged = function(value) {
  176. this.len = value;
  177. this.update();
  178. };
  179. LineBisector.prototype.slotNumberChanged = function(value) {
  180. this.number = value;
  181. this.update();
  182. };
  183. LineBisector.prototype.typeChanged = function() {
  184. var optionsToolBar = EAction.getOptionsToolBar();
  185. var ws = ["LengthLabel", "Length"];
  186. for (var i=0; i<ws.length; i++) {
  187. var w = optionsToolBar.findChild(ws[i]);
  188. if (!isNull(w)) {
  189. w.enabled = !this.isRayOrXLine();
  190. }
  191. }
  192. };