99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
![]() |
|
|||
|
import os
|
|||
|
import subprocess
|
|||
|
import time
|
|||
|
|
|||
|
import FreeCAD
|
|||
|
import Mesh
|
|||
|
|
|||
|
|
|||
|
import gcoder
|
|||
|
|
|||
|
|
|||
|
def export_to_stl(doc, output_dir):
|
|||
|
objects = doc.Objects
|
|||
|
|
|||
|
for obj in objects:
|
|||
|
if isinstance(obj, Part.Feature):
|
|||
|
stl_path = os.path.join(output_dir, obj.Label + ".stl")
|
|||
|
mesh = obj.Shape.tessellate(0.1)
|
|||
|
|
|||
|
# Создаем меш из геометрии объекта
|
|||
|
mesh_obj = Mesh.Mesh(mesh)
|
|||
|
|
|||
|
# Проверяем, что меш содержит данные
|
|||
|
if len(mesh_obj.Points) > 0:
|
|||
|
# Экспортируем меш в формат STL
|
|||
|
mesh_obj.write(stl_path)
|
|||
|
|
|||
|
|
|||
|
def create_gcode(stl_dir, output_dir):
|
|||
|
stl_files = [f for f in os.listdir(stl_dir) if f.endswith(".stl")]
|
|||
|
|
|||
|
for stl_file in stl_files:
|
|||
|
stl_path = os.path.join(stl_dir, stl_file)
|
|||
|
|
|||
|
gcode_file = stl_file.replace(".stl", ".gcode")
|
|||
|
gcode_path = os.path.join(output_dir, gcode_file)
|
|||
|
|
|||
|
# Команда для запуска Slic3r (в Ubuntu используется slic3r-пакет)
|
|||
|
cmd = ["slic3r", "--load", "/home/mark-voltov/my_config.ini", "-o", gcode_path, stl_path]
|
|||
|
|
|||
|
# Запускаем Slic3r
|
|||
|
subprocess.run(cmd)
|
|||
|
|
|||
|
def get_print_duration(gcode_dir):
|
|||
|
|
|||
|
gcoder.G
|
|||
|
gcode_files = [f for f in os.listdir(gcode_dir) if f.endswith(".gcode")]
|
|||
|
|
|||
|
total_duration = 0
|
|||
|
|
|||
|
for gcode_file in gcode_files:
|
|||
|
gcode_path = os.path.join(gcode_dir, gcode_file)
|
|||
|
|
|||
|
with open(gcode_path, "r") as file:
|
|||
|
lines = file.readlines()
|
|||
|
|
|||
|
for line in lines:
|
|||
|
if line.startswith("; estimated printing time"):
|
|||
|
duration = float(line.split(":")[1].strip()) * 60
|
|||
|
total_duration += duration
|
|||
|
break
|
|||
|
|
|||
|
return total_duration
|
|||
|
|
|||
|
|
|||
|
file_location = App.ActiveDocument.FileName
|
|||
|
file_name = os.path.basename(file_location ) #eds_report.csv
|
|||
|
location = os.path.dirname(file_location )
|
|||
|
|
|||
|
|
|||
|
# Путь к документу FreeCAD
|
|||
|
doc_path = file_location
|
|||
|
|
|||
|
# Каталог для сохранения STL-файлов
|
|||
|
stl_dir = location + '/stl'
|
|||
|
|
|||
|
# Каталог для сохранения G-code файлов
|
|||
|
gcode_dir = location + '/gcode'
|
|||
|
|
|||
|
# Открываем документ FreeCAD
|
|||
|
doc = FreeCAD.open(doc_path)
|
|||
|
|
|||
|
# Экспортируем модель в файлы STL
|
|||
|
export_to_stl(doc, stl_dir)
|
|||
|
print("STL файлы успешно созданы.")
|
|||
|
|
|||
|
# Создаем G-code файлы с помощью Slic3r
|
|||
|
create_gcode(stl_dir, gcode_dir)
|
|||
|
print("G-code файлы успешно созданы.")
|
|||
|
|
|||
|
# Получаем оценочную длительность печати
|
|||
|
print_duration = get_print_duration(gcode_dir)
|
|||
|
print("Оценочная длительность печати: {} секунд.".format(print_duration))
|
|||
|
|
|||
|
# Закрываем документ FreeCAD
|
|||
|
FreeCAD.closeDocument(doc)
|
|||
|
|