Arc2PL.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 length).
  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 Arc2PL
  22. * \brief Arc from two points and length.
  23. * \ingroup ecma_draw_arc
  24. */
  25. function Arc2PL(guiAction) {
  26. Arc.call(this, guiAction);
  27. this.point1 = undefined;
  28. this.point2 = undefined;
  29. this.center = undefined;
  30. this.len = undefined;
  31. this.radius = undefined;
  32. this.reversed = false;
  33. this.setUiOptions("Arc2PL.ui");
  34. }
  35. Arc2PL.prototype = new Arc();
  36. Arc2PL.State = {
  37. SettingPoint1 : 0,
  38. SettingPoint2 : 1
  39. };
  40. Arc2PL.prototype.beginEvent = function() {
  41. Arc.prototype.beginEvent.call(this);
  42. this.setState(Arc2PL.State.SettingPoint1);
  43. };
  44. Arc2PL.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 Arc2PL.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 Arc2PL.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. Arc2PL.prototype.escapeEvent = function() {
  71. switch (this.state) {
  72. case Arc2PL.State.SettingPoint1:
  73. EAction.prototype.escapeEvent.call(this);
  74. break;
  75. case Arc2PL.State.SettingPoint2:
  76. this.setState(Arc2PL.State.SettingPoint1);
  77. break;
  78. }
  79. };
  80. Arc2PL.prototype.pickCoordinate = function(event, preview) {
  81. this.error = "";
  82. var di = this.getDocumentInterface();
  83. switch (this.state) {
  84. case Arc2PL.State.SettingPoint1:
  85. this.point1 = event.getModelPosition();
  86. if (!preview) {
  87. di.setRelativeZero(this.point1);
  88. this.setState(Arc2PL.State.SettingPoint2);
  89. }
  90. break;
  91. case Arc2PL.State.SettingPoint2:
  92. this.point2 = event.getModelPosition();
  93. if (this.point1.getDistanceTo(this.point2) > this.len) {
  94. this.point2 = undefined;
  95. }
  96. if (preview) {
  97. this.updatePreview();
  98. }
  99. else {
  100. var op = this.getOperation(false);
  101. if (!isNull(op)) {
  102. di.applyOperation(op);
  103. di.setRelativeZero(this.point2);
  104. this.setState(Arc2PL.State.SettingPoint1);
  105. }
  106. }
  107. break;
  108. }
  109. if (!preview && this.error.length!==0) {
  110. EAction.handleUserWarning(this.error);
  111. }
  112. };
  113. Arc2PL.prototype.getOperation = function(preview) {
  114. var shape = this.getArc2PL(preview);
  115. if (isNull(shape)) {
  116. return undefined;
  117. }
  118. var doc = this.getDocument();
  119. var entity = new RArcEntity(doc, new RArcData(shape));
  120. if (!isEntity(entity)) {
  121. return undefined;
  122. }
  123. return new RAddObjectOperation(entity, this.getToolTitle());
  124. };
  125. Arc2PL.prototype.getArc2PL = function(preview) {
  126. if (isNull(this.point1) || isNull(this.point2) || !isNumber(this.len)) {
  127. return undefined;
  128. }
  129. if (this.len <= 0.0 || this.len > 1.0e6) {
  130. if (!preview) {
  131. this.error = qsTr("Invalid length");
  132. }
  133. return undefined;
  134. }
  135. if (this.point1.equalsFuzzy(this.point2)) {
  136. if (!preview) {
  137. this.error = qsTr("The two points are identical");
  138. }
  139. return undefined;
  140. }
  141. var chordlen = this.point1.getDistanceTo(this.point2);
  142. var arclen = this.len;
  143. if (chordlen >= arclen) {
  144. if (!preview) {
  145. this.error = qsTr("The distance between the two points must be less than the arc length");
  146. }
  147. return undefined;
  148. }
  149. var ang = this.arcChordAngle(arclen, chordlen);
  150. this.radius = chordlen/(2.0 * (Math.sin(ang/2.0)));
  151. var circle1 = new RCircle(this.point1, this.radius);
  152. var circle2 = new RCircle(this.point2, this.radius);
  153. var ips = circle1.getIntersectionPoints(circle2, false);
  154. if (ips.length===2) {
  155. var ipRight, ipLeft;
  156. var line = new RLine(this.point1, this.point2);
  157. if (line.getSideOfPoint(ips[0]) === RS.RightHand) {
  158. ipRight = ips[0];
  159. ipLeft = ips[1];
  160. }
  161. else {
  162. ipLeft = ips[0];
  163. ipRight = ips[1];
  164. }
  165. if (this.reversed === false) {
  166. if (rad2deg(ang) < 180) {
  167. this.center = ipLeft;
  168. } else {
  169. this.center = ipRight;
  170. }
  171. } else {
  172. if (rad2deg(ang) < 180) {
  173. this.center = ipRight
  174. } else {
  175. this.center = ipLeft;
  176. }
  177. }
  178. } else {
  179. // center in the middle:
  180. var v = new RVector();
  181. v.setPolar(this.radius, this.point1.getAngleTo(this.point2));
  182. this.center = this.point1.operator_add(v);
  183. this.point2 = this.point1.operator_add(v.operator_multiply(2.0));
  184. }
  185. var angle1 = this.center.getAngleTo(this.point1);
  186. var angle2 = this.center.getAngleTo(this.point2);
  187. return new RArc(this.center, this.radius, angle1, angle2, this.reversed);
  188. };
  189. Arc2PL.prototype.slotLengthChanged = function(value) {
  190. this.len = value;
  191. this.updatePreview(true);
  192. };
  193. Arc2PL.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. Arc2PL.prototype.getAuxPreview = function() {
  206. if (isNull(this.center) || isNull(this.point1) || isNull(this.point2)) {
  207. return undefined;
  208. }
  209. var ret = [];
  210. if (this.state==Arc2PL.State.SettingPoint2) {
  211. ret.push(new RLine(this.center, this.point1));
  212. ret.push(new RLine(this.center, this.point2));
  213. }
  214. return ret;
  215. };
  216. // ArcChordAngle (Newton method) by _gile
  217. // Returns the arc angle according to its chord and length
  218. // Converted from autolisp to ecmascript by Robert S.
  219. Arc2PL.prototype.arcChordAngle = function (arclen, chordlen) {
  220. var k = chordlen/arclen;
  221. var x = Math.sqrt(6 - (6 * k));
  222. for (var i = 0; i < 6; i++) {
  223. x = x - ((Math.sin(x) - (k * x)) / (Math.cos(x) - k));
  224. }
  225. return (2 * x);
  226. };