DimAngular.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 DimAngular
  22. * \brief Draw angular dimension.
  23. * The first entity is a line or arc. If the first entity is a line,
  24. * a second entity has to be chosen which is also a line.
  25. * If using max angle, this action creates a 3 point angular dimension.
  26. * \ingroup ecma_draw_dimension
  27. */
  28. function DimAngular(guiAction) {
  29. Dimension.call(this, guiAction);
  30. // for common dimension data (set by options toolbar in Dimension class)
  31. this.data = new RDimensionData();
  32. //this.data2L = new RDimAngular2LData();
  33. //this.data3P = new RDimAngular3PData();
  34. this.dimArcPosition = RVector.invalid;
  35. this.firstEntity = undefined;
  36. this.firstShape = undefined;
  37. this.secondEntity = undefined;
  38. this.secondShape = undefined;
  39. this.useMaxAngle = false;
  40. this.setUiOptions(["../Dimension.ui", "DimAngular.ui"], false);
  41. }
  42. DimAngular.prototype = new Dimension();
  43. DimAngular.State = {
  44. SettingFirstEntity : 0,
  45. SettingSecondEntity : 1,
  46. SettingDimPos : 2
  47. };
  48. DimAngular.prototype.beginEvent = function() {
  49. Dimension.prototype.beginEvent.call(this);
  50. this.setState(DimAngular.State.SettingFirstEntity);
  51. };
  52. DimAngular.prototype.setState = function(state) {
  53. Dimension.prototype.setState.call(this, state);
  54. this.setCrosshairCursor();
  55. var appWin = RMainWindowQt.getMainWindow();
  56. switch (this.state) {
  57. case DimAngular.State.SettingFirstEntity:
  58. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  59. // this.data2L.setExtensionLine1Start(RVector.invalid);
  60. // this.data2L.setExtensionLine1End(RVector.invalid);
  61. // this.data2L.setExtensionLine2Start(RVector.invalid);
  62. // this.data2L.setExtensionLine2End(RVector.invalid);
  63. // this.data2L.setDimArcPosition(RVector.invalid);
  64. this.firstEntity = undefined;
  65. this.firstShape = undefined;
  66. this.secondEntity = undefined;
  67. this.secondShape = undefined;
  68. this.dimArcPosition = RVector.invalid;
  69. var trFirstEntity = qsTr("Arc or first of two lines");
  70. this.setLeftMouseTip(trFirstEntity);
  71. this.setCommandPrompt(trFirstEntity);
  72. this.setRightMouseTip(EAction.trCancel);
  73. break;
  74. case DimAngular.State.SettingSecondEntity:
  75. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  76. // this.data2L.setExtensionLine2Start(RVector.invalid);
  77. // this.data2L.setExtensionLine2End(RVector.invalid);
  78. // this.data2L.setDimArcPosition(RVector.invalid);
  79. this.secondEntity = undefined;
  80. this.secondShape = undefined;
  81. this.dimArcPosition = RVector.invalid;
  82. var trSecondEntity = qsTr("Second line");
  83. this.setLeftMouseTip(trSecondEntity);
  84. this.setCommandPrompt(trSecondEntity);
  85. this.setRightMouseTip(EAction.trBack);
  86. break;
  87. case DimAngular.State.SettingDimPos:
  88. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  89. //this.data2L.setDefinitionPoint(RVector.invalid);
  90. var trDimPos = qsTr("Dimension arc location");
  91. this.setCommandPrompt(trDimPos);
  92. this.setLeftMouseTip(trDimPos);
  93. this.setRightMouseTip(EAction.trBack);
  94. break;
  95. }
  96. EAction.showSnapTools();
  97. };
  98. DimAngular.prototype.escapeEvent = function() {
  99. switch (this.state) {
  100. case DimAngular.State.SettingFirstEntity:
  101. EAction.prototype.escapeEvent.call(this);
  102. break;
  103. case DimAngular.State.SettingSecondEntity:
  104. this.setState(DimAngular.State.SettingFirstEntity);
  105. break;
  106. case DimAngular.State.SettingDimPos:
  107. if (isArcShape(this.firstShape)) {
  108. this.setState(DimAngular.State.SettingFirstEntity);
  109. }
  110. else {
  111. this.setState(DimAngular.State.SettingSecondEntity);
  112. }
  113. break;
  114. }
  115. };
  116. DimAngular.prototype.initUiOptions = function(resume, optionsToolBar) {
  117. Dimension.prototype.initUiOptions.call(this, resume, optionsToolBar);
  118. var widgetNames = [ "ScaleAction", "ScaleLabelAction", "ScaleSeparatorAction", "AutoScaleAction" ];
  119. for (var i=0; i<widgetNames.length; i++) {
  120. var w = optionsToolBar.findChild(widgetNames[i]);
  121. if (!isNull(w)) {
  122. w.visible = false;
  123. }
  124. }
  125. };
  126. DimAngular.prototype.pickEntity = function(event, preview) {
  127. var shape, appWin;
  128. var di = this.getDocumentInterface();
  129. var doc = this.getDocument();
  130. var entityId = this.getEntityId(event, preview);
  131. var entity = doc.queryEntity(entityId);
  132. var pos = event.getModelPosition();
  133. if (isNull(entity)) {
  134. return;
  135. }
  136. switch (this.state) {
  137. case DimAngular.State.SettingFirstEntity:
  138. shape = entity.getClosestSimpleShape(pos);
  139. if (isLineBasedShape(shape)) {
  140. this.firstEntity = entity;
  141. this.firstShape = shape;
  142. if (preview) {
  143. this.updatePreview();
  144. }
  145. else {
  146. this.setState(DimAngular.State.SettingSecondEntity);
  147. }
  148. }
  149. else if (isArcShape(shape)) {
  150. this.firstEntity = entity;
  151. this.firstShape = shape;
  152. if (preview) {
  153. this.updatePreview();
  154. }
  155. else {
  156. this.setState(DimAngular.State.SettingDimPos);
  157. }
  158. } else {
  159. if (!preview) {
  160. EAction.warnNotLineOrArc();
  161. break;
  162. }
  163. }
  164. break;
  165. case DimAngular.State.SettingSecondEntity:
  166. shape = entity.getClosestSimpleShape(pos);
  167. if (isLineBasedShape(shape)) {
  168. this.secondEntity = entity;
  169. this.secondShape = shape;
  170. if (preview) {
  171. this.updatePreview();
  172. }
  173. else {
  174. this.setState(DimAngular.State.SettingDimPos);
  175. }
  176. }
  177. else {
  178. if (!preview) {
  179. EAction.warnNotLine();
  180. break;
  181. }
  182. }
  183. break;
  184. }
  185. };
  186. DimAngular.prototype.pickCoordinate = function(event, preview) {
  187. var di = this.getDocumentInterface();
  188. switch (this.state) {
  189. case DimAngular.State.SettingDimPos:
  190. this.dimArcPosition = event.getModelPosition();
  191. if (preview) {
  192. this.updatePreview();
  193. }
  194. else {
  195. var op = this.getOperation(false);
  196. if (!isNull(op)) {
  197. di.applyOperation(op);
  198. this.setState(DimAngular.State.SettingFirstEntity);
  199. }
  200. }
  201. break;
  202. }
  203. };
  204. DimAngular.prototype.getEntity = function(preview) {
  205. if (!isLineBasedShape(this.firstShape) && !isArcShape(this.firstShape)) {
  206. return undefined;
  207. }
  208. if (isLineBasedShape(this.firstShape) && !isLineBasedShape(this.secondShape)) {
  209. return undefined;
  210. }
  211. var di = this.getDocumentInterface();
  212. if (isLineBasedShape(this.firstShape)) {
  213. var intersections = this.firstShape.getIntersectionPoints(getPtr(this.secondShape), false);
  214. if (intersections.length!==1) {
  215. return undefined;
  216. }
  217. var center = intersections[0];
  218. if (this.useMaxAngle) {
  219. this.data3P = new RDimAngular3PData(this.data, RVector.invalid, RVector.invalid, RVector.invalid);
  220. this.data3P.setDimArcPosition(this.dimArcPosition);
  221. this.data3P.setCenter(center);
  222. // this.data3P.setText(this.data.getText());
  223. // this.data3P.setUpperTolerance();
  224. // this.data3P.setDimArcPosition(this.data2L.getDimArcPosition());
  225. if (center.getDistanceTo(this.firstShape.getStartPoint()) <
  226. center.getDistanceTo(this.firstShape.getEndPoint())) {
  227. this.data3P.setExtensionLine1End(this.firstShape.getEndPoint());
  228. }
  229. else {
  230. this.data3P.setExtensionLine1End(this.firstShape.getStartPoint());
  231. }
  232. if (center.getDistanceTo(this.secondShape.getStartPoint()) <
  233. center.getDistanceTo(this.secondShape.getEndPoint())) {
  234. this.data3P.setExtensionLine2End(this.secondShape.getEndPoint());
  235. }
  236. else {
  237. this.data3P.setExtensionLine2End(this.secondShape.getStartPoint());
  238. }
  239. }
  240. else {
  241. this.data2L = new RDimAngular2LData(this.data, RVector.invalid, RVector.invalid, RVector.invalid, RVector.invalid);
  242. this.data2L.setDimArcPosition(this.dimArcPosition);
  243. if (center.getDistanceTo(this.firstShape.getStartPoint()) <
  244. center.getDistanceTo(this.firstShape.getEndPoint())) {
  245. this.data2L.setExtensionLine1Start(this.firstShape.getEndPoint());
  246. this.data2L.setExtensionLine1End(this.firstShape.getStartPoint());
  247. } else {
  248. this.data2L.setExtensionLine1Start(this.firstShape.getStartPoint());
  249. this.data2L.setExtensionLine1End(this.firstShape.getEndPoint());
  250. }
  251. if (center.getDistanceTo(this.secondShape.getStartPoint()) <
  252. center.getDistanceTo(this.secondShape.getEndPoint())) {
  253. this.data2L.setExtensionLine2Start(this.secondShape.getStartPoint());
  254. this.data2L.setExtensionLine2End(this.secondShape.getEndPoint());
  255. } else {
  256. this.data2L.setExtensionLine2Start(this.secondShape.getEndPoint());
  257. this.data2L.setExtensionLine2End(this.secondShape.getStartPoint());
  258. }
  259. if (!preview) {
  260. di.setRelativeZero(center);
  261. }
  262. }
  263. if (!preview) {
  264. di.setRelativeZero(center);
  265. }
  266. }
  267. if (isArcShape(this.firstShape)) {
  268. if (this.useMaxAngle) {
  269. this.data3P = new RDimAngular3PData(this.data, RVector.invalid, RVector.invalid, RVector.invalid);
  270. this.data3P.setDimArcPosition(this.dimArcPosition);
  271. this.data3P.setCenter(this.firstShape.getCenter());
  272. this.data3P.setExtensionLine1End(this.firstShape.getStartPoint());
  273. this.data3P.setExtensionLine2End(this.firstShape.getEndPoint());
  274. }
  275. else {
  276. this.data2L = new RDimAngular2LData(this.data, RVector.invalid, RVector.invalid, RVector.invalid, RVector.invalid);
  277. this.data2L.setDimArcPosition(this.dimArcPosition);
  278. this.data2L.setExtensionLine1Start(this.firstShape.getCenter());
  279. this.data2L.setExtensionLine1End(this.firstShape.getStartPoint());
  280. this.data2L.setExtensionLine2Start(this.firstShape.getCenter());
  281. this.data2L.setExtensionLine2End(this.firstShape.getEndPoint());
  282. }
  283. }
  284. var doc = this.getDocument();
  285. var entity;
  286. if (this.useMaxAngle) {
  287. if (isNull(this.data3P) || !this.data3P.isValid()) {
  288. return undefined;
  289. }
  290. entity = new RDimAngular3PEntity(doc, this.data3P);
  291. }
  292. else {
  293. if (isNull(this.data2L) || !this.data2L.isValid()) {
  294. return undefined;
  295. }
  296. entity = new RDimAngular2LEntity(doc, this.data2L);
  297. }
  298. if (!isEntity(entity)) {
  299. return undefined;
  300. }
  301. this.initEntity(entity, preview);
  302. return entity;
  303. };
  304. /**
  305. * Can be overwritten to initialize the added entity.
  306. */
  307. DimAngular.prototype.initEntity = function(entity, preview) {
  308. };
  309. DimAngular.prototype.getOperation = function(preview) {
  310. var entity = this.getEntity(preview);
  311. if (isNull(entity)) {
  312. return undefined;
  313. }
  314. return new RAddObjectOperation(entity, this.getToolTitle());
  315. };
  316. DimAngular.prototype.getHighlightedEntities = function() {
  317. var ret = [];
  318. if (isEntity(this.firstEntity)) {
  319. ret.push(this.firstEntity.getId());
  320. }
  321. if (isEntity(this.secondEntity)) {
  322. ret.push(this.secondEntity.getId());
  323. }
  324. return ret;
  325. };
  326. DimAngular.prototype.slotUseMaxAngleChanged = function(v) {
  327. this.useMaxAngle = v;
  328. };