ExAddBlock.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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("../BlockExamples.js");
  20. /**
  21. * \ingroup ecma_misc_examples_block
  22. * \class ExAddBlock
  23. * This action adds a block called 'MyBlock' with a line and an arc as contents.
  24. * A block reference is also inserted into the main drawing.
  25. */
  26. function ExAddBlock(guiAction) {
  27. BlockExamples.call(this, guiAction);
  28. }
  29. ExAddBlock.prototype = new BlockExamples();
  30. /**
  31. * Adds a block and terminates.
  32. */
  33. ExAddBlock.prototype.beginEvent = function() {
  34. BlockExamples.prototype.beginEvent.call(this);
  35. var document = this.getDocument();
  36. var di = this.getDocumentInterface();
  37. // create new block definition:
  38. var block = new RBlock(document, "MyBlock", new RVector(0,0));
  39. var operation = new RAddObjectOperation(block);
  40. di.applyOperation(operation);
  41. // make new block the current block:
  42. di.setCurrentBlock("MyBlock");
  43. // add a line and an arc to the block definition:
  44. var p1 = new RVector(0,0);
  45. var p2 = new RVector(50,0);
  46. var line = new RLineEntity(document, new RLineData(p1, p2));
  47. var center = new RVector(25,0);
  48. var radius = 25;
  49. var arc = new RArcEntity(document, new RArcData(center, radius, 0.0, Math.PI, false));
  50. operation = new RAddObjectsOperation();
  51. operation.addObject(line);
  52. operation.addObject(arc);
  53. di.applyOperation(operation);
  54. // switch back to main drawing (block "*Model_Space"):
  55. di.setCurrentBlock(RBlock.modelSpaceName);
  56. // create block reference in main drawing at 10/20, scale 1, angle 30:
  57. var position = new RVector(10,20);
  58. var scale = new RVector(1,1);
  59. var angle = RMath.deg2rad(30);
  60. var blockId = document.getBlockId("MyBlock");
  61. var blockRef = new RBlockReferenceEntity(document, new RBlockReferenceData(blockId, position, scale, angle));
  62. operation = new RAddObjectOperation(blockRef);
  63. di.applyOperation(operation);
  64. this.terminate();
  65. };
  66. /**
  67. * Adds a menu for this action to Examples/Math Examples/ExAddBlock.
  68. */
  69. ExAddBlock.init = function(basePath) {
  70. var action = new RGuiAction(qsTr("&Create Block"), RMainWindowQt.getMainWindow());
  71. action.setRequiresDocument(true);
  72. action.setScriptFile(basePath + "/ExAddBlock.js");
  73. action.setGroupSortOrder(79100);
  74. action.setSortOrder(100);
  75. action.setWidgetNames(["BlockExamplesMenu"]);
  76. };