Circle2PR.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /**
  21. * \class Circle2PR
  22. * \brief Circle from start point, end point and radius.
  23. * \ingroup ecma_draw_circle
  24. */
  25. function Circle2PR(guiAction) {
  26. Circle.call(this, guiAction);
  27. this.point1 = undefined;
  28. this.point2 = undefined;
  29. this.radius = undefined;
  30. // for aux construction:
  31. this.center = undefined;
  32. this.setUiOptions("Circle2PR.ui");
  33. }
  34. Circle2PR.prototype = new Circle();
  35. Circle2PR.State = {
  36. SettingPoint1 : 0,
  37. SettingPoint2 : 1
  38. //ChoosingSolution : 2
  39. };
  40. Circle2PR.prototype.beginEvent = function() {
  41. Circle.prototype.beginEvent.call(this);
  42. this.setState(Circle2PR.State.SettingPoint1);
  43. };
  44. Circle2PR.prototype.setState = function(state) {
  45. Circle.prototype.setState.call(this, state);
  46. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  47. this.setCrosshairCursor();
  48. var appWin = RMainWindowQt.getMainWindow();
  49. switch (this.state) {
  50. case Circle2PR.State.SettingPoint1:
  51. this.point1 = undefined;
  52. this.point2 = undefined;
  53. var trFirstPoint = qsTr("First point");
  54. this.setCommandPrompt(trFirstPoint);
  55. this.setLeftMouseTip(trFirstPoint);
  56. this.setRightMouseTip(EAction.trCancel);
  57. break;
  58. case Circle2PR.State.SettingPoint2:
  59. var trSecondPoint = qsTr("Second point");
  60. this.setCommandPrompt(trSecondPoint);
  61. this.setLeftMouseTip(trSecondPoint);
  62. this.setRightMouseTip(EAction.trBack);
  63. break;
  64. /*case Circle2PR.State.ChoosingSolution:
  65. var trSolution = qsTr("Solution");
  66. this.setCommandPrompt(trSolution);
  67. this.setLeftMouseTip(trSolution);
  68. this.setRightMouseTip(EAction.trBack);
  69. break;*/
  70. }
  71. EAction.showSnapTools();
  72. };
  73. Circle2PR.prototype.escapeEvent = function() {
  74. switch (this.state) {
  75. case Circle2PR.State.SettingPoint1:
  76. EAction.prototype.escapeEvent.call(this);
  77. break;
  78. case Circle2PR.State.SettingPoint2:
  79. this.setState(Circle2PR.State.SettingPoint1);
  80. break;
  81. //case Circle2PR.State.ChoosingSolution:
  82. // this.setState(Circle2PR.State.SettingPoint2);
  83. // break;
  84. }
  85. };
  86. Circle2PR.prototype.pickCoordinate = function(event, preview) {
  87. this.error = "";
  88. var di = this.getDocumentInterface();
  89. switch (this.state) {
  90. case Circle2PR.State.SettingPoint1:
  91. this.point1 = event.getModelPosition();
  92. if (!preview) {
  93. di.setRelativeZero(this.point1);
  94. this.setState(Circle2PR.State.SettingPoint2);
  95. }
  96. break;
  97. case Circle2PR.State.SettingPoint2:
  98. this.point2 = event.getModelPosition();
  99. if (preview) {
  100. this.updatePreview();
  101. }
  102. else {
  103. var op = this.getOperation(false);
  104. if (!isNull(op)) {
  105. di.applyOperation(op);
  106. di.setRelativeZero(this.point2);
  107. this.setState(Circle2PR.State.SettingPoint1);
  108. }
  109. }
  110. break;
  111. // case Circle2PR.State.ChoosingSolution:
  112. // this.point2 = event.getModelPosition();
  113. // if (preview) {
  114. // this.updatePreview();
  115. // }
  116. // else {
  117. // var op = this.getOperation(false);
  118. // if (!isNull(op)) {
  119. // di.applyOperation(op);
  120. // di.setRelativeZero(this.point2);
  121. // this.setState(Circle2PR.State.SettingPoint1);
  122. // }
  123. // }
  124. // break;
  125. }
  126. if (!preview && this.error.length!==0) {
  127. EAction.handleUserWarning(this.error);
  128. }
  129. };
  130. Circle2PR.prototype.getOperation = function(preview) {
  131. var shape = this.getCircle2PR(preview);
  132. if (isNull(shape)) {
  133. return undefined;
  134. }
  135. var doc = this.getDocument();
  136. var entity = new RCircleEntity(doc, new RCircleData(shape));
  137. if (!isEntity(entity)) {
  138. return undefined;
  139. }
  140. return new RAddObjectOperation(entity, this.getToolTitle());
  141. };
  142. Circle2PR.prototype.getCircle2PR = function(preview) {
  143. if (isNull(this.point1) || isNull(this.point2) || !isNumber(this.radius)) {
  144. return undefined;
  145. }
  146. if (this.radius <= 0.0 || this.radius > 1.0e6) {
  147. if (!preview) {
  148. this.error = qsTr("Invalid radius");
  149. }
  150. return undefined;
  151. }
  152. if (this.point1.equalsFuzzy(this.point2)) {
  153. if (!preview) {
  154. this.error = qsTr("The two points are identical");
  155. }
  156. return undefined;
  157. }
  158. var circle1 = new RCircle(this.point1, this.radius);
  159. var circle2 = new RCircle(this.point2, this.radius);
  160. var ips = circle1.getIntersectionPoints(circle2, false);
  161. if (ips.length===2) {
  162. var ipRight, ipLeft;
  163. var line = new RLine(this.point1, this.point2);
  164. if (line.getSideOfPoint(ips[0])==RS.RightHand) {
  165. ipRight = ips[0];
  166. ipLeft = ips[1];
  167. }
  168. else {
  169. ipLeft = ips[0];
  170. ipRight = ips[1];
  171. }
  172. // XOR:
  173. if ((this.reversed==true) != (this.alternativeSolution==true)) {
  174. this.center = ipRight;
  175. }
  176. else {
  177. this.center = ipLeft;
  178. }
  179. }
  180. else {
  181. // center in the middle:
  182. var v = new RVector();
  183. v.setPolar(this.radius, this.point1.getAngleTo(this.point2));
  184. this.center = this.point1.operator_add(v);
  185. //this.point2 = this.point1.operator_add(v.operator_multiply(2.0));
  186. }
  187. return new RCircle(this.center, this.radius);
  188. };
  189. Circle2PR.prototype.slotRadiusChanged = function(value) {
  190. this.radius = value;
  191. this.updatePreview(true);
  192. };
  193. Circle2PR.prototype.slotDirectionChanged = function(button) {
  194. if (!isQObject(button) || button.checked!=true) {
  195. return;
  196. }
  197. if (button.objectName==="Clockwise") {
  198. this.reversed = true;
  199. }
  200. else {
  201. this.reversed = false;
  202. }
  203. this.updatePreview(true);
  204. };
  205. Circle2PR.prototype.slotSolutionChanged = function(button) {
  206. if (!isQObject(button) || button.checked!=true) {
  207. return;
  208. }
  209. if (button.objectName==="Solution2") {
  210. this.alternativeSolution = true;
  211. }
  212. else {
  213. this.alternativeSolution = false;
  214. }
  215. this.updatePreview(true);
  216. };
  217. Circle2PR.prototype.getAuxPreview = function() {
  218. if (isNull(this.center) || isNull(this.point1) || isNull(this.point2)) {
  219. return undefined;
  220. }
  221. var ret = [];
  222. if (this.state===Circle2PR.State.SettingPoint2) {
  223. ret.push(new RLine(this.point1, this.point2));
  224. }
  225. return ret;
  226. };