LineTangent1.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 LineTangent1
  22. * \brief Line tangential to arc, circle or ellipse.
  23. * \ingroup ecma_draw_line
  24. */
  25. function LineTangent1(guiAction) {
  26. Line.call(this, guiAction);
  27. // chosen arc, circle, ellipse, polyline, ... entity to draw the tangent to:
  28. this.entity = undefined;
  29. // shape within chosen polyline or shape of entity self:
  30. this.shape = undefined;
  31. // start point of line:
  32. this.pos1 = undefined;
  33. // click point when choosing arc, circle or ellipse:
  34. this.pos2 = undefined;
  35. this.setUiOptions("../Line.ui");
  36. }
  37. LineTangent1.prototype = new Line();
  38. LineTangent1.State = {
  39. SettingPos : 0,
  40. ChoosingEntity : 1
  41. };
  42. LineTangent1.prototype.beginEvent = function() {
  43. Line.prototype.beginEvent.call(this);
  44. this.setState(LineTangent1.State.SettingPos);
  45. };
  46. LineTangent1.prototype.setState = function(state) {
  47. Line.prototype.setState.call(this, state);
  48. this.setCrosshairCursor();
  49. var tr;
  50. var appWin = RMainWindowQt.getMainWindow();
  51. switch (this.state) {
  52. case LineTangent1.State.SettingPos:
  53. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  54. tr = qsTr("Choose start point of tangent");
  55. this.setLeftMouseTip(tr);
  56. this.setCommandPrompt(tr);
  57. this.setRightMouseTip(EAction.trCancel);
  58. this.entity = undefined;
  59. this.shape = undefined;
  60. this.pos1 = undefined;
  61. this.pos2 = undefined;
  62. EAction.showSnapTools();
  63. break;
  64. case LineTangent1.State.ChoosingEntity:
  65. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  66. tr = qsTr("Choose circle, arc or ellipse");
  67. this.setLeftMouseTip(tr);
  68. this.setCommandPrompt(tr);
  69. this.setRightMouseTip(EAction.trBack);
  70. //EAction.showMainTools();
  71. break;
  72. }
  73. };
  74. LineTangent1.prototype.escapeEvent = function() {
  75. switch (this.state) {
  76. case LineTangent1.State.SettingPos:
  77. EAction.prototype.escapeEvent.call(this);
  78. break;
  79. case LineTangent1.State.ChoosingEntity:
  80. this.setState(LineTangent1.State.SettingPos);
  81. break;
  82. }
  83. };
  84. LineTangent1.prototype.pickCoordinate = function(event, preview) {
  85. var di = this.getDocumentInterface();
  86. switch (this.state) {
  87. case LineTangent1.State.SettingPos:
  88. this.pos1 = event.getModelPosition();
  89. if (!preview) {
  90. di.setRelativeZero(this.pos1);
  91. //di.repaintViews();
  92. this.setState(LineTangent1.State.ChoosingEntity);
  93. }
  94. break;
  95. }
  96. };
  97. LineTangent1.prototype.pickEntity = function(event, preview) {
  98. this.error = "";
  99. var di = this.getDocumentInterface();
  100. var doc = this.getDocument();
  101. var entityId = this.getEntityId(event, preview);
  102. var entity = doc.queryEntity(entityId);
  103. var pos = event.getModelPosition();
  104. if (isNull(entity)) {
  105. this.entity = undefined;
  106. this.shape = undefined;
  107. return;
  108. }
  109. switch (this.state) {
  110. case LineTangent1.State.ChoosingEntity:
  111. var shape = entity.getClosestSimpleShape(pos);
  112. if (isArcShape(shape) ||
  113. isCircleShape(shape) ||
  114. isEllipseShape(shape)) {
  115. this.entity = entity;
  116. this.shape = shape;
  117. this.pos2 = pos;
  118. }
  119. else {
  120. this.entity = undefined;
  121. this.shape = undefined;
  122. this.pos2 = undefined;
  123. if (!preview) {
  124. EAction.warnNotArcCircleEllipse();
  125. }
  126. break;
  127. }
  128. if (preview) {
  129. this.updatePreview();
  130. }
  131. else {
  132. var op = this.getOperation(false);
  133. if (!isNull(op)) {
  134. di.applyOperation(op);
  135. if (this.error.length==0) {
  136. this.setState(LineTangent1.State.SettingPos);
  137. }
  138. }
  139. else {
  140. EAction.handleUserWarning(
  141. qsTr("No tangent possible.")
  142. );
  143. }
  144. }
  145. break;
  146. }
  147. if (!preview && this.error.length!==0) {
  148. EAction.handleUserWarning(this.error);
  149. }
  150. };
  151. LineTangent1.prototype.getOperation = function(preview) {
  152. if (isNull(this.pos1) || isNull(this.pos2) ||
  153. isNull(this.entity) || isNull(this.shape)) {
  154. return undefined;
  155. }
  156. var doc = this.getDocument();
  157. var tangent;
  158. var tangents = this.shape.getTangents(this.pos1);
  159. if (tangents.length===0) {
  160. if (!preview) {
  161. this.error = qsTr("Cannot draw tangent from point inside circle to circle")
  162. }
  163. }
  164. else {
  165. if (tangents.length!==2 ||
  166. tangents[0].getDistanceTo(this.pos2) < tangents[1].getDistanceTo(this.pos2)) {
  167. tangent = tangents[0];
  168. }
  169. else {
  170. tangent = tangents[1];
  171. }
  172. }
  173. if (isNull(tangent)) {
  174. return undefined;
  175. }
  176. var op = new RAddObjectsOperation();
  177. op.setText(this.getToolTitle());
  178. op.addObject(this.createLineEntity(doc, tangent.getStartPoint(), tangent.getEndPoint()));
  179. return op;
  180. };
  181. LineTangent1.prototype.getTangentArcOrCircle = function(arcOrCircle, preview) {
  182. var doc = this.getDocument();
  183. // create temporary thales circle:
  184. var thalesCenter = this.pos1.operator_add(arcOrCircle.getCenter()).operator_divide(2.0);
  185. var thalesRadius = this.pos1.getDistanceTo(thalesCenter);
  186. if (thalesRadius<arcOrCircle.getRadius()/2.0) {
  187. if (!preview) {
  188. this.error = qsTr("Cannot draw tangent from point inside circle to circle");
  189. }
  190. return undefined;
  191. }
  192. var thalesCircle = new RCircle(thalesCenter, thalesRadius);
  193. // get the two intersection points which are the tangent points:
  194. var ips = thalesCircle.getIntersectionPoints(
  195. arcOrCircle.data(),
  196. false
  197. );
  198. if (ips.length>0) {
  199. var tangent1 = new RLine(this.pos1, ips[0]);
  200. if (ips.length>1) {
  201. var tangent2 = new RLine(this.pos1, ips[1]);
  202. if (tangent1.getDistanceTo(this.pos2) <
  203. tangent2.getDistanceTo(this.pos2)) {
  204. return tangent1;
  205. }
  206. else {
  207. return tangent2;
  208. }
  209. }
  210. else {
  211. return tangent1;
  212. }
  213. }
  214. else {
  215. return undefined;
  216. }
  217. };
  218. LineTangent1.prototype.getAuxPreview = function() {
  219. var ret = [];
  220. if (isArcShape(this.shape)) {
  221. ret.push(new RCircle(this.shape.getCenter(), this.shape.getRadius()));
  222. }
  223. else if (isEllipseShape(this.shape)) {
  224. var ellipse = this.shape.clone();
  225. ellipse.setStartParam(0.0);
  226. ellipse.setEndParam(2*Math.PI);
  227. ret.push(ellipse);
  228. }
  229. return ret;
  230. };
  231. LineTangent1.prototype.getHighlightedEntities = function() {
  232. var ret = [];
  233. if (isEntity(this.entity)) {
  234. ret.push(this.entity.getId());
  235. }
  236. return ret;
  237. };