/*====================================================================*\
FILE    : UgFemUse.c
PURPOSE : Illustrating the FEM functions 

HISTORY..
DATE      BUILD   AUTHOR   MODIFICATIONS
04-Dec-97 H-02-02 Stefan   $$1  Created
\*====================================================================*/

/*---------------------- Pro/Develop Includes ------------------------*/
#include "prodevelop.h"
#include "pro_mesh.h"

/*---------------------- Pro/Toolkit Includes ------------------------*/
#include <ProToolkit.h>

/*---------------------- Application Includes ------------------------*/
#include <TestError.h>

/*---------------------- Function Prototypes -------------------------*/
int user_get_fem_menu();
char *user_load_to_name ();
char *user_mctype_to_name ();
char *user_region_to_name ();
char *user_contact_to_name();
int user_get_fem_action ();


/*-----------------------------------------------------------*\
    User-defined constants for the GET FEM options
\*-----------------------------------------------------------*/
#define USER_FEM_GET_CONS_NAMES   0
#define USER_FEM_GET_CONSTRAINTS  1
#define USER_FEM_MASS_ELEMENTS    2
#define USER_FEM_MESH_CONTROLS    3
#define USER_FEM_GET_BAR_ELEMS    4
#define USER_FEM_GET_CONTACTS     5
#define USER_FEM_GET_RUNNERS      6

/*-----------------------------------------------------------*\
    To convert FEM load constants to strings for file input
    and output
\*-----------------------------------------------------------*/
typedef struct load_to_name
{
  int   load;
  char  name[PRODEV_NAME_SIZE];
} Load_to_name;

static Load_to_name load_names[] =
{ 
    {PRO_FEM_PRESSURE, "PRO_FEM_PRESSURE"},
    {PRO_FEM_FORCE, "PRO_FEM_FORCE"},
    {PRO_FEM_MOMENT, "PRO_FEM_MOMENT"},
    {PRO_FEM_DISPLACEMENT, "PRO_FEM_DISPLACEMENT"},
    {PRO_FEM_EDGE_PRESSURE, "PRO_FEM_EDGE_PRESSURE"},
    {PRO_FEM_STRUCT_TEMP, "PRO_FEM_STRUCT_TEMP"},
    {PRO_FEM_ACCELERATION, "PRO_FEM_ACCELERATION"},
    {PRO_FEM_ANG_VELOCITY, "PRO_FEM_ANG_VELOCITY"},
    {PRO_FEM_TOTAL_FORCE, "PRO_FEM_TOTAL_FORCE"},
    {-1, "Not Implemented"},
};

char *user_load_to_name (load)
int   load;
{
    char   name[PRODEV_NAME_SIZE];
    int    i;
    int    nloads = sizeof (load_names) / sizeof (Load_to_name);
    for (i = 0; i < nloads; i++)
         if (load_names[i].load == load)
             break;
    if (i == nloads)  i--;
    return (load_names[i].name);
}

/*-----------------------------------------------------------*\
    To convert FEM mesh control constants to strings for
    file input and output
\*-----------------------------------------------------------*/
typedef struct mctype_to_name
{
    int   mctype;
    char  name[PRODEV_NAME_SIZE];
} Mctype_to_name;

static Mctype_to_name mctype_names[] =
{
    {PRO_FEM_LOCAL_MAX, "PRO_FEM_LOCAL_MAX"},
    {PRO_FEM_GLOBAL_MAX, "PRO_FEM_GLOBAL_MAX"},
    {PRO_FEM_LOCAL_MIN, "PRO_FEM_LOCAL_MIN"},
    {PRO_FEM_GLOBAL_MIN, "PRO_FEM_GLOBAL_MIN"},
    {PRO_FEM_ON_EDGE, "PRO_FEM_ON_EDGE"},
    {PRO_FEM_HARD_POINT, "PRO_FEM_HARD_POINT"},
    {PRO_FEM_ENTER_POINT, "PRO_FEM_ENTER_POINT"},
    {-1, "Not Implemented"},
};

