/** * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved. * * This file is part of the QCAD project. * * QCAD is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * QCAD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with QCAD. */ include("scripts/Modify/Modify.js"); include("scripts/ShapeAlgorithms.js"); function Explode(guiAction) { Modify.call(this, guiAction); } Explode.prototype = new Modify(); Explode.getPreferencesCategory = function() { return [qsTr("Modify"), qsTr("Explode")]; }; Explode.initPreferences = function(pageWidget, calledByPrefDialog, document) { var widgets = getWidgets(pageWidget); if (!RTextBasedData.hasProxy()) { // remove MultilineTextToSimpleText option: widgets["MultilineTextToSimpleText"].visible = false; } if (!RSpline.hasProxy()) { widgets["SplineTolerance"].visible = false; widgets["SplineTolerance_Label"].visible = false; widgets["SplinesToLineSegments"].visible = false; destr(widgets["Indent"]); pageWidget.findChild("GridLayout").addWidget(widgets["SplineSegments_Label"], 2, 0, 1, 2); } else { widgets["SplinesToLineSegments"].toggled.connect(function(state) { widgets["SplineSegments"].enabled = state; widgets["SplineSegments_Label"].enabled = state; }); widgets["SplineSegments"].enabled = widgets["SplinesToLineSegments"].checked; widgets["SplineSegments_Label"].enabled = widgets["SplinesToLineSegments"].checked; } }; Explode.prototype.beginEvent = function() { Modify.prototype.beginEvent.call(this); var di = this.getDocumentInterface(); Explode.explodeSelection(di, this.getToolTitle()); this.terminate(); }; /** * Explodes all selected entities. * \param di Document interface * \param toolTitle Tool title used for undo/redo information. */ Explode.explodeSelection = function(di, toolTitle) { var document = di.getDocument(); var storage = document.getStorage(); var ids = document.querySelectedEntities(); var i, k, e, n; var polyline, shapes; var options = {}; options["splineTolerance"] = RSettings.getDoubleValue("Explode/SplineTolerance", 0.01); options["splineSegments"] = RSettings.getIntValue("Explode/SplineSegments", 64); options["ellipseSegments"] = RSettings.getIntValue("Explode/EllipseSegments", 32); options["splinesToLineSegments"] = RSettings.getBoolValue("Explode/SplinesToLineSegments", false); options["textToPolylines"] = RSettings.getBoolValue("Explode/TextToPolylines", true); options["textSplineToLineOrArc"] = RSettings.getBoolValue("Explode/TextSplineToLineOrArc", true); options["multilineTextToSimpleText"] = RSettings.getBoolValue("Explode/MultilineTextToSimpleText", true); options["circlesToPolylines"] = RSettings.getBoolValue("Explode/CirclesToPolylines", true); var op = new RAddObjectsOperation(); if (!isNull(toolTitle)) { op.setText(toolTitle); } // map old block reference IDs to block reference entities: var blockReferenceMap = {}; // list of attribute entities: var attributeEntities = []; for (i=0; i0) { op = new RAddObjectsOperation(); //op.setTransactionGroup(doc.getTransactionGroup()); if (!isNull(toolTitle)) { op.setText(toolTitle); } // fix attribute links to block references: for (i=0; i=0x060000) { var newAttributeId = t.getNewObjectId(attributeEntity.getId()); storage.setObjectId(attributeEntity, newAttributeId); } // find parent entity of attribute: var blockReferenceEntity = blockReferenceMap[attributeEntity.getParentId()]; if (!isNull(blockReferenceEntity)) { // update parent ID: if (RSettings.getQtVersion()>=0x060000) { var newAttributeParentId = t.getNewObjectId(blockReferenceEntity.getId()); storage.setEntityParentId(attributeEntity, newAttributeParentId); } else { storage.setEntityParentId(attributeEntity, blockReferenceEntity.getId()); } op.addObject(attributeEntity, false); } } di.applyOperation(op); } }; /** * \return Array of new shapes and entities or undefined to do nothing. */ Explode.explodeEntity = function(entity, options) { var ret = []; if (isNull(options)) { options = {}; } // these entity types are not explodable: if (isPointEntity(entity) || isLineEntity(entity) || isXLineEntity(entity) || isRayEntity(entity) || isImageEntity(entity) || isArcEntity(entity) || isViewportEntity(entity)) { return undefined; } var k, e, n, d; var col, row; var painterPaths; var document = entity.getDocument(); var storage = document.getStorage(); var splineTolerance = options["splineTolerance"]; var splineSegments = options["splineSegments"]; var ellipseSegments = options["ellipseSegments"]; var splinesToLineSegments = options["splinesToLineSegments"]; var textToPolylines = options["textToPolylines"]; var textSplineToLineOrArc = options["textSplineToLineOrArc"]; var multilineTextToSimpleText = options["multilineTextToSimpleText"]; var circlesToPolylines = options["circlesToPolylines"]; if (isNull(splineTolerance)) { splineTolerance = RSettings.getDoubleValue("Explode/SplineTolerance", 0.01); } if (isNull(splineSegments)) { splineSegments = RSettings.getIntValue("Explode/SplineSegments", 64); } if (isNull(ellipseSegments)) { ellipseSegments = RSettings.getIntValue("Explode/EllipseSegments", 32); } if (isNull(splinesToLineSegments)) { splinesToLineSegments = RSettings.getBoolValue("Explode/SplinesToLineSegments", false); } if (isNull(textToPolylines)) { textToPolylines = RSettings.getBoolValue("Explode/TextToPolylines", true); } if (isNull(textSplineToLineOrArc)) { textSplineToLineOrArc = RSettings.getBoolValue("Explode/TextSplineToLineOrArc", true); } if (isNull(multilineTextToSimpleText)) { multilineTextToSimpleText = RSettings.getBoolValue("Explode/MultilineTextToSimpleText", true); } if (isNull(circlesToPolylines)) { circlesToPolylines = RSettings.getBoolValue("Explode/CirclesToPolylines", true); } // explode ellipse into polyline with arc segments: if (isEllipseEntity(entity)) { if (REllipse.hasProxy()) { var ellipse = entity.getData().castToShape(); var polyline = ellipse.approximateWithArcs(ellipseSegments); if (!polyline.isEmpty()) { ret.push(polyline); } } else { return undefined; } } // explode circle into polyline with two arc segments: else if (isCircleEntity(entity)) { if (circlesToPolylines) { var circle = entity.getData().castToShape(); polyline = new RPolyline(); polyline.appendShape(new RArc(circle.getCenter(), circle.getRadius(), 0.0, Math.PI, false)); polyline.appendShape(new RArc(circle.getCenter(), circle.getRadius(), Math.PI, 2*Math.PI, false)); polyline.autoClose(); ret.push(polyline); } else { return undefined; } } // explode polyline into line and arc segments: else if (isPolylineEntity(entity)) { polyline = entity.getData().castToShape(); // explode polyline with segment widths: if (RPolyline.hasProxy() && polyline.hasWidths()) { // list of polyline pairs (left / right): var pls; if (RSettings.getQtVersion() >= 0x060000) { pls = []; var plsLeft = polyline.getLeftOutline(); var plsRight = polyline.getRightOutline(); for (var i=0; i1 || rightPl.countSegments()>1) { else if (!leftPl.isEmpty()) { //ret.push(pls[k]); if (!rightPl.isEmpty()) { ret.push(new RLine(leftPl.getStartPoint(), rightPl.getStartPoint())); } ret.push(leftPl); if (!rightPl.isEmpty()) { ret.push(rightPl); ret.push(new RLine(leftPl.getEndPoint(), rightPl.getEndPoint())); } first = true; if (!isNull(lastSegment)) { ret.push(lastSegment); lastSegment = undefined; prevLeftPl = undefined; prevRightPl = undefined; } continue; } } if (!isNull(lastSegment)) { ret.push(lastSegment); } } else { var polySegments = polyline.getExploded(); if (polySegments.length>0) { for (k=0; k1 || data.getRowCount()>1) { for (col=0; col