BlockRefExport.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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("../MiscBlock.js");
  20. include("scripts/File/File.js");
  21. /**
  22. * \class BlockRefExport
  23. * \brief This action exports block references and positions as CSV file.
  24. * \ingroup ecma_misc_block
  25. */
  26. function BlockRefExport(guiAction) {
  27. MiscBlock.call(this, guiAction);
  28. }
  29. BlockRefExport.prototype = new MiscBlock();
  30. BlockRefExport.prototype.beginEvent = function() {
  31. MiscBlock.prototype.beginEvent.call(this);
  32. var fileName = this.getFileName();
  33. if (isNull(fileName)) {
  34. this.terminate();
  35. return;
  36. }
  37. var file = new QFile(fileName);
  38. var flags = makeQIODeviceOpenMode(QIODevice.WriteOnly, QIODevice.Text);
  39. if (!file.open(flags)) {
  40. this.terminate();
  41. return;
  42. }
  43. var ts = new QTextStream(file);
  44. setUtf8Codec(ts);
  45. ts.writeString("Block Name\tX\tY");
  46. var doc = this.getDocument();
  47. var block;
  48. var result = doc.queryAllBlocks();
  49. result = doc.sortBlocks(result);
  50. for (var i=0; i<result.length; ++i) {
  51. var id = result[i];
  52. block = doc.queryBlock(id);
  53. if (isNull(block)) {
  54. continue;
  55. }
  56. var blockRefIds = doc.queryBlockReferences(id);
  57. for (var k=0; k<blockRefIds.length; k++) {
  58. var blockRef = doc.queryEntity(blockRefIds[k]);
  59. ts.writeString("\n%1\t%2\t%3".arg(block.getName()).arg(blockRef.getPosition().x).arg(blockRef.getPosition().y));
  60. }
  61. }
  62. file.close();
  63. this.terminate();
  64. };
  65. BlockRefExport.prototype.getFileName = function() {
  66. var drawingFileName = this.getDocument().getFileName();
  67. var fi;
  68. var fileName;
  69. var initialPath = "";
  70. if (drawingFileName.length === 0) {
  71. var path = RSettings.getStringValue("BlockRefExport/Path", QDir.homePath());
  72. fi = new QFileInfo(path);
  73. initialPath = fi.absoluteFilePath() + QDir.separator
  74. + stripDirtyFlag(EAction.getMdiChild().windowTitle) + ".csv";
  75. } else {
  76. fi = new QFileInfo(drawingFileName);
  77. initialPath = fi.path() + QDir.separator + fi.completeBaseName() + ".csv";
  78. }
  79. var appWin = EAction.getMainWindow();
  80. var ret = File.getSaveFileName(appWin, qsTr("Export Block References List (CSV)"),
  81. initialPath, [ qsTr("CSV") + " (*.csv)" ]);
  82. if (isNull(ret)) {
  83. return undefined;
  84. }
  85. fileName = ret[0];
  86. if (fileName === "") {
  87. return undefined;
  88. }
  89. if (drawingFileName.length === 0) {
  90. RSettings.setValue("BlockRefExport/Path", new QFileInfo(fileName).absolutePath());
  91. }
  92. return fileName;
  93. };
  94. /**
  95. * Adds a menu for this action to the Misc menu.
  96. */
  97. BlockRefExport.init = function(basePath) {
  98. var action = new RGuiAction(qsTr("&Export Block References List"), RMainWindowQt.getMainWindow());
  99. action.setRequiresDocument(true);
  100. action.setScriptFile(basePath + "/BlockRefExport.js");
  101. action.setGroupSortOrder(60100);
  102. action.setSortOrder(250);
  103. action.setWidgetNames(["MiscBlockMenu", "MiscBlockToolBar", "MiscBlockToolsPanel"]);
  104. };