# /**********************************************************************
# *                                                                     *
# * Copyright (c) 2021 Javier Braña <javier.branagutierrez@gmail.com>  *
# *                                                                     *
# * This program is free software; you can redistribute it and/or modify*
# * it under the terms of the GNU Lesser General Public License (LGPL)  *
# * as published by the Free Software Foundation; either version 2 of   *
# * the License, or (at your option) any later version.                 *
# * for detail see the LICENCE text file.                               *
# *                                                                     *
# * This program is distributed in the hope that it will be useful,     *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of      *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
# * GNU Library General Public License for more details.                *
# *                                                                     *
# * You should have received a copy of the GNU Library General Public   *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307*
# * USA                                                                 *
# *                                                                     *
# ***********************************************************************

import FreeCAD, Draft
import copy

if FreeCAD.GuiUp:
    import FreeCADGui
    from DraftTools import translate
    from PySide.QtCore import QT_TRANSLATE_NOOP
    import draftguitools.gui_trackers as DraftTrackers

    import Part
    import pivy
    from pivy import coin
    import os
else:
    # \cond
    def translate(ctxt, txt):
        return txt


    def QT_TRANSLATE_NOOP(ctxt, txt):
        return txt
    # \endcond

__title__ = "PVPlant Trench"
__author__ = "Javier Braña"
__url__ = "http://www.sogos-solar.com"

from PVPlantResources import DirIcons as DirIcons


def makeBOQElectrical():
    ''' create a excel '''
    print("makeBOQElectrical")
    import sys
    sys.path.append(r"/")
    funct = 0
    if funct == 0:
        print(" ------- Prueba generar layout en excel:  ")
        # export layout to Excel:
        from Export import layoutToExcel
        layoutToExcel.generateLayout()
        return
    else:
        print(" ------- Prueba dibujar contorno de los objetos seleccionados:  ")
        import numpy as np
        import MeshTools.MeshGetBoundary as mgb
        # contorno:
        maxdist = 6000
        if FreeCADGui.Selection.hasSelection():
            sel = FreeCADGui.Selection.getSelection()
            pts = []
            for obj in sel:
                for panel in obj.Shape.SubShapes[0].SubShapes[0].SubShapes:
                    zm = panel.BoundBox.ZMax
                    for i in range(8):
                        pt = panel.BoundBox.getPoint(i)
                        if pt.z == zm:
                            pts.append(pt)
            import PVPlantCreateTerrainMesh
            m = PVPlantCreateTerrainMesh.Triangulate(np.array(pts), MaxlengthLE=maxdist, use3d=False)
            b = mgb.get_boundary(m)
            Part.show(b)

class _CommandBOQElectrical:
    def GetResources(self):
        return {'Pixmap': str(os.path.join(DirIcons, "boqe.svg")),
                'Accel': "R, E",
                'MenuText': "BOQ Electrical",
                'ToolTip': ""}

    def Activated(self):
        makeBOQElectrical()

    def IsActive(self):
        if FreeCAD.ActiveDocument:
            return True
        else:
            return False


if FreeCAD.GuiUp:
    FreeCADGui.addCommand('BOQElectrical', _CommandBOQElectrical())
