DimRotated.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/Dimension/Dimension.js");
  20. /**
  21. * \class DimRotated
  22. * \brief Draw rotated dimension (horizontal, vertical or other fixed angle).
  23. * \ingroup ecma_draw_dimension
  24. */
  25. function DimRotated(guiAction) {
  26. Dimension.call(this, guiAction);
  27. this.data = new RDimRotatedData();
  28. this.setUiOptions(["../Dimension.ui", "DimRotated.ui"], false);
  29. }
  30. DimRotated.prototype = new Dimension();
  31. DimRotated.State = {
  32. SettingPoint1 : 0,
  33. SettingPoint2 : 1,
  34. SettingDimPos : 2
  35. };
  36. DimRotated.prototype.beginEvent = function() {
  37. Dimension.prototype.beginEvent.call(this);
  38. this.setState(DimRotated.State.SettingPoint1);
  39. };
  40. DimRotated.prototype.setState = function(state) {
  41. Dimension.prototype.setState.call(this, state);
  42. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  43. this.setCrosshairCursor();
  44. var appWin = RMainWindowQt.getMainWindow();
  45. switch (this.state) {
  46. case DimRotated.State.SettingPoint1:
  47. this.data.setExtensionPoint1(RVector.invalid);
  48. this.data.setExtensionPoint2(RVector.invalid);
  49. this.data.setDefinitionPoint(RVector.invalid);
  50. var trFirstPoint = qsTr("First extension line origin");
  51. this.setCommandPrompt(trFirstPoint);
  52. this.setLeftMouseTip(trFirstPoint);
  53. this.setRightMouseTip(EAction.trCancel);
  54. break;
  55. case DimRotated.State.SettingPoint2:
  56. this.data.setExtensionPoint2(RVector.invalid);
  57. this.data.setDefinitionPoint(RVector.invalid);
  58. var trSecondPoint = qsTr("Second extension line origin");
  59. this.setCommandPrompt(trSecondPoint);
  60. this.setLeftMouseTip(trSecondPoint);
  61. this.setRightMouseTip(EAction.trBack);
  62. break;
  63. case DimRotated.State.SettingDimPos:
  64. this.data.setDefinitionPoint(RVector.invalid);
  65. var trDimPos = qsTr("Dimension line location");
  66. this.setCommandPrompt(trDimPos);
  67. this.setLeftMouseTip(trDimPos);
  68. this.setRightMouseTip(EAction.trBack);
  69. break;
  70. }
  71. EAction.showSnapTools();
  72. };
  73. DimRotated.prototype.escapeEvent = function() {
  74. switch (this.state) {
  75. case DimRotated.State.SettingPoint1:
  76. EAction.prototype.escapeEvent.call(this);
  77. break;
  78. case DimRotated.State.SettingPoint2:
  79. this.setState(DimRotated.State.SettingPoint1);
  80. break;
  81. case DimRotated.State.SettingDimPos:
  82. this.setState(DimRotated.State.SettingPoint2);
  83. break;
  84. }
  85. };
  86. DimRotated.prototype.pickCoordinate = function(event, preview) {
  87. var di = this.getDocumentInterface();
  88. var pos = event.getModelPosition();
  89. switch (this.state) {
  90. case DimRotated.State.SettingPoint1:
  91. this.autoAdjustScale(pos);
  92. this.data.setExtensionPoint1(pos);
  93. if (!preview) {
  94. di.setRelativeZero(pos);
  95. this.setState(DimRotated.State.SettingPoint2);
  96. }
  97. break;
  98. case DimRotated.State.SettingPoint2:
  99. this.data.setExtensionPoint2(pos);
  100. // set definition point (dimension line position) to something
  101. // reasonable for the preview:
  102. var angle =
  103. this.data.getExtensionPoint1().getAngleTo(this.data.getExtensionPoint2()) +
  104. Math.PI/2.0;
  105. var doc = di.getDocument();
  106. var dimStyle = doc.queryDimStyleDirect();
  107. var dimtxt = dimStyle.getDouble(RS.DIMTXT);
  108. var dimscale = dimStyle.getDouble(RS.DIMSCALE);
  109. var dp = this.data.getExtensionPoint2();
  110. dp = dp.operator_add(RVector.createPolar(dimtxt*2*dimscale, angle));
  111. this.data.setDefinitionPoint(dp);
  112. if (preview) {
  113. this.updatePreview();
  114. }
  115. else {
  116. di.setRelativeZero(pos);
  117. this.setState(DimRotated.State.SettingDimPos);
  118. }
  119. break;
  120. case DimRotated.State.SettingDimPos:
  121. this.data.setDefinitionPoint(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. di.setRelativeZero(pos);
  130. this.setState(DimRotated.State.SettingPoint1);
  131. }
  132. }
  133. break;
  134. }
  135. };
  136. DimRotated.prototype.getOperation = function(preview) {
  137. var entity = this.getEntity(preview);
  138. if (isNull(entity)) {
  139. return undefined;
  140. }
  141. return new RAddObjectOperation(entity, this.getToolTitle());
  142. };
  143. DimRotated.prototype.getEntity = function(preview) {
  144. if (!this.data.isValid()) {
  145. return undefined;
  146. }
  147. var doc = this.getDocument();
  148. var factor = this.getFactor();
  149. var scaled_data = this.data;
  150. scaled_data.setLinearFactor(factor);
  151. var entity = new RDimRotatedEntity(doc, scaled_data);
  152. if (!isEntity(entity)) {
  153. return undefined;
  154. }
  155. this.initEntity(entity, preview);
  156. return entity;
  157. };
  158. /**
  159. * Can be overwritten to initialize the added entity.
  160. */
  161. DimRotated.prototype.initEntity = function(entity, preview) {
  162. };
  163. /*
  164. DimRotated.prototype.showUiOptions = function(resume, restoreFromSettings) {
  165. Dimension.prototype.showUiOptions.call(this, resume, restoreFromSettings);
  166. };
  167. DimRotated.prototype.hideUiOptions = function(saveToSettings) {
  168. debugger;
  169. Dimension.prototype.hideUiOptions.call(this, saveToSettings);
  170. };
  171. */
  172. /**
  173. * Called when the user changes the angle in the options toolbar.
  174. */
  175. DimRotated.prototype.slotAngleChanged = function(value) {
  176. if (!isNull(this.data)) {
  177. if (isNaN(value)) {
  178. value=0.0;
  179. }
  180. this.data.setRotation(value);
  181. }
  182. };