Arc2PH.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. * Copyright (c) 2015 by Robert S. (Modification to draw arc with specified height).
  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. /**
  21. * \class Arc2PH
  22. * \brief Arc from two points and height.
  23. * \ingroup ecma_draw_arc
  24. */
  25. function Arc2PH(guiAction) {
  26. Arc.call(this, guiAction);
  27. this.point1 = undefined;
  28. this.point2 = undefined;
  29. this.center = undefined;
  30. this.height = undefined;
  31. this.radius = undefined;
  32. this.reversed = false;
  33. this.setUiOptions("Arc2PH.ui");
  34. }
  35. Arc2PH.prototype = new Arc();
  36. Arc2PH.State = {
  37. SettingPoint1 : 0,
  38. SettingPoint2 : 1
  39. };
  40. Arc2PH.prototype.beginEvent = function() {
  41. Arc.prototype.beginEvent.call(this);
  42. this.setState(Arc2PH.State.SettingPoint1);
  43. };
  44. Arc2PH.prototype.setState = function(state) {
  45. Arc.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 Arc2PH.State.SettingPoint1:
  51. this.point1 = undefined;
  52. this.point2 = undefined;
  53. this.center = undefined;
  54. var trStartPoint = qsTr("Start point");
  55. this.setCommandPrompt(trStartPoint);
  56. this.setLeftMouseTip(trStartPoint);
  57. this.setRightMouseTip(EAction.trCancel);
  58. break;
  59. case Arc2PH.State.SettingPoint2:
  60. this.point2 = undefined;
  61. this.center = undefined;
  62. var trEndPoint = qsTr("End point");
  63. this.setCommandPrompt(trEndPoint);
  64. this.setLeftMouseTip(trEndPoint);
  65. this.setRightMouseTip(EAction.trBack);
  66. break;
  67. }
  68. EAction.showSnapTools();
  69. };
  70. Arc2PH.prototype.escapeEvent = function() {
  71. switch (this.state) {
  72. case Arc2PH.State.SettingPoint1:
  73. EAction.prototype.escapeEvent.call(this);
  74. break;
  75. case Arc2PH.State.SettingPoint2:
  76. this.setState(Arc2PH.State.SettingPoint1);
  77. break;
  78. }
  79. };
  80. Arc2PH.prototype.pickCoordinate = function(event, preview) {
  81. this.error = "";
  82. var di = this.getDocumentInterface();
  83. switch (this.state) {
  84. case Arc2PH.State.SettingPoint1:
  85. this.point1 = event.getModelPosition();
  86. if (!preview) {
  87. di.setRelativeZero(this.point1);
  88. this.setState(Arc2PH.State.SettingPoint2);
  89. }
  90. break;
  91. case Arc2PH.State.SettingPoint2:
  92. this.point2 = event.getModelPosition();
  93. if (preview) {
  94. this.updatePreview();
  95. }
  96. else {
  97. var op = this.getOperation(false);
  98. if (!isNull(op)) {
  99. di.applyOperation(op);
  100. di.setRelativeZero(this.point2);
  101. this.setState(Arc2PH.State.SettingPoint1);
  102. }
  103. }
  104. break;
  105. }
  106. if (!preview && this.error.length!==0) {
  107. EAction.handleUserWarning(this.error);
  108. }
  109. };
  110. Arc2PH.prototype.getOperation = function(preview) {
  111. var shape = this.getArc2PH(preview);
  112. if (isNull(shape)) {
  113. return undefined;
  114. }
  115. var doc = this.getDocument();
  116. var entity = new RArcEntity(doc, new RArcData(shape));
  117. if (!isEntity(entity)) {
  118. return undefined;
  119. }
  120. return new RAddObjectOperation(entity, this.getToolTitle());
  121. };
  122. Arc2PH.prototype.getArc2PH = function(preview) {
  123. if (isNull(this.point1) || isNull(this.point2) || !isNumber(this.height)) {
  124. return undefined;
  125. }
  126. if (this.height <= 0.0 || this.height > 1.0e6) {
  127. if (!preview) {
  128. this.error = qsTr("Invalid height");
  129. }
  130. return undefined;
  131. }
  132. if (this.point1.equalsFuzzy(this.point2)) {
  133. if (!preview) {
  134. this.error = qsTr("The two points are identical");
  135. }
  136. return undefined;
  137. }
  138. var chordlen = this.point1.getDistanceTo(this.point2);
  139. var archeight = this.height;
  140. this.radius = (archeight / 2) + ((chordlen * chordlen) / (8 * archeight));
  141. var circle1 = new RCircle(this.point1, this.radius);
  142. var circle2 = new RCircle(this.point2, this.radius);
  143. var ips = circle1.getIntersectionPoints(circle2, false);
  144. if (ips.length===2) {
  145. var ipRight, ipLeft;
  146. var line = new RLine(this.point1, this.point2);
  147. if (line.getSideOfPoint(ips[0]) === RS.RightHand) {
  148. ipRight = ips[0];
  149. ipLeft = ips[1];
  150. }
  151. else {
  152. ipLeft = ips[0];
  153. ipRight = ips[1];
  154. }
  155. if (this.reversed === false) {
  156. if (this.height > (chordlen / 2)) {
  157. this.center = ipRight;
  158. } else {
  159. this.center = ipLeft;
  160. }
  161. } else {
  162. if (this.height > (chordlen / 2)) {
  163. this.center = ipLeft;
  164. } else {
  165. this.center = ipRight;
  166. }
  167. }
  168. } else {
  169. // center in the middle:
  170. var v = new RVector();
  171. v.setPolar(this.radius, this.point1.getAngleTo(this.point2));
  172. this.center = this.point1.operator_add(v);
  173. this.point2 = this.point1.operator_add(v.operator_multiply(2.0));
  174. }
  175. var angle1 = this.center.getAngleTo(this.point1);
  176. var angle2 = this.center.getAngleTo(this.point2);
  177. return new RArc(this.center, this.radius, angle1, angle2, this.reversed);
  178. };
  179. Arc2PH.prototype.slotHeightChanged = function(value) {
  180. this.height = value;
  181. this.updatePreview(true);
  182. };
  183. Arc2PH.prototype.slotDirectionChanged = function(button) {
  184. if (!isQObject(button) || button.checked!=true) {
  185. return;
  186. }
  187. if (button.objectName==="Clockwise") {
  188. this.reversed = true;
  189. }
  190. else {
  191. this.reversed = false;
  192. }
  193. this.updatePreview(true);
  194. };
  195. Arc2PH.prototype.getAuxPreview = function() {
  196. if (isNull(this.center) || isNull(this.point1) || isNull(this.point2)) {
  197. return undefined;
  198. }
  199. var ret = [];
  200. if (this.state==Arc2PH.State.SettingPoint2) {
  201. ret.push(new RLine(this.center, this.point1));
  202. ret.push(new RLine(this.center, this.point2));
  203. }
  204. return ret;
  205. };