ArcTPR.js 8.8 KB

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