184 lines
5.0 KiB
Python
Executable File
184 lines
5.0 KiB
Python
Executable File
from __future__ import division
|
|
import serial
|
|
|
|
|
|
|
|
|
|
|
|
class Command:
|
|
|
|
def __init__(self, name, *args):
|
|
self.name = name # hpgl Command name e.g. PU, PA,
|
|
self.args = args # list of Position args
|
|
|
|
@property
|
|
def x(self):
|
|
return self.args[0] \
|
|
if self.name in ("PA", "PD", "PR", "PU" ) and self.args else \
|
|
None # Only these Commads have args
|
|
|
|
@property
|
|
def y(self):
|
|
return self.args[1] \
|
|
if self.name in ("PA", "PD", "PR", "PU" ) and self.args else \
|
|
None
|
|
|
|
def __len__(self):
|
|
return len(str(self))
|
|
|
|
def __str__(self):
|
|
return self.name + ",".join(str(arg) for arg in self.args) + ";"
|
|
|
|
def __mul__(self, factor):
|
|
if self.name not in ("PA", "PD", "PR", "PU" ) or not self.args:
|
|
return self
|
|
if type(factor) == type(0) or type(factor) == type(0.0):
|
|
factor = (factor, factor)
|
|
return Command(self.name,
|
|
int(self.x * factor[0]),
|
|
int(self.y * factor[1]))
|
|
|
|
|
|
def __add__(self, addend):
|
|
if self.name not in ("PA", "PD", "PR", "PU" ) or not self.args:
|
|
return self
|
|
if type(addend) == type(0) or type(addend) == type(0.0):
|
|
addend = (addend, addend)
|
|
return Command(self.name,
|
|
int(self.x + addend[0]),
|
|
int(self.y + addend[1]))
|
|
|
|
def __sub__(self, addend):
|
|
if type(addend) == type(0) or type(addend) == type(0.0):
|
|
addend = (addend, addend)
|
|
return self + (-addend[0], -addend[1])
|
|
|
|
class Program:
|
|
|
|
def __init__(self, commands=[]):
|
|
self.commands = commands
|
|
|
|
def parsefile(self, filename):
|
|
with open(filename) as file:
|
|
self.parse( file.read())
|
|
|
|
def parse(self, code):
|
|
for command in code.strip().split(";")[:-1]:
|
|
name, args = command[:2], command[2:]
|
|
args = [int(arg) for arg in args.split(",")] if args else []
|
|
#print name , args
|
|
self.commands.append(Command(name, *args))
|
|
|
|
def __str__(self):
|
|
return "".join(str(command) for command in self.commands)
|
|
|
|
|
|
def __mul__(self, arg):
|
|
return Program([command * arg for command in self.commands])
|
|
|
|
|
|
def __add__(self, arg):
|
|
return Program([command + arg for command in self.commands])
|
|
|
|
def __sub__(self, arg):
|
|
return Program([command - arg for command in self.commands])
|
|
|
|
def __len__(self):
|
|
return len(str(self))
|
|
|
|
@property
|
|
def xmax(self):
|
|
return max(command.x for command in self.commands if command.x)
|
|
@property
|
|
def xmin(self):
|
|
return min(command.x for command in self.commands if command.x)
|
|
@property
|
|
def ymax(self):
|
|
return max(command.y for command in self.commands if command.y)
|
|
@property
|
|
def ymin(self):
|
|
return min(command.y for command in self.commands if command.y)
|
|
@property
|
|
def center(self):
|
|
return (self.xmin+self.xmax)/2,(self.ymin+self.ymax)/2
|
|
@property
|
|
def size(self): return self.xmax-self.xmin , self.yax - self.ymin
|
|
|
|
@property
|
|
def winsize(self): return self.xmax-self.xmin , self.ymax - self.ymin
|
|
|
|
def centralize(self):
|
|
return self-self.center
|
|
#arg= self.center
|
|
#return Program([command - arg for command in self.commands])
|
|
|
|
def oob(self, pltr):
|
|
|
|
return (self.xmax > pltr.xmax or self.xmin < pltr.xmin or self.ymin < pltr.ymin or self.ymax > pltr.ymax)
|
|
|
|
def full(self,pltr):
|
|
arg = min(pltr.winsize[0]/self.winsize[0],pltr.winsize[1]/self.winsize[1])
|
|
return Program([command * arg for command in self.commands])
|
|
|
|
|
|
class Plotter:
|
|
|
|
def __init__(self):
|
|
try:
|
|
self.ser = serial.Serial('/dev/ttyUSB0', 9600)
|
|
self.ser.write('OW;')
|
|
s = []
|
|
while True:
|
|
x = self.ser.read()
|
|
if x == "\x0d":
|
|
break
|
|
s.append(x)
|
|
if not s:
|
|
print 'I/O Fehler ist der Plotter bereit'
|
|
self.__boundaries = tuple(int(x) for x in "".join(s).split(","))
|
|
|
|
except OSError:
|
|
self.__boundaries = -1000,-1000,1000,1000
|
|
|
|
print "No such file or directory: '/dev/ttyUSB0'"
|
|
|
|
|
|
|
|
|
|
@property
|
|
def xmin(self):
|
|
return self.__boundaries[0]
|
|
|
|
@property
|
|
def ymin(self):
|
|
return self.__boundaries[1]
|
|
|
|
@property
|
|
def xmax(self):
|
|
return self.__boundaries[2]
|
|
|
|
@property
|
|
def ymax(self):
|
|
return self.__boundaries[3]
|
|
|
|
@property
|
|
def center(self):
|
|
return (self.xmin + self.xmax)/2, (self.ymin + self.ymax)/2
|
|
|
|
def write(self,programm):
|
|
self.ser.write(str(programm))
|
|
@property
|
|
def winsize(self):
|
|
return self.xmax-self.xmin , self.ymax - self.ymin
|
|
|
|
|
|
plt=Plotter()
|
|
p=Program()
|
|
p.parsefile("kreis.hpgl" )
|
|
|
|
|
|
print len(p)
|
|
plt.write(p)
|
|
|
|
|