81 lines
2.7 KiB
Python
81 lines
2.7 KiB
Python
import FreeCAD
|
|
import Part
|
|
import ImportGui
|
|
|
|
class GeometryValidateUseCase:
|
|
def __init__(self):
|
|
self.doc = None
|
|
|
|
|
|
|
|
|
|
def import_step_and_check(self, file_path):
|
|
# Очистить сцену перед импортом новой сборки
|
|
FreeCAD.closeDocument("Unnamed")
|
|
|
|
# Создать новый документ FreeCAD
|
|
self.doc = FreeCAD.newDocument("Unnamed")
|
|
|
|
# Импортируйте STEP-файл
|
|
ImportGui.open(file_path, "Unnamed")
|
|
|
|
# Отобразите объект в 3D-виде
|
|
FreeCAD.ActiveDocument.recompute()
|
|
Gui.SendMsgToActiveView("ViewFit")
|
|
|
|
# Получить список тел в сборке
|
|
bodies = getBodies(self.doc)
|
|
|
|
# Проверить пересечения между телами
|
|
intersections = self.check_intersections(bodies)
|
|
self.print_intersections(intersections)
|
|
|
|
# Проверить тела на нулевой объем
|
|
zero_volume_bodies = self.check_zero_volume(bodies)
|
|
self.print_zero_volume(zero_volume_bodies)
|
|
|
|
# Сохранить документ
|
|
self.save_checked_document(file_path)
|
|
|
|
|
|
def getBodies(doc):
|
|
bodies = []
|
|
for obj in doc.Objects:
|
|
if hasattr(obj, 'TypeId') and obj.TypeId == "Part::Feature":
|
|
bodies.append(obj)
|
|
return bodies
|
|
|
|
|
|
|
|
def check_intersections(self, bodies):
|
|
intersections = []
|
|
for i in range(len(bodies)):
|
|
for j in range(i + 1, len(bodies)):
|
|
if bodies[i].intersects(bodies[j]):
|
|
intersections.append((i + 1, j + 1))
|
|
return intersections
|
|
|
|
def check_zero_volume(self, bodies):
|
|
zero_volume_bodies = [i + 1 for i, body in enumerate(bodies) if body.Volume == 0]
|
|
return zero_volume_bodies
|
|
|
|
def print_intersections(self, intersections):
|
|
for i, j in intersections:
|
|
print("Тела пересекаются: Body {} и Body {}".format(i, j))
|
|
|
|
def print_zero_volume(self, zero_volume_bodies):
|
|
for i in zero_volume_bodies:
|
|
print("Тело {} имеет нулевой объем".format(i))
|
|
|
|
def save_checked_document(self, original_file_path):
|
|
checked_file_path = original_file_path.replace(".step", "_checked.FCStd")
|
|
FreeCAD.saveDocument(self.doc, checked_file_path)
|
|
print("Проверенная сборка сохранена в файл:", checked_file_path)
|
|
FreeCAD.closeDocument("Unnamed")
|
|
|
|
|
|
|
|
use_case = GeometryValidateUseCase()
|
|
step_file_path = "/home/mark-voltov/Загрузки/Telegram Desktop/test_asm.STEP"
|
|
use_case.import_step_and_check(step_file_path)
|
|
|