# Path: lasercut/helper.py
# def transform(part, referentiel_face, transform_matrix=None, y_invert = False):
#     normal_face = referentiel_face.normalAt(0, 0)
#     # original center is (0,0,0)
#     transformed_center = referentiel_face.CenterOfMass #+ normal_face.normalize() * x_origin
#     if transform_matrix is None:
#         transform_matrix = get_matrix_transform(referentiel_face)
#     part.Placement = FreeCAD.Placement(transform_matrix).multiply(part.Placement)
#     part.translate(transformed_center)
#     if y_invert:
#         part.rotate(transformed_center, normal_face, 180.)
#     return part
# 
# def compare_freecad_vector(vector1, vector2, epsilon=10e-6):
#     vector = vector1.sub(vector2)
#     if math.fabs(vector.x) < epsilon and math.fabs(vector.y) < epsilon and math.fabs(vector.z) < epsilon:
#         return True
#     return False
# 
# def compare_value(value1, value2, epsilon=10e-6):
#     value = value1 - value2
#     if math.fabs(value) < epsilon:
#         return True
#     return False
# 
# class Segment:
#     def __init__(self, first=FreeCAD.Vector(0, 0, 0), second=FreeCAD.Vector(0, 0, 0)):
#         self.A = first
#         self.B = second
# 
#     def a(self):
#         return self.A
# 
#     def b(self):
#         return self.B * 1
# 
#     def clone_a(self):
#         return self.A * 1
# 
#     def clone_b(self):
#         return self.B * 1
# 
#     def vector(self):
#         return self.B.sub(self.A)
# 
#     def length(self):
#         return self.vector().Length
# 
#     def mid_point(self):
#         mid_point_b = self.A.add(self.B)
#         mid_point_b.scale(0.5, 0.5, 0.5)
#         return mid_point_b
# 
#     def get_angle(self, segment):
#         return self.vector().getAngle(segment.vector())
# 
#     def add(self, vector):
#         return Segment(self.A.add(vector), self.B.add(vector))
# 
#     def rotate_z(self, angle):
#         return Segment(rotate_vector_z(self.A, angle), rotate_vector_z(self.B, angle))
# 
#     def __repr__(self):
#         return "Segment A: " + str(self.A) + ", B: " + str(self.B)
# 
# def assemble_list_element(el_list):
#     if len(el_list) == 0:
#         return None
# 
#     part = el_list[0]
#     for el in el_list[1:]:
#         part = part.fuse(el)
# 
#     return part


import Part
import FreeCAD
import math
from lasercut.helper import transform, compare_freecad_vector, compare_value, Segment, assemble_list_element
def make_hinges(hinge_properties, global_hinges_properties, referentiel_face):
    x_hinges_positions = get_hinges_x_positions(hinge_properties.nb_link, hinge_properties.arc_length)
    hinges_list = []
    y_length = hinge_properties.extrustion_vector.Length
    thickness = hinge_properties.thickness
    length_ratio = global_hinges_properties.occupancy_ratio

    nb_holes_by_column = int(global_hinges_properties.alternate_nb_hinge)
    hole_length = (y_length * length_ratio) / float(nb_holes_by_column)
    hole_space = y_length * (1. - length_ratio) /float(nb_holes_by_column + 1)
    y_pos_list = get_hinges_y_positions(nb_holes_by_column, hole_length, hole_space)
    nb_holes_by_column = int(nb_holes_by_column + 1)
    y_pos_list_2 = get_hinges_y_positions(nb_holes_by_column, hole_length, hole_space)

    index = 0
    for hinge_x in x_hinges_positions:

        if index % 2 == 0:
            pos_list = y_pos_list
        else:
            pos_list = y_pos_list_2

        for pos_y in pos_list:
            new_hinge = create_hole_hinge(global_hinges_properties.link_clearance, hole_length, thickness,
                                          global_hinges_properties.laser_beam_diameter)
            new_hinge.translate(FreeCAD.Vector(hinge_x, pos_y, 0))
            hinges_list.append(new_hinge)
        index += 1

    hinges_to_remove = assemble_list_element(hinges_list)

