BlockFixNames.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  21. * This action fixes invalid block names from imported drawings.
  22. */
  23. function BlockFixNames(guiAction) {
  24. MiscBlock.call(this, guiAction);
  25. }
  26. BlockFixNames.prototype = new MiscBlock();
  27. /**
  28. * Renames blocks and terminates.
  29. */
  30. BlockFixNames.prototype.beginEvent = function() {
  31. MiscBlock.prototype.beginEvent.call(this);
  32. var document = this.getDocument();
  33. var di = this.getDocumentInterface();
  34. var operation = new RModifyObjectsOperation();
  35. var blockIds = document.queryAllBlocks();
  36. for (var i=0; i<blockIds.length; i++) {
  37. var blockId = blockIds[i];
  38. var block = document.queryBlock(blockId);
  39. var name = block.getName();
  40. if (!name.startsWith("*")) {
  41. continue;
  42. }
  43. var nameUpper = name.toUpperCase();
  44. if (nameUpper===RBlock.modelSpaceName.toUpperCase() ||
  45. nameUpper.startsWith("*PAPER_SPACE")) {
  46. continue;
  47. }
  48. name = name.substring(1);
  49. var nameBase = name;
  50. var c = 1;
  51. while (document.hasBlock(name)) {
  52. name = nameBase + '_' + c++;
  53. }
  54. block.setName(name);
  55. operation.addObject(block);
  56. }
  57. di.applyOperation(operation);
  58. this.terminate();
  59. };
  60. /**
  61. * Adds a menu for this action to Examples/Math Examples/BlockFixNames.
  62. */
  63. BlockFixNames.init = function(basePath) {
  64. var action = new RGuiAction(qsTr("&Fix Block Names"), RMainWindowQt.getMainWindow());
  65. action.setRequiresDocument(true);
  66. action.setScriptFile(basePath + "/BlockFixNames.js");
  67. action.setGroupSortOrder(60100);
  68. action.setSortOrder(100);
  69. action.setWidgetNames(["MiscBlockMenu", "MiscBlockToolBar", "MiscBlockToolsPanel"]);
  70. };