Stretch.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/Modify/Modify.js");
  20. function Stretch(guiAction) {
  21. Modify.call(this, guiAction);
  22. this.corner1 = undefined;
  23. this.corner2 = undefined;
  24. this.referencePoint = undefined;
  25. this.targetPoint = undefined;
  26. }
  27. Stretch.prototype = new Modify();
  28. Stretch.State = {
  29. SettingCorner1 : 0,
  30. SettingCorner2 : 1,
  31. SettingReferencePoint : 2,
  32. SettingTargetPoint : 3
  33. };
  34. Stretch.prototype.beginEvent = function() {
  35. Modify.prototype.beginEvent.call(this);
  36. this.setState(Stretch.State.SettingCorner1);
  37. };
  38. Stretch.prototype.setState = function(state) {
  39. Modify.prototype.setState.call(this, state);
  40. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  41. this.setCrosshairCursor();
  42. var appWin = RMainWindowQt.getMainWindow();
  43. switch (this.state) {
  44. case Stretch.State.SettingCorner1:
  45. this.corner1 = undefined;
  46. this.corner2 = undefined;
  47. this.referencePoint = undefined;
  48. this.targetPoint = undefined;
  49. var trFirstCorner = qsTr("First corner");
  50. this.setCommandPrompt(trFirstCorner);
  51. this.setLeftMouseTip(trFirstCorner);
  52. this.setRightMouseTip(EAction.trCancel);
  53. break;
  54. case Stretch.State.SettingCorner2:
  55. this.corner2 = undefined;
  56. this.referencePoint = undefined;
  57. this.targetPoint = undefined;
  58. var trSecondCorner = qsTr("Second corner");
  59. this.setCommandPrompt(trSecondCorner);
  60. this.setLeftMouseTip(trSecondCorner);
  61. this.setRightMouseTip(EAction.trCancel);
  62. break;
  63. case Stretch.State.SettingReferencePoint:
  64. this.referencePoint = undefined;
  65. this.targetPoint = undefined;
  66. var trReferencePoint = qsTr("Reference point");
  67. this.setCommandPrompt(trReferencePoint);
  68. this.setLeftMouseTip(trReferencePoint);
  69. this.setRightMouseTip(EAction.trCancel);
  70. break;
  71. case Stretch.State.SettingTargetPoint:
  72. this.targetPoint = undefined;
  73. var trTargetPoint = qsTr("Target point");
  74. this.setCommandPrompt(trTargetPoint);
  75. this.setLeftMouseTip(trTargetPoint);
  76. this.setRightMouseTip(EAction.trBack);
  77. break;
  78. }
  79. EAction.showSnapTools();
  80. };
  81. Stretch.prototype.escapeEvent = function() {
  82. switch (this.state) {
  83. case Stretch.State.SettingCorner1:
  84. EAction.prototype.escapeEvent.call(this);
  85. break;
  86. case Stretch.State.SettingCorner2:
  87. this.setState(Stretch.State.SettingCorner1);
  88. break;
  89. case Stretch.State.SettingReferencePoint:
  90. this.setState(Stretch.State.SettingCorner2);
  91. break;
  92. case Stretch.State.SettingTargetPoint:
  93. this.setState(Stretch.State.SettingReferencePoint);
  94. break;
  95. }
  96. };
  97. Stretch.prototype.pickCoordinate = function(event, preview) {
  98. var di = this.getDocumentInterface();
  99. switch (this.state) {
  100. case Stretch.State.SettingCorner1:
  101. this.corner1 = event.getModelPosition();
  102. if (!preview) {
  103. di.setRelativeZero(this.corner1);
  104. this.setState(Stretch.State.SettingCorner2);
  105. }
  106. break;
  107. case Stretch.State.SettingCorner2:
  108. this.corner2 = event.getModelPosition();
  109. if (preview) {
  110. this.updatePreview();
  111. }
  112. else {
  113. di.setRelativeZero(this.corner2);
  114. this.setState(Stretch.State.SettingReferencePoint);
  115. }
  116. break;
  117. case Stretch.State.SettingReferencePoint:
  118. this.referencePoint = event.getModelPosition();
  119. if (preview) {
  120. this.updatePreview();
  121. }
  122. else {
  123. di.setRelativeZero(this.referencePoint);
  124. this.setState(Stretch.State.SettingTargetPoint);
  125. }
  126. break;
  127. case Stretch.State.SettingTargetPoint:
  128. this.targetPoint = event.getModelPosition();
  129. if (preview) {
  130. this.updatePreview();
  131. }
  132. else {
  133. var op = this.getOperation(false);
  134. if (!isNull(op)) {
  135. op.setText(this.getToolTitle());
  136. di.applyOperation(op);
  137. di.setRelativeZero(this.targetPoint);
  138. this.setState(Stretch.State.SettingCorner1);
  139. }
  140. }
  141. break;
  142. }
  143. };
  144. Stretch.prototype.getOperation = function(preview) {
  145. if (!isValidVector(this.corner1) ||
  146. !isValidVector(this.corner2) ||
  147. !isValidVector(this.referencePoint) ||
  148. !isValidVector(this.targetPoint)) {
  149. return undefined;
  150. }
  151. // increase box slightly (see FS#435)
  152. var minX = Math.min(this.corner1.x, this.corner2.x);
  153. var minY = Math.min(this.corner1.y, this.corner2.y);
  154. var maxX = Math.max(this.corner1.x, this.corner2.x);
  155. var maxY = Math.max(this.corner1.y, this.corner2.y);
  156. var corners = new Array(
  157. new RVector(minX, minY),
  158. new RVector(maxX, minY),
  159. new RVector(maxX, maxY),
  160. new RVector(minX, maxY)
  161. );
  162. var polygon = new RPolyline();
  163. for (var i=0; i<4; ++i) {
  164. polygon.appendVertex(corners[i]);
  165. }
  166. polygon.setClosed(true);
  167. var offset = this.targetPoint.operator_subtract(this.referencePoint);
  168. return Stretch.getStrechOperation(this.getDocument(), polygon, preview, offset);
  169. };
  170. /**
  171. * \return Operation that stretches all entities inside polygon by the given offset.
  172. *
  173. * \param document RDocument
  174. * \param polygon RPolyline (closed polygon)
  175. * \param preview True: preview mode
  176. * \param offset RVector offset for the stretch operation
  177. * \param layerId Restrict operation to this layer (optional)
  178. * \param op Add stretch operation to existing operation (optional)
  179. */
  180. Stretch.getStrechOperation = function(document, polygon, preview, offset, layerId, op) {
  181. // limit search to bounding box of polygon:
  182. var box = polygon.getBoundingBox();
  183. // only stretch selected if there is a selection,
  184. // otherwise stretch all:
  185. // 20200121: only go by bounding box:
  186. // allows stretching of ordinate dimension origin:
  187. var entities = document.queryIntersectedEntitiesXY(box, true, false, RObject.INVALID_ID, [], document.hasSelection());
  188. //var entities = document.queryIntersectedEntitiesXY(box, false, false, RObject.INVALID_ID, [], document.hasSelection());
  189. if (entities.length===0 && !preview) {
  190. if (document.hasSelection()) {
  191. EAction.handleUserWarning(qsTr("No selected entities in given range"));
  192. }
  193. else {
  194. EAction.handleUserWarning(qsTr("No entities in given range"));
  195. }
  196. }
  197. if (isNull(op)) {
  198. op = new RAddObjectsOperation();
  199. }
  200. for (var e = 0; e < entities.length; ++e) {
  201. var entity = document.queryEntity(entities[e]);
  202. // check if the entity is editable:
  203. if (!EAction.assertEditable(entity, true)) {
  204. continue;
  205. }
  206. // check if the entity is on the given layer:
  207. if (!isNull(layerId) && entity.getLayerId()===layerId) {
  208. continue;
  209. }
  210. // check if the entity intersects with any of the polygon edges:
  211. var gotIntersection = false;
  212. if (entity.intersectsWith(polygon)) {
  213. gotIntersection = true;
  214. }
  215. // ordinate dimension might be completely inside stretch area and
  216. // still needs stretching (origin outside stretch area):
  217. if (isDimOrdinateEntity(entity)) {
  218. gotIntersection = true;
  219. }
  220. // hatch entities might be partially inside stretch area but not
  221. // intersect with stretch area as pattern might not intersect even
  222. // if boundary intersects or pattern might only contain points:
  223. if (isHatchEntity(entity)) {
  224. gotIntersection = true;
  225. }
  226. if (gotIntersection) {
  227. // stretch:
  228. if (entity.stretch(polygon, offset)) {
  229. op.addObject(entity, false);
  230. }
  231. }
  232. else {
  233. // check if the entity is completely inside the polygon.
  234. // this is the case if one point on the entity is inside the polygon
  235. // and the entity does not intersect with the polygon.
  236. var pointOnEntity = entity.getPointOnEntity();
  237. if (polygon.contains(pointOnEntity, true)) {
  238. // move entity:
  239. entity.move(offset);
  240. op.addObject(entity, false);
  241. }
  242. }
  243. }
  244. return op;
  245. };
  246. Stretch.prototype.getAuxPreview = function() {
  247. if (!isValidVector(this.corner1) || !isValidVector(this.corner2)) {
  248. return undefined;
  249. }
  250. var ret = [];
  251. var offset = undefined;
  252. if (isValidVector(this.referencePoint) && isValidVector(this.targetPoint)) {
  253. offset = this.targetPoint.operator_subtract(this.referencePoint);
  254. }
  255. var corners = new Array(
  256. new RVector(this.corner1.x, this.corner1.y),
  257. new RVector(this.corner2.x, this.corner1.y),
  258. new RVector(this.corner2.x, this.corner2.y),
  259. new RVector(this.corner1.x, this.corner2.y)
  260. );
  261. for (var i=0; i<4; ++i) {
  262. var line = new RLine(corners[i], corners[(i+1)%4]);
  263. ret.push(line);
  264. if (!isNull(offset)) {
  265. if (RSettings.getQtVersion()>=0x060000) {
  266. line = line.clone();
  267. }
  268. else {
  269. line = line.copy();
  270. }
  271. line.move(offset);
  272. ret.push(line);
  273. }
  274. }
  275. return ret;
  276. };