public
Last active

printcraft - this is the js craftscript code used for mc.printcraft.org - it writes out an OpenSCAD file

  • Download Gist
print.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit);
 
//joins up blocks in the dimension dim
function squash(blockarray, dim, arraysize){
var i = new Array();
var dims = new Array();
 
for (var y = 0; y <= arraysize*2; y++){
for (var x = 0; x <= arraysize * 2; x++){
for (var z = 0; z <= arraysize * 2; z++){
var thisblock = blockarray[y][x][z];
if(thisblock != false){
i[0] = y;
i[1] = x;
i[2] = z;
for (var m = i[dim] + 1; m <= arraysize*2; m++){
i[dim] = m;
var nextblock = blockarray[i[0]][i[1]][i[2]];
if(nextblock == false){
break;
}
else{
dims[0] = thisblock[0];
dims[1] = thisblock[1];
dims[2] = thisblock[2];
dims[dim] = 1;
//if they are the same size
if(nextblock[0] == dims[0] && nextblock[1] == dims[1] && nextblock[2] == dims[2]){
thisblock[dim]++;
blockarray[i[0]][i[1]][i[2]] = false;
}
else{
break;
}
}
}
}
}
}
}
}
 
printToStl = function name(area){
 
var session = context.remember();
var size = area[3];
var origin = new Vector(area[0], area[1], area[2]);
var count = 0;
var finalcount = 0;
var email = "";
var blocks = new Array();
var blocklist = new Array();
player.print("Starting print");
//copy blocks into a js array
for (var y = 0; y <= size*2; y++){
blocks[y] = new Array();
for (var x = 0; x <= size * 2; x++){
blocks[y][x] = new Array();
for (var z = 0; z <= size * 2; z++) {
blocks[y][x][z] = new Array();
var pt = origin.add(x-size, y, z-size);
var id = session.getBlockType(pt);
if (id == BlockID.SAND || id == BlockID.CLAY) {
blocks[y][x][z] = new Array();
blocks[y][x][z][0] = 1;
blocks[y][x][z][1] = 1;
blocks[y][x][z][2] = 1;
count++;
}
else{
blocks[y][x][z] = false;
}
if (id == 63) {
var block = session.getBlock(pt);
var t = block.getText();
t.map(function(e){email = email + String(e);})
}
}
}
}
 
//squash them
squash(blocks, 0, size);
squash(blocks, 1, size);
squash(blocks, 2, size);
//write them into a list
for (var y = 0; y <= size*2; y++){
for (var x = 0; x <= size * 2; x++){
for (var z = 0; z <= size * 2; z++){
var thisblock = blocks[y][x][z];
if(thisblock){
//note swapping axes
blocklist.push([-(x-size + thisblock[1]), z-size, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
}
}
}
if(count == 0){
player.print("First build a model with sand or clay in the middle, There are both in the chest opposite");
}
else{
if(email == ""){
player.print("You have to put a sign with your email address in the print area with your model, There are signs in the chest");
}
else{
player.print(count + " blocks as " + finalcount + " primitives sent to OpenSCAD");
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
var src = "scale = 2.5;for(c = [" + blocklist.map(function(b){return "[" + b + "]";}) + "]){translate(v = [(c[0] * scale) - 0.005, (c[1] * scale) - 0.005, (c[2] * scale) - 0.005]) cube(size = [scale * (c[3] + 0.01), scale * (c[4] + 0.01), scale * (c[5] + 0.01)], center = false);}"
var path = "../scans/inbox/" + uuid + "_scan.scad"
var out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
var text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
path = "../scans/inbox/" + uuid + "_email.txt"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( email || "" );
out.print(text);
out.flush();
out.close();
player.print("Printing - Please be patient");
player.print("Your model will be emailed to " + email);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.