HPGLWrap/HPGLWrap.py

90 lines
2.8 KiB
Python
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import os
import re
import sys
# Based off of notes from
# https://github.com/fellesverkstedet/gcc-laserpro-driver
# and http://www.wiki.cl.cam.ac.uk/rowiki/CompArch/HardwareLab/LaserCutter
# and src/core/path_gcc.c from
# https://github.com/fellesverkstedet/fabmodules
def write_pen_values(values, char):
return "!v64" + char + "".join(["{:04d}".format(value) for value in values])
def pre_content(filename, power, speed, rate, pen_settings):
jobName = os.path.basename(filename)[:80]
# Start
outString = "@PJL JOB NAME={}\r\n".format(jobName)
outString += "E@PJL ENTER LANGUAGE=PCL\r\n%-12345X"
# Reset
outString += "E"
# Unit
outString += "&u254D"
# Cursor
outString += "*p100X"
outString += "*p100Y"
# Set filename
outString += "!m{:d}N".format(len(jobName)) + jobName
# Set raster power+velocity. Only affects raster. Shows in display before start
outString += "!r{:d}I".format(speed * 10)
outString += "!r{:d}P".format(power * 10);
# Select first (and only) pen (commented out in original source)
# outString += "!v1D"
# outString += "!m2A" # triggers autofocus
# Use same value for all 16 pens
# doesn't seem to affect vector settings
# TODO: try unsetting values in HPGL and see if these take over
outString += write_pen_values(pen_settings['power'], 'P') # Power, 1000=100%
outString += write_pen_values(pen_settings['speed'], 'V') # Velocity, 1000=100%
outString += write_pen_values(pen_settings['rate'], 'I') # PPI, does affect vector
# Enter HPGL vector mode
outString += "%1B"
return outString
def set_vector_pens(content, pen_settings):
settings = "".join(
["FS{power:d},{penNum:d};VS{speed:d},{penNum:d};".format(
power=pen_settings['power'][num],
speed=pen_settings['speed'][num],
penNum=num+1) for num in range(0, 16)])
# TODO: find out why slow movement in PU (pen up) until first PD (pen down)
content = re.sub(
"FS\d{1,4};VS\d{1,4};SP1;",
settings + "SP1;",
content, count=1)
return content
def post_content():
outString = "%1A" # Leave HPGL vector mode
outString += "E" # Reset
outString += "%-12345X@PJL EOJ \r\n" # End
return outString
def main(infile, outfile, power=50, speed=50, rate=100):
# set all pens the same
pen_settings = {'power': [int(power) * 10] * 16,
'speed': [int(speed) * 10] * 16,
'rate': [int(rate)] * 16}
with open(outfile, 'w') as f:
f.write(pre_content(
outfile, int(power), int(speed), int(rate), pen_settings))
with open(infile, 'r') as input_f:
f.write(set_vector_pens(input_f.read(), pen_settings))
f.write(post_content())
if __name__ == '__main__':
print(sys.argv)
main(*sys.argv[1:])