ShapePolygonAFCP.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Modifications Copyright (c) 2015 by Robert S.
  3. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  4. *
  5. * This file is part of the QCAD project.
  6. *
  7. * QCAD is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * QCAD is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with QCAD.
  19. */
  20. include("../Shape.js");
  21. /**
  22. * \class ShapePolygonAFCP
  23. * \brief Polygon with center, center point of flat edge and number of edges.
  24. * \ingroup ecma_draw_shape
  25. */
  26. function ShapePolygonAFCP(guiAction) {
  27. Shape.call(this, guiAction);
  28. this.center = undefined;
  29. this.middleOfSide = undefined;
  30. this.setUiOptions(["../Shape.ui", "ShapePolygonAFCP.ui"]);
  31. }
  32. ShapePolygonAFCP.prototype = new Shape();
  33. ShapePolygonAFCP.State = {
  34. SettingCenter : 0,
  35. SettingCorner : 1
  36. };
  37. ShapePolygonAFCP.prototype.beginEvent = function() {
  38. Shape.prototype.beginEvent.call(this);
  39. this.setState(ShapePolygonAFCP.State.SettingCenter);
  40. };
  41. ShapePolygonAFCP.prototype.setState = function(state) {
  42. Shape.prototype.setState.call(this, state);
  43. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  44. this.setCrosshairCursor();
  45. var appWin = RMainWindowQt.getMainWindow();
  46. switch (this.state) {
  47. case ShapePolygonAFCP.State.SettingCenter:
  48. this.center = undefined;
  49. this.middleOfSide = undefined;
  50. var trCenter = qsTr("Center");
  51. this.setCommandPrompt(trCenter);
  52. this.setLeftMouseTip(trCenter);
  53. this.setRightMouseTip(EAction.trCancel);
  54. break;
  55. case ShapePolygonAFCP.State.SettingCorner:
  56. this.middleOfSide = undefined;
  57. var trMiddlePoint = qsTr("Middle of Side");
  58. this.setCommandPrompt(trMiddlePoint);
  59. this.setLeftMouseTip(trMiddlePoint);
  60. this.setRightMouseTip(EAction.trDone);
  61. break;
  62. }
  63. EAction.showSnapTools();
  64. };
  65. ShapePolygonAFCP.prototype.escapeEvent = function() {
  66. switch (this.state) {
  67. case ShapePolygonAFCP.State.SettingCenter:
  68. EAction.prototype.escapeEvent.call(this);
  69. break;
  70. case ShapePolygonAFCP.State.SettingCorner:
  71. this.setState(ShapePolygonAFCP.State.SettingCenter);
  72. break;
  73. }
  74. };
  75. ShapePolygonAFCP.prototype.pickCoordinate = function(event, preview) {
  76. var di = this.getDocumentInterface();
  77. switch (this.state) {
  78. case ShapePolygonAFCP.State.SettingCenter:
  79. if (!preview) {
  80. this.center = event.getModelPosition();
  81. di.setRelativeZero(this.center);
  82. //di.repaintViews();
  83. this.setState(ShapePolygonAFCP.State.SettingCorner);
  84. }
  85. break;
  86. case ShapePolygonAFCP.State.SettingCorner:
  87. this.middleOfSide = event.getModelPosition();
  88. if (preview) {
  89. this.updatePreview();
  90. }
  91. else {
  92. var op = this.getOperation(false);
  93. if (!isNull(op)) {
  94. di.applyOperation(op);
  95. di.setRelativeZero(this.middleOfSide);
  96. this.setState(ShapePolygonAFCP.State.SettingCenter);
  97. }
  98. }
  99. break;
  100. }
  101. };
  102. ShapePolygonAFCP.prototype.getOperation = function(preview) {
  103. if (isNull(this.center) || isNull(this.middleOfSide)) {
  104. return undefined;
  105. }
  106. var corners = [];
  107. var angle = Math.PI/this.numberOfCorners;
  108. var dist = this.center.getDistanceTo(this.middleOfSide);
  109. var opp = Math.tan(angle)*dist;
  110. var hyp = Math.sqrt((dist * dist) + (opp * opp));
  111. var v = RVector.createPolar(hyp, this.center.getAngleTo(this.middleOfSide) + angle);
  112. var corner = this.center.operator_add(v);
  113. for (var n=1; n<=this.numberOfCorners; ++n) {
  114. var c = corner.copy();
  115. c.rotate((Math.PI*2.0)/this.numberOfCorners*n, this.center);
  116. corners.push(c);
  117. }
  118. var op = new RAddObjectsOperation();
  119. op.setText(this.getToolTitle());
  120. var shapes = this.getShapes(corners);
  121. for (var i=0; i<shapes.length; ++i) {
  122. var e = shapeToEntity(this.getDocument(), shapes[i]);
  123. if (isNull(e)) {
  124. continue;
  125. }
  126. op.addObject(e);
  127. }
  128. this.complementOperation(op, shapes);
  129. return op;
  130. };
  131. ShapePolygonAFCP.prototype.getAuxPreview = function() {
  132. if (isNull(this.center) || isNull(this.middleOfSide)) {
  133. return undefined;
  134. }
  135. return [ new RCircle(this.center, this.center.getDistanceTo(this.middleOfSide)) ];
  136. };
  137. ShapePolygonAFCP.prototype.slotCornersChanged = function(value) {
  138. this.numberOfCorners = value;
  139. this.updatePreview(true);
  140. };