/** * 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/File/SvgImport/SvgImport.js"); include("scripts/Block/BlockInsert.js"); /** * \class InsertBlockItem * \brief Called when a block is inserted from the part library. * \ingroup ecma_block */ function InsertBlockItem(guiAction) { BlockInsert.call(this, guiAction); if (!isNull(guiAction)) { this.setUiOptions("InsertBlockItem.ui"); } this.diItem = undefined; this.docItem = undefined; this.blockName = undefined; // set to flatten all entities to this one layer: this.layerName = undefined; this.offset = undefined; this.scale = 1.0; this.rotation = 0.0; this.flipHorizontal = false; this.flipVertical = false; this.toCurrentLayer = false; this.overwriteLayers = false; this.overwriteBlocks = false; this.referencePoint = new RVector(0,0); this.attributes = {}; this.attributeTags = []; this.url = undefined; if (!isNull(guiAction)) { this.url = this.guiAction.data(); } this.autoTerminate = false; } InsertBlockItem.State = { SettingPosition : 0 }; InsertBlockItem.prototype = new BlockInsert(); InsertBlockItem.prototype.beginEvent = function() { // part library item is loaded into this document: if (isNull(this.diItem)) { var ms = new RMemoryStorage(); var si = createSpatialIndex(); this.docItem = new RDocument(ms, si); this.diItem = new RDocumentInterface(this.docItem); this.diItem.setNotifyListeners(false); } // TODO refactor BlockInsert.prototype.beginEvent.call(this); var path = undefined; if (!isNull(this.url)) { var err; if (this.url.isLocalFile()) { path = this.url.toLocalFile(); qDebug("importing file: " + path); err = this.diItem.importFile(path, "", false); } else { qDebug("importing URL: " + this.url); if (isFunction(this.url.encodedPath)) { path = QUrl.fromPercentEncoding(this.url.encodedPath()); } else { path = this.url.path(); } err = this.diItem.importUrl(this.url, "", false); } if (err!==RDocumentInterface.IoErrorNoError) { EAction.handleUserWarning(qsTr("Cannot import file from URL: ") + this.url.toString()); this.terminate(); return; } } // no block name given: // create block name from file name: if (isNull(this.blockName)) { if (isNull(path)) { this.terminate(); return; } this.blockName = new QFileInfo(path).completeBaseName(); // fix block name if necessary: this.blockName = fixSymbolTableName(this.blockName); } // invalid block name characters: if (isNull(this.blockName)) { var doc = this.getDocument(); var c = 1; while (isNull(this.blockName) || doc.hasBlock(this.blockName)) { this.blockName = 'block_' + c++; } EAction.handleUserMessage(qsTr("Adjusted invalid block name to \"%1\"").arg(this.blockName)); } if (this.docItem.hasBlock(this.blockName)) { // if the item contains a block with the same name as the file base name, // insert without creating a new block: this.blockName = undefined; } var i, id; // init block attribute inputs to options tool bar: // assign values to attributes: var first = true; var optionsToolBar = EAction.getOptionsToolBar(); var tagCombo = optionsToolBar.findChild("AttributeTag"); var tags = {}; if (!isNull(tagCombo)) { var ids = this.docItem.queryAllEntities(); for (i=0; i