Skip to content

Instantly share code, notes, and snippets.

@blairmacintyre
Forked from paulharter/print.js
Created July 10, 2013 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blairmacintyre/5969567 to your computer and use it in GitHub Desktop.
Save blairmacintyre/5969567 to your computer and use it in GitHub Desktop.
Modified print.js to create two openscad files, so I can dual-print
importPackage(Packages.com.sk89q.worldedit.blocks);
importPackage(Packages.com.sk89q.worldedit);
//joins up blocks in the dimension dim
function squash(blockarray, dim, sx, sy, sz, arraysize){
var i = new Array();
var dims = new Array();
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; 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; 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]);
*/
printToStl = function name(session, origin, sx, sy, sz) {
var count = 0;
var finalcount = 0;
var email = "blair@cc.gatech.edu";
var blocks1 = new Array();
var blocklist1 = new Array();
var blocks2 = new Array();
var blocklist2 = new Array();
player.print("Starting print");
//copy blocks into a js array
for (var y = 0; y <= sy; y++){
blocks1[y] = new Array();
blocks2[y] = new Array();
for (var x = 0; x <= sx; x++){
blocks1[y][x] = new Array();
blocks2[y][x] = new Array();
for (var z = 0; z <= sz; z++) {
blocks1[y][x][z] = new Array();
blocks2[y][x][z] = new Array();
var pt = origin.add(x, y, z);
var id = session.getBlockType(pt);
if (id == BlockID.BRICK || id == BlockID.CLOTH || id == BlockID.COBBLESTONE
|| id == BlockID.MOSSY_COBBLESTONE || id == BlockID.REDSTONE_ORE
|| id == BlockID.REDSTONE_BLOCK
|| id == BlockID.DIAMOND_BLOCK || id == BlockID.DIAMOND_ORE
|| id == BlockID.EMERALD_BLOCK || id == BlockID.EMERALD_ORE
|| id == BlockID.GOLD_BLOCK || id == BlockID.GOLD_ORE
|| id == BlockID.COAL_ORE
|| id == BlockID.GLASS || id == BlockID.STONE || id == BlockID.TNT
|| id == BlockID.QUARTZ_BLOCK || id == BlockID.QUARTZ_ORE
|| id == BlockID.LAPIS_LAZULI_BLOCK || id == BlockID.LAPIS_LAZULI_ORE
|| id == BlockID.BEDROCK || id == BlockID.OBSIDIAN
|| id == BlockID.NETHER_BRICK
|| id == BlockID.IRON_BLOCK || id == BlockID.IRON_ORE || id == BlockID.STONE_BRICK
|| id == BlockID.LEAVES) {
blocks1[y][x][z] = new Array();
blocks1[y][x][z][0] = 1;
blocks1[y][x][z][1] = 1;
blocks1[y][x][z][2] = 1;
blocks2[y][x][z] = false;
count++;
} else if (id == BlockID.GRASS || id == BlockID.DIRT || id == BlockID.SAND
|| id == BlockID.SANDSTONE || id == BlockID.SLOW_SAND
|| id == BlockID.GRAVEL || id == BlockID.CLAY || id == BlockID.SOIL
|| id == BlockID.ICE || id == BlockID.SNOW || id == BlockID.SNOW_BLOCK
|| id == BlockID.LOG || id == BlockID.WOOD) {
blocks1[y][x][z] = false;
blocks2[y][x][z] = new Array();
blocks2[y][x][z][0] = 1;
blocks2[y][x][z][1] = 1;
blocks2[y][x][z][2] = 1;
count++;
} else {
/*
other blocks I want to support:
BED
BOOKCASE
BREWING_STAND
CAULDRON
CHEST
ENDER_CHEST
LOCKED_CHEST
ENCHANTMENT_TABLE
TRAPPED_CHEST
WORKBENCH
FURNACE
BURNING_FURNACE
END_PORTAL
END_PORTAL_FRAME
END_STONE
PORTAL
CACTUS
CAKE_BLOCK
COBBLESTONE_WALL
HEAD
HOPPER
JACKOLANTERN
JUKEBOX
GLOWING_REDSTONE_ORE
LIGHTSTONE
STATIONARY_LAVA
STATIONARY_WATER
// flowing, I think; can't deal with that!
WATER
LAVA
LEAVES
LILY_PAD
WEB
BIRCH_WOOD_STAIRS
BRICK_STAIRS
COBBLESTONE_STAIRS
JUNGLE_WOOD_STAIRS
NETHER_BRICK_STAIRS
OAK_WOOD_STAIRS
QUARTZ_STAIRS
SANDSTONE_STAIRS
SPRUCE_WOOD_STAIRS
STONE_BRICK_STAIRS
WOODEN_STAIRS
STEP
DOUBLE_STEP
DOUBLE_WOODEN_STEP
WOODEN_STEP
IRON_BARS
IRON_DOOR
WOODEN_DOOR
TRAP_DOOR
WALL_SIGN
LADDER
LEVER
PRESSURE_PLATE_HEAVY
PRESSURE_PLATE_LIGHT
TORCH
REDSTONE_TORCH_OFF
REDSTONE_TORCH_ON
STONE_BUTTON
STONE_PRESSURE_PLATE
WOODEN_BUTTON
WOODEN_PRESSURE_PLATE
// unclear what these would do
GLASS
GLASS_PANE
// HARD because of the intersection and corner variations
MINECART_TRACKS
POWERED_RAIL
NETHER_BRICK_FENCE
FENCE
FENCE_GATE
*/
blocks1[y][x][z] = false;
blocks2[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(blocks1, 0, sx, sy, sz, sy);
squash(blocks1, 1, sx, sy, sz, sx);
squash(blocks1, 2, sx, sy, sz, sz);
squash(blocks2, 0, sx, sy, sz, sy);
squash(blocks2, 1, sx, sy, sz, sx);
squash(blocks2, 2, sx, sy, sz, sz);
//write them into a list
for (var y = 0; y <= sy; y++){
for (var x = 0; x <= sx; x++){
for (var z = 0; z <= sz; z++){
var thisblock = blocks1[y][x][z];
if(thisblock){
//note swapping axes
blocklist1.push([-(x + thisblock[1]), z, y, thisblock[1], thisblock[2], thisblock[0]]);
finalcount++;
}
thisblock = blocks2[y][x][z];
if(thisblock){
//note swapping axes
blocklist2.push([-(x + thisblock[1]), z, 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 = [" + blocklist1.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 path = "../models/file1" + 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();
src = "scale = 2.5;for(c = [" + blocklist2.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"
path = "../models/file2" + uuid + "_scan.scad"
out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();
//path = "../scans/inbox/" + uuid + "_email.txt"
path = "../models/file" + 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);
}
}
}
//context.checkArgs(1, -1, "<block> [width] [length]");
var sess = context.remember();
var sel = context.getSession().getSelection(sess.world);
var origin = sel.getMinimumPoint();
var sx = sel.getWidth() - 1;
var sy = sel.getHeight() - 1;
var sz = sel.getLength() - 1;
player.print("Building model starting at " + origin + ", of size (" + sx + "," + sy + "," + sz + ",");
printToStl(sess, origin, sx, sy, sz);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment