initial commit

This commit is contained in:
Lukas Cremer
2026-02-03 21:09:26 +01:00
commit 9fe02d248f
333 changed files with 25642 additions and 0 deletions

79
refac/Command.py Executable file
View File

@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
import math
class Command:
inicoms= ("IN", "SP", "LT") # nicht benutzt
scalecoms=("PA", "PD", "PR", "PU", "CI" ) # Liste skalierbarer HPGL-Befehle
movecoms=("PA", "PD", "PU" ) # Liste verschiebbarer HPGL-Befehle#
abscoms=("PA", "PD", "PU" )
relcoms=("PR")
arccoms=("CI", "AA")
def __init__(self, name, *args):
self.name = name # Befehlname
self.args = args # Argsliste
@property
def scalable(self):
return self.name in Command.scalecoms and self.args
@property
def movable(self):
return self.name in Command.movecoms and self.args
@property
def x(self):
#Baustelle da es Befehle gibt die mehrere Args haben
return self.args[0] if self.args else None
@property
def y(self):
return self.args[-1] if self.args else None
def __trunc__(self):
return Command(self.name,*[int(arg) for arg in self.args])
def __len__(self):
return len(str(self)) # Byte-Lnge des Befehls
def __str__(self):
return self.name + ",".join(str(int(arg)) for arg in self.args) + ";"
def __mul__(self, factor): # multipliziert falls skalable mit factor
if not self.scalable:
return self
if type(factor) == type(0) or type(factor) == type(0.0):
# Faktor kann skalar oder Tuple sein
factor = (factor, factor)
return Command(self.name,(self.args[0] * factor[0]),(self.args[1] * factor[1])) \
if len(self.args)>1 else\
Command(self.name,(self.args[0]*factor[0]) ) # wichtig fr Befehle mit nur einem Argument
def __add__(self, addend):
if not self.movable:
return self
if type(addend) == type(0) or type(addend) == type(0.0):
addend = (addend, addend)
#print( "ADD ", self.name )
return Command(self.name,(self.x + addend[0]),(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): # multiplikation mit Rot-Matrix
if not self.movable:
return self
cosa=math.cos(angl*math.pi/180)
sina=math.sin(angl*math.pi/180)
return Command(self.name,(self.x*cosa-self.y*sina),(self.y*cosa+self.x*sina))
def flip(self): # Spiegelung
if not self.movable:
return self
return Command(self.name,self.x,-self.y)

BIN
refac/Command.pyc Normal file

Binary file not shown.

37
refac/K18650.py Executable file
View File

@@ -0,0 +1,37 @@
from Command import *
from Program import *
import math
class K18650:
def __init__(self):
self.out='IN;SP1;'
def hpgl(self,n,di):
h=40*65
da=40*9
zbreite=2*math.pi*da/n
self.out+=('PU0,'+str(da)+';')
for i in range(n):
self.out+=('PD'+str(i*zbreite+zbreite/2)+',0;')
self.out+=('PD'+str(i*zbreite+zbreite)+','+str(da)+';')
self.out+=('PU0,'+str(h+da)+';')
for i in range(n):
self.out+=('PD'+str(i*zbreite+zbreite/2)+','+str(h+2*da)+';')
self.out+=('PD'+str(i*zbreite+zbreite)+','+str(h+da)+';')
self.out+=('PU0,'+str(di)+';')
self.out+=('PD'+str(n*zbreite)+','+str(di)+';')
self.out+=('PD'+str(n*zbreite)+','+str(2*da+h-di)+';')
self.out+=('PD0,'+str(2*da+h-di)+';')
self.out+=('PD0,'+str(di)+';')

BIN
refac/K18650.pyc Executable file

Binary file not shown.

13
refac/Kleber.py Executable file
View File

@@ -0,0 +1,13 @@
from Command import *
from Program import *
class Kleber:
def __init__(self):
self.out='IN;SP1;'
def hpgl(self,w,h,d):
dist=1.05
for i in range(w):
for j in range(h):
self.out+=('PU'+str(dist*(i*2*d+d))+','+str(dist*(j*2*d+d))+';CI'+str(d)+';')

BIN
refac/Kleber.pyc Executable file

Binary file not shown.

84
refac/Plotter.py Executable file
View File

@@ -0,0 +1,84 @@
from __future__ import division
import serial
class Plotter:
def __init__(self,boundaries=None):
self.boundaries=boundaries
self.p0incenter = False
if not boundaries:
s=self.getoutput(b'OW;')
print(s)
if not s:
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 == b"\x0d" or not x:
break
s.append(x)
return b''.join(s).decode()
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).encode())
@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 plot(self,prog):
if self.ready and (not self.oob(prog)) :
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;'))

BIN
refac/Plotter.pyc Normal file

Binary file not shown.

127
refac/Program.py Executable file
View File

