Skip to content

Instantly share code, notes, and snippets.

@fogleman
Created January 10, 2015 22:20
Show Gist options
  • Save fogleman/5970e5afbd482701ee0c to your computer and use it in GitHub Desktop.
Save fogleman/5970e5afbd482701ee0c to your computer and use it in GitHub Desktop.
G-Code Mountain
def load_blocks(path):
height = {}
with open(path, 'r') as fp:
for line in fp:
x, y, z = map(int, line.strip().split('|'))
height[(x, z)] = max(y, height.get((x, z), 0))
return height
def main():
height = load_blocks('blocks.txt')
max_height = max(height.values())
x_size = 5.0
y_size = 5.0
z_size = 7 / 8.0
z_clear = 0.5
size = 64
pz = 0.0
print 'G17'
print 'G0Z%f' % z_clear
print 'M3'
print 'M8'
for x in range(-size, size + 1):
tx = x_size * (x + size) / (size * 2)
# raise bit
print 'G0Z%f' % z_clear
# move to start of row (y=0)
if x % 2:
print 'G0X%0.6fY%0.6f' % (tx, y_size)
else:
print 'G0X%0.6fY0' % tx
pz = z_clear
for y in range(-size, size + 1):
if x % 2:
y = -y
ty = y_size * (y + size) / (size * 2)
z = height.get((x, y), 0)
tz = z_size * z / max_height - z_size
print 'G1X%0.6fY%0.6fZ%0.6fF75' % (tx, ty, tz)
# # raise z if needed
# if tz > pz:
# print 'G1Z%fF75' % tz
# # move to xy
# print 'G1X%0.6fY%0.6fF75' % (tx, ty)
# # lower z if needed
# if tz < pz:
# print 'G1Z%fF75' % tz
pz = z
print 'M5'
print 'M9'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment