first try for calculate from stl

This commit is contained in:
2025-05-19 16:35:42 +02:00
parent b8bd3b0398
commit 00e62fc558
9 changed files with 255 additions and 18 deletions

63
backend/calculator.py Normal file
View File

@@ -0,0 +1,63 @@
import trimesh
import sys
def calcola_volumi(volume_totale, superficie, wall_line_width=0.4, wall_line_count=3,
layer_height=0.2, infill_percentage=0.15):
# Volume perimetrale stimato = superficie * spessore parete
spessore_parete = wall_line_width * wall_line_count
volume_pareti = superficie * spessore_parete
# Volume interno (infill)
volume_infill = (volume_totale - volume_pareti) * infill_percentage
volume_effettivo = volume_pareti + max(volume_infill, 0)
return volume_effettivo, volume_pareti, max(volume_infill, 0)
def calcola_peso(volume_mm3, densita_g_cm3=1.24):
densita_g_mm3 = densita_g_cm3 / 1000
return volume_mm3 * densita_g_mm3
def calcola_costo(peso_g, prezzo_kg=20.0):
return round((peso_g / 1000) * prezzo_kg, 2)
def stima_tempo(volume_mm3 ):
velocita_mm3_min = 0.4 *0.2 * 100 *60 # mm/s * mm * 60 s/min
tempo_minuti = volume_mm3 / velocita_mm3_min
return round(tempo_minuti, 1)
def main(percorso_stl):
try:
mesh = trimesh.load(percorso_stl)
volume_modello = mesh.volume
superficie = mesh.area
volume_stampa, volume_pareti, volume_infill = calcola_volumi(
volume_totale=volume_modello,
superficie=superficie,
wall_line_width=0.4,
wall_line_count=3,
layer_height=0.2,
infill_percentage=0.15
)
peso = calcola_peso(volume_stampa)
costo = calcola_costo(peso)
tempo = stima_tempo(volume_stampa)
print(f"Volume STL: {volume_modello:.2f} mm³")
print(f"Superficie esterna: {superficie:.2f} mm²")
print(f"Volume stimato pareti: {volume_pareti:.2f} mm³")
print(f"Volume stimato infill: {volume_infill:.2f} mm³")
print(f"Volume totale da stampare: {volume_stampa:.2f} mm³")
print(f"Peso stimato: {peso:.2f} g")
print(f"Costo stimato: CHF {costo}")
print(f"Tempo stimato: {tempo} min")
except Exception as e:
print("Errore durante l'elaborazione:", e)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Uso: python calcolatore_stl.py modello.stl")
else:
main(sys.argv[1])

View File

@@ -1,16 +1,72 @@
from fastapi import FastAPI
import io
import trimesh
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # oppure ["http://localhost:4200"] se vuoi essere più restrittivo
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/hello")
def hello():
return {"message": "Hello from Python API"}
def calculate_volumes(total_volume_mm3: float,
surface_area_mm2: float,
nozzle_width_mm: float = 0.4,
wall_line_count: int = 3,
layer_height_mm: float = 0.2,
infill_fraction: float = 0.15):
wall_thickness_mm = nozzle_width_mm * wall_line_count
wall_volume_mm3 = surface_area_mm2 * wall_thickness_mm
infill_volume_mm3 = max(total_volume_mm3 - wall_volume_mm3, 0) * infill_fraction
total_print_volume_mm3 = wall_volume_mm3 + infill_volume_mm3
return total_print_volume_mm3, wall_volume_mm3, infill_volume_mm3
def calculate_weight(volume_mm3: float, density_g_cm3: float = 1.24):
density_g_mm3 = density_g_cm3 / 1000.0
return volume_mm3 * density_g_mm3
def calculate_cost(weight_g: float, price_per_kg: float = 20.0):
return round((weight_g / 1000.0) * price_per_kg, 2)
def estimate_time(volume_mm3: float,
nozzle_width_mm: float = 0.4,
layer_height_mm: float = 0.2,
print_speed_mm_per_s: float = 100.0):
volumetric_speed_mm3_per_min = nozzle_width_mm * layer_height_mm * print_speed_mm_per_s * 60.0
return round(volume_mm3 / volumetric_speed_mm3_per_min, 1)
@app.post("/calculate/stl")
async def calculate_from_stl(file: UploadFile = File(...)):
if not file.filename.lower().endswith(".stl"):
raise HTTPException(status_code=400, detail="Please upload an STL file.")
try:
contents = await file.read()
mesh = trimesh.load(io.BytesIO(contents), file_type="stl")
model_volume_mm3 = mesh.volume
model_surface_area_mm2 = mesh.area
print_volume, wall_volume, infill_volume = calculate_volumes(
total_volume_mm3=model_volume_mm3,
surface_area_mm2=model_surface_area_mm2
)
weight_g = calculate_weight(print_volume)
cost_chf = calculate_cost(weight_g)
time_min = estimate_time(print_volume)
return {
"stl_volume_mm3": round(model_volume_mm3, 2),
"surface_area_mm2": round(model_surface_area_mm2, 2),
"wall_volume_mm3": round(wall_volume, 2),
"infill_volume_mm3": round(infill_volume, 2),
"print_volume_mm3": round(print_volume, 2),
"weight_g": round(weight_g, 2),
"cost_chf": cost_chf,
"time_min": time_min
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))