@@ -0,0 +1,127 @@
import math
from Command import *
import tkinter as tk
class Program:
def __init__(self, commands=None):
self.commands = commands
self.active=True
self.visible=False
self.filename=''
if commands:
self.xmin,self.ymin,self.xmax,self.ymax=self.__simulate()
def __simulate(self):
(x,y) = (0,0)
xy=[]
for command in self.commands :
if not command.args:
continue
elif command.name in command.abscoms:
x,y=command.x,command.y
xy.append((x,y))
elif command.name in command.relcoms:
x,y =x+command.x,y+command.y
xy.append((x,y))
elif command.name in command.arccoms:
xy+=[(x+command.x,y+command.x) , ( x-command.x,y-command.x)]
return [m([v[i] for v in xy]) for m, i in ((min,0),(min,1),(max,0),(max,1))]
@staticmethod
def parsefile(filename):
with open(filename) as file:
return Program.parse( file.read())
@staticmethod
def parse(code):
commands = []
for command in code.strip().split(";")[:-1]:
name, args = command[:2], command[2:]
args = [float(arg) for arg in args.split(",")] if args else []
if name in Command.movecoms and len(args)>2:
for i in range(0,len(args),2):
commands.append(Command(name, args[i], args[i+1]))
else:
commands.append(Command(name, *args))
return Program(commands)
def show(self,w=None):
p=self.flip()
p=p.rotate(270)
p=p.fitin((0,0,1024,600),(0,0))
win=tk.Tk()
win.title('HPGLPLOTTER')
canvas=tk.Canvas(win,width=p.winsize[0],height=p.winsize[1])
canvas.grid(row=0,column=0)
x = y = 0
for command in p.commands:
if command.name == 'PU':
x,y = command.x,command.y
elif command.name=='PD':
canvas.create_line(x,y,command.x,command.y)
x,y = command.x,command.y
elif command.name == 'CI':
r=command.args[0]
canvas.create_oval(x-r,y-r,x+r,y+r)
win.mainloop()
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):
if type(arg)== type(self):
return Program( self.commands + arg.commands )
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 if command])
def scaleto(self,ab):
a,b = ab
return self*min(a/self.winsize[0],b/self.winsize[1])
def moveto(self,ab):
a,b = ab
return self-(self.xmin-a,self.ymin-b)
def fitin(self,xys,fxy): # fx fy Alignment Parameter: 0 links/oben 0,5 Mitte 1 rechts/unten
(x1,y1,x2,y2) = xys
(fx,fy) = fxy
print("fx",fx,"fy",fy)
p = self.scaleto((x2-x1,y2-y1))
return p.moveto((x1+fx*(x2-x1-p.winsize[0]),y1+fy*(y2-y1-p.winsize[1])))
def multi(self,w,h):
dist=40
out=self
p=self.winsize
for i in range(w):
for j in range(h):
if (w!=0 and h!=0):
out=out+(self+(i*p[0]+i*dist,j*p[1]+j*dist))
else:
pass
return out
@property
def center(self):
return ((self.xmin+self.xmax)/2),((self.ymin+self.ymax)/2)
@property
def winsize(self):
return self.xmax-self.xmin , self.ymax - self.ymin

BIN
refac/Program.pyc Executable file

Binary file not shown.

1
refac/Zeichnung.hpgl Executable file

File diff suppressed because one or more lines are too long

1
refac/Zeichnung2.hpgl Executable file

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

20
refac/hilbert.py Executable file
View File

@@ -0,0 +1,20 @@
import math
from Program import Program
from Command import Command
from hilbertcurve.hilbertcurve import HilbertCurve
penthickness=28
def hilbert( laenge, dicke):
tiefe=math.floor(math.log(laenge/dicke,2)+3)
print("Tiefe",tiefe)
hilbert_curve = HilbertCurve(tiefe , 2)
pts = [hilbert_curve.coordinates_from_distance(i) for i in range(4*(2**tiefe))]
return pts
def hilbert_curve(plt):
list=hilbert(min(plt.winsize[0],plt.winsize[1]),penthickness)
return Program([Command('IN'),Command('SP1'),Command('PU',*list[0])]\
+[Command('PD',*p) for p in list[1:]]\
+[Command('PU')])

BIN
refac/hilbert.pyc Normal file

Binary file not shown.

56
refac/lorenz.py Normal file
View File

@@ -0,0 +1,56 @@
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from Program import Program
from Command import Command
# Lorenz paramters and initial conditions
sigma, beta, rho = 10, 2.667, 28
u0, v0, w0 = 0, 1, 1.05
# Maximum time point and total number of time points
tmax, n = 100, 10000
def lorenz(X, t, sigma, beta, rho):
"""The Lorenz equations."""
u, v, w = X
up = -sigma*(u - v)
vp = rho*u - v - u*w
wp = -beta*w + u*v
return up, vp, wp
# Integrate the Lorenz equations on the time grid t
t = np.linspace(0, tmax, n)
f = odeint(lorenz, (u0, v0, w0), t, args=(sigma, beta, rho))
x, y, z = f.T
pts=[]
_x = x+y
_y=z
pts=(_x,_y)
print(pts)
plt.plot(_x,_y)
# Plot the Lorenz attractor using a Matplotlib 3D projection
fig = plt.figure()
ax = fig.gca(projection='3d')
plt.show()
def lorenz(plt):
list=hilbert(min(plt.winsize[0],plt.winsize[1]),penthickness)
return Program([Command('IN'),Command('SP1'),Command('PU',*list[0])]\
+[Command('PD',*p) for p in plt]\
+[Command('PU')])
# Remove all the axis clutter, leaving just the curve.
#ax.set_axis_off()

23
refac/main.py Executable file
View File

@@ -0,0 +1,23 @@
from Plotter import *
from Command import *
from Program import *
from Kleber import *
from K18650 import *
#plt = Plotter()#(0,0,400,400))
p = Program.parsefile('../hpgl/ag.hpgl')
#p=p.
print(str(p))
p=p.flip()
p=p.rotate(270)
print(p.winsize[0]/40, p.winsize[1]/40)
#p = p.fitin((0, 0, plt.xmax, plt.ymax), (0.5, 0.5))
print(p.winsize[0]/40, p.winsize[1]/40)
p.show()
#plt.write(p)