Mustergenerator/mustergenerator.py

264 lines
8.7 KiB
Python

import drawsvg as draw
import os
import math
class Circle:
def __init__(self, name: str, filepath: str, length=1000, height=1000, radius=10, distance=60, offset=50):
assert length >= 0, "length can not be negative"
assert height >= 0, "height can not be negative"
assert radius >= 0, "radius can not be negative"
assert distance >= 2*radius, "distance must be grater then two times the radius"
assert offset >= 0, "offset can only be 0 - 100 %"
assert offset <= 100, "offset can only be 0 - 100 %"
self.__name = name
self.__filepath = filepath
self.__length = length
self.__height = height
self.__radius = radius
self.__distance = distance
self.__offset_percentage = offset
self.__offset = (distance / 100) * offset
@property
def length(self):
return self.__length
@length.setter
def length(self, val):
assert val >= 0, "length can not be negative"
self.__length = val
@property
def height(self):
return self.__height
@height.setter
def height(self, val):
assert val >= 0, "height can not be negative"
self.__height = val
@property
def radius(self):
return self.__radius
@radius.setter
def radius(self, val):
assert val >= 0, "radius can not be negative"
self.__radius = val
@property
def distance(self):
return self.__distance
@distance.setter
def distance(self, val):
assert val >= 2 * radius, "distance must be grater then two times the radius"
self.__distance = val
@property
def offset_percentage(self):
return self.__offset_percentage
@property
def offset(self):
return self.__offset
@offset.setter
def offset(self, val):
assert val >= 0, "offset can only be 0 - 100 %"
assert val <= 100, "offset can only be 0 - 100 %"
self.__offset = (self.__distance / 100) * val
def circle_generation(self):
x1 = 10
y1 = 10
y = y1
v = 1
g = 1
canvas = draw.Drawing(self.__length, self.__height, origin=(0, 0))
while y <= self.__height:
x = x1 + self.__offset * v
while x <= self.__length:
canvas.append(draw.Circle(x, y, self.__radius, fill='none', stroke_width=1, stroke='black'))
x = x + self.__distance
y = y + self.__distance
v = -((1 - g) / 2)
g = -g
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
class Hexagon:
def __init__(self, name: str, filepath: str, length=1000, height=1000, key_width=50, bridge_width=10):
assert length >= 0, "length can not be negative"
assert height >= 0, "height can not be negative"
assert key_width >= 0, "radius can not be negative"
assert bridge_width >= 0, "bridge width can not be negative"
self.__name = name
self.__filepath = filepath
self.__length = length
self.__height = height
self.__key_width = key_width
self.__bridge_width = bridge_width
@property
def length(self):
return self.__length
@length.setter
def length(self, val):
assert val >= 0, "length can not be negative"
self.__length = val
@property
def height(self):
return self.__height
@height.setter
def height(self, val):
assert val >= 0, "height can not be negative"
self.__height = val
@property
def key_width(self):
return self.__key_width
@key_width.setter
def key_width(self, val):
assert val >= 0, "length can not be negative"
self.__key_width = val
@property
def bridge_width(self):
return self.__bridge_width
@bridge_width.setter
def bridge_width(self, val):
assert val >= 0, "bridge width can not be negative"
self.__bridge_width = val
def hexagon_generation(self):
i = 1
x = 0
y = 0
side_length = self.__key_width / math.sqrt(3)
sin_expression = math.sin(math.radians(30)) * side_length
distance = self.__key_width + self.__bridge_width
distance_y = (math.sqrt(3) / 2) * distance
canvas = draw.Drawing(self.__length, self.__height, origin=(0, 0))
while y + (2 * side_length) < self.__height:
while x + self.__key_width < self.__length:
p1_x = x + self.__key_width / 2
p1_y = y
p2_x = x
p2_y = y + sin_expression
p3_x = x
p3_y = y + sin_expression + side_length
p4_x = x + self.__key_width / 2
p4_y = y + 2 * side_length
p5_x = x + self.__key_width
p5_y = y + sin_expression + side_length
p6_x = x + self.__key_width
p6_y = y + sin_expression
hexagon = draw.Lines(p1_x, p1_y, p2_x, p2_y, p3_x, p3_y, p4_x, p4_y, p5_x, p5_y, p6_x, p6_y,
fill='none', stroke='black', close='true')
canvas.append(hexagon)
x = x + distance
if i % 2 == 0:
x = 0
else:
x = distance / 2
y = y + distance_y
i = i + 1
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
if __name__ == '__main__':
name = input("Dateinamen:")
path = input("Dateipfad (0 für aktuelles Verzeichnis):")
if path == "0":
path = os.getcwd()
shape = int(input("Form (1: Kreis, 2: Hexagon, 3: Dreieck):"))
if shape == 1:
circle = Circle(name, path)
print(f"\nStandardwerte\nHöhe: {circle.height} px\nLänge: {circle.length} px\nRadius: {circle.radius} px\nAbstand: {circle.distance} px\nOffset: {circle.offset_percentage} %\n")
selection_flag = True
while selection_flag:
try:
parameter_selection = int(input("Parameter ändern (0: keinen Parameter ändern, 1: Höhe, 2: Länge, 3: Radius, 4: Abstand, 5: Offste):"))
if parameter_selection == 0:
selection_flag = False
elif parameter_selection == 1:
height = int(input("Höhe in Pixeln eingeben:"))
circle.height = height
elif parameter_selection == 2:
length = int(input("Länge in Pixeln:"))
circle.length = length
elif parameter_selection == 3:
radius = int(input("Radius in Pixeln:"))
circle.radius = radius
elif parameter_selection == 4:
distance = int(input("Abstand in Pixeln:"))
circle.distance = distance
elif parameter_selection == 5:
offset = int(input("Offset in %:"))
circle.offset = offset
else:
print("ungültiger Wert")
except ValueError:
print("ungültiger Wert")
except AssertionError as error:
print(error)
circle.circle_generation()
elif shape == 2:
hexagon = Hexagon(name, path)
print(
f"\nStandardwerte\nHöhe: {hexagon.height} px\nLänge: {hexagon.length} px\nSchlüsselweite: {hexagon.key_width} px\nStegbreite: {hexagon.bridge_width} px\n")
selection_flag = True
while selection_flag:
try:
parameter_selection = int(input("Parameter ändern (0: keinen Parameter ändern, 1: Höhe, 2: Länge, 3: Schlüsselweite, 4: Stegbreite):"))
if parameter_selection == 0:
selection_flag = False
elif parameter_selection == 1:
height = int(input("Höhe in Pixeln eingeben:"))
hexagon.height = height
elif parameter_selection == 2:
length = int(input("Länge in Pixeln:"))
hexagon.length = length
elif parameter_selection == 3:
key_width = int(input("Schlüsselweite in Pixeln:"))
hexagon.key_width = key_width
elif parameter_selection == 4:
bridge_width = int(input("Stegbreite in Pixeln:"))
hexagon.bridge_width = bridge_width
else:
print("ungültiger Wert")
except ValueError:
print("ungültiger Wert")
except AssertionError as error:
print(error)
hexagon.hexagon_generation()
elif shape == 3:
print("noch nicht implementiert")
else:
print("ungültiger Wert")