framework/freecad_workbench/importFreecadUsecase.py

82 lines
2.2 KiB
Python
Raw Normal View History

2024-04-14 18:54:47 +00:00
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")
self.doc = FreeCAD.newDocument("Unnamed")
ImportGui.open(file_path, "Unnamed")
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 = '/path/to/file'
use_case.import_step_and_check(step_file_path)