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 __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]) @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.ymin)/2,(self.ymin+self.ymax)/2 @property def size(self): return self.xmax-self.xmin , self.yax - self.ymin class Plotter: def __init__(self): try: self.ser = serial.Serial('/dev/ttyUSB0', 9600) self.ser.write('OW;') s = [] if not s: print 'I/O Fehler ist der Plotter bereit' while True: x = self.ser.read() if x == "\x0d": break s.append(x) self.__boundaries = tuple(int(x) for x in "".join(s).split(",")) except OSError: self.__boundaries = 0,0,0,0 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.yax - self.ymin p = Program() p.parsefile("Clitoris.hpgl" ) #print p p=p*(1.2,-1.2) p= p-(4000,-4500) plt=Plotter() print p.xmax print p.ymax print p.xmin print p.ymin print p.center #plt.write(p) #print getWin()