Cross.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * Copyright (c) 2013 by Robert S.. 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/EAction.js");
  20. include("scripts/ShapeAlgorithms.js");
  21. /**
  22. * \class Cross
  23. * \brief Draw cross at the center of an arc, circle or ellipse.
  24. * \ingroup ecma_draw_line
  25. */
  26. function Cross(guiAction) {
  27. EAction.call(this, guiAction);
  28. this.shape = undefined;
  29. this.setUiOptions("Cross.ui");
  30. }
  31. Cross.prototype = new EAction();
  32. Cross.State = {
  33. ChoosingShape : 0
  34. };
  35. Cross.prototype.beginEvent = function() {
  36. EAction.prototype.beginEvent.call(this);
  37. this.setState(Cross.State.ChoosingShape);
  38. };
  39. Cross.prototype.setState = function(state) {
  40. EAction.prototype.setState.call(this, state);
  41. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  42. this.setCrosshairCursor();
  43. var appWin = RMainWindowQt.getMainWindow();
  44. this.shape = undefined;
  45. var trEntity = qsTr("Select arc, circle, ellipse or elliptical arc");
  46. this.setCommandPrompt(trEntity);
  47. this.setLeftMouseTip(trEntity);
  48. this.setRightMouseTip(EAction.trCancel);
  49. EAction.showSnapTools();
  50. };
  51. Cross.prototype.pickEntity = function(event, preview) {
  52. var doc = this.getDocument();
  53. var di = this.getDocumentInterface();
  54. var entityId = this.getEntityId(event, preview);
  55. var entity = doc.queryEntity(entityId);
  56. var pos = event.getModelPosition();
  57. if (isNull(entity)) {
  58. return;
  59. }
  60. var shape = entity.getClosestSimpleShape(pos);
  61. if (!isArcShape(shape) &&
  62. !isCircleShape(shape) &&
  63. !isEllipseShape(shape) &&
  64. !isEllipseArcShape(shape)) {
  65. if (!preview) {
  66. EAction.warnNotArcCircleEllipse();
  67. }
  68. return;
  69. }
  70. else {
  71. this.shape = shape;
  72. }
  73. if (preview) {
  74. this.updatePreview();
  75. }
  76. else {
  77. this.applyOperation();
  78. }
  79. };
  80. Cross.prototype.getOperation = function() {
  81. var lengthX, lengthY;
  82. var ellipseangle = 0.0;
  83. var cp = this.shape.getCenter();
  84. var ex = objectFromPath("MainWindow::Options::ExtendX");
  85. var ey = objectFromPath("MainWindow::Options::ExtendY");
  86. var mode = objectFromPath("MainWindow::Options::Modes");
  87. var or = objectFromPath("MainWindow::Options::Orientation");
  88. var orient = or.getValue();
  89. if (mode.currentIndex === 0) { //Extend
  90. if (isArcShape(this.shape) || isCircleShape(this.shape)) {
  91. lengthX = this.shape.getRadius() + RMath.eval(ex.text);
  92. if (ey.text === "") {
  93. lengthY = lengthX;
  94. } else {
  95. lengthY = this.shape.getRadius() + RMath.eval(ey.text);
  96. }
  97. ellipseangle = 0.0;
  98. } else if (isEllipseShape(this.shape) || isEllipseArcShape(this.shape)) {
  99. lengthX = this.shape.getMajorRadius() + RMath.eval(ex.text);
  100. if (ey.text === "") {
  101. lengthY = this.shape.getMinorRadius() + RMath.eval(ex.text);
  102. } else {
  103. lengthY = this.shape.getMinorRadius() + RMath.eval(ey.text);
  104. }
  105. ellipseangle = this.shape.getAngle();
  106. }
  107. } else if (mode.currentIndex === 1) { //Length
  108. // divide length by 2 because cp.operator_add(v) (see below)
  109. // adds the length to both ends of the line.
  110. lengthX = RMath.eval(ex.text) / 2;
  111. if (ey.text === "") {
  112. lengthY = lengthX;
  113. } else {
  114. lengthY = RMath.eval(ey.text) / 2;
  115. }
  116. if (isEllipseShape(this.shape) || isEllipseArcShape(this.shape)) {
  117. ellipseangle = this.shape.getAngle();
  118. } else {
  119. ellipseangle = 0.0;
  120. }
  121. } else if (mode.currentIndex === 2) { //Percent
  122. if (isArcShape(this.shape) || isCircleShape(this.shape)) {
  123. lengthX = this.shape.getRadius() * RMath.eval(ex.text) / 100;
  124. if (ey.text === "") {
  125. lengthY = lengthX;
  126. } else {
  127. lengthY = this.shape.getRadius() * RMath.eval(ey.text) / 100;
  128. }
  129. ellipseangle = 0.0;
  130. } else if (isEllipseShape(this.shape) || isEllipseArcShape(this.shape)) {
  131. lengthX = this.shape.getMajorRadius() * RMath.eval(ex.text) / 100;
  132. if (ey.text === "") {
  133. lengthY = this.shape.getMinorRadius() * RMath.eval(ex.text) / 100;
  134. } else {
  135. lengthY = this.shape.getMinorRadius() * RMath.eval(ey.text) / 100;
  136. }
  137. ellipseangle = this.shape.getAngle();
  138. }
  139. }
  140. var v = new RVector();
  141. if (lengthX <= 0.0) {
  142. lengthX = 0.0;
  143. var point1 = cp;
  144. var point2 = cp
  145. } else {
  146. v.setPolar(lengthX, orient + Math.PI + ellipseangle);
  147. point1 = cp.operator_add(v);
  148. v.setPolar(lengthX, orient + ellipseangle);
  149. point2 = cp.operator_add(v);
  150. }
  151. if (lengthY <= 0.0) {
  152. lengthY = 0.0;
  153. var point3 = cp;
  154. var point4 = cp;
  155. } else {
  156. v.setPolar(lengthY, orient - Math.PI/2 + ellipseangle);
  157. point3 = cp.operator_add(v);
  158. v.setPolar(lengthY, orient + Math.PI/2 + ellipseangle);
  159. point4 = cp.operator_add(v);
  160. }
  161. var op = new RAddObjectsOperation();
  162. var empty = true;
  163. // draw horizontal line from point1 to point2
  164. if (!point1.equalsFuzzy(point2)) {
  165. var line = new RLineEntity(
  166. this.getDocument(),
  167. new RLineData(point1, point2)
  168. )
  169. op.addObject(line);
  170. empty = false;
  171. }
  172. // draw vertical line from point3 to point4
  173. if (!point3.equalsFuzzy(point4)) {
  174. line = new RLineEntity(
  175. this.getDocument(),
  176. new RLineData(point3, point4)
  177. )
  178. op.addObject(line);
  179. empty = false;
  180. }
  181. if (!empty) {
  182. return op;
  183. }
  184. };