Circle2TP.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/Apollonius.js");
  21. include("scripts/ShapeAlgorithms.js");
  22. /**
  23. * \class Circle2TP
  24. * \brief Circle tangential to two entities and through point.
  25. * \ingroup ecma_draw_circle
  26. */
  27. function Circle2TP(guiAction) {
  28. Circle.call(this, guiAction);
  29. this.entity1 = undefined;
  30. this.shape1 = undefined;
  31. this.pos1 = undefined;
  32. this.entity2 = undefined;
  33. this.shape2 = undefined;
  34. this.pos2 = undefined;
  35. this.pos3 = undefined;
  36. this.pos4 = undefined;
  37. this.candidates = undefined;
  38. }
  39. Circle2TP.prototype = new Circle();
  40. Circle2TP.State = {
  41. ChoosingShape1 : 0,
  42. ChoosingShape2 : 1,
  43. SettingPoint : 2,
  44. ChoosingSolution : 3
  45. };
  46. Circle2TP.prototype.beginEvent = function() {
  47. Circle.prototype.beginEvent.call(this);
  48. this.setState(Circle2TP.State.ChoosingShape1);
  49. };
  50. Circle2TP.prototype.setState = function(state) {
  51. Circle.prototype.setState.call(this, state);
  52. var di = this.getDocumentInterface();
  53. this.setCrosshairCursor();
  54. var appWin = RMainWindowQt.getMainWindow();
  55. switch (this.state) {
  56. case Circle2TP.State.ChoosingShape1:
  57. di.setClickMode(RAction.PickEntity);
  58. this.entity1 = undefined;
  59. this.shape1 = undefined;
  60. this.entity2 = undefined;
  61. this.shape2 = undefined;
  62. this.pos4 = undefined;
  63. this.candidates = undefined;
  64. var trFirstEntity = qsTr("First line, arc or circle");
  65. this.setCommandPrompt(trFirstEntity);
  66. this.setLeftMouseTip(trFirstEntity);
  67. this.setRightMouseTip(EAction.trCancel);
  68. break;
  69. case Circle2TP.State.ChoosingShape2:
  70. di.setClickMode(RAction.PickEntity);
  71. this.entity2 = undefined;
  72. this.shape2 = undefined;
  73. this.pos4 = undefined;
  74. this.candidates = undefined;
  75. var trSecondEntity = qsTr("Second line, arc or circle");
  76. this.setCommandPrompt(trSecondEntity);
  77. this.setLeftMouseTip(trSecondEntity);
  78. this.setRightMouseTip(EAction.trBack);
  79. break;
  80. case Circle2TP.State.SettingPoint:
  81. di.setClickMode(RAction.PickCoordinate);
  82. this.pos3 = undefined;
  83. this.pos4 = undefined;
  84. this.candidates = undefined;
  85. var trPoint = qsTr("Point on circle line");
  86. this.setCommandPrompt(trPoint);
  87. this.setLeftMouseTip(trPoint);
  88. this.setRightMouseTip(EAction.trBack);
  89. EAction.showSnapTools();
  90. break;
  91. case Circle2TP.State.ChoosingSolution:
  92. di.setClickMode(RAction.PickEntity);
  93. this.pos4 = undefined;
  94. var trSolution = qsTr("Choose solution");
  95. this.setCommandPrompt(trSolution);
  96. this.setLeftMouseTip(trSolution);
  97. this.setRightMouseTip(EAction.trBack);
  98. break;
  99. }
  100. this.simulateMouseMoveEvent();
  101. };
  102. Circle2TP.prototype.escapeEvent = function() {
  103. switch (this.state) {
  104. case Circle2TP.State.ChoosingShape1:
  105. EAction.prototype.escapeEvent.call(this);
  106. break;
  107. case Circle2TP.State.ChoosingShape2:
  108. this.setState(Circle2TP.State.ChoosingShape1);
  109. break;
  110. case Circle2TP.State.SettingPoint:
  111. this.setState(Circle2TP.State.ChoosingShape2);
  112. break;
  113. case Circle2TP.State.ChoosingSolution:
  114. this.setState(Circle2TP.State.SettingPoint);
  115. break;
  116. }
  117. };
  118. Circle2TP.prototype.pickEntity = function(event, preview) {
  119. this.error = "";
  120. var di = this.getDocumentInterface();
  121. var doc = this.getDocument();
  122. var entityId = this.getEntityId(event, preview);
  123. var entity = doc.queryEntity(entityId);
  124. var pos = event.getModelPosition();
  125. var shape = undefined;
  126. if (this.state!==Circle2TP.State.ChoosingSolution) {
  127. if (!this.isEntitySnappable(entity)) {
  128. // entity not on a snappable layer:
  129. return;
  130. }
  131. if (isNull(entity)) {
  132. return;
  133. }
  134. else {
  135. shape = entity.getClosestSimpleShape(pos);
  136. if (!isLineBasedShape(shape) &&
  137. !isArcShape(shape) &&
  138. !isCircleShape(shape)) {
  139. if (!preview) {
  140. EAction.warnNotLineArcCircle();
  141. return;
  142. }
  143. shape = undefined;
  144. }
  145. }
  146. }
  147. switch (this.state) {
  148. case Circle2TP.State.ChoosingShape1:
  149. this.entity1 = entity;
  150. this.shape1 = shape;
  151. this.pos1 = pos;
  152. if (preview) {
  153. this.updatePreview();
  154. }
  155. else {
  156. this.setState(Circle2TP.State.ChoosingShape2);
  157. }
  158. break;
  159. case Circle2TP.State.ChoosingShape2:
  160. this.entity2 = entity;
  161. this.shape2 = shape;
  162. this.pos2 = pos;
  163. if (preview) {
  164. this.updatePreview();
  165. }
  166. else {
  167. this.setState(Circle2TP.State.SettingPoint);
  168. }
  169. break;
  170. case Circle2TP.State.ChoosingSolution:
  171. this.pos4 = event.getModelPosition();
  172. if (preview) {
  173. this.updatePreview();
  174. }
  175. else {
  176. var op = this.getOperation(false);
  177. if (!isNull(op)) {
  178. di.applyOperation(op);
  179. this.setState(Circle2TP.State.ChoosingShape1);
  180. }
  181. }
  182. break;
  183. }
  184. if (!preview && this.error.length!==0) {
  185. EAction.handleUserWarning(this.error);
  186. }
  187. };
  188. Circle2TP.prototype.pickCoordinate = function(event, preview) {
  189. var di = this.getDocumentInterface();
  190. var pos = event.getModelPosition();
  191. switch (this.state) {
  192. case Circle2TP.State.SettingPoint:
  193. if (isNull(this.pos3) || !pos.equalsFuzzy(this.pos3)) {
  194. this.candidates = undefined;
  195. }
  196. this.pos3 = pos;
  197. if (preview) {
  198. this.updatePreview();
  199. }
  200. else {
  201. var op = this.getOperation(false);
  202. if (!isNull(op)) {
  203. // only one solution:
  204. if (this.candidates.length===1) {
  205. di.applyOperation(op);
  206. this.setState(Circle2TP.State.ChoosingShape1);
  207. }
  208. // multiple solutions:
  209. else {
  210. destr(op);
  211. this.setState(Circle2TP.State.ChoosingSolution);
  212. }
  213. }
  214. // no solution:
  215. else {
  216. this.setState(Circle2TP.State.ChoosingShape1);
  217. this.error = qsTr("No solution");
  218. }
  219. }
  220. break;
  221. }
  222. if (!preview && this.error.length!==0) {
  223. EAction.handleUserWarning(this.error);
  224. }
  225. };
  226. Circle2TP.prototype.getOperation = function(preview) {
  227. var shape = this.getShapes(preview);
  228. if (isNull(shape)) {
  229. return undefined;
  230. }
  231. var doc = this.getDocument();
  232. var op = new RAddObjectsOperation();
  233. op.setText(this.getToolTitle());
  234. for (var i=0; i<shape.length; i++) {
  235. var entity = new RCircleEntity(doc, new RCircleData(shape[i]));
  236. op.addObject(entity);
  237. }
  238. return op;
  239. };
  240. Circle2TP.prototype.getShapes = function(preview) {
  241. if (isNull(this.shape1) || isNull(this.shape2) || isNull(this.pos3)) {
  242. return undefined;
  243. }
  244. if (isNull(this.candidates)) {
  245. var shape3 = new RPoint(this.pos3);
  246. var s1 = this.shape1;
  247. if (isFunction(this.shape1.data)) {
  248. s1 = this.shape1.data();
  249. }
  250. else {
  251. s1 = this.shape1;
  252. }
  253. var s2 = this.shape2;
  254. if (isFunction(this.shape2.data)) {
  255. s2 = this.shape2.data();
  256. }
  257. else {
  258. s2 = this.shape2;
  259. }
  260. this.candidates = Apollonius.getSolutions(s1, s2, shape3);
  261. // filter out lines:
  262. this.candidates = ShapeAlgorithms.getCircleShapes(this.candidates);
  263. }
  264. if (this.candidates.length===0) {
  265. if (!preview) {
  266. this.error = qsTr("No solution");
  267. }
  268. return undefined;
  269. }
  270. // no position yet: return all candidates for preview:
  271. if (isNull(this.pos4)) {
  272. return this.candidates;
  273. }
  274. return [ ShapeAlgorithms.getClosestShape(this.candidates, this.pos4) ];
  275. };
  276. Circle2TP.prototype.getHighlightedEntities = function() {
  277. var ret = [];
  278. if (isEntity(this.entity1)) {
  279. ret.push(this.entity1.getId());
  280. }
  281. if (isEntity(this.entity2)) {
  282. ret.push(this.entity2.getId());
  283. }
  284. return ret;
  285. };
  286. Circle2TP.prototype.getAuxPreview = function() {
  287. if (isNull(this.shape1) || isNull(this.shape2) || isNull(this.pos3)) {
  288. return [];
  289. }
  290. if (!isNull(this.pos4)) {
  291. return this.candidates;
  292. }
  293. return [];
  294. };