import sys import re #open file passed as argument if len(sys.argv)>1: f = open(sys.argv[1],'r') else: print('usage: python hpgl2code.py inputfile.hpgl > outputfile.gcode') sys.exit(1) #decoded commands #PA x,y; //pen advance xy unit=25um ie 40 units/mm #PU; //pen up - replace with G1 Z'up' (safe Z heigh constant) #PD; //pen down - - replace with G1 Z'dn' (engrawing depth constant) #constants zup=1 # 5mm should be safe enough zdn=0 # using 0, for engraving negative values to be used upm=40.2 # hpgl units per mm (in theory should be 40.2) epm=0 # extrude per mm - in case I'm trying to plot with PLA feed="3000" #default feedrate #init stuff print "(--------------------------------)" print "(- HPGL to G-Code Converter -)" print "(- (C)2016-10-12 - Paolo Iocco) -)" print "(--------------------------------)" print "" print "(CNC Setup)" print "G21" # units in mm print "M300 S50 (pen up)" print "G4 P150 (wait 150ms)" print "G1 X0.000 Y0.000 F"+feed print "(Plots a point in 0,0)" print "M300 S30 (pen down)" print "G4 P150 (wait 150ms)" print "M300 S50 (pen up)" print "G4 P150 (wait 150ms)" print "(-------------)" print "(- Start -)" print "(-------------)" #process the input file pa = re.compile('^PA ([0-9]*),([0-9]*);') #regex for parsing PA command for line in f: if re.search('^PA',line): #parse a bit mpa=pa.match(line); x=float(mpa.group(1))/upm y=float(mpa.group(2))/upm print "G1 X"+"%.3f" % x," Y"+"%.3f" %y," F"+feed else: if re.search('^PU',line ): #print "G1 Z",zup," F"+feed print "M300 S50 (pen up)" print "G4 P150 (wait 150ms)" else: if re.search('^PD',line ): #print "G1 Z",zdn," F"+feed print "M300 S30 (pen down)" print "G4 P150 (wait 150ms)" print "" print "(-----------)" print "(- End -)" print "(-----------)" print "G1 X0.00 Y0.00 F500.0 (Go Home)"