DimRadial.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. function DimRadial(guiAction) {
  21. Dimension.call(this, guiAction);
  22. this.data = new RDimRadialData();
  23. this.setUiOptions("../Dimension.ui");
  24. this.entity = undefined;
  25. this.shape = undefined;
  26. this.pos = undefined;
  27. }
  28. DimRadial.prototype = new Dimension();
  29. DimRadial.State = {
  30. SettingEntity : 0,
  31. SettingDimPos : 1
  32. };
  33. DimRadial.prototype.beginEvent = function() {
  34. Dimension.prototype.beginEvent.call(this);
  35. this.setState(DimRadial.State.SettingEntity);
  36. };
  37. DimRadial.prototype.setState = function(state) {
  38. Dimension.prototype.setState.call(this, state);
  39. this.setCrosshairCursor();
  40. var appWin = RMainWindowQt.getMainWindow();
  41. switch (this.state) {
  42. case DimRadial.State.SettingEntity:
  43. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  44. this.entity = undefined;
  45. this.shape = undefined;
  46. this.pos = undefined;
  47. var trEntity = qsTr("Choose arc or circle entity");
  48. this.setCommandPrompt(trEntity);
  49. this.setLeftMouseTip(trEntity);
  50. this.setRightMouseTip(EAction.trCancel);
  51. break;
  52. case DimRadial.State.SettingDimPos:
  53. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  54. var trDimPos = qsTr("Dimension line location or angle");
  55. this.setCommandPrompt(trDimPos);
  56. this.setLeftMouseTip(trDimPos);
  57. this.setRightMouseTip(EAction.trBack);
  58. EAction.showSnapTools();
  59. break;
  60. }
  61. };
  62. DimRadial.prototype.escapeEvent = function() {
  63. switch (this.state) {
  64. case DimRadial.State.SettingEntity:
  65. EAction.prototype.escapeEvent.call(this);
  66. break;
  67. case DimRadial.State.SettingDimPos:
  68. this.setState(DimRadial.State.SettingEntity);
  69. break;
  70. }
  71. };
  72. DimRadial.prototype.pickEntity = function(event, preview) {
  73. var doc = this.getDocument();
  74. var di = this.getDocumentInterface();
  75. var entityId = this.getEntityId(event, preview);
  76. var entity = doc.queryEntity(entityId);
  77. var pos = event.getModelPosition();
  78. if (isNull(entity)) {
  79. return;
  80. }
  81. switch (this.state) {
  82. case DimRadial.State.SettingEntity:
  83. this.autoAdjustScale(pos);
  84. var shape = entity.getClosestSimpleShape(pos);
  85. if (isArcShape(shape) ||
  86. isCircleShape(shape)) {
  87. this.entity = entity;
  88. this.shape = shape;
  89. }
  90. else {
  91. if (!preview) {
  92. EAction.warnNotArcCircle();
  93. break;
  94. }
  95. }
  96. if (preview) {
  97. this.updatePreview();
  98. }
  99. else {
  100. di.setRelativeZero(this.shape.getCenter());
  101. this.setState(DimRadial.State.SettingDimPos);
  102. }
  103. break;
  104. }
  105. };
  106. DimRadial.prototype.pickCoordinate = function(event, preview) {
  107. var di = this.getDocumentInterface();
  108. switch (this.state) {
  109. case DimRadial.State.SettingDimPos:
  110. this.pos = event.getModelPosition();
  111. if (preview) {
  112. this.updatePreview();
  113. }
  114. else {
  115. var op = this.getOperation(false);
  116. if (!isNull(op)) {
  117. di.applyOperation(op);
  118. di.setRelativeZero(event.getModelPosition());
  119. this.setState(DimRadial.State.SettingEntity);
  120. }
  121. }
  122. break;
  123. }
  124. };
  125. DimRadial.prototype.getOperation = function(preview) {
  126. if (!isValidVector(this.pos) || isNull(this.shape)) {
  127. return undefined;
  128. }
  129. var radius = this.shape.getRadius();
  130. var center = this.shape.getCenter();
  131. var angle = center.getAngleTo(this.pos);
  132. this.data.setCenter(center);
  133. this.data.setChordPoint(RVector.createPolar(radius, angle).operator_add(center));
  134. var doc = this.getDocument();
  135. var factor = this.getFactor();
  136. var scaled_data = this.data;
  137. scaled_data.setLinearFactor(factor);
  138. var entity = new RDimRadialEntity(doc, scaled_data);
  139. if (!isEntity(entity)) {
  140. return undefined;
  141. }
  142. return new RAddObjectOperation(entity, this.getToolTitle());
  143. };
  144. DimRadial.prototype.getHighlightedEntities = function() {
  145. var ret = [];
  146. if (isEntity(this.entity)) {
  147. ret.push(this.entity.getId());
  148. }
  149. return ret;
  150. };
  151. DimRadial.prototype.applyCommand = function(event, preview) {
  152. var di = this.getDocumentInterface();
  153. var cmd = event.getCommand();
  154. var pos = RMath.parseCoordinate(cmd);
  155. if (pos.isValid()) {
  156. return;
  157. }
  158. var value = RMath.eval(cmd);
  159. if (isNaN(value)) {
  160. return;
  161. }
  162. switch (this.state) {
  163. case DimRadial.State.SettingDimPos:
  164. event.accept();
  165. var radius = this.shape.getRadius();
  166. var center = this.shape.getCenter();
  167. var v = RVector.createPolar(radius, RMath.deg2rad(value));
  168. this.pos = center.operator_add(v);
  169. if (preview) {
  170. this.updatePreview(true);
  171. }
  172. else {
  173. var op = this.getOperation(false);
  174. if (!isNull(op)) {
  175. di.applyOperation(op);
  176. di.setRelativeZero(this.pos);
  177. this.setState(DimRadial.State.SettingEntity);
  178. }
  179. }
  180. break;
  181. }
  182. };