mustergenerator.py um png export erweitert (packet cairoSVG nötig -> macht ggf. Probleme bei der installation)

This commit is contained in:
Mika 2023-11-06 22:07:49 +01:00
parent b6d99796d1
commit 04efdeb757

View File

@ -4,7 +4,7 @@ import math
class Circle:
def __init__(self, name: str, filepath: str, length=1000, height=1000, radius=10, distance=60, offset=50):
def __init__(self, name: str, filepath: str, length=472, height=472, 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"
@ -71,7 +71,8 @@ class Circle:
assert val <= 100, "offset can only be 0 - 100 %"
self.__offset = (self.__distance / 100) * val
def circle_generation(self):
def circle_generation(self, filetype='svg'):
assert filetype == 'svg' or 'png', "Dateityp mus svg oder png sein"
x = self.__radius
y = self.__radius
i = 1
@ -90,11 +91,15 @@ class Circle:
y = y + self.__distance
i = i + 1
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
if filetype == 'svg':
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
if filetype == 'png':
canvas.save_png(f'{self.__filepath}/{self.__name}.png')
class Hexagon:
def __init__(self, name: str, filepath: str, length=1000, height=1000, key_width=50, bridge_width=10):
def __init__(self, name: str, filepath: str, length=472, height=472, 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, "key width can not be negative"
@ -143,7 +148,9 @@ class Hexagon:
assert val >= 0, "bridge width can not be negative"
self.__bridge_width = val
def hexagon_generation(self):
def hexagon_generation(self, filetype='svg'):
assert filetype == 'svg' or 'png', "Dateityp mus svg oder png sein"
i = 1
x = 0
y = 0
@ -183,11 +190,14 @@ class Hexagon:
y = y + distance_y
i = i + 1
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
if filetype == 'svg':
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
if filetype == 'png':
canvas.save_png(f'{self.__filepath}/{self.__name}.png')
class Triangle:
def __init__(self, name: str, filepath: str, length=1000, height=1000, side_length=50, bridge_width=10):
def __init__(self, name: str, filepath: str, length=472, height=472, side_length=50, bridge_width=10):
assert length >= 0, "length can not be negative"
assert height >= 0, "height can not be negative"
assert side_length >= 0, "side length can not be negative"
@ -236,7 +246,8 @@ class Triangle:
assert val >= 0, "bridge width can not be negative"
self.__bridge_width = val
def triangle_generation(self):
def triangle_generation(self, filetype='svg'):
assert filetype == 'svg' or 'png', "Dateityp mus svg oder png sein"
x = 0
y = 0
i = 1
@ -278,7 +289,10 @@ class Triangle:
y = y + self.__bridge_width + math.sqrt(3) / 2 * self.__side_length
i = i + 1
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
if filetype == 'svg':
canvas.save_svg(f'{self.__filepath}/{self.__name}.svg')
if filetype == 'png':
canvas.save_png(f'{self.__filepath}/{self.__name}.png')
if __name__ == '__main__':
@ -290,13 +304,15 @@ if __name__ == '__main__':
shape = int(input("Form (1: Kreis, 2: Hexagon, 3: Dreieck):"))
filetype = 'svg'
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")
print(f"\nStandardwerte\n1. Höhe: {circle.height} px\n2. Länge: {circle.length} px\n3. Radius: {circle.radius} px\n4. Abstand: {circle.distance} px\n5. Offset: {circle.offset_percentage} %\n6. Dateityp: svg\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):"))
parameter_selection = int(input("Parameter ändern (0: keinen Parameter ändern, 1: Höhe, 2: Länge, 3: Radius, 4: Abstand, 5: Offste, 6: Dateityp):"))
if parameter_selection == 0:
selection_flag = False
elif parameter_selection == 1:
@ -314,6 +330,14 @@ if __name__ == '__main__':
elif parameter_selection == 5:
offset = int(input("Offset in %:"))
circle.offset = offset
elif parameter_selection == 6:
filetype_selection = int(input(f"1: {name}.svg, 2: {name}.png:"))
if filetype_selection == 1:
filetype = "svg"
elif filetype_selection == 2:
filetype = "png"
else:
raise ValueError
else:
print("ungültiger Wert")
except ValueError:
@ -321,16 +345,16 @@ if __name__ == '__main__':
except AssertionError as error:
print(error)
circle.circle_generation()
circle.circle_generation(filetype)
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")
f"\nStandardwerte\n1. Höhe: {hexagon.height} px\n2. Länge: {hexagon.length} px\n3. Schlüsselweite: {hexagon.key_width} px\n4. Stegbreite: {hexagon.bridge_width} px\n5. Dateityp: svg\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):"))
parameter_selection = int(input("Parameter ändern (0: keinen Parameter ändern, 1: Höhe, 2: Länge, 3: Schlüsselweite, 4: Stegbreite, 5: Dateityp):"))
if parameter_selection == 0:
selection_flag = False
elif parameter_selection == 1:
@ -345,6 +369,14 @@ if __name__ == '__main__':
elif parameter_selection == 4:
bridge_width = int(input("Stegbreite in Pixeln:"))
hexagon.bridge_width = bridge_width
elif parameter_selection == 5:
filetype_selection = int(input(f"1: {name}.svg, 2: {name}.png:"))
if filetype_selection == 1:
filetype = "svg"
elif filetype_selection == 2:
filetype = "png"
else:
raise ValueError
else:
print("ungültiger Wert")
except ValueError:
@ -352,16 +384,16 @@ if __name__ == '__main__':
except AssertionError as error:
print(error)
hexagon.hexagon_generation()
hexagon.hexagon_generation(filetype)
elif shape == 3:
triangle = Triangle(name, path)
print(
f"\nStandardwerte\nHöhe: {triangle.height} px\nLänge: {triangle.length} px\nSeitenlänge: {triangle.side_length} px\nStegbreite: {triangle.bridge_width} px\n")
f"\nStandardwerte\n1. Höhe: {triangle.height} px\n2. Länge: {triangle.length} px\n3. Seitenlänge: {triangle.side_length} px\n4. Stegbreite: {triangle.bridge_width} px\n5. Dateityp: svg\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: Seitenlänge, 4: Stegbreite):"))
"Parameter ändern (0: keinen Parameter ändern, 1: Höhe, 2: Länge, 3: Seitenlänge, 4: Stegbreite, 5: Dateityp):"))
if parameter_selection == 0:
selection_flag = False
elif parameter_selection == 1:
@ -376,6 +408,14 @@ if __name__ == '__main__':
elif parameter_selection == 4:
bridge_width = int(input("Stegbreite in Pixeln:"))
triangle.bridge_width = bridge_width
elif parameter_selection == 5:
filetype_selection = int(input(f"1: {name}.svg, 2: {name}.png:"))
if filetype_selection == 1:
filetype = "svg"
elif filetype_selection == 2:
filetype = "png"
else:
raise ValueError
else:
print("ungültiger Wert")
except ValueError:
@ -383,6 +423,6 @@ if __name__ == '__main__':
except AssertionError as error:
print(error)
triangle.triangle_generation()
triangle.triangle_generation(filetype)
else:
print("ungültiger Wert")