LineTangent2.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. include("scripts/ShapeAlgorithms.js");
  21. /**
  22. * \class LineTangent2
  23. * \brief Line tangential to two arcs or circles.
  24. * \ingroup ecma_draw_line
  25. */
  26. function LineTangent2(guiAction) {
  27. Line.call(this, guiAction);
  28. // first entity (circle, arc, polyline, ...):
  29. this.entity1 = undefined;
  30. // shape within chosen polyline or shape of entity self:
  31. this.shape1 = undefined;
  32. // click position when choosing first entity:
  33. this.pos1 = undefined;
  34. // second entity (circle, arc, polyline, ...):
  35. this.entity2 = undefined;
  36. // shape within chosen polyline or shape of entity self:
  37. this.shape2 = undefined;
  38. // click position when choosing second entity:
  39. this.pos2 = undefined;
  40. this.setUiOptions("../Line.ui");
  41. }
  42. LineTangent2.prototype = new Line();
  43. LineTangent2.State = {
  44. ChoosingEntity1 : 0,
  45. ChoosingEntity2 : 1
  46. };
  47. LineTangent2.prototype.beginEvent = function() {
  48. Line.prototype.beginEvent.call(this);
  49. this.setState(LineTangent2.State.ChoosingEntity1);
  50. };
  51. LineTangent2.prototype.setState = function(state) {
  52. Line.prototype.setState.call(this, state);
  53. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  54. this.setCrosshairCursor();
  55. var tr;
  56. var appWin = RMainWindowQt.getMainWindow();
  57. switch (this.state) {
  58. case LineTangent2.State.ChoosingEntity1:
  59. tr = qsTr("Choose first circle or arc");
  60. this.setLeftMouseTip(tr);
  61. this.setCommandPrompt(tr);
  62. this.setRightMouseTip(EAction.trCancel);
  63. this.entity1 = undefined;
  64. this.shape1 = undefined;
  65. this.pos1 = undefined;
  66. this.entity2 = undefined;
  67. this.shape2 = undefined;
  68. this.pos2 = undefined;
  69. break;
  70. case LineTangent2.State.ChoosingEntity2:
  71. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  72. tr = qsTr("Choose second circle or arc");
  73. this.setLeftMouseTip(tr);
  74. this.setCommandPrompt(tr);
  75. this.setRightMouseTip(EAction.trBack);
  76. break;
  77. }
  78. };
  79. LineTangent2.prototype.escapeEvent = function() {
  80. switch (this.state) {
  81. case LineTangent2.State.ChoosingEntity1:
  82. EAction.prototype.escapeEvent.call(this);
  83. break;
  84. case LineTangent2.State.ChoosingEntity2:
  85. this.setState(LineTangent2.State.ChoosingEntity1);
  86. break;
  87. }
  88. };
  89. LineTangent2.prototype.pickEntity = function(event, preview) {
  90. this.error = "";
  91. var di = this.getDocumentInterface();
  92. var doc = this.getDocument();
  93. var entityId = this.getEntityId(event, preview);
  94. var entity = doc.queryEntity(entityId);
  95. var pos = event.getModelPosition();
  96. if (isNull(entity)) {
  97. return;
  98. }
  99. var shape = entity.getClosestSimpleShape(pos);
  100. if (isArcShape(shape) ||
  101. isCircleShape(shape)) {
  102. if (this.state===LineTangent2.State.ChoosingEntity1) {
  103. this.entity1 = entity;
  104. this.shape1 = shape;
  105. this.pos1 = pos;
  106. if (!preview) {
  107. this.setState(LineTangent2.State.ChoosingEntity2);
  108. return;
  109. }
  110. }
  111. else {
  112. this.entity2 = entity;
  113. this.shape2 = shape;
  114. this.pos2 = pos;
  115. }
  116. }
  117. else {
  118. // handle error:
  119. if (!preview) {
  120. EAction.warnNotArcCircle();
  121. }
  122. if (this.state===LineTangent2.State.ChoosingEntity1) {
  123. this.entity1 = undefined;
  124. this.shape1 = undefined;
  125. this.pos1 = undefined;
  126. }
  127. else {
  128. this.entity2 = undefined;
  129. this.shape2 = undefined;
  130. this.pos2 = undefined;
  131. }
  132. }
  133. if (preview) {
  134. this.updatePreview();
  135. }
  136. else {
  137. if (this.state===LineTangent2.State.ChoosingEntity2) {
  138. var op = this.getOperation(preview);
  139. if (!isNull(op)) {
  140. di.applyOperation(op);
  141. if (this.error.length===0) {
  142. this.setState(LineTangent2.State.ChoosingEntity1);
  143. }
  144. }
  145. }
  146. }
  147. if (!preview && this.error.length!==0) {
  148. EAction.handleUserWarning(this.error);
  149. }
  150. };
  151. LineTangent2.prototype.getHighlightedEntities = function() {
  152. var ret = [];
  153. if (isEntity(this.entity1)) {
  154. ret.push(this.entity1.getId());
  155. }
  156. if (isEntity(this.entity2)) {
  157. ret.push(this.entity2.getId());
  158. }
  159. return ret;
  160. };
  161. LineTangent2.prototype.getAuxPreview = function() {
  162. var ellipse;
  163. var ret = [];
  164. if (isArcShape(this.shape1)) {
  165. ret.push(new RCircle(this.shape1.getCenter(), this.shape1.getRadius()));
  166. }
  167. else if (isEllipseShape(this.shape1)) {
  168. ellipse = this.shape1.clone();
  169. ellipse.setStartParam(0.0);
  170. ellipse.setEndParam(2*Math.PI);
  171. ret.push(ellipse);
  172. }
  173. if (isArcShape(this.shape2)) {
  174. ret.push(new RCircle(this.shape2.getCenter(), this.shape2.getRadius()));
  175. }
  176. else if (isEllipseShape(this.shape2)) {
  177. ellipse = this.shape2.clone();
  178. ellipse.setStartParam(0.0);
  179. ellipse.setEndParam(2*Math.PI);
  180. ret.push(ellipse);
  181. }
  182. return ret;
  183. };
  184. LineTangent2.prototype.getOperation = function(preview) {
  185. if (isNull(this.pos1) || isNull(this.pos2) ||
  186. isNull(this.entity1) || isNull(this.entity2) ||
  187. isNull(this.shape1) || isNull(this.shape2)) {
  188. return undefined;
  189. }
  190. var doc = this.getDocument();
  191. var tangent = this.getTangent();
  192. if (isNull(tangent)) {
  193. if (!preview) {
  194. this.error = qsTr("No tangent possible");
  195. }
  196. return undefined;
  197. }
  198. var op = new RAddObjectsOperation();
  199. op.setText(this.getToolTitle());
  200. op.addObject(this.createLineEntity(doc, tangent.getStartPoint(), tangent.getEndPoint()));
  201. return op;
  202. };
  203. LineTangent2.prototype.getTangent = function() {
  204. var ret;
  205. var tangents = ShapeAlgorithms.getTangents(this.shape1, this.shape2);
  206. // find closest tangent:
  207. var minDist;
  208. var dist;
  209. var idx = -1;
  210. for (var i=0; i<tangents.length; ++i) {
  211. if (isNull(tangents[i])) {
  212. continue;
  213. }
  214. dist = tangents[i].getDistanceTo(this.pos2, false);
  215. if (isNull(minDist) || dist<minDist) {
  216. minDist = dist;
  217. ret = tangents[i];
  218. }
  219. }
  220. return ret;
  221. };