Circle2TR.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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("../Circle.js");
  20. include("scripts/ShapeAlgorithms.js");
  21. include("scripts/Apollonius.js");
  22. /**
  23. * \class Circle2TR
  24. * \brief Circle from radius and two tangential entities.
  25. * \ingroup ecma_draw_circle
  26. */
  27. function Circle2TR(guiAction) {
  28. Circle.call(this, guiAction);
  29. this.entity1 = undefined;
  30. this.shape1 = undefined;
  31. this.entity2 = undefined;
  32. this.entity2Id = undefined;
  33. this.shape2 = undefined;
  34. this.radius = undefined;
  35. this.center = undefined;
  36. this.pos = undefined;
  37. this.candidates = [];
  38. this.setUiOptions("Circle2TR.ui");
  39. }
  40. Circle2TR.prototype = new Circle();
  41. Circle2TR.State = {
  42. ChoosingShape1 : 0,
  43. ChoosingShape2 : 1,
  44. ChoosingSolution : 2
  45. };
  46. Circle2TR.prototype.beginEvent = function() {
  47. Circle.prototype.beginEvent.call(this);
  48. this.setState(Circle2TR.State.ChoosingShape1);
  49. };
  50. Circle2TR.prototype.setState = function(state) {
  51. Circle.prototype.setState.call(this, state);
  52. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  53. this.setCrosshairCursor();
  54. var appWin = RMainWindowQt.getMainWindow();
  55. switch (this.state) {
  56. case Circle2TR.State.ChoosingShape1:
  57. this.entity1 = undefined;
  58. this.shape1 = undefined;
  59. this.entity2 = undefined;
  60. this.entity2Id = undefined;
  61. this.shape2 = undefined;
  62. this.center = undefined;
  63. this.pos = undefined;
  64. this.candidates = [];
  65. var trFirstEntity = qsTr("First line, arc or circle");
  66. this.setCommandPrompt(trFirstEntity);
  67. this.setLeftMouseTip(trFirstEntity);
  68. this.setRightMouseTip(EAction.trCancel);
  69. break;
  70. case Circle2TR.State.ChoosingShape2:
  71. this.entity2 = undefined;
  72. this.entity2Id = undefined;
  73. this.shape2 = undefined;
  74. this.center = undefined;
  75. this.pos = undefined;
  76. this.candidates = [];
  77. var trSecondEntity = qsTr("Second line, arc or circle");
  78. this.setCommandPrompt(trSecondEntity);
  79. this.setLeftMouseTip(trSecondEntity);
  80. this.setRightMouseTip(EAction.trBack);
  81. break;
  82. case Circle2TR.State.ChoosingSolution:
  83. this.pos = undefined;
  84. var trSolution = qsTr("Choose solution");
  85. this.setCommandPrompt(trSolution);
  86. this.setLeftMouseTip(trSolution);
  87. this.setRightMouseTip(EAction.trBack);
  88. break;
  89. }
  90. this.simulateMouseMoveEvent();
  91. };
  92. Circle2TR.prototype.escapeEvent = function() {
  93. switch (this.state) {
  94. case Circle2TR.State.ChoosingShape1:
  95. EAction.prototype.escapeEvent.call(this);
  96. break;
  97. case Circle2TR.State.ChoosingShape2:
  98. this.setState(Circle2TR.State.ChoosingShape1);
  99. break;
  100. case Circle2TR.State.ChoosingSolution:
  101. this.setState(Circle2TR.State.ChoosingShape2);
  102. break;
  103. }
  104. };
  105. Circle2TR.prototype.pickEntity = function(event, preview) {
  106. this.error = "";
  107. var di = this.getDocumentInterface();
  108. var doc = this.getDocument();
  109. var entityId = this.getEntityId(event, preview);
  110. var entity = doc.queryEntity(entityId);
  111. var pos = event.getModelPosition();
  112. var shape = undefined;
  113. if (this.state!==Circle2TR.State.ChoosingSolution) {
  114. if (!this.isEntitySnappable(entity)) {
  115. // entity not on a snappable layer:
  116. return;
  117. }
  118. if (isNull(entity)) {
  119. return;
  120. }
  121. else {
  122. shape = entity.getClosestSimpleShape(pos);
  123. if (!isLineBasedShape(shape) &&
  124. !isArcShape(shape) &&
  125. !isCircleShape(shape)) {
  126. if (!preview) {
  127. EAction.warnNotLineArcCircle();
  128. return;
  129. }
  130. shape = undefined;
  131. }
  132. }
  133. }
  134. switch (this.state) {
  135. case Circle2TR.State.ChoosingShape1:
  136. this.entity1 = entity;
  137. this.shape1 = shape;
  138. if (preview) {
  139. this.updatePreview();
  140. }
  141. else {
  142. this.setState(Circle2TR.State.ChoosingShape2);
  143. }
  144. break;
  145. case Circle2TR.State.ChoosingShape2:
  146. // optimization breaks when choosing two segments of the same polyline:
  147. //if (entityId!==this.entity2Id) {
  148. // force re-calculation:
  149. this.candidates = [];
  150. //}
  151. this.entity2 = entity;
  152. this.entity2Id = entityId;
  153. this.shape2 = shape;
  154. if (preview) {
  155. this.updatePreview();
  156. }
  157. else {
  158. var op = this.getOperation(false);
  159. if (!isNull(op)) {
  160. // di.applyOperation(op);
  161. // this.setState(Circle2TR.State.ChoosingShape1);
  162. // only one solution:
  163. if (this.candidates.length===1) {
  164. di.applyOperation(op);
  165. this.setState(Circle2TR.State.ChoosingShape1);
  166. }
  167. // multiple solutions:
  168. else {
  169. this.setState(Circle2TR.State.ChoosingSolution);
  170. }
  171. }
  172. else {
  173. this.setState(Circle2TR.State.ChoosingShape1);
  174. this.error = qsTr("No solution");
  175. }
  176. }
  177. break;
  178. case Circle2TR.State.ChoosingSolution:
  179. this.pos = event.getModelPosition();
  180. if (preview) {
  181. this.updatePreview();
  182. }
  183. else {
  184. var op = this.getOperation(false);
  185. if (!isNull(op)) {
  186. di.applyOperation(op);
  187. this.setState(Circle2TR.State.ChoosingShape1);
  188. }
  189. }
  190. break;
  191. }
  192. if (!preview && this.error.length!==0) {
  193. EAction.handleUserWarning(this.error);
  194. }
  195. };
  196. Circle2TR.prototype.getOperation = function(preview) {
  197. var shapes = this.getCircles2TR(preview);
  198. if (isNull(shapes)) {
  199. return undefined;
  200. }
  201. var doc = this.getDocument();
  202. var op = new RAddObjectsOperation();
  203. op.setText(this.getToolTitle());
  204. for (var i=0; i<shapes.length; i++) {
  205. var entity = new RCircleEntity(doc, new RCircleData(shapes[i]));
  206. op.addObject(entity);
  207. }
  208. return op;
  209. };
  210. Circle2TR.prototype.getCircles2TR = function(preview) {
  211. var ret = Apollonius.getCircles2TR(this.shape1, this.shape2, this.radius, this.pos, this.candidates, preview);
  212. this.error = Apollonius.error;
  213. return ret;
  214. };
  215. Circle2TR.prototype.slotRadiusChanged = function(value) {
  216. this.radius = value;
  217. // update candidates in case we are already choosing a solution:
  218. this.candidates = [];
  219. this.getCircles2TR(true);
  220. this.updatePreview(true);
  221. };
  222. Circle2TR.prototype.getHighlightedEntities = function() {
  223. var ret = [];
  224. if (isEntity(this.entity1)) {
  225. ret.push(this.entity1.getId());
  226. }
  227. if (isEntity(this.entity2)) {
  228. ret.push(this.entity2.getId());
  229. }
  230. return ret;
  231. };
  232. Circle2TR.prototype.getAuxPreview = function() {
  233. var ret = [];
  234. // var a;
  235. // if (isVector(this.center)) {
  236. // if (isArcShape(this.shape1)) {
  237. // a = this.shape1.getCenter().getAngleTo(this.center);
  238. // if (!this.shape1.isAngleWithinArc(a)) {
  239. // ret.push(new RCircle(this.shape1.getCenter(), this.shape1.getRadius()));
  240. // }
  241. // }
  242. // if (isArcShape(this.shape2)) {
  243. // a = this.shape2.getCenter().getAngleTo(this.center);
  244. // if (!this.shape2.isAngleWithinArc(a)) {
  245. // ret.push(new RCircle(this.shape2.getCenter(), this.shape2.getRadius()));
  246. // }
  247. // }
  248. // }
  249. if (!isNull(this.pos)) {
  250. return this.candidates;
  251. }
  252. return ret;
  253. };