MoonlightOwl

Print3D by Sangar

Apr 1st, 2015
4,165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local shell = require("shell")
  3.  
  4. addresses = {}
  5. for address in component.list("printer3d") do
  6.   table.insert(addresses, address)
  7.   print(#addresses .. ": " .. address)
  8. end
  9. if #addresses > 1 then
  10.   io.write("Choose printer: ")
  11.   local index
  12.   repeat
  13.     index = tonumber(io.read("*n"))
  14.     if not (index and addresses[index]) then
  15.       io.write("\nInvalid index!\nChoose printer: ")
  16.     end
  17.   until index and addresses[index]
  18.   component.setPrimary("printer3d", addresses[index])
  19. end
  20.  
  21. local printer = component.printer3d
  22.  
  23. local args = shell.parse(...)
  24. if #args < 1 then
  25.   io.write("Usage: print3d FILE [count]\n")
  26.   os.exit(0)
  27. end
  28. local count = 1
  29. if #args > 1 then
  30.   count = assert(tonumber(args[2]), tostring(args[2]) .. " is not a valid count")
  31. end
  32.  
  33. local file, reason = io.open(args[1], "r")
  34. if not file then
  35.   io.stderr:write("Failed opening file: " .. reason .. "\n")
  36.   os.exit(1)
  37. end
  38.  
  39. local rawdata = file:read("*all")
  40. file:close()
  41. local data, reason = load("return " .. rawdata)
  42. if not data then
  43.   io.stderr:write("Failed loading model: " .. reason .. "\n")
  44.   os.exit(2)
  45. end
  46. data = data()
  47.  
  48. io.write("Configuring...\n")
  49.  
  50. printer.reset()
  51. if data.label then
  52.   printer.setLabel(data.label)
  53. end
  54. if data.tooltip then
  55.   printer.setTooltip(data.tooltip)
  56. end
  57. if data.lightLevel and printer.setLightLevel then -- as of OC 1.5.7
  58.   printer.setLightLevel(data.lightLevel)
  59. end
  60. if data.emitRedstone then
  61.   printer.setRedstoneEmitter(data.emitRedstone)
  62. end
  63. if data.buttonMode then
  64.   printer.setButtonMode(data.buttonMode)
  65. end
  66. if data.collidable and printer.setCollidable then
  67.   printer.setCollidable(not not data.collidable[1], not not data.collidable[2])
  68. end
  69. for i, shape in ipairs(data.shapes or {}) do
  70.   local result, reason = printer.addShape(shape[1], shape[2], shape[3], shape[4], shape[5], shape[6], shape.texture, shape.state, shape.tint)
  71.   if not result then
  72.     io.write("Failed adding shape: " .. tostring(reason) .. "\n")
  73.   end
  74. end
  75.  
  76. io.write("Label: '" .. (printer.getLabel() or "not set") .. "'\n")
  77. io.write("Tooltip: '" .. (printer.getTooltip() or "not set") .. "'\n")
  78. if printer.getLightLevel then -- as of OC 1.5.7
  79.   io.write("Light level: " .. printer.getLightLevel() .. "\n")
  80. end
  81. io.write("Redstone level: " .. select(2, printer.isRedstoneEmitter()) .. "\n")
  82. io.write("Button mode: " .. tostring(printer.isButtonMode()) .. "\n")
  83. if printer.isCollidable then -- as of OC 1.5.something
  84.   io.write("Collidable: " .. tostring(select(1, printer.isCollidable())) .. "/" .. tostring(select(2, printer.isCollidable())) .. "\n")
  85. end
  86. io.write("Shapes: " .. printer.getShapeCount() .. " inactive, " .. select(2, printer.getShapeCount()) .. " active\n")
  87.  
  88. local result, reason = printer.commit(count)
  89. if result then
  90.   io.write("Job successfully committed!\n")
  91. else
  92.   io.stderr:write("Failed committing job: " .. tostring(reason) .. "\n")
  93. end
RAW Paste Data