#!/usr/bin/env python

###
### This file is generated automatically by SALOME v9.3.0 with dump python functionality
###

import sys
import salome
import os

salome.salome_init()
import salome_notebook
notebook = salome_notebook.NoteBook()
sys.path.insert(0, r'/home/schwara2/Documents/Open_Knees/knee_hub/src')

###
### 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)


def CloseSalome():
    """Try to close out of Salome gracefully"""
    # there is nothing graceful about this
    try:
        sys.stdout = 'redirect to nowhere...'  # so I don't see the junk it prints
        from killSalomeWithPort import killMyPort
        killMyPort(os.getenv('NSPORT'))
    except:		pass


def convert_to_quad(med_file):

  mesh, status = smesh.CreateMeshesFromMED(med_file)
  mesh = mesh[0]
  NodeGroups = mesh.GetGroups(SMESH.NODE)
  FaceGroups = mesh.GetGroups(SMESH.FACE)
  mesh.ConvertToQuadratic(1)

  # need to re-create all node groups
  for ng in NodeGroups:
    ng_name = ng.GetName()
    fg_name = ng_name.replace('Nodes','Faces')

    # find the equivalent face group, and create a new node group with that name
    for fg in FaceGroups:
      group_name = fg.GetName()
      if group_name == fg_name:
        new_node_group = mesh.CreateDimGroup([fg], SMESH.NODE, ng_name, SMESH.ALL_NODES, 0)

    # delete the original node group
    mesh.RemoveGroup(ng)

    # export the new mesh as a med file
    new_med_filename = med_file.replace('.med','_quad.med')
    mesh.ExportMED(new_med_filename)

def test():

  med_directory = '/home/schwara2/Documents/Open_Knees/knee_hub/oks003/calibration/Registration/model2/MED/'
  file_list = os.listdir(med_directory)

  for file in file_list:
      if 'FMB' in file or 'FBB' in file or 'PTB' in file or 'TBB' in file:
          filepath = os.path.join(med_directory, file)
          convert_to_quad(filepath)
      else:
            pass



if __name__ == '__main__':

    test()
    CloseSalome()





# ([ACL], status) = smesh.CreateMeshesFromMED(r'/home/schwara2/Documents/Open_Knees/knee_hub/oks003/calibration/MeshDensity/MED/oks003_ACL_IP4.med')
# [ ACL_All_Volumes, ACL__FMB_ContactFaces, ACL__PCL_ContactFaces, ACL__TBB_ContactFaces, ACL_All_Faces, ACL__FMB_TiesFaces, ACL__TBB_TiesFaces, smeshObj_1, ACL__FMB_TiesNodes ] = ACL.GetGroups()
# ACL.ConvertToQuadratic(1)
# ACL__TBB_TiesNodes = ACL.CreateDimGroup( [ ACL__TBB_TiesFaces ], SMESH.NODE, 'ACL_@_TBB_TiesNodes', SMESH.ALL_NODES, 0)
# ACL.RemoveGroup( smeshObj_1 )
# smesh.SetName(ACL, 'ACL')
# try:
#   ACL.ExportMED(r'/home/schwara2/Documents/Open_Knees/knee_hub/oks003/calibration/MeshDensity/MED/oks003_ACL_IP4_quad_test.med',auto_groups=0,minor=40,overwrite=1,meshPart=None,autoDimension=1)
#   pass
# except:
#   print('ExportMED() failed. Invalid file name?')
#
# ## some objects were removed
# aStudyBuilder = salome.myStudy.NewBuilder()
# SO = salome.myStudy.FindObjectIOR(salome.myStudy.ConvertObjectToIOR(smeshObj_1))
# if SO: aStudyBuilder.RemoveObjectWithChildren(SO)
#
# ## Set names of Mesh objects
# smesh.SetName(ACL__TBB_ContactFaces, 'ACL_@_TBB_ContactFaces')
# smesh.SetName(ACL__PCL_ContactFaces, 'ACL_@_PCL_ContactFaces')
# smesh.SetName(ACL__FMB_ContactFaces, 'ACL_@_FMB_ContactFaces')
# smesh.SetName(ACL__TBB_TiesFaces, 'ACL_@_TBB_TiesFaces')
# smesh.SetName(ACL__FMB_TiesFaces, 'ACL_@_FMB_TiesFaces')
# smesh.SetName(ACL_All_Faces, 'ACL_All_Faces')
# smesh.SetName(ACL.GetMesh(), 'ACL')
# smesh.SetName(ACL__TBB_TiesNodes, 'ACL_@_TBB_TiesNodes')
# smesh.SetName(ACL__FMB_TiesNodes, 'ACL_@_FMB_TiesNodes')
# smesh.SetName(ACL_All_Volumes, 'ACL_All_Volumes')
#
#
# if salome.sg.hasDesktop():
#   salome.sg.updateObjBrowser()
