import sys
import os
import json
os.environ["SESSION_GUID"] = "6035fb5f-0d99-41bb-851d-ea6cc6241f91"
sys.path.append(r"D:\Development\Work\CENOS\cenos-backend\app\INDUCTION\salome\plugin") 
import GmeshBuild
import  SMESH
GMSH= GmeshBuild.GmeshBuild()
jsondata= {}
wDir = r"D:\Development\sheat\CENOS cases\IH\parameterizedCase" 
meshName = GMSH.MESH_FILE_NAME 

try:
    #!/usr/bin/env python
    
    ###
    ### This file is generated automatically by SALOME v9.3.0 with dump python functionality
    ###
    
    import sys
    import salome
    
    salome.salome_init()
    import salome_notebook
    notebook = salome_notebook.NoteBook()
    sys.path.insert(0, r'D:\Cases\Simulations\IH_tests\parameterizedCase//geometry//')
    
    ####################################################
    ##       Begin of NoteBook variables section      ##
    ####################################################
    notebook.set("coilDiameter", 0.000000) 
    notebook.set("dummy", 10.000000) 
    ####################################################
    ##        End of NoteBook variables section       ##
    ####################################################
    ###
    ### SHAPER component
    ###
    from salome.shaper import model
    
    model.begin()
    partSet = model.moduleDocument()
    Part_1 = model.addPart(partSet)
    Part_1_doc = Part_1.document()
    Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
    SketchLine_1 = Sketch_1.addLine(90.24896977149979, 80.78278656256583, 0, 80.78278656256583)
    SketchProjection_1 = Sketch_1.addProjection(model.selection("EDGE", "PartSet/OY"), False)
    SketchLine_2 = SketchProjection_1.createdFeature()
    SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.result())
    SketchLine_3 = Sketch_1.addLine(0, 80.78278656256583, 0, -85.16693973070593)
    SketchLine_4 = Sketch_1.addLine(0, -85.16693973070593, 90.24896977149979, -85.16693973070593)
    SketchLine_5 = Sketch_1.addLine(90.24896977149979, -85.16693973070593, 90.24896977149979, 80.78278656256583)
    SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_5.endPoint(), SketchLine_1.startPoint())
    SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_3.startPoint())
    SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
    SketchConstraintCoincidence_5 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_5.startPoint())
    SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
    SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_3.result())
    SketchConstraintHorizontal_2 = Sketch_1.setHorizontal(SketchLine_4.result())
    SketchConstraintVertical_2 = Sketch_1.setVertical(SketchLine_5.result())
    model.do()
    Sketch_2 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
    SketchLine_6 = Sketch_2.addLine(15.6863047318961, 58.98754601252786, 5.362243418720226, 58.98754601252786)
    SketchLine_7 = Sketch_2.addLine(5.362243418720226, 58.98754601252786, 5.362243418720226, -62.9893265394392)
    SketchLine_8 = Sketch_2.addLine(5.362243418720226, -62.9893265394392, 15.6863047318961, -62.9893265394392)
    SketchLine_9 = Sketch_2.addLine(15.6863047318961, -62.9893265394392, 15.6863047318961, 58.98754601252786)
    SketchConstraintCoincidence_6 = Sketch_2.setCoincident(SketchLine_9.endPoint(), SketchLine_6.startPoint())
    SketchConstraintCoincidence_7 = Sketch_2.setCoincident(SketchLine_6.endPoint(), SketchLine_7.startPoint())
    SketchConstraintCoincidence_8 = Sketch_2.setCoincident(SketchLine_7.endPoint(), SketchLine_8.startPoint())
    SketchConstraintCoincidence_9 = Sketch_2.setCoincident(SketchLine_8.endPoint(), SketchLine_9.startPoint())
    SketchConstraintHorizontal_3 = Sketch_2.setHorizontal(SketchLine_6.result())
    SketchConstraintVertical_3 = Sketch_2.setVertical(SketchLine_7.result())
    SketchConstraintHorizontal_4 = Sketch_2.setHorizontal(SketchLine_8.result())
    SketchConstraintVertical_4 = Sketch_2.setVertical(SketchLine_9.result())
    SketchLine_10 = Sketch_2.addLine(56.21780470214222, -48.4591661727472, 41.30527169422147, -48.4591661727472)
    SketchLine_11 = Sketch_2.addLine(41.30527169422147, -48.4591661727472, 41.30527169422147, -64.51881710435416)
    SketchLine_12 = Sketch_2.addLine(41.30527169422147, -64.51881710435416, 56.21780470214222, -64.51881710435416)
    SketchLine_13 = Sketch_2.addLine(56.21780470214222, -64.51881710435416, 56.21780470214222, -48.4591661727472)
    SketchConstraintCoincidence_10 = Sketch_2.setCoincident(SketchLine_13.endPoint(), SketchLine_10.startPoint())
    SketchConstraintCoincidence_11 = Sketch_2.setCoincident(SketchLine_10.endPoint(), SketchLine_11.startPoint())
    SketchConstraintCoincidence_12 = Sketch_2.setCoincident(SketchLine_11.endPoint(), SketchLine_12.startPoint())
    SketchConstraintCoincidence_13 = Sketch_2.setCoincident(SketchLine_12.endPoint(), SketchLine_13.startPoint())
    SketchConstraintHorizontal_5 = Sketch_2.setHorizontal(SketchLine_10.result())
    SketchConstraintVertical_5 = Sketch_2.setVertical(SketchLine_11.result())
    SketchConstraintHorizontal_6 = Sketch_2.setHorizontal(SketchLine_12.result())
    SketchConstraintVertical_6 = Sketch_2.setVertical(SketchLine_13.result())
    model.do()
    Face_1 = model.addFace(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchLine_1r-SketchLine_3f-SketchLine_4f-SketchLine_5f")])
    Face_2 = model.addFace(Part_1_doc, [model.selection("FACE", "Sketch_2/Face-SketchLine_6r-SketchLine_7f-SketchLine_8f-SketchLine_9f"), model.selection("FACE", "Sketch_2/Face-SketchLine_10f-SketchLine_11f-SketchLine_12f-SketchLine_13f")])
    Export_1 = model.exportToXAO(Part_1_doc, 'C:\\Users\\User\\AppData\\Local\\Temp\\shaper_fl3akw_4.xao', model.selection("FACE", "Face_1_1"), 'XAO')
    assert(Export_1 .feature().error() == "")
    Export_2 = model.exportToXAO(Part_1_doc, 'C:\\Users\\User\\AppData\\Local\\Temp\\shaper_dqe6aky3.xao', model.selection("FACE", "Face_2_1"), 'XAO')
    assert(Export_2 .feature().error() == "")
    Export_3 = model.exportToXAO(Part_1_doc, 'C:\\Users\\User\\AppData\\Local\\Temp\\shaper_qkesx8tb.xao', model.selection("FACE", "Face_2_2"), 'XAO')
    assert(Export_3 .feature().error() == "")
    model.do()
    model.end()
    
    ###
    ### GEOM component
    ###
    
    import GEOM
    from salome.geom import geomBuilder
    import math
    import SALOMEDS
    
    
    geompy = geomBuilder.New()
    
    (imported, Face_1_1, [], [], []) = geompy.ImportXAO("C:/Users/User/AppData/Local/Temp/shaper_fl3akw_4.xao")
    (imported, Face_2_1, [], [], []) = geompy.ImportXAO("C:/Users/User/AppData/Local/Temp/shaper_dqe6aky3.xao")
    (imported, Face_2_2, [], [], []) = geompy.ImportXAO("C:/Users/User/AppData/Local/Temp/shaper_qkesx8tb.xao")
    Translation_1 = geompy.MakeTranslation(Face_2_2, "coilDiameter", 0, 0)
    Partition_1 = geompy.MakePartition([Face_1_1, Face_2_1, Translation_1], [], [], [], geompy.ShapeType["FACE"], 0, [], 0)
    [Edge4,Edge7,Edge9,Edge11,Edge13,Edge16,Edge18,Edge20,Edge22,Edge25,Edge27,Edge29] = geompy.SubShapeAll(Partition_1, geompy.ShapeType["EDGE"])
    [Face2,Face30,Face32] = geompy.SubShapeAll(Partition_1, geompy.ShapeType["FACE"])
    [geomObj_1,geomObj_2,geomObj_3,geomObj_4,geomObj_5,geomObj_6,geomObj_7,geomObj_8,geomObj_9,geomObj_10,geomObj_11,geomObj_12] = geompy.SubShapeAll(Partition_1, geompy.ShapeType["EDGE"])
    [geomObj_13,geomObj_14,geomObj_15] = geompy.SubShapeAll(Partition_1, geompy.ShapeType["FACE"])
    [geomObj_16,geomObj_17,geomObj_18] = geompy.SubShapeAll(Partition_1, geompy.ShapeType["FACE"])
    [geomObj_19,geomObj_20,geomObj_21,geomObj_22,geomObj_23,geomObj_24,geomObj_25,geomObj_26,geomObj_27,geomObj_28,geomObj_29,geomObj_30] = geompy.SubShapeAll(Partition_1, geompy.ShapeType["EDGE"])
    air = geompy.CreateGroup(Partition_1, geompy.ShapeType["FACE"])
    geompy.UnionIDs(air, [2])
    [geomObj_31] = geompy.SubShapeAll(air, geompy.ShapeType["FACE"])
    coil = geompy.CreateGroup(Partition_1, geompy.ShapeType["FACE"])
    geompy.UnionIDs(coil, [32])
    [geomObj_32] = geompy.SubShapeAll(coil, geompy.ShapeType["FACE"])
    wp = geompy.CreateGroup(Partition_1, geompy.ShapeType["FACE"])
    geompy.UnionIDs(wp, [30])
    [geomObj_33] = geompy.SubShapeAll(wp, geompy.ShapeType["FACE"])
    air_symmetry = geompy.CreateGroup(Partition_1, geompy.ShapeType["EDGE"])
    geompy.UnionIDs(air_symmetry, [9])
    infinity = geompy.CreateGroup(Partition_1, geompy.ShapeType["EDGE"])
    geompy.UnionIDs(infinity, [4, 7, 11])
    wp_air = geompy.CreateGroup(Partition_1, geompy.ShapeType["EDGE"])
    geompy.UnionIDs(wp_air, [18, 20, 13, 16])
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    [air, coil, wp, air_symmetry, infinity, wp_air] = geompy.GetExistingSubObjects(Partition_1, True)
    geompy.addToStudy( Face_1_1, 'Face_1_1' )
    geompy.addToStudy( Face_2_1, 'Face_2_1' )
    geompy.addToStudy( Face_2_2, 'Face_2_2' )
    geompy.addToStudy( Translation_1, 'Translation_1' )
    geompy.addToStudy( Partition_1, 'Partition_1' )
    geompy.addToStudyInFather( Partition_1, air, 'air' )
    geompy.addToStudyInFather( Partition_1, coil, 'coil' )
    geompy.addToStudyInFather( Partition_1, wp, 'wp' )
    geompy.addToStudyInFather( Partition_1, air_symmetry, 'air_symmetry' )
    geompy.addToStudyInFather( Partition_1, infinity, 'infinity' )
    geompy.addToStudyInFather( Partition_1, wp_air, 'wp_air' )
    geompy.addToStudyInFather( Partition_1, Edge4, 'Edge4' )
    geompy.addToStudyInFather( Partition_1, Edge7, 'Edge7' )
    geompy.addToStudyInFather( Partition_1, Edge9, 'Edge9' )
    geompy.addToStudyInFather( Partition_1, Edge11, 'Edge11' )
    geompy.addToStudyInFather( Partition_1, Edge13, 'Edge13' )
    geompy.addToStudyInFather( Partition_1, Edge16, 'Edge16' )
    geompy.addToStudyInFather( Partition_1, Edge18, 'Edge18' )
    geompy.addToStudyInFather( Partition_1, Edge20, 'Edge20' )
    geompy.addToStudyInFather( Partition_1, Edge22, 'Edge22' )
    geompy.addToStudyInFather( Partition_1, Edge25, 'Edge25' )
    geompy.addToStudyInFather( Partition_1, Edge27, 'Edge27' )
    geompy.addToStudyInFather( Partition_1, Edge29, 'Edge29' )
    geompy.addToStudyInFather( Partition_1, Face2, 'Face2' )
    geompy.addToStudyInFather( Partition_1, Face30, 'Face30' )
    geompy.addToStudyInFather( Partition_1, Face32, 'Face32' )
    
    ###
    ### SMESH component
    ###
    
    import  SMESH, SALOMEDS
    from salome.smesh import smeshBuilder
    
    smesh = smeshBuilder.New()
    #smesh.SetEnablePublish( False ) # Set to False to avoid publish in study if not needed or in some particular situations:
                                     # multiples meshes built in parallel, complex and numerous mesh edition (performance)
    
    Mesh_1 = smesh.Mesh(Partition_1)
    Regular_1D = Mesh_1.Segment()
    Max_Size_1 = Regular_1D.MaxSize(4)
    MEFISTO_2D = Mesh_1.Triangle(algo=smeshBuilder.MEFISTO)
    if not GMSH.isSameTopology(wDir, Mesh_1):
        jsondata['status'] = "error"
        jsondata['message'] = "Topology of your geometry changed. Please open Salome and update geometry manually for this parameter set!"
        with open(wDir + "//geometry//batch_response.json", "w") as write_file :
            json.dump(jsondata, write_file)
        if not salome.sg.hasDesktop():
            from killSalomeWithPort import killMyPort
            killMyPort(os.getenv('NSPORT'))
            exit()
    try:
        isDone = Mesh_1.Compute(True)
    except:
        jsondata['status'] = "error"
        jsondata['message'] = "Error while generating mesh"
        with open(wDir + "//geometry//batch_response.json", "w") as write_file :
            json.dump(jsondata, write_file)
        if not salome.sg.hasDesktop():
            from killSalomeWithPort import killMyPort
            killMyPort(os.getenv('NSPORT'))
            exit()
    
    if not isDone:
        jsondata['status'] = "error"
        jsondata['message'] = "Error while generating mesh"
        with open(wDir + "//geometry//batch_response.json", "w") as write_file :
            json.dump(jsondata, write_file)
        if not GMSH.isSameTopology(wDir, Mesh_1):
            jsondata['message'] = "Topology of your geometry changed. Please open Salome and update geometry manually for this parameter set!"
        else:
            jsondata['message'] = "Please check input!"
        if not salome.sg.hasDesktop():
            from killSalomeWithPort import killMyPort
            killMyPort(os.getenv('NSPORT'))
            exit()
    
    Edge4_1 = Mesh_1.GroupOnGeom(Edge4,'Edge4',SMESH.EDGE)
    Edge7_1 = Mesh_1.GroupOnGeom(Edge7,'Edge7',SMESH.EDGE)
    Edge9_1 = Mesh_1.GroupOnGeom(Edge9,'Edge9',SMESH.EDGE)
    Edge11_1 = Mesh_1.GroupOnGeom(Edge11,'Edge11',SMESH.EDGE)
    Edge13_1 = Mesh_1.GroupOnGeom(Edge13,'Edge13',SMESH.EDGE)
    Edge16_1 = Mesh_1.GroupOnGeom(Edge16,'Edge16',SMESH.EDGE)
    Edge18_1 = Mesh_1.GroupOnGeom(Edge18,'Edge18',SMESH.EDGE)
    Edge20_1 = Mesh_1.GroupOnGeom(Edge20,'Edge20',SMESH.EDGE)
    Edge22_1 = Mesh_1.GroupOnGeom(Edge22,'Edge22',SMESH.EDGE)
    Edge25_1 = Mesh_1.GroupOnGeom(Edge25,'Edge25',SMESH.EDGE)
    Edge27_1 = Mesh_1.GroupOnGeom(Edge27,'Edge27',SMESH.EDGE)
    Edge29_1 = Mesh_1.GroupOnGeom(Edge29,'Edge29',SMESH.EDGE)
    Face2_1 = Mesh_1.GroupOnGeom(Face2,'Face2',SMESH.FACE)
    Face30_1 = Mesh_1.GroupOnGeom(Face30,'Face30',SMESH.FACE)
    Face32_1 = Mesh_1.GroupOnGeom(Face32,'Face32',SMESH.FACE)
    aCriteria = []
    aCriterion = smesh.GetCriterion(SMESH.FACE,SMESH.FT_AspectRatio,SMESH.FT_MoreThan,1000)
    aCriteria.append(aCriterion)
    aCriteria = []
    aCriterion = smesh.GetCriterion(SMESH.FACE,SMESH.FT_AspectRatio,SMESH.FT_MoreThan,10000)
    aCriteria.append(aCriterion)
    aCriteria = []
    aCriterion = smesh.GetCriterion(SMESH.FACE,SMESH.FT_Taper,SMESH.FT_MoreThan,0.3)
    aCriteria.append(aCriterion)
    aCriteria = []
    aCriterion = smesh.GetCriterion(SMESH.FACE,SMESH.FT_Taper,SMESH.FT_MoreThan,0.5)
    aCriteria.append(aCriterion)
    [ Edge4_1, Edge7_1, Edge9_1, Edge11_1, Edge13_1, Edge16_1, Edge18_1, Edge20_1, Edge22_1, Edge25_1, Edge27_1, Edge29_1, Face2_1, Face30_1, Face32_1 ] = Mesh_1.GetGroups()
    
    
    ## Set names of Mesh objects
    smesh.SetName(Regular_1D.GetAlgorithm(), 'Regular_1D')
    smesh.SetName(MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D')
    smesh.SetName(Face2_1, 'Face2')
    smesh.SetName(Face32_1, 'Face32')
    smesh.SetName(Face30_1, 'Face30')
    smesh.SetName(Edge20_1, 'Edge20')
    smesh.SetName(Edge22_1, 'Edge22')
    smesh.SetName(Edge27_1, 'Edge27')
    smesh.SetName(Edge25_1, 'Edge25')
    smesh.SetName(Edge29_1, 'Edge29')
    smesh.SetName(Mesh_1.GetMesh(), 'Mesh_1')
    smesh.SetName(Edge4_1, 'Edge4')
    smesh.SetName(Edge7_1, 'Edge7')
    smesh.SetName(Edge9_1, 'Edge9')
    smesh.SetName(Edge11_1, 'Edge11')
    smesh.SetName(Edge13_1, 'Edge13')
    smesh.SetName(Edge16_1, 'Edge16')
    smesh.SetName(Edge18_1, 'Edge18')
    smesh.SetName(Max_Size_1, 'Max Size_1')
    
    
    if salome.sg.hasDesktop():
      salome.sg.updateObjBrowser()
except:
    jsondata['status'] = "error"
    jsondata['message'] = "Could not generate geometry with input parameters."
    with open(wDir + "//geometry//batch_response.json", "w") as write_file :
        json.dump(jsondata, write_file)
    if not salome.sg.hasDesktop():
        from killSalomeWithPort import killMyPort
        killMyPort(os.getenv('NSPORT'))

        quit()

GMSH.WriteMesh(wDir, "BATCH",Mesh_1,meshName)
if not salome.sg.hasDesktop():
    from killSalomeWithPort import killMyPort
    killMyPort(os.getenv('NSPORT'))
    quit()

