SplineToLine.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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("../MiscModify.js");
  20. include("scripts/ShapeAlgorithms.js");
  21. /**
  22. * Converts (selected) straight splines to lines.
  23. */
  24. function SplineToLine(guiAction) {
  25. MiscModify.call(this, guiAction);
  26. }
  27. SplineToLine.prototype = new MiscModify();
  28. SplineToLine.prototype.beginEvent = function() {
  29. MiscModify.prototype.beginEvent.call(this);
  30. var di = this.getDocumentInterface();
  31. var document = this.getDocument();
  32. var op = new RModifyObjectsOperation();
  33. var ids;
  34. if (document.hasSelection()) {
  35. ids = document.querySelectedEntities();
  36. }
  37. else {
  38. ids = document.queryAllVisibleEntities();
  39. }
  40. for (var i=0; i<ids.length; i++) {
  41. var id = ids[i];
  42. var entity = document.queryEntity(id);
  43. if (isSplineEntity(entity)) {
  44. var shape = entity.castToShape();
  45. var line = ShapeAlgorithms.splineToLineOrArc(shape, 0.01, true);
  46. if (isLineShape(line)) {
  47. var newEntity = shapeToEntity(document, line);
  48. newEntity.copyAttributesFrom(getPtr(entity));
  49. op.deleteObject(entity.clone());
  50. op.addObject(newEntity);
  51. }
  52. }
  53. }
  54. di.applyOperation(op);
  55. this.terminate();
  56. };
  57. SplineToLine.init = function(basePath) {
  58. var action = new RGuiAction(qsTr("Convert Straight Splines to Lines"), RMainWindowQt.getMainWindow());
  59. action.setRequiresDocument(true);
  60. action.setScriptFile(basePath + "/SplineToLine.js");
  61. action.setGroupSortOrder(56170);
  62. action.setSortOrder(200);
  63. action.setWidgetNames(["MiscModifyMenu", "MiscModifyToolBar", "MiscModifyToolsPanel"]);
  64. };