char *user_mctype_to_name (mctype)
int   mctype;
{
    char  name[PRODEV_NAME_SIZE];
    int   i;
    int   nmctypes = sizeof (mctype_names) / 
          sizeof (Mctype_to_name);
    for (i = 0; i < nmctypes; i++)
         if (mctype_names[i].mctype == mctype)
             break;
    if (i == nmctypes)  i--;
    return (mctype_names[i].name);
}


/*-----------------------------------------------------------*\
    To convert FEM region constants to strings for file
    input and output
\*-----------------------------------------------------------*/

typedef struct region_to_name
{
    int   region;
    char  name[PRODEV_NAME_SIZE];
} Region_to_name;

static Region_to_name region_names[] =
{
    {PRO_FEM_REG_POINT, "PRO_FEM_REG_POINT"},
    {PRO_FEM_REG_EDGE, "PRO_FEM_REG_EDGE"},
    {PRO_FEM_REG_SURFACE, "PRO_FEM_REG_SURFACE"},
    {PRO_FEM_REG_VOLUME, "PRO_FEM_REG_VOLUME"},
    {PRO_FEM_REG_ALL_SURFACES, "PRO_FEM_REG_ALL_SURFACES"},
    {PRO_FEM_REG_PATTERN_PNT, "PRO_FEM_REG_PATTERN_PNT"},
    {PRO_FEM_REG_FEATURE_PNT, "PRO_FEM_REG_FEATURE_PNT"},
    {PRO_FEM_Y_NOT_DEFINED, "PRO_FEM_Y_NOT_DEFINED"},
    {PRO_FEM_AXIS, "PRO_FEM_AXIS"},
    {PRO_FEM_SURFACE, "PRO_FEM_SURFACE"},
    {-1, "Not Implemented"},  
};

char  *user_region_to_name (region)
int    region;
{
    char   name[PRODEV_NAME_SIZE];
    int    i;
    int    nregions = sizeof (region_names) / 
           sizeof (Region_to_name);
    for (i = 0; i < nregions; i++)
         if (region_names[i].region == region )
             break;
    if (i == nregions )  i--;
    return (region_names[i].name);
}


/*-----------------------------------------------------------*\
    To convert FEM contact constants to strings for file
    input and output
\*-----------------------------------------------------------*/

typedef struct contact_to_name
{
    int   contact;
    char  name[PRODEV_NAME_SIZE];
} Contact_to_name;

static Contact_to_name contact_names[] =
{
    {PRO_FEM_SPAR, "PRO_FEM_SPAR"},
    {PRO_FEM_BEAM, "PRO_FEM_BEAM"},
    {PRO_FEM_GAP, "PRO_FEM_GAP"},
    {PRO_FEM_SPRING, "PRO_FEM_SPRING"},
    {PRO_FEM_CBEAM, "PRO_FEM_CBEAM"},
    {PRO_FEM_ADV_SPRING, "PRO_FEM_ADV_SPRING"},
    {PRO_FEM_COLD_RUNNER, "PRO_FEM_COLD_RUNNER"},
    {PRO_FEM_HOT_RUNNER, "PRO_FEM_HOT_RUNNER"},
    {PRO_FEM_FREE, "PRO_FEM_FREE"},
    {PRO_FEM_BOND, "PRO_FEM_BOND"},
    {-1, "Not Implemented"},  
};

char  *user_contact_to_name(contact)
int    contact;
{
    char  name[PRODEV_NAME_SIZE];
    int   i;
    int   ncontacts = sizeof (contact_names) / 
          sizeof (Contact_to_name);
    for (i = 0; i < ncontacts; i++)
         if (contact_names[i].contact == contact )
             break;
    if (i == ncontacts)  i--;
    return (contact_names[i].name);
}


