Bevel.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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("../ModifyCorner.js");
  20. include("scripts/ShapeAlgorithms.js");
  21. function Bevel(guiAction) {
  22. ModifyCorner.call(this, guiAction);
  23. this.distance1 = 1.0;
  24. this.distance2 = 1.0;
  25. this.setUiOptions("Bevel.ui");
  26. }
  27. Bevel.prototype = new ModifyCorner();
  28. Bevel.prototype.beginEvent = function() {
  29. ModifyCorner.prototype.beginEvent.call(this);
  30. };
  31. Bevel.prototype.getOperation = function(preview) {
  32. if (!isEntity(this.entity1) || !isEntity(this.entity2)) {
  33. return undefined;
  34. }
  35. var op = new RAddObjectsOperation();
  36. op.setText(this.getToolTitle());
  37. var success = Bevel.bevel(
  38. op,
  39. this.entity1.clone(), this.clickPos1,
  40. this.entity2.clone(), this.clickPos2,
  41. this.trim,
  42. this.distance1, this.distance2,
  43. preview);
  44. if (!success && !preview) {
  45. EAction.handleUserWarning(qsTr("The two entities cannot be bevelled."));
  46. }
  47. return op;
  48. };
  49. /**
  50. * Bevels or extends the given entity2 to the intersection point of the
  51. * entity2 and the limitingShape.
  52. *
  53. * \param entity1 First entity which will be bevelled.
  54. * \param clickPos1 Coordinate that was clicked when the user selected entity1.
  55. * \param entity2 Second entity which will be bevelled.
  56. * \param clickPos2 Coordinate that was clicked when the user selected entity2.
  57. * \param trim true: Trim both entities to bevel line.
  58. * \param distance1 Distance of bevel line from end point of entity1.
  59. * \param distance2 Distance of bevel line from end point of entity2.
  60. * \param preview Operation is used for preview.
  61. */
  62. Bevel.bevel = function(op, entity1, clickPos1, entity2, clickPos2, trim, distance1, distance2, preview) {
  63. if (!isEntity(entity1) || !isValidVector(clickPos1) ||
  64. !isEntity(entity2) || !isValidVector(clickPos2) ||
  65. !isBoolean(trim)) {
  66. return false;
  67. }
  68. var samePolyline = (entity1.getId()===entity2.getId() && isPolylineEntity(entity1));
  69. var shape1P = entity1.getClosestShape(clickPos1);
  70. var shape2P = entity2.getClosestShape(clickPos2);
  71. var shape1 = getPtr(shape1P);
  72. var shape2 = getPtr(shape2P);
  73. var newShapes = Bevel.bevelShapes(shape1, clickPos1, shape2, clickPos2, trim, samePolyline, distance1, distance2);
  74. if (newShapes.length===0) {
  75. return false;
  76. }
  77. // add new trimmed entities or polyline:
  78. if (trim && !modifyEntity(op, entity1, newShapes[0])) {
  79. if (!preview) {
  80. EAction.handleUserWarning(qsTr("First entity cannot be trimmed."));
  81. }
  82. }
  83. if (newShapes.length<3) {
  84. // beveling was within same polyline:
  85. return true;
  86. }
  87. if (trim && !modifyEntity(op, entity2, newShapes[2])) {
  88. if (!preview) {
  89. EAction.handleUserWarning(qsTr("Second entity cannot be trimmed."));
  90. }
  91. }
  92. if (newShapes[1].getLength()>RS.PointTolerance) {
  93. op.addObject(new RLineEntity(entity1.getDocument(), new RLineData(newShapes[1])));
  94. }
  95. return true;
  96. };
  97. Bevel.bevelShapes = function(shape1, clickPos1, shape2, clickPos2, trim, samePolyline, distance1, distance2) {
  98. if (!isShape(shape1) || !isShape(shape2)) {
  99. return [];
  100. }
  101. // convert circles to arcs:
  102. if (isCircleShape(shape1)) {
  103. shape1 = ShapeAlgorithms.circleToArc(shape1);
  104. }
  105. if (isCircleShape(shape2)) {
  106. shape2 = ShapeAlgorithms.circleToArc(shape2);
  107. }
  108. var simpleShape1 = shape1;
  109. var simpleShape2 = shape2;
  110. var i1, i2;
  111. if (isPolylineShape(shape1)) {
  112. i1 = shape1.getClosestSegment(clickPos1);
  113. if (i1===-1) {
  114. return [];
  115. }
  116. simpleShape1 = getPtr(shape1.getSegmentAt(i1));
  117. }
  118. if (isPolylineShape(shape2)) {
  119. i2 = shape2.getClosestSegment(clickPos2);
  120. if (i2===-1) {
  121. return [];
  122. }
  123. simpleShape2 = getPtr(shape2.getSegmentAt(i2));
  124. }
  125. // get intersection point(s) between two shapes:
  126. var sol = simpleShape1.getIntersectionPoints(simpleShape2, false);
  127. if (sol.length===0) {
  128. return [];
  129. }
  130. //var trimmed1 = shape1.clone();
  131. //var trimmed2 = shape2.clone();
  132. var trimmed1, trimmed2;
  133. if (samePolyline) {
  134. trimmed1 = simpleShape1.clone();
  135. trimmed2 = simpleShape2.clone();
  136. }
  137. else {
  138. trimmed1 = shape1.clone();
  139. trimmed2 = shape2.clone();
  140. }
  141. // trim shapes to intersection:
  142. var start1 = RS.FromAny;
  143. var is = clickPos1.getClosest(sol);
  144. var ending1 = RS.EndingNone;
  145. if (isFunction(trimmed1.getTrimEnd)) {
  146. ending1 = trimmed1.getTrimEnd(is, clickPos1);
  147. switch (ending1) {
  148. case RS.EndingStart:
  149. trimmed1 = trimStartPoint(trimmed1, is, clickPos1);
  150. start1 = RS.FromStart;
  151. break;
  152. case RS.EndingEnd:
  153. trimmed1 = trimEndPoint(trimmed1, is, clickPos1);
  154. if (isRayShape(trimmed1)) {
  155. start1 = RS.FromStart;
  156. ending1 = RS.EndingStart;
  157. }
  158. else {
  159. start1 = RS.FromEnd;
  160. }
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. var start2 = RS.FromAny;
  167. is = clickPos2.getClosest(sol);
  168. var ending2 = RS.EndingNone;
  169. if (isFunction(trimmed2.getTrimEnd)) {
  170. ending2 = trimmed2.getTrimEnd(is, clickPos2);
  171. switch (ending2) {
  172. case RS.EndingStart:
  173. trimmed2 = trimStartPoint(trimmed2, is, clickPos2);
  174. start2 = RS.FromStart;
  175. break;
  176. case RS.EndingEnd:
  177. trimmed2 = trimEndPoint(trimmed2, is, clickPos2);
  178. if (isRayShape(trimmed2)) {
  179. start2 = RS.FromStart;
  180. ending2 = RS.EndingStart;
  181. }
  182. else {
  183. start2 = RS.FromEnd;
  184. }
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. // find definitive bevel points:
  191. var t1 = trimmed1;
  192. if (isCircleShape(trimmed1)) {
  193. t1 = ShapeAlgorithms.circleToArc(trimmed1, t1.getCenter().getAngleTo(is));
  194. start1 = RS.FromAny;
  195. }
  196. var t2 = trimmed2;
  197. if (isCircleShape(trimmed2)) {
  198. t2 = ShapeAlgorithms.circleToArc(trimmed2, t2.getCenter().getAngleTo(is));
  199. start2 = RS.FromAny;
  200. }
  201. var bp1 = t1.getPointsWithDistanceToEnd(distance1, start1);
  202. if (bp1.length===0) {
  203. return [];
  204. }
  205. bp1 = clickPos2.getClosest(bp1);
  206. var bp2 = t2.getPointsWithDistanceToEnd(distance2, start2);
  207. if (bp2.length===0) {
  208. return [];
  209. }
  210. bp2 = clickPos2.getClosest(bp2);
  211. // final trim:
  212. if (trim || samePolyline) {
  213. switch (ending1) {
  214. case RS.EndingStart:
  215. trimmed1.trimStartPoint(bp1);
  216. break;
  217. case RS.EndingEnd:
  218. trimmed1.trimEndPoint(bp1);
  219. break;
  220. default:
  221. break;
  222. }
  223. switch (ending2) {
  224. case RS.EndingStart:
  225. trimmed2.trimStartPoint(bp2);
  226. break;
  227. case RS.EndingEnd:
  228. trimmed2.trimEndPoint(bp2);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. var bevel = new RLine(bp1, bp2);
  235. if (samePolyline) {
  236. var pl = shape1.modifyPolylineCorner(trimmed1, ending1, i1, trimmed2, ending2, i2, bevel);
  237. return [ pl ];
  238. }
  239. return [ trimmed1, bevel, trimmed2 ];
  240. };
  241. Bevel.prototype.slotLength1Changed = function(value) {
  242. this.distance1 = value;
  243. }
  244. Bevel.prototype.slotLength2Changed = function(value) {
  245. this.distance2 = value;
  246. }