diff --git a/BlockRefExportAll/BlockRefExportAll.js b/BlockRefExportAll/BlockRefExportAll.js index 8ec3a3c..128d378 100644 --- a/BlockRefExportAll/BlockRefExportAll.js +++ b/BlockRefExportAll/BlockRefExportAll.js @@ -1,53 +1,26 @@ /** - * 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. - * + * Copyright (c) 2026 by SebMas. All rights reserved. * You should have received a copy of the GNU General Public License - * along with QCAD. + * along with this script. */ include("../QcadStagePlot.js"); include("scripts/File/File.js"); -/** - * \class BlockRefExport - * \brief This action exports block references and positions as CSV file. - * \ingroup ecma_misc_block - */ function BlockRefExportAll(guiAction) { QcadStagePlot.call(this, guiAction); } BlockRefExportAll.prototype = new QcadStagePlot(); -BlockRefExportAll.prototype.dmxAttributes = [ - "dmx", - "id", - "name", - "power", - "weigth", - "type", - "gel", - "iris", - "barndoor", - "mode", - "heigth" -]; +BlockRefExportAll.prototype.keyword = "dmx"; BlockRefExportAll.prototype.beginEvent = function () { QcadStagePlot.prototype.beginEvent.call(this); + // get the doc + var doc = this.getDocument(); + // get the file name to use it for export var fileName = this.getFileName(); if (isNull(fileName)) { @@ -62,36 +35,41 @@ BlockRefExportAll.prototype.beginEvent = function () { return; } - var ts = new QTextStream(file); - setUtf8Codec(ts); - ts.writeString("Block Name\tPos X\tPos Y\tDmx\tId\tName\tPower\tWeigth\tType\tGel\tIris\tBarndoor\tMode\tHeigth"); - - var doc = this.getDocument(); + // init some vars + var headers = "Block Name\tPos X\tPos Y"; + var stagePlotReferences = []; + var attributesNames = []; // get blocks var block; var result = doc.queryAllBlocks(); result = doc.sortBlocks(result); + // iterate blocks for (var i = 0; i < result.length; ++i) { var id = result[i]; block = doc.queryBlock(id); + if (isNull(block)) { continue; } - var blockRefIds = doc.queryBlockReferences(id); - var dmxReferences = []; + var blockRefIds = doc.queryBlockReferences(id); // iterate block references for (var k = 0; k < blockRefIds.length; k++) { var blockRefId = blockRefIds[k]; var blockRef = doc.queryEntity(blockRefIds[k]); - // iterate through all attributes of the current block reference: + // get the attributes var attributeIds = doc.queryChildEntities(blockRefId, RS.EntityAttribute); - if (this.checkIfDmx(doc, attributeIds)) { - var referenceDmxAttributes = {}; + if (this.checkIfValid(doc, attributeIds)) { + var referenceStagePlotAttributes = {}; + // get basic values + referenceStagePlotAttributes["block_name"] = block.getName(); + referenceStagePlotAttributes["pos_x"] = blockRef.getPosition().x; + referenceStagePlotAttributes["pos_y"] = blockRef.getPosition().y; + // iterate through all attributes of the current block reference: for (var c = 0; c < attributeIds.length; c++) { var attributeId = attributeIds[c]; var attribute = doc.queryEntityDirect(attributeId); @@ -99,98 +77,42 @@ BlockRefExportAll.prototype.beginEvent = function () { continue; } + // get attributes names and values var attributeName = attribute.getTag(); var attributeValue = attribute.getPlainText(); - if (this.dmxAttributes.indexOf(attributeName) !== -1) { - referenceDmxAttributes[attributeName] = attributeValue; + if (attributesNames.indexOf(attributeName) === -1) { + attributesNames.push(attributeName); } + + // store them in a temporary array + referenceStagePlotAttributes[attributeName] = attributeValue; } - if (this.countObjects(referenceDmxAttributes)) { - dmxReferences.push(referenceDmxAttributes); + if (this.countObjects(referenceStagePlotAttributes)) { + // store the block attributes in a list + stagePlotReferences.push(referenceStagePlotAttributes); } } } - if (dmxReferences.length > 0) { - for (ref = 0; ref < dmxReferences.length; ref++) { - ts.writeString("\n%1\t%2\t%3".arg(block.getName()).arg(blockRef.getPosition().x).arg(blockRef.getPosition().y)); - // dmx - if (dmxReferences[ref].dmx !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].dmx)); + } + if (stagePlotReferences.length > 0) { + var ts = new QTextStream(file); + setUtf8Codec(ts); + // get and write the headers + for (attributeNameIndex = 0; attributeNameIndex < attributesNames.length; attributeNameIndex++) { + headers = headers + "\t" + attributesNames[attributeNameIndex]; + } + ts.writeString(headers); + + // iterate through the stored blocks and write them in the csv + for (ref = 0; ref < stagePlotReferences.length; ref++) { + ts.writeString("\n%1\t%2\t%3".arg(stagePlotReferences[ref].block_name).arg(stagePlotReferences[ref].pos_x).arg(stagePlotReferences[ref].pos_y)); + for (attributeNameIndex = 0; attributeNameIndex < attributesNames.length; attributeNameIndex++) { + if (stagePlotReferences[ref][attributesNames[attributeNameIndex]] !== undefined) { + ts.writeString("\t%1".arg(stagePlotReferences[ref][attributesNames[attributeNameIndex]])); } else { ts.writeString("\t"); } - // id - if (dmxReferences[ref].id !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].id)); - } - else { - ts.writeString("\t"); - } - // name - if (dmxReferences[ref].name !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].name)); - } - else { - ts.writeString("\t"); - } - // power - if (dmxReferences[ref].power !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].power)); - } - else { - ts.writeString("\t"); - } - // weigth - if (dmxReferences[ref].weigth !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].weigth)); - } - else { - ts.writeString("\t"); - } - // type - if (dmxReferences[ref].type !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].type)); - } - else { - ts.writeString("\t"); - } - // gel - if (dmxReferences[ref].gel !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].gel)); - } - else { - ts.writeString("\t"); - } - // iris - if (dmxReferences[ref].iris !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].iris)); - } - else { - ts.writeString("\t"); - } - // barndoor - if (dmxReferences[ref].barndoor !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].barndoor)); - } - else { - ts.writeString("\t"); - } - // mode - if (dmxReferences[ref].mode !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].mode)); - } - else { - ts.writeString("\t"); - } - // heigth - if (dmxReferences[ref].heigth !== undefined) { - ts.writeString("\t%1".arg(dmxReferences[ref].heigth)); - } - else { - ts.writeString("\t"); - } - } } } @@ -208,11 +130,11 @@ BlockRefExportAll.prototype.countObjects = function (obj) { return count; } -BlockRefExportAll.prototype.checkIfDmx = function (doc, attributeIds) { +BlockRefExportAll.prototype.checkIfValid = function (doc, attributeIds) { for (var i = 0; i < attributeIds.length; i++) { var attribute = doc.queryEntityDirect(attributeIds[i]); if (attribute.isNull()) continue; - if (attribute.getTag() === "dmx") { + if (attribute.getTag() === this.keyword) { return true; } }