/*===========================================================*\
FUNCTION : user_get_fem_action()
PURPOSE  : To process each FEM action for file input
           and output
\*===========================================================*/
int user_get_fem_action (p_model, action)
char   *p_model;
int     action;
{
    char             fname[PRODEV_NAME_SIZE],
                     name[PRODEV_NAME_SIZE];
    int              i, j, k, nnames, nitems;
    FILE            *fp;
    PRODEV_Name     *wnames;

    Pro_fem_constraint    *constr_arr;
    Pro_fem_mesh_control  *mesh_ctrls;
    Pro_fem_mass_element  *mass_elems;
    Pro_fem_contact       *contacts;
    Pro_fem_runner        *runners;
    Pro_fem_bar_element   *bar_elems;

    ProError              err;
    ProFileName           msg_fil;
    ProPath               path;

/*--------------------------------------------------------------------*\
    Open the output file 
\*--------------------------------------------------------------------*/
    ProStringToWstring( msg_fil, "msg_ugfund.txt" );
    strcpy (fname, "femdata.txt");
    if ((fp = fopen (fname, "a+")) == NULL)
    {
         ProMessageDisplay(msg_fil, "USER %0s", 
              "Cannot open file for write");
         return (0);
    }
    fprintf (fp, "\n");

/*--------------------------------------------------------------------*\
    Apply the appropriate action corresponding to the menu selection 
\*--------------------------------------------------------------------*/
    switch (action)
    {
       case  USER_FEM_GET_CONS_NAMES:
             nnames = profem_get_con_case_names (p_model,
                      &wnames);
             fprintf (fp, "Constraint Case Names[%d]\n",
                      nnames);
             for (i = 0; i < nnames; i++)
                  fprintf (fp, "\t%d.   %s\n", i,
                     ProWstringToString (name, wnames[i]));
             break;

       case  USER_FEM_GET_CONSTRAINTS:
             nnames = profem_get_con_case_names (p_model,
                      &wnames);
             fprintf (fp, "Constraints[%d]\n", nnames);
             for (i = 0; i < nnames; i++)
             {
                  fprintf (fp, "\t%d.   %s\n", i,
                      ProWstringToString (name, wnames[i]));
                  nitems = profem_get_constraints (p_model,
                      wnames[i], &constr_arr);
                  for (j = 0; j < nitems; j++)
                       fprintf (fp, "\t   %s\n",
                                user_load_to_name (
                                constr_arr[j].constraint_type));
             }
             break;

       case  USER_FEM_MASS_ELEMENTS:
             nitems = profem_get_mass_elements (p_model,
                      &mass_elems);
             fprintf (fp, "Mass Elements[%d]\n", nitems);
             for (i = 0; i < nitems; i++)  
             {
                  fprintf (fp, "\t   MassValue: %8.3f\n",
                     mass_elems[i].p_properties->value);
                  fprintf (fp, "\t   Region: %s\n",
                     user_region_to_name (
                     mass_elems[i].p_location->region_type));
             }
             break;

       case  USER_FEM_MESH_CONTROLS:
             nitems = profem_get_mesh_controls (p_model,
                      &mesh_ctrls);
             fprintf (fp, "Mesh Controls[%d]\n", nitems);
             for (i = 0; i < nitems; i++)
                  fprintf (fp, "\t   %s\n",
                        user_mctype_to_name (
                        mesh_ctrls[i].control_type));
             break;

       case  USER_FEM_GET_BAR_ELEMS:
             nitems = profem_get_bar_elements (p_model,
                      &bar_elems);
             fprintf (fp, "Bar Elements[%d]\n", nitems);
             for (i = 0; i < nitems; i++)
                  fprintf (fp, "\t   %s\n",
                        user_contact_to_name (
                        bar_elems[i].type));
             break;

       case  USER_FEM_GET_CONTACTS:
             nitems = profem_get_contacts (p_model,
                      &contacts);
             fprintf (fp, "Contacts[%d]\n", nitems);
             for (i = 0; i < nitems; i++)
                  fprintf (fp, "\t   %s\n",
                        user_contact_to_name (
                        contacts[i].type));
             break;

       case  USER_FEM_GET_RUNNERS:
             nitems = profem_get_runners (p_model,
                      &runners);
             fprintf (fp, "Runners[%d]\n", nitems);
             for (i = 0; i < nitems; i++)
                  fprintf (fp, "\t   %s\n",
                        user_contact_to_name (
                        runners[i].type));
             break;
       default:
             break;
    }


/*--------------------------------------------------------------------*\
    Close the output file and display it 
\*--------------------------------------------------------------------*/
    fclose (fp);

    ProStringToWstring( path, "femdata.txt" );
    err = ProInfoWindowDisplay( path, NULL, NULL );
    ERROR_CHECK( "user_get_fem_action", "ProInfoWindowDisplay", err );

    return (0);
}