# Path: lasercut/helper.py
# def transform(part, referentiel_face, transform_matrix=None, y_invert = False):
#     normal_face = referentiel_face.normalAt(0, 0)
#     # original center is (0,0,0)
#     transformed_center = referentiel_face.CenterOfMass #+ normal_face.normalize() * x_origin
#     if transform_matrix is None:
#         transform_matrix = get_matrix_transform(referentiel_face)
#     part.Placement = FreeCAD.Placement(transform_matrix).multiply(part.Placement)
#     part.translate(transformed_center)
#     if y_invert:
#         part.rotate(transformed_center, normal_face, 180.)
#     return part
# 
# def compare_freecad_vector(vector1, vector2, epsilon=10e-6):
#     vector = vector1.sub(vector2)
#     if math.fabs(vector.x) < epsilon and math.fabs(vector.y) < epsilon and math.fabs(vector.z) < epsilon:
#         return True
#     return False
# 
# def compare_value(value1, value2, epsilon=10e-6):
#     value = value1 - value2
#     if math.fabs(value) < epsilon:
#         return True
#     return False
# 
# class Segment:
#     def __init__(self, first=FreeCAD.Vector(0, 0, 0), second=FreeCAD.Vector(0, 0, 0)):
#         self.A = first
#         self.B = second
# 
#     def a(self):
#         return self.A
# 
#     def b(self):
#         return self.B * 1
# 
#     def clone_a(self):
#         return self.A * 1
# 
#     def clone_b(self):
#         return self.B * 1
# 
#     def vector(self):
#         return self.B.sub(self.A)
# 
#     def length(self):
#         return self.vector().Length
# 
#     def mid_point(self):
#         mid_point_b = self.A.add(self.B)
#         mid_point_b.scale(0.5, 0.5, 0.5)
#         return mid_point_b
# 
#     def get_angle(self, segment):
#         return self.vector().getAngle(segment.vector())
# 
#     def add(self, vector):
#         return Segment(self.A.add(vector), self.B.add(vector))
# 
#     def rotate_z(self, angle):
#         return Segment(rotate_vector_z(self.A, angle), rotate_vector_z(self.B, angle))
# 
#     def __repr__(self):
#         return "Segment A: " + str(self.A) + ", B: " + str(self.B)
# 
# def assemble_list_element(el_list):
#     if len(el_list) == 0:
#         return None
# 
#     part = el_list[0]
#     for el in el_list[1:]:
#         part = part.fuse(el)
# 
#     return part


# Path: lasercut/makehinges.py
import Part
import FreeCAD
import math
from lasercut.helper import transform, compare_freecad_vector, compare_value, Segment, assemble_list_element

def make_hinges(hinge_properties, global_hinges_properties, referentiel_face):
    x_hinges_positions = get_hinges_x_positions(hinge_properties.nb_link, hinge_properties.arc_length)
    hinges_list = []
    y_length = hinge_properties.extrustion_vector.Length
    thickness = hinge_properties.thickness
    length_ratio = global_hinges_properties.occupancy_ratio

    nb_holes_by_column = int(global_hinges_properties.alternate_nb_hinge)
    hole_length = (y_length * length_ratio) / float(nb_holes_by_column)
    hole_space = y_length * (1. - length_ratio) /float(nb_holes_by_column + 1)
    y_pos_list = get_hinges_y_positions(nb_holes_by_column, hole_length, hole_space)
    nb_holes_by_column = int(nb_holes_by_column + 1)
    y_pos_list_2 = get_hinges_y_positions(nb_holes_by_column, hole_length, hole_space)

    index = 0
    for hinge_x in x_hinges_positions:

        if index % 2 == 0:
            pos_list = y_pos_list
        else:
            pos_list = y_pos_list_2

        for pos_y in pos_list:
            new_hinge = create_hole_hinge(global_hinges_properties.link_clearance, hole_length, thickness,
                                          global_hinges_properties.laser_beam_diameter)
            new_hinge.translate(FreeCAD.Vector(hinge_x, pos_y, 0))
            hinges_list.append(new_hinge)
        index += 1

    hinges_to_remove = assemble_list_element(hinges_list)

    hinges_to_remove_transformed = transform(hinges_to_remove, referentiel_face)