Circle3T.js 9.8 KB

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