Gintarus

3d printer

Sep 10th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. local component = require("component")
  2. local shell = require("shell")
  3. local gpu = component.gpu
  4. local x , y = gpu.getResolution()
  5.  
  6. local function setColor(c)
  7. if c and gpu.getForeground() ~= c then
  8. gpu.setForeground(c)
  9. end
  10. end
  11.  
  12. local function cwrite(c, ...)
  13. local oldCol = gpu.getForeground()
  14. setColor(c)
  15. io.write(...)
  16. setColor(oldCol)
  17. end
  18.  
  19. local args = shell.parse(...)
  20. if #args < 1 then
  21. io.write("Используйте: printAll FILE [Количество] [Номер блока]\n")
  22. os.exit(0)
  23. end
  24.  
  25. local count = tonumber(args[2]) or 1
  26. local block = tonumber(args[3]) or nil
  27.  
  28. -- Загружаем файл
  29. local file, reason = io.open(args[1], "r")
  30. if not file then
  31. io.stderr:write("Ошибка при открытии файла: " .. reason .. "\n")
  32. os.exit(1)
  33. end
  34.  
  35. local rawdata = file:read("*all")
  36. file:close()
  37. local data, reason = load("return " .. rawdata)
  38. if not data then
  39. io.stderr:write("Ошибка загрузки модели: " .. reason .. "\n")
  40. os.exit(2)
  41. end
  42.  
  43. -- Фильтруем модели
  44. local data1 = {}
  45. for _, i in pairs({data()}) do
  46. if (#i.shapes > 0) then
  47. table.insert(data1, i)
  48. end
  49. end
  50.  
  51. cwrite(nil,"Блоков найдено: ")
  52. cwrite(0x00ffff, #data1, "\n")
  53.  
  54. -- Получаем список принтеров
  55. local printers = {}
  56. for addr in component.list("printer3d") do
  57. table.insert(printers, component.proxy(addr))
  58. end
  59.  
  60. cwrite(nil,"Принтеров найдено: ")
  61. cwrite(0x00ffff, #printers, "\n")
  62.  
  63. if #printers == 0 then
  64. io.stderr:write("Нет доступных принтеров!\n")
  65. os.exit(3)
  66. end
  67.  
  68. -- Очередь печати
  69. local blockNumber = block or 1
  70. while blockNumber <= #data1 do
  71. for _, printer in ipairs(printers) do
  72. if blockNumber > #data1 then break end
  73. if printer.status() == "idle" then
  74. local m = data1[blockNumber]
  75. printer.reset()
  76. printer.setLabel(m.label or ("Block "..blockNumber))
  77. printer.setTooltip(m.tooltip or ("["..blockNumber.."]"))
  78. if m.lightLevel and printer.setLightLevel then
  79. printer.setLightLevel(m.lightLevel)
  80. end
  81. if m.emitRedstone then
  82. printer.setRedstoneEmitter(m.emitRedstone)
  83. end
  84. if m.buttonMode then
  85. printer.setButtonMode(m.buttonMode)
  86. end
  87. for _, shape in ipairs(m.shapes) do
  88. local result, reason = printer.addShape(
  89. shape[1], shape[2], shape[3], shape[4], shape[5], shape[6],
  90. shape.texture, shape.state, shape.tint
  91. )
  92. if not result then
  93. io.write("Ошибка добавления фигуры: " .. tostring(reason) .. "\n")
  94. end
  95. end
  96. local result, reason = printer.commit(count)
  97. if result then
  98. cwrite(0x00ff00,"Принтер "..tostring(printer.address).." печатает блок "..blockNumber.."\n")
  99. blockNumber = blockNumber + 1
  100. else
  101. io.stderr:write("Ошибка печати: " .. tostring(reason) .. "\n")
  102. end
  103. end
  104. end
  105. os.sleep(1) -- проверяем принтеры несколько раз в секунду
  106. end
  107.  
  108. cwrite(nil,"Все задания отправлены на печать\n")
  109.  
Advertisement
Add Comment
Please, Sign In to add comment