import FreeCAD
import Mesh
import FreeCAD as App
from model.mesh_part_model import MeshPartModel


class JoinMeshModel:
    id = None
    mesh = None

    def __init__(self, meshesPartModels: list['MeshPartModel']) -> None:
        meshes = []
        import Mesh
        from random import randrange
        for el in meshesPartModels:
            meshes.append(el.mesh.Mesh)

        self.id = 'MergedMesh' + str(randrange(1000000))
        document = App.ActiveDocument
        merged_mesh = Mesh.Mesh()
        for el in meshes:
            merged_mesh.addMesh(el)

        new_obj = App.activeDocument().addObject("Mesh::Feature", self.id)
        new_obj.Mesh = merged_mesh
        new_obj.ViewObject.DisplayMode = "Flat Lines"  # Set display mode to flat lines
        self.mesh = new_obj

    def remove(self):
        try:
            App.ActiveDocument.removeObject(self.id)
        except Exception as e:
            print(e)
