Arc3P.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 Arc3P
  23. * \brief Arc from three points.
  24. * \ingroup ecma_draw_arc
  25. */
  26. function Arc3P(guiAction) {
  27. Arc.call(this, guiAction);
  28. this.point1 = undefined;
  29. this.point2 = undefined;
  30. this.point3 = undefined;
  31. }
  32. Arc3P.prototype = new Arc();
  33. Arc3P.State = {
  34. SettingPoint1 : 0,
  35. SettingPoint2 : 1,
  36. SettingPoint3 : 2
  37. };
  38. Arc3P.prototype.beginEvent = function() {
  39. Arc.prototype.beginEvent.call(this);
  40. this.setState(Arc3P.State.SettingPoint1);
  41. };
  42. Arc3P.prototype.setState = function(state) {
  43. Arc.prototype.setState.call(this, state);
  44. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  45. this.setCrosshairCursor();
  46. var appWin = RMainWindowQt.getMainWindow();
  47. switch (this.state) {
  48. case Arc3P.State.SettingPoint1:
  49. this.point1 = undefined;
  50. this.point2 = undefined;
  51. this.point3 = undefined;
  52. var trStartPoint = qsTr("Start point");
  53. this.setCommandPrompt(trStartPoint);
  54. this.setLeftMouseTip(trStartPoint);
  55. this.setRightMouseTip(EAction.trCancel);
  56. break;
  57. case Arc3P.State.SettingPoint2:
  58. this.point3 = undefined;
  59. var trPoint = qsTr("Point on arc");
  60. this.setCommandPrompt(trPoint);
  61. this.setLeftMouseTip(trPoint);
  62. this.setRightMouseTip(EAction.trBack);
  63. break;
  64. case Arc3P.State.SettingPoint3:
  65. var trEndPoint = qsTr("End point");
  66. this.setCommandPrompt(trEndPoint);
  67. this.setLeftMouseTip(trEndPoint);
  68. this.setRightMouseTip(EAction.trBack);
  69. break;
  70. }
  71. EAction.showSnapTools();
  72. };
  73. Arc3P.prototype.escapeEvent = function() {
  74. switch (this.state) {
  75. case Arc3P.State.SettingPoint1:
  76. EAction.prototype.escapeEvent.call(this);
  77. break;
  78. case Arc3P.State.SettingPoint2:
  79. this.setState(Arc3P.State.SettingPoint1);
  80. break;
  81. case Arc3P.State.SettingPoint3:
  82. this.setState(Arc3P.State.SettingPoint2);
  83. break;
  84. }
  85. };
  86. Arc3P.prototype.pickCoordinate = function(event, preview) {
  87. this.error = "";
  88. var di = this.getDocumentInterface();
  89. switch (this.state) {
  90. case Arc3P.State.SettingPoint1:
  91. this.point1 = event.getModelPosition();
  92. if (!preview) {
  93. di.setRelativeZero(this.point1);
  94. this.setState(Arc3P.State.SettingPoint2);
  95. }
  96. break;
  97. case Arc3P.State.SettingPoint2:
  98. this.point2 = event.getModelPosition();
  99. if (preview) {
  100. this.updatePreview();
  101. }
  102. else {
  103. di.setRelativeZero(this.point2);
  104. this.setState(Arc3P.State.SettingPoint3);
  105. }
  106. break;
  107. case Arc3P.State.SettingPoint3:
  108. this.point3 = event.getModelPosition();
  109. if (preview) {
  110. this.updatePreview();
  111. }
  112. else {
  113. var op = this.getOperation(false);
  114. if (!isNull(op)) {
  115. di.applyOperation(op);
  116. di.setRelativeZero(this.point3);
  117. this.setState(Arc3P.State.SettingPoint1);
  118. }
  119. }
  120. if (!preview && this.error.length!==0) {
  121. EAction.handleUserWarning(this.error);
  122. }
  123. break;
  124. }
  125. };
  126. Arc3P.prototype.getOperation = function(preview) {
  127. var shape = ShapeAlgorithms.createArcFrom3Points(this.point1, this.point2, this.point3);
  128. if (!preview) {
  129. this.error = ShapeAlgorithms.error;
  130. }
  131. if (isNull(shape)) {
  132. return undefined;
  133. }
  134. var entity;
  135. var doc = this.getDocument();
  136. if (isArcShape(shape)) {
  137. entity = new RArcEntity(doc, new RArcData(shape));
  138. }
  139. else if (isLineShape(shape)) {
  140. entity = new RLineEntity(doc, new RLineData(shape));
  141. }
  142. if (!isEntity(entity)) {
  143. return undefined;
  144. }
  145. return new RAddObjectOperation(entity, this.getToolTitle());
  146. };