geom_set_ops.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // geom_set_ops.js
  2. // contains the code generation functions for the Geometric Set operations
  3. // union, difference, intersection, and hull
  4. // These blocks are mutators that can have a variable number of statements
  5. // hence the loops.
  6. 'use strict'
  7. goog.provide('Blockly.OpenSCAD.geom_set_ops');
  8. goog.require('Blockly.OpenSCAD');
  9. Blockly.OpenSCAD['union'] = function(block) {
  10. var n = 0;
  11. var statements_a = Blockly.OpenSCAD.statementToCode(block, 'A');
  12. if (statements_a != '') statements_a += '\n';
  13. var varCode = Blockly.OpenSCAD.returnVarCode(block);
  14. var aC = varCode[0];
  15. var aP = varCode[1];
  16. var code = 'group(){\n' + aC + statements_a;
  17. for (n = 0; n<= block.plusCount_; n++) {
  18. var statements_b = Blockly.OpenSCAD.statementToCode(block, 'PLUS' + n);
  19. if (statements_b != '') code += statements_b + '\n';
  20. }
  21. code += aP + '}';
  22. return code;
  23. };
  24. // order matters. That makes the variable assignment more of a pain here.
  25. // also, I want to insert a easily/removed union around the stuff in the first bay, since
  26. // I don't implicitly union anymore. Hopefully that's fast if it has a single object.
  27. Blockly.OpenSCAD['difference'] = function(block) {
  28. var n = 0;
  29. var statements_a = Blockly.OpenSCAD.statementToCode(block, 'A');
  30. if (statements_a != '') statements_a += '\n';
  31. var varCode = Blockly.OpenSCAD.returnVarCode(block);
  32. var aC = varCode[0];
  33. var aP = varCode[1];
  34. // var code = 'difference(){\n' + aC + statements_a + aP;
  35. // without implicit unions in the parser, I want to explicitly union everything in the first
  36. // bay of the difference block. However, since openscad DOES implicit unions, I don't
  37. // want the union in the code exported to openScad (redundant, plus confusing because
  38. // the user didn't request it).
  39. // So, I put the union in, and have my postprocessing strip it out before making the openscad
  40. // downloadable code. So my parser sees the union.
  41. // the comment //end assign
  42. // indicates that the line will be stripped for openscad code. I should change that
  43. // comment to better indicate what is going on.
  44. var code = 'difference() {\n';
  45. code += 'union() { //end assign\n';
  46. code += aC + statements_a + aP;
  47. code += '\n } //end assign\n';
  48. var takeAway = '';
  49. for (n = 0; n<= block.minusCount_; n++) {
  50. var statements_b = Blockly.OpenSCAD.statementToCode(block, 'MINUS' + n);
  51. if (statements_b != '') takeAway += statements_b + '\n';
  52. }
  53. var newAC = aC.slice(0, -1) + '//end assign\n';
  54. code += newAC + takeAway + aP + '}';
  55. return code;
  56. };
  57. // without implicit unions in the parser, I will explicitly union EVERY bay of intersection.
  58. // with intersection, each object needs to be separate for variable assignment. What a pain.
  59. Blockly.OpenSCAD['intersection'] = function(block) {
  60. var n = 0;
  61. var statements_a = Blockly.OpenSCAD.statementToCode(block, 'A');
  62. if (statements_a != '') statements_a += '\n';
  63. var varCode = Blockly.OpenSCAD.returnVarCode(block);
  64. var aC = varCode[0];
  65. var aP = varCode[1];
  66. var newAC = aC.slice(0, -1) + '//end assign\n';
  67. var code = 'intersection() {\n';
  68. code += 'union() { //end assign\n';
  69. code += aC + statements_a + aP;
  70. code += '\n } //end assign\n';
  71. // var code = 'intersection(){\n' + aC + statements_a + aP;
  72. for (n = 0; n<= block.withCount_; n++) {
  73. var statements_b = Blockly.OpenSCAD.statementToCode(block, 'WITH' + n);
  74. if (statements_b != '') {
  75. code += 'union() { //end assign\n';
  76. code += newAC + statements_b + '\n' + aP;
  77. code += '\n } //end assign\n';
  78. }
  79. }
  80. code += '}';
  81. return code;
  82. };
  83. Blockly.OpenSCAD['hull'] = function(block) {
  84. var n = 0;
  85. var code = '';
  86. var statements_a = Blockly.OpenSCAD.statementToCode(block, 'A');
  87. if (statements_a != '') statements_a += '\n';
  88. var varCode = Blockly.OpenSCAD.returnVarCode(block);
  89. var aC = varCode[0];
  90. var aP = varCode[1];
  91. code += 'hull(){\n' + aC + statements_a;
  92. for (n = 0; n<= block.withCount_; n++) {
  93. var statements_b = Blockly.OpenSCAD.statementToCode(block, 'WITH' + n);
  94. if (statements_b != '') code += statements_b + '\n';
  95. }
  96. code += aP + '}';
  97. return code;
  98. };
  99. Blockly.OpenSCAD['chull'] = function(block) {
  100. var n = 0;
  101. var code = '';
  102. var varCode = Blockly.OpenSCAD.returnVarCode(block);
  103. var aC = varCode[0];
  104. var aP = varCode[1];
  105. var statements_a = Blockly.OpenSCAD.statementToCode(block, 'A');
  106. code += '// chain hull\n';
  107. code += 'union() {\n';
  108. code += ' hull(){\n ' + aC + statements_a + '\n';
  109. for (n = 0; n<= block.withCount_ - 1; n++) {
  110. var statements_b = Blockly.OpenSCAD.statementToCode(block, 'WITH' + n);
  111. code += ' ' + statements_b + '\n';
  112. code += ' }\n';
  113. code += ' hull() {\n ' + statements_b + '\n';
  114. }
  115. var last = Blockly.OpenSCAD.statementToCode(block, 'WITH' + block.withCount_);
  116. code += ' ' + last + '\n';
  117. code += aP + ' }\n}\n';
  118. return code;
  119. };