Skip to content

Instantly share code, notes, and snippets.

@caizixian
Last active March 22, 2023 09:17
Show Gist options
  • Save caizixian/e68a2a519cec85fdf0b6 to your computer and use it in GitHub Desktop.
Save caizixian/e68a2a519cec85fdf0b6 to your computer and use it in GitHub Desktop.
G Code Generator
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
def GetPoint(function,xaxis):
function=function.replace("^","**")
function=function.replace("x",str('('+str(xaxis)+')'))
return eval(function)
def main():
original_function=raw_input("Input something like y=x^2\n")
useful_function=original_function[2:]
range_start=float(raw_input("Input where x starts like 2\n"))
range_end=float(raw_input("Input where x ends like 5\n"))
deci=float(raw_input("input what precision you need like 0.1\n"))
ifcm=bool(raw_input("would you like it in cm True or False\n"))
#speed=float(raw_input("input what speed you need like 5.0\n"))
start_x=range_start
start_y=GetPoint(useful_function,start_x)
xaxis=range_start
print "G90\nG21"
print "G0 X%f Y%f" % (start_x,start_y)
print
print 'M03'
#print 'G1F%f' % speed
while xaxis<=range_end+deci:
if ifcm:
print 'G01 X%f Y%f' % (xaxis*10,GetPoint(useful_function,xaxis)*10)
else:
print 'G01 X%f Y%f' % (xaxis,GetPoint(useful_function,xaxis))
xaxis+=deci
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment