PointPole.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/EAction.js");
  20. include("scripts/Apollonius.js");
  21. include("scripts/ShapeAlgorithms.js");
  22. /**
  23. * \class PointPole
  24. * \ingroup ecma_misc_draw
  25. *
  26. * \brief Point at the power center of three chosen circles.
  27. */
  28. function PointPole(guiAction) {
  29. EAction.call(this, guiAction);
  30. this.entity1 = undefined;
  31. this.shape1 = undefined;
  32. this.pos1 = undefined;
  33. this.entity2 = undefined;
  34. this.shape2 = undefined;
  35. this.pos2 = undefined;
  36. }
  37. PointPole.prototype = new EAction();
  38. PointPole.State = {
  39. ChoosingShape1 : 0,
  40. ChoosingShape2 : 1
  41. };
  42. PointPole.prototype.beginEvent = function() {
  43. EAction.prototype.beginEvent.call(this);
  44. this.setState(PointPole.State.ChoosingShape1);
  45. };
  46. PointPole.prototype.setState = function(state) {
  47. EAction.prototype.setState.call(this, state);
  48. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  49. this.setCrosshairCursor();
  50. var appWin = RMainWindowQt.getMainWindow();
  51. switch (this.state) {
  52. case PointPole.State.ChoosingShape1:
  53. this.entity1 = undefined;
  54. this.shape1 = undefined;
  55. this.entity2 = undefined;
  56. this.shape2 = undefined;
  57. var trFirstEntity = qsTr("Arc or circle");
  58. this.setCommandPrompt(trFirstEntity);
  59. this.setLeftMouseTip(trFirstEntity);
  60. this.setRightMouseTip(EAction.trCancel);
  61. break;
  62. case PointPole.State.ChoosingShape2:
  63. this.entity2 = undefined;
  64. this.shape2 = undefined;
  65. var trSecondEntity = qsTr("Line");
  66. this.setCommandPrompt(trSecondEntity);
  67. this.setLeftMouseTip(trSecondEntity);
  68. this.setRightMouseTip(EAction.trBack);
  69. break;
  70. }
  71. };
  72. PointPole.prototype.escapeEvent = function() {
  73. switch (this.state) {
  74. case PointPole.State.ChoosingShape1:
  75. EAction.prototype.escapeEvent.call(this);
  76. break;
  77. case PointPole.State.ChoosingShape2:
  78. this.setState(PointPole.State.ChoosingShape1);
  79. break;
  80. }
  81. };
  82. PointPole.prototype.pickEntity = function(event, preview) {
  83. this.error = "";
  84. var di = this.getDocumentInterface();
  85. var doc = this.getDocument();
  86. var entityId = this.getEntityId(event, preview);
  87. var entity = doc.queryEntity(entityId);
  88. var pos = event.getModelPosition();
  89. if (isNull(entity)) {
  90. return;
  91. }
  92. var shape = entity.getClosestSimpleShape(pos);
  93. switch (this.state) {
  94. case PointPole.State.ChoosingShape1:
  95. if (!isArcShape(shape) &&
  96. !isCircleShape(shape)) {
  97. if (!preview) {
  98. EAction.warnNotArcCircle();
  99. }
  100. return;
  101. }
  102. this.entity1 = entity;
  103. this.shape1 = shape;
  104. this.pos1 = pos;
  105. if (preview) {
  106. this.updatePreview();
  107. }
  108. else {
  109. this.setState(PointPole.State.ChoosingShape2);
  110. }
  111. break;
  112. case PointPole.State.ChoosingShape2:
  113. if (!isLineBasedShape(shape)) {
  114. if (!preview) {
  115. EAction.warnNotLine();
  116. }
  117. return;
  118. }
  119. this.entity2 = entity;
  120. this.shape2 = shape;
  121. this.pos2 = pos;
  122. if (preview) {
  123. this.updatePreview();
  124. }
  125. else {
  126. var op = this.getOperation(false);
  127. if (!isNull(op)) {
  128. di.applyOperation(op);
  129. this.setState(PointPole.State.ChoosingShape1);
  130. }
  131. }
  132. break;
  133. }
  134. if (!preview && this.error.length!==0) {
  135. EAction.handleUserWarning(this.error);
  136. }
  137. };
  138. PointPole.prototype.getOperation = function(preview) {
  139. var shape = this.getPointPole(preview);
  140. if (isNull(shape)) {
  141. return undefined;
  142. }
  143. var doc = this.getDocument();
  144. var entity = shapeToEntity(doc, shape);
  145. if (!isEntity(entity)) {
  146. return undefined;
  147. }
  148. return new RAddObjectOperation(entity, this.getToolTitle());
  149. };
  150. PointPole.prototype.getPointPole = function(preview) {
  151. if (isNull(this.shape1) || isNull(this.shape2)) {
  152. return undefined;
  153. }
  154. var pole = Apollonius.getPole(getPtr(this.shape1), getPtr(this.shape2));
  155. if (isNull(pole)) {
  156. return undefined;
  157. }
  158. return new RPoint(pole);
  159. };
  160. PointPole.prototype.getHighlightedEntities = function() {
  161. var ret = [];
  162. if (isEntity(this.entity1)) {
  163. ret.push(this.entity1.getId());
  164. }
  165. if (isEntity(this.entity2)) {
  166. ret.push(this.entity2.getId());
  167. }
  168. return ret;
  169. };