Advertisement
klindley

print3dm

Feb 7th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. -- print3d-multiple
  2. -- takes a file with multiple models, sends then to the printer one by one.
  3. local component = require("component")
  4. local shell = require("shell")
  5. local ser = require("serialization")
  6. local event = require("event")
  7.  
  8. function print_r ( t )
  9. local print_r_cache={}
  10. local function sub_print_r(t,indent)
  11. if (print_r_cache[tostring(t)]) then
  12. print(indent.."*"..tostring(t))
  13. else
  14. print_r_cache[tostring(t)]=true
  15. if (type(t)=="table") then
  16. for pos,val in pairs(t) do
  17. if (type(val)=="table") then
  18. print(indent.."["..pos.."] => "..tostring(t).." {")
  19. sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
  20. print(indent..string.rep(" ",string.len(pos)+6).."}")
  21. else
  22. print(indent.."["..pos.."] => "..tostring(val))
  23. end
  24. end
  25. else
  26. print(indent..tostring(t))
  27. end
  28. end
  29. end
  30. sub_print_r(t," ")
  31. end
  32.  
  33. addresses = {}
  34. for address in component.list("printer3d") do
  35. table.insert(addresses, address)
  36. print(#addresses .. ": " .. address)
  37. end
  38. if #addresses > 1 then
  39. io.write("Choose printer: ")
  40. local index
  41. repeat
  42. index = tonumber(io.read("*n"))
  43. if not (index and addresses[index]) then
  44. io.write("\nInvalid index!\nChoose printer: ")
  45. end
  46. until index and addresses[index]
  47. component.setPrimary("printer3d", addresses[index])
  48. end
  49.  
  50. local printer = component.printer3d
  51.  
  52. local args = shell.parse(...)
  53. if #args < 1 then
  54. io.write("Usage: print3d FILE [count]\n")
  55. os.exit(0)
  56. end
  57. local count = 1
  58. if #args > 1 then
  59. count = assert(tonumber(args[2]), tostring(args[2]) .. " is not a valid count")
  60. end
  61.  
  62. local file, reason = io.open(args[1], "r")
  63. if not file then
  64. io.stderr:write("Failed opening file: " .. reason .. "\n")
  65. os.exit(1)
  66. end
  67.  
  68. local rawdata = file:read("*all")
  69. file:close()
  70. local data, reason = load("return " .. rawdata)
  71. if not data then
  72. io.stderr:write("Failed loading model: " .. reason .. "\n")
  73. os.exit(2)
  74. end
  75.  
  76. data = ser.unserialize(rawdata)
  77.  
  78. function printed(d)
  79. io.write("Configuring...\n")
  80.  
  81. printer.reset()
  82. if d.label then
  83. printer.setLabel(d.label)
  84. end
  85. if d.tooltip then
  86. printer.setTooltip(d.tooltip)
  87. end
  88. if d.lightLevel and printer.setLightLevel then -- as of OC 1.5.7
  89. printer.setLightLevel(d.lightLevel)
  90. end
  91. if d.emitRedstone then
  92. printer.setRedstoneEmitter(d.emitRedstone)
  93. end
  94. if d.buttonMode then
  95. printer.setButtonMode(d.buttonMode)
  96. end
  97. if d.collidable and printer.setCollidable then
  98. printer.setCollidable(not not d.collidable[1], not not d.collidable[2])
  99. end
  100. for i, shape in ipairs(d.shapes or {}) do
  101. local result, reason = printer.addShape(shape[1], shape[2], shape[3], shape[4], shape[5], shape[6], shape.texture, shape.state)
  102. if not result then
  103. io.write("Failed adding shape: " .. tostring(reason) .. "\n")
  104. end
  105. end
  106.  
  107. io.write("Label: '" .. (printer.getLabel() or "not set") .. "'\n")
  108. io.write("Tooltip: '" .. (printer.getTooltip() or "not set") .. "'\n")
  109. if printer.getLightLevel then -- as of OC 1.5.7
  110. io.write("Light level: " .. printer.getLightLevel() .. "\n")
  111. end
  112. io.write("Redstone level: " .. select(2, printer.isRedstoneEmitter()) .. "\n")
  113. io.write("Button mode: " .. tostring(printer.isButtonMode()) .. "\n")
  114. if printer.isCollidable then -- as of OC 1.5.something
  115. io.write("Collidable: " .. tostring(select(1, printer.isCollidable())) .. "/" .. tostring(select(2, printer.isCollidable())) .. "\n")
  116. end
  117. io.write("Shapes: " .. printer.getShapeCount() .. " inactive, " .. select(2, printer.getShapeCount()) .. " active\n")
  118.  
  119. local result, reason = printer.commit(count)
  120. if result then
  121. io.write("Job successfully committed!\n")
  122. else
  123. io.stderr:write("Failed committing job: " .. tostring(reason) .. "\n")
  124. end
  125. end
  126.  
  127. for i, value in pairs(data) do
  128. --print_r(value)
  129. --print(value["label"])
  130. printed(value)
  131. print("Press any key to continue...")
  132. event.pull("key_down")
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement