Created
November 2, 2012 14:04
-
-
Save paulharter/4001539 to your computer and use it in GitHub Desktop.
printcraft - this is the js craftscript code used for mc.printcraft.org - it writes out an OpenSCAD file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment