/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/


/*----------------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*----------------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProFemMesh.h>
#include <ProMenu.h>
#include <ProMessage.h>
#include <ProMdl.h>

/*----------------------------------------------------------------------------*\
    Application includes
\*----------------------------------------------------------------------------*/
#include "TestError.h"
#include "UtilMenu.h"
#include <ProUtil.h>
/*============================================================================*\
    FUNCTION : ProTestInitMesh()	
    PURPOSE  : Default initialize mesh data structure  
\*============================================================================*/
int ProTestInitMesh(ProFemmeshData *mesh)
{
    mesh->mesh_type = PRO_FEM_SOLID_MESH;
    mesh->shell_type = PRO_FEM_SHELL_MESH_TRIANGLE;
    mesh->num_quilts = 0;
    mesh->analysis = PRO_FEM_ANALYSIS_STRUCTURAL;
    mesh->elem_shape = PRO_FEM_MIDPNT_LINEAR;
    mesh->solver = PRO_FEM_FEAS_COSMOS;
    mesh->num_aux_csys = 0;
    mesh->pro_quilt_ref_arr = NULL;
    mesh->aux_csys_ref_arr = NULL;
    mesh->csys_ref.id = PRO_VALUE_UNUSED;
    
    return(0);
}

/*============================================================================*\
    FUNCTION : ProTestFemMeshMenu()	
    PURPOSE  : Create menu for test options for mesh functionality
\*============================================================================*/
ProError ProTestFemMeshMenu()
{
    ProError status;
    int menu_id;
    ProFemmeshData mesh;
    ProSolid solid;
    
static char
    *menu[]={"TkMeshOpt","ShellType","Analisis","Shape","Solver","OutFile",""};
    
    ProError ProTestExportFemMesh();                     
    int ProUtilAssignMesh();
    
    status = ProMdlCurrentGet((ProMdl*)&solid);
    ProTestInitMesh(&mesh);
    
    /* Setup menu for chose fem mesh options */  
    status = ProMenuFileRegister((char*)"TkMeshOpt", (char*)"tk_mesh_opt.mnu", &menu_id);
    status = ProMenubuttonActionSet((char*)"TkMeshOpt", (char*)"TkMeshOpt",
        (ProMenubuttonAction)ProMenuDelete, NULL, 0);
    status = ProMenubuttonActionSet((char*)"TkMeshOpt", (char*)"Solid",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.mesh_type),
         PRO_FEM_SOLID_MESH );
    status = ProMenubuttonActionSet((char*)"TkMeshOpt", (char*)"Shell",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.mesh_type), 
        PRO_FEM_SHELL_MESH ); 
    status = ProMenubuttonActionSet((char*)"TkMeshOpt", (char*)"Mixed",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.mesh_type), 
        PRO_FEM_MIXED_MESH );
    status = ProMenubuttonActionSet((char*)"TkMeshOpt", (char*)"Quilt",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.mesh_type), 
        PRO_FEM_QUILT_MESH );
    status = ProMenubuttonActionSet((char*)"TkMeshOpt", (char*)"Boundary",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.mesh_type), 
        PRO_FEM_BOUNDARY_MESH ); 
    status = ProMenubuttonActionSet((char*)"TkMeshOpt", (char*)"Bar",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.mesh_type), 
        PRO_FEM_BAR_MESH );
    
    status = ProMenuFileRegister((char*)"ShellType", (char*)"tk_shell.mnu", &menu_id);
    status = ProMenubuttonActionSet((char*)"ShellType", (char*)"ShellType", 
        (ProMenubuttonAction)ProMenuDelete, NULL, 0);
    status = ProMenubuttonActionSet((char*)"ShellType", (char*)"Triangle", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.shell_type),
         PRO_FEM_SHELL_MESH_TRIANGLE);
    status = ProMenubuttonActionSet((char*)"ShellType", (char*)"Qadrangle",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.shell_type),
         PRO_FEM_SHELL_MESH_QUADRANGLE ); 
    
    status = ProMenuFileRegister((char*)"Analisis", (char*)"tk_analisis.mnu", &menu_id);
    status = ProMenubuttonActionSet((char*)"Analisis", (char*)"Analisis",
        (ProMenubuttonAction) ProMenuDelete, NULL, 0);
    status = ProMenubuttonActionSet((char*)"Analisis", (char*)"Structural",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.analysis), 
        PRO_FEM_ANALYSIS_STRUCTURAL );
    status = ProMenubuttonActionSet((char*)"Analisis", (char*)"Modal", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.analysis), 
        PRO_FEM_ANALYSIS_MODAL);
    status = ProMenubuttonActionSet((char*)"Analisis", (char*)"Termal",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.analysis),  
        PRO_FEM_ANALYSIS_THERMAL); 
    status = ProMenubuttonActionSet((char*)"Analisis", (char*)"Flow",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.analysis), 
        PRO_FEM_ANALYSIS_FLOW );
    
    status = ProMenuFileRegister((char*)"Shape", (char*)"tk_shape.mnu", &menu_id);
    status = ProMenubuttonActionSet((char*)"Shape", (char*)"Shape", 
        (ProMenubuttonAction)ProMenuDelete,NULL, 0);
    status = ProMenubuttonActionSet((char*)"Shape", (char*)"Linear", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.elem_shape),
         PRO_FEM_MIDPNT_LINEAR );
    status = ProMenubuttonActionSet((char*)"Shape", (char*)"Parabolic", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.elem_shape),
         PRO_FEM_MIDPNT_PARABOLIC ); 
    status = ProMenubuttonActionSet((char*)"Shape", (char*)"ParabolicFix",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.elem_shape),
         PRO_FEM_MIDPNT_PARABOLIC_FIXED ); 
    
    status = ProMenuFileRegister((char*)"Solver", (char*)"tk_solver.mnu", &menu_id);
    status = ProMenubuttonActionSet((char*)"Solver", (char*)"Solver", 
        (ProMenubuttonAction)ProMenuDelete,NULL, 0);
    status = ProMenubuttonActionSet((char*)"Solver", (char*)"None", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.solver),
         PRO_FEM_FEAS_NONE );
    status = ProMenubuttonActionSet((char*)"Solver", (char*)"Neutral", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.solver), 
         PRO_FEM_FEAS_NEUTRAL );
    status = ProMenubuttonActionSet((char*)"Solver", (char*)"Ansys", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.solver), 
         PRO_FEM_FEAS_ANSYS ); 
    status = ProMenubuttonActionSet((char*)"Solver", (char*)"Nastran",
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.solver), 
         PRO_FEM_FEAS_NASTRAN);
    status = ProMenubuttonActionSet((char*)"Solver", (char*)"Mold", 
        (ProMenubuttonAction)ProUtilAssignMesh, &(mesh.solver), 
         PRO_FEM_FEAS_C_MOLD );
    
    status = ProMenuFileRegister((char*)"OutFile", (char*)"tk_out_file.mnu", &menu_id);
    status = ProMenubuttonActionSet((char*)"OutFile", (char*)"OutFile", 
        (ProMenubuttonAction)ProMenuDelete,NULL, 0);
    status = ProMenubuttonGenactionSet((char*)"OutFile", (char*)"OutMesh",   
        (ProMenubuttonGenaction)ProTestExportFemMesh, solid, &mesh, NULL, NULL,
        NULL, NULL);
        
    status = ProCompoundmenuCreate(menu, &menu_id);
    /* Highlight default settings */
    status = ProMenubuttonHighlight((char*)"TkMeshOpt", (char*)"Solid");
    status = ProMenubuttonHighlight((char*)"ShellType", (char*)"Triangle");
    status = ProMenubuttonHighlight((char*)"Analisis", (char*)"Structural");
    status = ProMenubuttonHighlight((char*)"Shape", (char*)"Linear");
    status = ProMenubuttonHighlight((char*)"Solver", (char*)"Cosmos");
    status = ProMenuProcess(menu[0], &menu_id);
        return(status);    

}

/*============================================================================*\
    FUNCTION : ProTestExportFemMesh	
    PURPOSE  : export fem mesh 
\*============================================================================*/
int ProUtilAssignMesh(ProAppData p_member, int option)
{
    *((int *)p_member) = option;
    return (0);
}

/*============================================================================*\
    FUNCTION : ProTestExportFemMesh	
    PURPOSE  : export fem mesh 
\*============================================================================*/
ProError ProTestExportFemMesh(ProSolid solid ,
                          ProFemmeshData *mesh)
{

    ProError status;
    ProName name;
    ProName MSGFIL;
    
    status = ProMenuDelete();
    status = ProMenuDelete();
    status = ProMenuDelete();
    status = ProMenuDelete();
    status = ProMenuDelete();
    status = ProMenuDelete();
    
    ProStringToWstring( MSGFIL, (char*)"usermsg.txt");
    
    if ( solid == NULL )
    {
        status = ProMessageDisplay(MSGFIL, (char*)"USER %0s", (char*)"No object found");
        return(PRO_TK_BAD_INPUTS);
    }
    /* Write output file */
    else
    {
        status = ProMessageDisplay (MSGFIL, (char*)"USER %0s",
            "Enter output file name :");
        status = ProMessageStringRead (PRO_NAME_SIZE, name);
        if(status != PRO_TK_NO_ERROR)
            return(PRO_TK_NO_ERROR);
        status = ProFemmeshExport(solid, mesh, name);
        TEST_CALL_REPORT("ProFemmeshExport()", "ProTestExportFemMesh()", 
                                    status, status != PRO_TK_NO_ERROR);
    }
    return(status);
}