/** * 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/EAction.js"); /** * \brief This tool converts a drawing from the current drawing unit to another unit. * The coordinates in the drawing are scaled accordingly. */ function ConvertUnit(guiAction) { EAction.call(this, guiAction); this.fromUnit = undefined; this.toUnit = undefined; this.setUiOptions("ConvertUnit.ui"); } ConvertUnit.prototype = new EAction(); ConvertUnit.includeBasePath = includeBasePath; ConvertUnit.prototype.beginEvent = function() { EAction.prototype.beginEvent.call(this); var di = this.getDocumentInterface(); var doc = di.getDocument(); var optionsToolBar = EAction.getOptionsToolBar(); // fill combo boxes with available units var fromCombo = optionsToolBar.findChild("From"); ConvertUnit.initUnitCombo(fromCombo); var toCombo = optionsToolBar.findChild("To"); ConvertUnit.initUnitCombo(toCombo); this.fromUnit = doc.getUnit(); fromCombo.currentIndex = this.fromUnit; this.toUnit = RS.None; toCombo.currentIndex = this.toUnit; }; ConvertUnit.prototype.doConvert = function() { var ret = ConvertUnit.convert(this.getDocumentInterface(), this.fromUnit, this.toUnit); if (!isNull(ret)) { if (ret[0]===0) { EAction.handleUserMessage(ret[1]); } if (ret[0]===1) { EAction.handleUserWarning(ret[1]); } } // The focus ends up in ToolMatrix filter box, for unknown reason // Change it back to drawing var mdiChild = EAction.getMdiChild(); mdiChild.setFocus(); this.terminate(); }; ConvertUnit.convert = function(di, fromUnit, toUnit) { if (isNull(di)) { return; } var doc = di.getDocument(); //var fromUnit = doc.getUnit(); if (fromUnit===toUnit) { return [1, qsTr("Nothing to be done.")]; } if (fromUnit === RS.None || toUnit === RS.None) { return [1, qsTr("Unit \"%1\" selected. Nothing to be done").arg(RUnit.unitToName(RS.None))]; } var factor = RUnit.convert(1.0, fromUnit, toUnit); doc.startTransactionGroup(); var dimStyle = doc.queryDimStyle(); var docVars = doc.queryDocumentVariables(); // make sure we don't end up with a metric document with imperial dimension format: if (doc.getLinearFormat()===RS.Engineering || doc.getLinearFormat()===RS.ArchitecturalStacked || doc.getLinearFormat()===RS.Architectural) { if (!RUnit.isMetric(doc.getUnit()) && RUnit.isMetric(toUnit)) { dimStyle.setInt(RS.DIMLUNIT, RS.Decimal); } } docVars.setUnit(toUnit); // scale dimension sizes: var dimscale = dimStyle.getDouble(RS.DIMSCALE); dimStyle.setDouble(RS.DIMSCALE, dimscale*factor); var op = new RAddObjectsOperation(); op.setTransactionGroup(doc.getTransactionGroup()); op.addObject(docVars); op.addObject(dimStyle); di.applyOperation(op); op = new RAddObjectsOperation(); op.setTransactionGroup(doc.getTransactionGroup()); op.setAllowAll(true); var ids, i, id; // all entities on all blocks: ids = doc.queryAllEntities(false, true); for (i=0; i