Files
wallter/plotter-app/app.py
Nathan-rek bb96bb4650 maj
2024-11-18 16:55:21 +01:00

110 lines
3.0 KiB
Python

from flask import Flask, render_template, request
import sys
import os
from streamer import stream_gcode
from text_to_gcode import convert_text
import svgToGcode
app = Flask(__name__)
ALLOWED_EXTENSIONS_SVG_FORM = {'svg'}
ALLOWED_EXTENSIONS_GCODE_FORM = {'gcode'}
def allowed_file_svg(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS_SVG_FORM
def allowed_file_gcode(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS_GCODE_FORM
@app.route("/", methods=["POST", "GET"])
def index():
return render_template("form.html")
@app.route("/svg_file", methods=["POST", "GET"])
def getSVG():
if request.method == "POST":
file = request.files.get("file")
if file and allowed_file_svg(file.filename):
output = 'gcode_output.gcode'
gcode = svgToGcode.convertToGcode(file.read() ,output)
stream_gcode(gcode.splitlines())
print(gcode)
print("done!")
else:
print('unaccepted file')
return render_template("form.html")
@app.route("/gcode_file", methods=["POST", "GET"])
def getGcode():
if request.method == "POST":
file = request.files.get("file")
if file and allowed_file_gcode(file.filename):
gcode = file.readlines()
print(str(gcode))
stream_gcode(gcode)
else:
print('empty file or unaccepted file')
return render_template("form.html")
@app.route("/text", methods=["POST", "GET"])
def getText():
if request.method == "POST":
txt = request.form["txt"].strip()
print(txt)
if txt:
formatted_text = ""
current_line = ""
for word in txt.split():
if len(current_line) + len(word) + 1 > 20:
formatted_text += current_line.strip() + "\n"
current_line = word + " "
else:
current_line += word + " " # Ajoute le mot à la ligne actuelle
# Ajoute la dernière ligne si elle n'est pas vide
if current_line.strip():
formatted_text += current_line.strip()
print("Formatted text with line breaks:")
print(formatted_text)
gcode_output = convert_text(formatted_text)
print("G-code generated:")
print(gcode_output)
gcode_filename = "retourligne.gcode"
with open(gcode_filename, "w") as gcode_file:
gcode_file.write(gcode_output)
print(f"G-code saved to {gcode_filename}")
else:
print('empty text')
return render_template("form.html")