Advertisement
asweigart

3dprinter

Aug 4th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. -- 3D Printer program
  2. -- By Al Sweigart
  3. -- turtleappstore.com/users/AlSweigart
  4. -- Builds based on a blueprint and legend file.
  5.  
  6. os.loadAPI('hare')
  7.  
  8. -- handle command line arguments
  9. local cliArgs = {...}
  10. blueprint = cliArgs[1]
  11. legend = cliArgs[2]
  12.  
  13. if legend == nil or cliArgs[1] == '?' then
  14. print('Usage: 3dprint <blueprint> <legend>')
  15. print('See source for blueprint & legend file')
  16. print('format details.')
  17. return
  18. end
  19.  
  20. if not fs.exists(blueprint) then
  21. error(blueprint .. ' file does not exist.')
  22. end
  23.  
  24. if not fs.exists(legend) then
  25. error(legend .. ' file does not exist.')
  26. end
  27.  
  28.  
  29. -- read in the legend file
  30. local legendData = {}
  31. local legendFile = fs.open(legend, 'r')
  32.  
  33. -- read in first line
  34. print('Legend:')
  35. local line = legendFile.readLine()
  36. while line ~= nil do
  37. -- remove leading & trailing whitespace
  38. line = hare.trim(line)
  39. print('LINE', line)
  40.  
  41. -- only parse if this line is not blank or a comment
  42. if line ~= '' and string.find(line, '--') == nil then
  43. local equalPos = string.find(line, '=')
  44.  
  45. -- make sure line is valid
  46. if equalPos == nil then
  47. error('Missing equal sign: ' + tostring(line))
  48. end
  49.  
  50. local symbol = string.sub(line, 1, equalPos - 1)
  51. local block = string.sub(line, equalPos + 1)
  52. legendData[symbol] = block
  53. print(symbol, block)
  54. end
  55.  
  56. -- read in next line
  57. line = legendFile.readLine()
  58. end
  59. print()
  60.  
  61. print('debug: read legend file')
  62.  
  63. -- validate the blueprint file
  64. local blueprintFile = fs.open(blueprint, 'r')
  65. line = blueprintFile.readLine()
  66. local blueprintWidth = nil
  67. local blueprintLength = 0
  68. local i
  69. while line ~= nil do
  70. -- remove leading & trailing whitespace
  71. line = hare.trim(line)
  72.  
  73. -- only parse if this line is not blank or a comment
  74. if line ~= '' and string.find(line, '--') == nil then
  75. if blueprintWidth == nil then
  76. -- set the width based on the first line
  77. blueprintWidth = #line
  78. else
  79. if #line ~= blueprintWidth then
  80. error('Width of line is not ' + tostring(blueprintWidth) + ': ' + line)
  81. end
  82. end
  83.  
  84. -- make sure all symbols exist in legendData
  85. for i = 1, blueprintWidth do
  86. if legendData[string.sub(line, i, i)] == nil then
  87. error('Symbol ' + string.sub(line, i, i) + ' does not exist in legend.')
  88. end
  89. end
  90.  
  91. blueprintLength = blueprintLength + 1
  92. end
  93. line = blueprintFile.readLine()
  94. end
  95.  
  96. print('debug: validated blueprint')
  97.  
  98. -- read in the blueprint file
  99. local blueprintData = {}
  100. blueprintFile = fs.open(blueprint, 'r')
  101.  
  102. -- read in first line
  103. line = blueprintFile.readLine()
  104.  
  105. while line ~= nil do
  106. -- remove leading & trailing whitespace
  107. line = hare.trim(line)
  108.  
  109. -- only parse if this line is not blank or a comment
  110. if line ~= '' and string.find(line, '--') then
  111. end
  112.  
  113. line = blueprintFile.readLine()
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement