initial commit
This commit is contained in:
165
oldpy/backupcg5-class_backup.py
Executable file
165
oldpy/backupcg5-class_backup.py
Executable file
@@ -0,0 +1,165 @@
|
||||
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()
|
||||
|
||||
280
oldpy/cg5-boolean_exp.py
Executable file
280
oldpy/cg5-boolean_exp.py
Executable file
@@ -0,0 +1,280 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import division
|
||||
import serial
|
||||
import time
|
||||
import math
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Command:
|
||||
inicoms= ("IN", "SP", "LT")
|
||||
scalecoms=("PA", "PD", "PR", "PU", "CI" )
|
||||
movecoms=("PA", "PD", "PR", "PU" )
|
||||
|
||||
def __init__(self, name, *args):
|
||||
self.name = name # hpgl Command name e.g. PU, PA,
|
||||
self.args = args # list of Position args
|
||||
|
||||
@property
|
||||
def scalable(self):
|
||||
return True \
|
||||
if self.name in Command.scalecoms and self.args else \
|
||||
False
|
||||
@property
|
||||
def movable(self):
|
||||
return True \
|
||||
if self.name in Command.movecoms and self.args else \
|
||||
False
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self.args[0] \
|
||||
if self.movable else\
|
||||
None
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.args[1] \
|
||||
if self.movable 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 not self.scalable:
|
||||
return self
|
||||
if type(factor) == type(0) or type(factor) == type(0.0):
|
||||
factor = (factor, factor)
|
||||
return Command(self.name,
|
||||
int(self.args[0] * factor[0]),
|
||||
int(self.args[1] * factor[1])) \
|
||||
if len(self.args)>1 else\
|
||||
Command(self.name,int(self.args[0]*factor[0]) )
|
||||
|
||||
def __add__(self, addend):
|
||||
if not self.movable:
|
||||
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])
|
||||
|
||||
def rotate(self, angl):
|
||||
if not self.movable:
|
||||
return self
|
||||
cosa=math.cos(angl*math.pi/180)
|
||||
sina=math.sin(angl*math.pi/180)
|
||||
return Command(self.name,int(self.x*cosa-self.y*sina),int(self.y*cosa+self.x*sina))
|
||||
|
||||
def flip(self):
|
||||
if not self.movable:
|
||||
return self
|
||||
return Command(self.name,self.y,-self.x)
|
||||
|
||||
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))
|
||||
|
||||
|
||||
def rotate(self, angl):
|
||||
return Program([command.rotate(angl) for command in self.commands])
|
||||
|
||||
def flip(self):
|
||||
return Program([command.flip() for command in self.commands])
|
||||
|
||||
|
||||
|
||||
@property
|
||||
def xmax(self):
|
||||
return max(command.x for command in self.commands if command.x and command.movable)
|
||||
@property
|
||||
def xmin(self):
|
||||
return min(command.x for command in self.commands if command.x and command.movable)
|
||||
@property
|
||||
def ymax(self):
|
||||
return max(command.y for command in self.commands if command.y and command.movable)
|
||||
@property
|
||||
def ymin(self):
|
||||
return min(command.y for command in self.commands if command.y and command.movable)
|
||||
@property
|
||||
def center(self):
|
||||
#return 42
|
||||
return int((self.xmin+self.xmax)/2),int((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
|
||||
|
||||
@property
|
||||
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 self*arg
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Plotter:
|
||||
|
||||
def __init__(self):
|
||||
try:
|
||||
print 'try to open Serial'
|
||||
self.ser = serial.Serial('/dev/ttyUSB0', 9600,serial.EIGHTBITS,serial.PARITY_NONE,serial.STOPBITS_ONE,15)
|
||||
print ' sending: OW;'
|
||||
self.ser.write('OW;')
|
||||
print 'OW; sent, start reading'
|
||||
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'
|
||||
print 'Gerät sagt:' + str(s)
|
||||
self.__boundaries = tuple(int(x) for x in "".join(s).split(","))
|
||||
|
||||
except OSError:
|
||||
self.__boundaries = -2000,-2000,2000,2000
|
||||
self.ser=None
|
||||
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
|
||||
|
||||
|
||||
def oob(self,p):
|
||||
return (p.xmax > self.xmax or p.xmin < self.xmin or p.ymin < self.ymin or p.ymax > self.ymax)
|
||||
|
||||
@property
|
||||
def ready(self):
|
||||
try:
|
||||
print 'try to get Status'
|
||||
if not self.ser:
|
||||
|
||||
return False
|
||||
self.ser.write('OS;')
|
||||
print 'OW; sent, start reading'
|
||||
s = []
|
||||
while True:
|
||||
x = self.ser.read()
|
||||
if x == "\x0d":
|
||||
break
|
||||
s.append(x)
|
||||
if not s:
|
||||
return False
|
||||
print s
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
def plot(self,prog):
|
||||
if (not self.oob(prog)) and self.ready:
|
||||
self.write(prog)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
plt=Plotter()
|
||||
print "p"
|
||||
p=Program()
|
||||
#print plt.winsize
|
||||
print "parsefile"
|
||||
p.parsefile("sign")
|
||||
#p=p.rotate(15)
|
||||
#print str(p)
|
||||
p=p*(-0.2,0.2)
|
||||
#p=p.flip()
|
||||
#p=p.full(plt)
|
||||
p=p.centralize()
|
||||
|
||||
#print str(p)
|
||||
plt.plot(p)
|
||||
print p.winsize
|
||||
print len(p)
|
||||
#print str(p)
|
||||
|
||||
|
||||
183
oldpy/cg5-class.py
Executable file
183
oldpy/cg5-class.py
Executable file
@@ -0,0 +1,183 @@
|
||||
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)
|
||||
|
||||
|
||||
2769
oldpy/cg5-class_1.py
Executable file
2769
oldpy/cg5-class_1.py
Executable file
File diff suppressed because it is too large
Load Diff
92
oldpy/old-cg5.py
Executable file
92
oldpy/old-cg5.py
Executable file
@@ -0,0 +1,92 @@
|
||||
|
||||
import serial
|
||||
|
||||
def getWin():
|
||||
ser = serial.Serial('/dev/ttyUSB0', 9600)
|
||||
ser.write('OW;')
|
||||
s=[]
|
||||
if not s:
|
||||
print 'Datei Fehler'
|
||||
while True:
|
||||
x=ser.read()
|
||||
if x == "\x0d":
|
||||
break
|
||||
s.append(x)
|
||||
return tuple(int(x) for x in "".join(s).split(","))
|
||||
|
||||
def parse(filename):
|
||||
with open(filename) as file:
|
||||
contents = file.read()
|
||||
commands = []
|
||||
for command in contents.split(";"):
|
||||
name, args = command[:2], command[2:]
|
||||
args = [int(arg) for arg in args.split(",")] if args else []
|
||||
commands.append((name, args))
|
||||
return commands
|
||||
|
||||
def compile(commands):
|
||||
return ";".join(name + ",".join(str(arg) for arg in args)
|
||||
for name, args in commands)
|
||||
|
||||
def scale(commands, factor):
|
||||
for name,args in commands:
|
||||
if name in ("PU","PD","PA","PR"):
|
||||
for i in range(len(args)):
|
||||
args[i] = int(args[i]*factor)
|
||||
return commands
|
||||
|
||||
def left(commands, left):
|
||||
for name,args in commands:
|
||||
if name in ("PU","PD","PA","PR"):
|
||||
if len(args):
|
||||
args[1] +=left
|
||||
return commands
|
||||
|
||||
def up(commands, up):
|
||||
for name,args in commands:
|
||||
if name in ("PU","PD","PA","PR"):
|
||||
if len(args):
|
||||
args[0] +=up
|
||||
return commands
|
||||
|
||||
|
||||
def oob(commands):
|
||||
xmin, ymin, xmax, ymax = getWin()
|
||||
for name,args in commands:
|
||||
if name in ("PU","PD","PA","PR"):
|
||||
if xmin < args[0] <xmax or ymin < args[1] < ymax:
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
|
||||
def center(command):
|
||||
xmin, ymin, xmax, ymax = getWin()
|
||||
maxx = maxy = minx = miny = 0
|
||||
for name,args in command:
|
||||
if len(args)>1:
|
||||
if args[0] > maxx:
|
||||
maxx = args[0]
|
||||
if args[0] < minx:
|
||||
minx = args[0]
|
||||
if args[1] < miny:
|
||||
miny = args[1]
|
||||
if args[1] > maxy:
|
||||
maxy =args[1]
|
||||
xcent= int((xmax-xmin)/2)
|
||||
ycent= int((ymax-ymin)/2)
|
||||
centy= int((maxy-miny)/2)
|
||||
centx= int((maxx-minx)/2)
|
||||
return up(left(command,centy-ycent),centx-xcent)
|
||||
|
||||
|
||||
|
||||
out = center(scale(parse("vishnu.hpgl"),2.5))
|
||||
print out
|
||||
print'test'
|
||||
print oob(out)
|
||||
|
||||
|
||||
# print name, args
|
||||
ser = serial.Serial('/dev/ttyUSB0', 9600)
|
||||
if not oob(out):
|
||||
ser.write(compile(out))
|
||||
255
oldpy/roll.py
Executable file
255
oldpy/roll.py
Executable file
@@ -0,0 +1,255 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import division
|
||||
import serial
|
||||
import math
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Command:
|
||||
inicoms= ("IN", "SP", "LT")
|
||||
scalecoms=("PA", "PD", "PR", "PU", "CI" )
|
||||
movecoms=("PA", "PD", "PR", "PU" )
|
||||
|
||||
def __init__(self, name, *args):
|
||||
self.name = name # hpgl Command name e.g. PU, PA,
|
||||
self.args = args # list of Position args
|
||||
|
||||
@property
|
||||
def scalable(self):
|
||||
return True \
|
||||
if self.name in Command.scalecoms and self.args else \
|
||||
False
|
||||
@property
|
||||
def movable(self):
|
||||
return True \
|
||||
if self.name in Command.movecoms and self.args else \
|
||||
False
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self.args[0] \
|
||||
if self.movable else\
|
||||
None
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.args[1] \
|
||||
if self.movable 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 not self.scalable:
|
||||
return self
|
||||
if type(factor) == type(0) or type(factor) == type(0.0):
|
||||
factor = (factor, factor)
|
||||
return Command(self.name,
|
||||
int(self.args[0] * factor[0]),
|
||||
int(self.args[1] * factor[1])) \
|
||||
if len(self.args)>1 else\
|
||||
Command(self.name,int(self.args[0]*factor[0]) )
|
||||
|
||||
def __add__(self, addend):
|
||||
if not self.movable:
|
||||
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])
|
||||
|
||||
def rotate(self, angl):
|
||||
if not self.movable:
|
||||
return self
|
||||
cosa=math.cos(angl*math.pi/180)
|
||||
sina=math.sin(angl*math.pi/180)
|
||||
return Command(self.name,int(self.x*cosa-self.y*sina),int(self.y*cosa+self.x*sina))
|
||||
|
||||
def flip(self):
|
||||
if not self.movable:
|
||||
return self
|
||||
return Command(self.name,self.y,-self.x)
|
||||
|
||||
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))
|
||||
|
||||
def rotate(self, angl):
|
||||
return Program([command.rotate(angl) for command in self.commands])
|
||||
|
||||
def flip(self):
|
||||
return Program([command.flip() for command in self.commands])
|
||||
|
||||
|
||||
|
||||
@property
|
||||
def xmax(self):
|
||||
return max(command.x for command in self.commands if command.x and command.movable)
|
||||
@property
|
||||
def xmin(self):
|
||||
return min(command.x for command in self.commands if command.x and command.movable)
|
||||
@property
|
||||
def ymax(self):
|
||||
return max(command.y for command in self.commands if command.y and command.movable)
|
||||
@property
|
||||
def ymin(self):
|
||||
return min(command.y for command in self.commands if command.y and command.movable)
|
||||
@property
|
||||
def center(self):
|
||||
return int((self.xmin+self.xmax)/2),int((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
|
||||
|
||||
class Plotter:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.p0incenter = False
|
||||
s=self.getoutput('OW;')
|
||||
print s
|
||||
if not s:
|
||||
self.__boundaries = -2000,-2000,2000,2000
|
||||
self.ser=None
|
||||
else:
|
||||
self.__boundaries = tuple(int(x) for x in "".join(s).split(","))
|
||||
|
||||
def getoutput(self,outstr):
|
||||
try:
|
||||
self.ser = serial.Serial('/dev/ttyUSB0',timeout=15)
|
||||
print 'try to get Status'
|
||||
if not self.ser:
|
||||
print 'Plotter not available'
|
||||
return None
|
||||
self.ser.write(outstr)
|
||||
print 'device busy'
|
||||
s = []
|
||||
while True:
|
||||
x = self.ser.read()
|
||||
|
||||
if x == "\x0d" or not x:
|
||||
break
|
||||
s.append(x)
|
||||
return ''.join(s)
|
||||
except OSError:
|
||||
return None
|
||||
@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
|
||||
|
||||
def oob(self, prog):
|
||||
return (prog.xmax > self.xmax or prog.xmin < self.xmin or prog.ymin < self.ymin or prog.ymax > self.ymax)
|
||||
|
||||
def full(self,prog):
|
||||
arg = min(self.winsize[0]/prog.winsize[0],self.winsize[1]/prog.winsize[1])
|
||||
print arg
|
||||
return prog*arg
|
||||
|
||||
def plot(self,prog):
|
||||
if (not self.oob(prog)) and self.ready:
|
||||
self.write(prog)
|
||||
|
||||
if self.oob(prog):
|
||||
print 'programm out of bound'
|
||||
if not self.ready:
|
||||
print 'device not ready'
|
||||
|
||||
def centralize(self,prog):
|
||||
return prog-prog.center \
|
||||
if self.p0incenter else \
|
||||
prog-(prog.xmin,prog.ymin)
|
||||
@property
|
||||
def ready(self):
|
||||
return bool(self.getoutput('OS;'))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
plt=Plotter()
|
||||
p=Program()
|
||||
p.parsefile("hpgl/nmk.hpgl")
|
||||
print p.xmin,p.xmax,p.ymin,p.ymax
|
||||
p=p.flip()
|
||||
p=p*(1,-1)
|
||||
p=plt.full(p)
|
||||
p=plt.centralize(p)
|
||||
|
||||
print 'Programm Größe' p.winsize
|
||||
print 'Program Abmessung' p.xmin,p.xmax,p.ymin,p.ymax
|
||||
print 'Programm Center' p.center
|
||||
print ' Plotter bereit' plt.ready
|
||||
#print str(p)
|
||||
plt.plot(p)
|
||||
|
||||
print len(p)
|
||||
#print str(p)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user