CubeCuttingOut.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // CubeCuttingOut.js
  20. //! [include]
  21. // library.js contains some convenience functions like 'isNull':
  22. include("scripts/library.js");
  23. //! [include]
  24. function CubeCuttingOut() {
  25. CubeCuttingOut.size = 10;
  26. CubeCuttingOut.drawPlates = true;
  27. }
  28. //! [init]
  29. CubeCuttingOut.init = function(formWidget) {
  30. if (!isNull(formWidget)) {
  31. CubeCuttingOut.widgets = getWidgets(formWidget);
  32. }
  33. };
  34. //! [init]
  35. //! [generate]
  36. CubeCuttingOut.generate = function(documentInterface, file) {
  37. CubeCuttingOut.size = parseInt(CubeCuttingOut.widgets["CubeSize"].text, 10);
  38. if (isNaN(CubeCuttingOut.size)) {
  39. // can't parse value as integer, set default size
  40. CubeCuttingOut.size = 10;
  41. }
  42. if (CubeCuttingOut.widgets["DrawPlates"].checked) {
  43. CubeCuttingOut.drawPlates = true;
  44. } else {
  45. CubeCuttingOut.drawPlates = false;
  46. }
  47. return CubeCuttingOut.createCuttingOut(documentInterface);
  48. };
  49. //! [generate]
  50. //! [generatePreview]
  51. CubeCuttingOut.generatePreview = function(documentInterface, iconSize) {
  52. CubeCuttingOut.size = iconSize / 6;
  53. CubeCuttingOut.drawPlates = true;
  54. return CubeCuttingOut.createCuttingOut(documentInterface);
  55. };
  56. //! [generatePreview]
  57. //! [createCuttingOut]
  58. CubeCuttingOut.createCuttingOut = function(documentInterface) {
  59. var addOperation = new RAddObjectsOperation(false);
  60. // create squares
  61. for ( var i = 0; i < 4; ++i) {
  62. var pos = new RVector(i * CubeCuttingOut.size, 0);
  63. CubeCuttingOut.createSquare(documentInterface, addOperation, pos);
  64. }
  65. var posTop = new RVector(CubeCuttingOut.size * 2, CubeCuttingOut.size);
  66. CubeCuttingOut.createSquare(documentInterface, addOperation, posTop);
  67. var posBottom = new RVector(CubeCuttingOut.size * 2, -CubeCuttingOut.size);
  68. CubeCuttingOut.createSquare(documentInterface, addOperation, posBottom);
  69. // create plates
  70. if (CubeCuttingOut.drawPlates) {
  71. var plates = new Array(
  72. [ new RVector(4 * CubeCuttingOut.size, 0), 0 ],
  73. [ new RVector(2 * CubeCuttingOut.size, -3 * CubeCuttingOut.size), 90 ],
  74. [ new RVector(1 * CubeCuttingOut.size, -4 * CubeCuttingOut.size), 90 ],
  75. [ new RVector(-2 * CubeCuttingOut.size, -2 * CubeCuttingOut.size), 180 ],
  76. [ new RVector(-2 * CubeCuttingOut.size, 0), 180 ],
  77. [ new RVector(1 * CubeCuttingOut.size, 2 * CubeCuttingOut.size), 270 ],
  78. [ new RVector(0, 3 * CubeCuttingOut.size), 270 ]
  79. );
  80. for ( var i = 0; i < plates.length; ++i) {
  81. var pos = plates[i][0];
  82. var angle = RMath.deg2rad(plates[i][1]);
  83. CubeCuttingOut.createPlate(documentInterface, addOperation, pos, angle);
  84. }
  85. }
  86. return addOperation;
  87. };
  88. //! [createCuttingOut]
  89. //! [createSquare]
  90. CubeCuttingOut.createSquare = function(documentInterface, addOperation, pos) {
  91. var va = new Array(
  92. new RVector(0, 0),
  93. new RVector(0, CubeCuttingOut.size),
  94. new RVector(CubeCuttingOut.size, CubeCuttingOut.size),
  95. new RVector(CubeCuttingOut.size, 0)
  96. );
  97. for ( var i = 0; i < va.length; ++i) {
  98. var v1 = va[i].operator_add(pos);
  99. var v2 = va[(i + 1) % va.length].operator_add(pos);
  100. var lineData = new RLineData(v1, v2);
  101. var line = new RLineEntity(documentInterface.getDocument(), lineData);
  102. addOperation.addObject(line);
  103. }
  104. };
  105. //! [createSquare]
  106. CubeCuttingOut.createPlate = function(documentInterface, addOperation, pos, angle) {
  107. var plateSize = CubeCuttingOut.size / 7;
  108. var off = plateSize * Math.sqrt(2);
  109. var va = new Array(
  110. new RVector(0, 0),
  111. new RVector(off, off),
  112. new RVector(off, CubeCuttingOut.size - off),
  113. new RVector(0, CubeCuttingOut.size)
  114. );
  115. for ( var i = 0; i < va.length; ++i) {
  116. var v1 = va[i].operator_add(pos);
  117. v1 = v1.rotate(angle);
  118. var v2 = va[(i + 1) % va.length].operator_add(pos);
  119. v2 = v2.rotate(angle);
  120. var lineData = new RLineData(v1, v2);
  121. var line = new RLineEntity(documentInterface.getDocument(), lineData);
  122. addOperation.addObject(line);
  123. }
  124. };