BreakOutGap.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Copyright (c) 2015 by Norbert Kolb. All rights reserved.
  3. */
  4. include("scripts/Modify/Modify.js");
  5. /**
  6. * \class BreakOutGap
  7. * \brief Divide entities (lines, arcs or circles) into two parts with specified distance.
  8. * \ingroup ecma_modify
  9. */
  10. function BreakOutGap(guiAction) {
  11. Modify.call(this, guiAction);
  12. this.setUiOptions("BreakOutGap.ui");
  13. this.entity = undefined;
  14. this.shape = undefined;
  15. // user click position:
  16. this.pos = undefined;
  17. // actual cutting point:
  18. this.cutPos = undefined;
  19. // offset between cutting points
  20. this.offset = 4;
  21. }
  22. BreakOutGap.prototype = new Modify();
  23. BreakOutGap.State = {
  24. ChoosingEntity : 0,
  25. SettingPos : 1
  26. };
  27. BreakOutGap.prototype.beginEvent = function() {
  28. Modify.prototype.beginEvent.call(this);
  29. this.setState(BreakOutGap.State.ChoosingEntity);
  30. };
  31. BreakOutGap.prototype.setState = function(state) {
  32. Modify.prototype.setState.call(this, state);
  33. this.setCrosshairCursor();
  34. var tr;
  35. var appWin = RMainWindowQt.getMainWindow();
  36. switch (this.state) {
  37. case BreakOutGap.State.ChoosingEntity:
  38. this.entity = undefined;
  39. this.shape = undefined;
  40. this.pos = undefined;
  41. this.cutPos = undefined;
  42. this.getDocumentInterface().setClickMode(RAction.PickEntity);
  43. tr = qsTr("Choose line, arc or circle");
  44. this.setLeftMouseTip(tr);
  45. this.setCommandPrompt(tr);
  46. this.setRightMouseTip(EAction.trCancel);
  47. EAction.showModificationTools();
  48. break;
  49. case BreakOutGap.State.SettingPos:
  50. this.pos = undefined;
  51. this.cutPos = undefined;
  52. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  53. tr = qsTr("Specify point");
  54. this.setLeftMouseTip(tr);
  55. this.setCommandPrompt(tr);
  56. this.setRightMouseTip(EAction.trBack);
  57. EAction.showSnapTools();
  58. break;
  59. }
  60. };
  61. BreakOutGap.prototype.escapeEvent = function() {
  62. switch (this.state) {
  63. case BreakOutGap.State.ChoosingEntity:
  64. EAction.prototype.escapeEvent.call(this);
  65. break;
  66. case BreakOutGap.State.SettingPos:
  67. this.setState(BreakOutGap.State.ChoosingEntity);
  68. break;
  69. }
  70. };
  71. BreakOutGap.prototype.pickEntity = function(event, preview) {
  72. var di = this.getDocumentInterface();
  73. var doc = this.getDocument();
  74. var entityId = this.getEntityId(event, preview);
  75. var entity = doc.queryEntity(entityId);
  76. var pos = event.getModelPosition();
  77. if (isNull(entity)) {
  78. this.entity = undefined;
  79. return;
  80. }
  81. switch (this.state) {
  82. case BreakOutGap.State.ChoosingEntity:
  83. if (isLineEntity(entity) ||
  84. isArcEntity(entity) ||
  85. isCircleEntity(entity)) {
  86. this.entity = entity;
  87. this.shape = entity.getClosestShape(pos);
  88. if (preview) {
  89. this.updatePreview();
  90. } else {
  91. this.setState(BreakOutGap.State.SettingPos);
  92. }
  93. } else {
  94. if (!preview) {
  95. EAction.warnNotLineArcCircle();
  96. }
  97. }
  98. break;
  99. }
  100. };
  101. BreakOutGap.prototype.pickCoordinate = function(event, preview) {
  102. var di = this.getDocumentInterface();
  103. var op;
  104. switch (this.state) {
  105. case BreakOutGap.State.SettingPos:
  106. this.pos = event.getModelPosition();
  107. if (preview) {
  108. this.updatePreview();
  109. } else {
  110. op = this.getOperation(false);
  111. if (!isNull(op)) {
  112. di.applyOperation(op);
  113. }
  114. this.setState(BreakOutGap.State.ChoosingEntity);
  115. }
  116. break;
  117. }
  118. };
  119. BreakOutGap.prototype.getOperation = function(preview) {
  120. if (isNull(this.pos) || isNull(this.entity) || !isShape(this.shape)) {
  121. return undefined;
  122. }
  123. var e;
  124. var op = new RMixedOperation();
  125. if (isCircleShape(this.shape)) {
  126. var center;
  127. var angle = undefined;
  128. var angle2 = undefined;
  129. op.deleteObject(this.entity);
  130. center = this.shape.getCenter();
  131. var radius = this.shape.getRadius();
  132. angle = center.getAngleTo(this.pos)+((this.offset/2)/radius);
  133. angle2 = center.getAngleTo(this.pos)-((this.offset/2)/radius);
  134. this.cutPos = center.operator_add(RVector.createPolar(radius, angle-((this.offset/2)/radius)));
  135. var arc = new RArc(
  136. this.shape.getCenter(),
  137. this.shape.getRadius(),
  138. angle,
  139. angle2,
  140. false);
  141. e = new RArcEntity(this.entity.getDocument(), new RArcData(arc));
  142. op.addObject(e, false);
  143. } else if (isLineShape(this.shape)) {
  144. var shape1 = this.shape.clone();
  145. var shape2 = this.shape.clone();
  146. shape1 = trimEndPoint(shape1, this.pos, this.pos);
  147. this.cutPos = shape1.getEndPoint();
  148. var points = shape1.getPointsWithDistanceToEnd(this.offset/2);
  149. var line = new RLine(shape1.getStartPoint(), points[1]);
  150. // modify chosen entity into first part:
  151. modifyEntity(op, this.entity, line);
  152. shape2 = trimStartPoint(shape2, this.pos, this.pos);
  153. points = shape2.getPointsWithDistanceToEnd(this.offset/2);
  154. var line2 = new RLine(points[0], shape2.getEndPoint());
  155. e = shapeToEntity(this.entity.getDocument(), line2);
  156. if (!isNull(e)) {
  157. e.copyAttributesFrom(getPtr(this.entity));
  158. op.addObject(e, false);
  159. }
  160. } else if (isArcShape(this.shape)) {
  161. var center;
  162. var angle = undefined;
  163. var angle2 = undefined;
  164. op.deleteObject(this.entity);
  165. center = this.shape.getCenter();
  166. var radius = this.shape.getRadius();
  167. angle = center.getAngleTo(this.shape.getStartPoint());
  168. angle2 = center.getAngleTo(this.pos)-((this.offset/2)/radius);
  169. this.cutPos = center.operator_add(RVector.createPolar(radius, angle2+((this.offset/2)/radius)));
  170. var arc = new RArc(
  171. this.shape.getCenter(),
  172. this.shape.getRadius(),
  173. angle,
  174. angle2,
  175. false);
  176. e = new RArcEntity(this.entity.getDocument(), new RArcData(arc));
  177. op.addObject(e, false);
  178. angle = center.getAngleTo(this.pos)+((this.offset/2)/radius);
  179. angle2 = center.getAngleTo(this.shape.getEndPoint());
  180. arc = new RArc(
  181. this.shape.getCenter(),
  182. this.shape.getRadius(),
  183. angle,
  184. angle2,
  185. false);
  186. e = new RArcEntity(this.entity.getDocument(), new RArcData(arc));
  187. op.addObject(e, false);
  188. }
  189. return op;
  190. };
  191. BreakOutGap.prototype.getHighlightedEntities = function() {
  192. var ret = [];
  193. if (isEntity(this.entity)) {
  194. ret.push(this.entity.getId());
  195. }
  196. return ret;
  197. };
  198. BreakOutGap.prototype.getAuxPreview = function() {
  199. if (!isValidVector(this.cutPos)) {
  200. return undefined;
  201. }
  202. var ret = [];
  203. var view = EAction.getGraphicsView();
  204. var overlap = view.mapDistanceFromView(25);
  205. var line = new RLine(this.cutPos, this.pos);
  206. var points = line.getPointsWithDistanceToEnd(-overlap);
  207. line = new RLine(points[0], points[1]);
  208. ret.push(line);
  209. var circle = new RCircle( this.cutPos, this.offset/2);
  210. ret.push(circle);
  211. return ret;
  212. };
  213. BreakOutGap.prototype.applyCommand = function(event, preview) {
  214. var di = this.getDocumentInterface();
  215. var cmd = event.getCommand();
  216. var pos = RMath.parseCoordinate(cmd);
  217. if (pos.isValid()) {
  218. return;
  219. }
  220. var value = RMath.eval(cmd);
  221. if (isNaN(value)) {
  222. return;
  223. }
  224. switch (this.state) {
  225. case BreakOutGap.State.SettingPos:
  226. event.accept();
  227. if (preview) {
  228. var r = this.offset;
  229. this.offset = value;
  230. this.updatePreview(true);
  231. this.offset = r;
  232. } else {
  233. this.setRadius(event.getCommand());
  234. }
  235. break;
  236. }
  237. };
  238. BreakOutGap.prototype.setRadius = function(expr) {
  239. var optionsToolBar = EAction.getOptionsToolBar();
  240. var w = optionsToolBar.findChild("Offset");
  241. if (isQWidget(w)) {
  242. w.text = expr;
  243. }
  244. };
  245. BreakOutGap.prototype.slotOffsetChanged = function(value) {
  246. this.offset = value;
  247. this.updatePreview(true);
  248. };