LineOrthogonalTangent.js 7.1 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 LineOrthogonalTangent
  23. * \brief Line orthogonal to line and tangential to arc, circle or ellipse.
  24. * \ingroup ecma_draw_line
  25. */
  26. function LineOrthogonalTangent(guiAction) {
  27. Line.call(this, guiAction);
  28. // first entity (line):
  29. this.entity1 = undefined;
  30. // first shape:
  31. this.shape1 = undefined;
  32. // click position when choosing first entity:
  33. this.pos1 = undefined;
  34. // second entity (circle, arc, ellipse):
  35. this.entity2 = undefined;
  36. // second shape:
  37. this.shape2 = undefined;
  38. // click position when choosing second entity:
  39. this.pos2 = undefined;
  40. this.setUiOptions("../Line.ui");
  41. }
  42. LineOrthogonalTangent.prototype = new Line();
  43. LineOrthogonalTangent.State = {
  44. ChoosingEntity1 : 0,
  45. ChoosingEntity2 : 1
  46. };
  47. LineOrthogonalTangent.prototype.beginEvent = function() {
  48. Line.prototype.beginEvent.call(this);
  49. this.setState(LineOrthogonalTangent.State.ChoosingEntity1);
  50. };
  51. LineOrthogonalTangent.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 LineOrthogonalTangent.State.ChoosingEntity1:
  59. tr = qsTr("Choose line");
  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 LineOrthogonalTangent.State.ChoosingEntity2:
  71. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  72. tr = qsTr("Choose circle, arc or ellipse");
  73. this.setLeftMouseTip(tr);
  74. this.setCommandPrompt(tr);
  75. this.setRightMouseTip(EAction.trBack);
  76. break;
  77. }
  78. };
  79. LineOrthogonalTangent.prototype.escapeEvent = function() {
  80. switch (this.state) {
  81. case LineOrthogonalTangent.State.ChoosingEntity1:
  82. EAction.prototype.escapeEvent.call(this);
  83. break;
  84. case LineOrthogonalTangent.State.ChoosingEntity2:
  85. this.setState(LineOrthogonalTangent.State.ChoosingEntity1);
  86. break;
  87. }
  88. };
  89. LineOrthogonalTangent.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. this.updatePreview();
  98. return;
  99. }
  100. var shape = entity.getClosestSimpleShape(pos);
  101. switch (this.state) {
  102. case LineOrthogonalTangent.State.ChoosingEntity1:
  103. if (isLineBasedShape(shape)) {
  104. this.entity1 = entity;
  105. this.shape1 = shape;
  106. this.pos1 = pos;
  107. if (preview) {
  108. this.updatePreview();
  109. }
  110. else {
  111. this.setState(LineOrthogonalTangent.State.ChoosingEntity2);
  112. }
  113. }
  114. else {
  115. this.entity1 = undefined;
  116. this.shape1 = undefined;
  117. this.pos1 = undefined;
  118. if (!preview) {
  119. EAction.warnNotLine();
  120. }
  121. }
  122. break;
  123. case LineOrthogonalTangent.State.ChoosingEntity2:
  124. if (isArcShape(shape) ||
  125. isCircleShape(shape) ||
  126. isEllipseShape(shape)) {
  127. this.entity2 = entity;
  128. this.shape2 = shape;
  129. this.pos2 = pos;
  130. if (preview) {
  131. this.updatePreview();
  132. }
  133. else {
  134. var op = this.getOperation(false);
  135. if (!isNull(op)) {
  136. di.applyOperation(op);
  137. if (this.error.length===0) {
  138. this.setState(LineOrthogonalTangent.State.ChoosingEntity1);
  139. }
  140. }
  141. }
  142. }
  143. else {
  144. this.entity2 = undefined;
  145. this.shape2 = undefined;
  146. this.pos2 = undefined;
  147. if (preview) {
  148. this.updatePreview();
  149. }
  150. else {
  151. EAction.warnNotArcCircleEllipse();
  152. }
  153. }
  154. break;
  155. }
  156. if (!preview && this.error.length!==0) {
  157. EAction.handleUserWarning(this.error);
  158. }
  159. };
  160. LineOrthogonalTangent.prototype.getHighlightedEntities = function() {
  161. var ret = [];
  162. if (isEntity(this.entity1)) {
  163. ret.push(this.entity1.getId());
  164. }
  165. if (isEntity(this.entity2)) {
  166. ret.push(this.entity2.getId());
  167. }
  168. return ret;
  169. };
  170. LineOrthogonalTangent.prototype.getAuxPreview = function() {
  171. var ret = [];
  172. if (isArcShape(this.shape2)) {
  173. ret.push(new RCircle(this.shape2.getCenter(), this.shape2.getRadius()));
  174. }
  175. else if (isEllipseShape(this.shape2) && !isFullEllipseShape(this.shape2)) {
  176. var ellipse = this.shape2.clone();
  177. ellipse.setStartParam(0.0);
  178. ellipse.setEndParam(2*Math.PI);
  179. ret.push(ellipse);
  180. }
  181. return ret;
  182. };
  183. LineOrthogonalTangent.prototype.getOperation = function(preview) {
  184. if (isNull(this.pos1) || isNull(this.pos2) ||
  185. isNull(this.entity1) || isNull(this.entity2) ||
  186. isNull(this.shape1) || isNull(this.shape2)) {
  187. return undefined;
  188. }
  189. var doc = this.getDocument();
  190. var tangent = this.getTangent();
  191. if (isNull(tangent)) {
  192. if (!preview) {
  193. this.error = qsTr("No tangent possible");
  194. }
  195. return undefined;
  196. }
  197. var e = this.createLineEntity(doc, tangent.getEndPoint(), tangent.getStartPoint());
  198. if (isNull(e)) {
  199. return undefined;
  200. }
  201. return new RAddObjectOperation(e, this.getToolTitle());
  202. };
  203. LineOrthogonalTangent.prototype.getTangent = function() {
  204. var ret;
  205. var tangents = ShapeAlgorithms.getOrthogonalTangents(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. };