/*====================================================================*\
FILE    : TestRout.c
PURPOSE : Create info for NCsequences and tools

HISTORY..
DATE      BUILD   AUTHOR     BUILD
16-Jan-97 H-01-24 Igor       $$1 Created
22-Jan-97 H-01-24 Jerry      $$2 Added prototype
15-Sep-97 H-03-22 Pavel      $$3 Replace Pro/D on Pro/E
06-Oct-97 H-03-25 Pavel	     $$4 More includes
27-Oct-97 H-03-28 Pavel      $$5 Added ProNcseq..Get functions calls
03-Nov-97 H-03-29 Pavel      $$6 Added call ProWcellMaxToolPosGet
05-Nov-97 H-03-30 Pavel      $$7 Added calls ProTool.. functions
03-Feb-98 H-03-38 Akula      $$8 Added Pro/T funcs, toll params, tool types...
20-Jul-98 I-01-15 Akula      $$9 Added SHANK_DIAM tool param.
27-Nov-98 I-01-27 agsh       $$10 Correct TEST_CALL_REPORT
31-May-99 I-03-11 mka        $$11 Delete unused variable
\*====================================================================*/

/*--------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*--------------------------------------------------------------------*/
#include <ProModelitem.h>
#include <ProNcseq.h>
#include <ProMfgoper.h>
#include <ProWcell.h>
#include <ProTool.h>

/*--------------------------------------------------------------------*\
Application includes
\*--------------------------------------------------------------------*/
#include "TestMfgNew.h"
#include "UtilString.h"
#include "UtilGeom.h"
#include "TestError.h"
#include "UtilMessage.h"

/*--------------------------------------------------------------------*\
Function prototypes
\*--------------------------------------------------------------------*/
ProError ProUtilToolChoose(ProMfg *p_mfg, ProTool *p_sel_tool);

/*====================================================================*\
FUNCTION : ProTestRouteNCseq()
PURPOSE  : Create info for NCsequence
\*====================================================================*/
ProError ProTestRouteNCseq()
{
    ProMfg mfg_model;
    ProAsmcomppath comp_path;
    ProSolid mfg_solid;
    ProNcseq* p_sel_ncseq; 
    ProError status;

    status = ProTestGetModels( &mfg_model, &mfg_solid, &comp_path);

    p_sel_ncseq = ProUtilNcseqChoose( &mfg_solid);   

    if (p_sel_ncseq != NULL)
       status = ProTestGetNcseqInfo(p_sel_ncseq);
    else
       ProUtilMsgPrint("gen", "TEST %0s","No NCsequences available");

    return (0);
}


/*====================================================================*\
FUNCTION : ProTestRouteTool()
PURPOSE  : Create info for Tool
\*====================================================================*/
ProError ProTestRouteTool()
{
    ProMfg mfg_model;
    ProAsmcomppath comp_path;
    ProSolid mfg_solid;
    ProError status;
    ProTool sel_tool;
 
     status = ProTestGetModels( &mfg_model, &mfg_solid, &comp_path);

     status = ProUtilToolChoose(&mfg_model, &sel_tool);

      if (status != PRO_TK_E_NOT_FOUND)
       {
         ProTestGetToolInfo(&sel_tool);
       }
      else
        ProUtilMsgPrint("gen", "TEST %0s","No Tools available");

    return (0);
}

/*====================================================================*\
FUNCTION : ProTestGetNcseqInfo()
PURPOSE  : Get Information about Nc sequence
\*====================================================================*/
ProError ProTestGetNcseqInfo(ProNcseq* p_ncseq)
{
    FILE *fp;
    wchar_t *w_string;
    char *string;
    ProError status;
    ProLine r_type_name, r_tool_id;
    ProFeature oper_feat, wcell_feat;
    ProSelection csys;
    ProWcellType wcell_type;
    wchar_t w_feat_name[PRO_NAME_SIZE];
    char feat_name[PRO_NAME_SIZE];
    char feat_name_u[PRO_NAME_SIZE];
    ProModelitem model_item;
    int seq_num;
    double cut_time, rem_vol;
    int max_tool_pos;
    ProMfg model;
    ProTool tool;
    int r_attr, i;
    ProName tool_mdl_name;
    ProMdlType tool_mdl_type;

    ProLine w_wcell_tid;
    char c_wcell_tid[PRO_LINE_SIZE];
    
/*--------------------------------------------------------------------*\
     Get Nc sequence name
\*--------------------------------------------------------------------*/
    w_string = (wchar_t *) calloc(PRO_NAME_SIZE, sizeof(wchar_t));
    string = (char *) calloc(PRO_NAME_SIZE, sizeof(char));

    status = ProModelitemNameGet((ProModelitem *) p_ncseq, w_string);
    TEST_CALL_REPORT("ProModelitemNameGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

/*--------------------------------------------------------------------*\
    Open info file
\*--------------------------------------------------------------------*/
    ProWstringToString (feat_name_u, w_string);
    ProUtilStringLower( feat_name_u, feat_name); 
    strcat(feat_name,".inf"); 
    fp = fopen(feat_name,"w"); 

    fprintf(fp, "\tInformation about \"%s\" NC sequence",
                                    ProWstringToString(string,w_string));

/*--------------------------------------------------------------------*\
     Get Nc sequence type
\*--------------------------------------------------------------------*/
    status = ProNcseqTypeGet(p_ncseq, r_type_name);
    TEST_CALL_REPORT("ProNcseqTypeGet()",
       "ProTestGetNcseqInfo()", status,
       (status != PRO_TK_NO_ERROR));
    fprintf(fp, "\n\nNc sequence type: %s",
       ProWstringToString(string, r_type_name));
    status = ProNcseqNumGet (p_ncseq, &seq_num);
    TEST_CALL_REPORT("ProNcseqNumGet()",
       "ProTestGetNcseqInfo()", status,
       (status != PRO_TK_NO_ERROR));
    fprintf (fp, "\nNc sequence number: %d", seq_num);
    status = ProNcseqCutTimeGet (p_ncseq, &cut_time);
    TEST_CALL_REPORT("ProNcseqCutTimeGet()",
       "ProTestGetNcseqInfo()", status,
       (status != PRO_TK_NO_ERROR));
    if (status == PRO_TK_NO_ERROR)
        fprintf (fp, "\nNc sequence cut time: %f", cut_time);
    status = ProNcseqRemovedVolGet (p_ncseq, &rem_vol);
    TEST_CALL_REPORT("ProNcseqRemovedVolGet()",
       "ProTestGetNcseqInfo()", status, status != PRO_TK_NO_ERROR &&
	status != PRO_TK_E_NOT_FOUND);
    if (status == PRO_TK_NO_ERROR)
       fprintf (fp, "\nNc sequence removed volume: %f", rem_vol);

/*--------------------------------------------------------------------*\
     Retrieve the operation and get its name
\*--------------------------------------------------------------------*/
    status = ProNcseqMfgoperGet(p_ncseq, &oper_feat);
    TEST_CALL_REPORT("ProNcseqMfgoperGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    status = ProModelitemNameGet((ProModelitem *) &oper_feat, w_string);
    TEST_CALL_REPORT("ProModelitemNameGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    fprintf(fp, "\nOperation name: %s", ProWstringToString(string,w_string));

/*--------------------------------------------------------------------*\
     Retrieve the operation's csys and get its name
\*--------------------------------------------------------------------*/
     status = ProMfgoperMachcsysGet(&oper_feat, &csys);
    TEST_CALL_REPORT("ProMfgoperMachcsysGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    status = ProSelectionModelitemGet(csys, &model_item);
    TEST_CALL_REPORT("ProSelectionModelitemGet()", "ProTestGetNcseqInfo()",
		    status, status != PRO_TK_NO_ERROR);

    status = ProModelitemNameGet(&model_item, w_string);
    TEST_CALL_REPORT("ProModelitemNameGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    fprintf(fp, "\nOperation's CSYS name: %s", 
                                  ProWstringToString(string,w_string));

/*--------------------------------------------------------------------*\
     Retrieve the workcell and get its name
\*--------------------------------------------------------------------*/
    status = ProNcseqWorkcellGet(p_ncseq, &wcell_feat);
    TEST_CALL_REPORT("ProNcseqWorkcellGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    status = ProModelitemNameGet((ProModelitem *) &wcell_feat, w_string);
    TEST_CALL_REPORT("ProModelitemNameGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    fprintf(fp, "\nWork Cell name: %s", ProWstringToString(string,w_string));

/*--------------------------------------------------------------------*\
     Get the workcell's type
\*--------------------------------------------------------------------*/
    status = ProWcellMaxToolPosGet (&wcell_feat, PRO_MACH_HEAD1, &max_tool_pos);
    TEST_CALL_REPORT("ProWcellMaxToolPosGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
    if (status == PRO_TK_NO_ERROR)
        fprintf(fp, "\nPRO_MACH_HEAD1: MaxToolPos %d", max_tool_pos);
    
    for (i=1;i<=max_tool_pos; i++)
    {
	status = ProWcellToolGet(&wcell_feat, PRO_MACH_HEAD1, i, w_wcell_tid);
        TEST_CALL_REPORT("ProWcellToolGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
	if (status == PRO_TK_NO_ERROR)
	{
	    ProWstringToString (c_wcell_tid, w_wcell_tid);
	    fprintf(fp, "\nPRO_MACH_HEAD1: ToolPos %d   ToolID  %s", i, c_wcell_tid);
	}
    }
    
	
    status = ProWcellMaxToolPosGet (&wcell_feat, PRO_MACH_HEAD2, &max_tool_pos);
    TEST_CALL_REPORT("ProWcellMaxToolPosGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
    if (status == PRO_TK_NO_ERROR)
        fprintf(fp, "\nPRO_MACH_HEAD2: MaxToolPos %d", max_tool_pos);
    
    for (i=1;i<=max_tool_pos; i++)
    {
	status = ProWcellToolGet(&wcell_feat, PRO_MACH_HEAD2, i, w_wcell_tid);
        TEST_CALL_REPORT("ProWcellToolGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
	if (status == PRO_TK_NO_ERROR)
	{
	    ProWstringToString (c_wcell_tid, w_wcell_tid);
	    fprintf(fp, "\nPRO_MACH_HEAD2: ToolPos %d   ToolID  %s", i, c_wcell_tid);
	}
    }
    status = ProWcellTypeGet((ProConstWcellPtr) &wcell_feat, &wcell_type);
    TEST_CALL_REPORT("ProWcellTypeGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    switch (wcell_type)
      {
        case PRO_WCELL_MILL_N_TRN:
           fprintf(fp, "\nWork Cell type:  MILL AND TURN");
           break;

        case PRO_WCELL_LATHE:
           fprintf(fp, "\nWork Cell type:  LATHE");
           break;

        case PRO_WCELL_GENERAL:
           fprintf(fp, "\nWork Cell type:  MILL");
           break;	   

        case PRO_WCELL_MILL:
           fprintf(fp, "\nWork Cell type:  MILL");
           break;	   
  
        case PRO_WCELL_HOLEMAKING:
           fprintf(fp, "\nWork Cell type:  HOLEMAKING");
           break;

        case PRO_WCELL_NONE:
           fprintf(fp, "\nWork Cell type:  not defined");
           break;

        default:
           fprintf(fp, "\nWork Cell type:  !!This case is not filled in!!");
           break;
      }

/*--------------------------------------------------------------------*\
     Retrieve the tool name
\*--------------------------------------------------------------------*/
    status = ProNcseqToolIdGet(p_ncseq, r_tool_id);
    TEST_CALL_REPORT("ProNcseqToolIdGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));

    fprintf(fp, "\nTool: %s", ProWstringToString(string, r_tool_id));
    
    status = ProMdlCurrentGet ((ProMdl*)&model);
    TEST_CALL_REPORT("ProMdlCurrentGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
    status = ProToolInit (r_tool_id, model, &tool);
    TEST_CALL_REPORT("ProToolInit()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
    status = ProToolVerify (&tool);
    TEST_CALL_REPORT("ProToolVerify()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
    if (status == PRO_TK_NO_ERROR)
    {
        status = ProToolAttributesGet (&tool, &r_attr);
        TEST_CALL_REPORT("ProToolAttributesGet()",
                        "ProTestGetNcseqInfo()", status,
                        (status != PRO_TK_NO_ERROR));
        fprintf (fp, "\nTool attributes: %d", r_attr);
        status = ProToolModelGet (&tool, tool_mdl_name, &tool_mdl_type);
        TEST_CALL_REPORT("ProToolModelGet()",
            "ProTestGetNcseqInfo()", status, 0);
        if (status != PRO_TK_NO_ERROR)
        {
            fprintf (fp, "\nTool does not have any model associated to it");
        }
        else
        {
            fprintf (fp, "\nTool model name: %s", 
                ProWstringToString(string, tool_mdl_name));
            fprintf (fp, "\nTool model type: %d", tool_mdl_type);
        }
    }
    
    fclose(fp);
    status = ProInfoWindowDisplay(ProStringToWstring(w_feat_name, feat_name),
                                                                NULL, NULL);
    TEST_CALL_REPORT("ProInfoWindowDisplay()", "ProTestGetNcseqInfo",
                                      status , status != PRO_TK_NO_ERROR);
    

    return PRO_TK_NO_ERROR;
}

/*====================================================================*\
FUNCTION : ProTestToolParamGet()
PURPOSE  : Read tool parameter (only double) and write to file.
\*====================================================================*/
ProError ProTestToolParamGet (
    FILE *fp,
    ProTool *p_tool,
    ProCharName tl_param_name)
{
    ProParamvalue param_val;
    ProError status;
    double param_value;
    
    
    status = ProToolParamGet(p_tool, tl_param_name, &param_val);
    TEST_CALL_REPORT("ProToolParamGet()",
	"ProTestGetToolInfo()", status, status != PRO_TK_NO_ERROR &&
	status != PRO_TK_E_NOT_FOUND);
    if (status != PRO_TK_NO_ERROR)
	return status;
    
     status = ProParamvalueValueGet (&param_val, PRO_PARAM_DOUBLE,
         &param_value);
     TEST_CALL_REPORT("ProParamvalueValueGet()",
                        "ProTestGetToolInfo()", status,
                        (status != PRO_TK_NO_ERROR));
    if (status != PRO_TK_NO_ERROR)
	return status;
    
    fprintf(fp, "\n%s parameter's value is %f", tl_param_name, param_value);
    return status;
}


/*====================================================================*\
FUNCTION : ProTestGetToolInfo()
PURPOSE  : Get Information about Nc sequence
\*====================================================================*/
ProError ProTestGetToolInfo(ProTool *p_tool)
{
    FILE *fp;
    char tool_name[PRO_LINE_SIZE];
    char tool_name_u[PRO_LINE_SIZE];
    ProToolType r_tool_type;
    wchar_t w_filename[PRO_LINE_SIZE];
    ProCharName tool_cutter_diam = "CUTTER_DIAM";
    ProCharName tool_length = "LENGTH";
    ProError status;
   
/*--------------------------------------------------------------------*\
    Open info file
\*--------------------------------------------------------------------*/
     ProWstringToString (tool_name_u, (wchar_t*) p_tool->tool_id);
     ProUtilStringLower( tool_name_u, tool_name);
     strcat(tool_name, ".inf"); 
     fp = fopen(tool_name,"w");

     fprintf(fp, "\tInformation about \"%s\" tool", tool_name_u); 

/*--------------------------------------------------------------------*\    
    Get the tool type
\*--------------------------------------------------------------------*/
     status = ProToolTypeGet (p_tool, &r_tool_type);
     TEST_CALL_REPORT("ProToolTypeGet()",
                        "ProTestGetToolInfo()", status,
                        (status != PRO_TK_NO_ERROR));

      switch (r_tool_type)
       {
         case PRO_TOOL_NONE:
           fprintf(fp, "\nTool type:  not defined");
           break;
	   
	 case PRO_TOOL_TURN:
	   fprintf(fp, "\nTool type:  Turning");
           break; 

         case PRO_TOOL_DRILL:
           fprintf(fp, "\nTool type:  Drilling");
           break;

         case PRO_TOOL_MILL:
           fprintf(fp, "\nTool type:  Milling");
           break;

         case PRO_TOOL_SIDE_MILL:
           fprintf(fp, "\nTool type:  Side Milling");
           break;

	 case PRO_TOOL_SAW:
	   fprintf(fp, "\nTool type:  Saw");
           break; 

         case PRO_TOOL_GROOVE:
           fprintf(fp, "\nTool type:  Groove");
           break;

         case PRO_TOOL_BORE:
           fprintf(fp, "\nTool type:  Bore");
           break;

         case PRO_TOOL_TAP:
           fprintf(fp, "\nTool type:  Tap");
           break;

	 case PRO_TOOL_CSINK:
           fprintf(fp, "\nTool type:  Counter sink");
           break;

         case PRO_TOOL_REAM:
           fprintf(fp, "\nTool type:  Ream");
           break;

	 case PRO_TOOL_CENTER_DRL:
	   fprintf(fp, "\nTool type:  Center drilling");
           break; 

         case PRO_TOOL_TURN_GRV:
           fprintf(fp, "\nTool type:  Turn groove");
           break;

         case PRO_TOOL_MILL_THREAD:
           fprintf(fp, "\nTool type:  Mill thread");
           break;

         case PRO_TOOL_TURN_THREAD:
           fprintf(fp, "\nTool type:  Turn thread");
           break;
	   
       } 

/*--------------------------------------------------------------------*\
    Get the tool parameters
\*--------------------------------------------------------------------*/
     ProTestToolParamGet(fp, p_tool, tool_cutter_diam);
     ProTestToolParamGet(fp, p_tool, tool_length);
     ProTestToolParamGet(fp, p_tool, "NOSE_RADIUS");
     ProTestToolParamGet(fp, p_tool, "SIDE_ANGLE");
     ProTestToolParamGet(fp, p_tool, "END_ANGLE");
     ProTestToolParamGet(fp, p_tool, "TOOL_WIDTH");
     ProTestToolParamGet(fp, p_tool, "CORNER_RADIUS");
     ProTestToolParamGet(fp, p_tool, "CUTTER_WIDTH");
     ProTestToolParamGet(fp, p_tool, "POINT_ANGLE");
     ProTestToolParamGet(fp, p_tool, "CHAMFER_LENGTH");
     ProTestToolParamGet(fp, p_tool, "DRILL_DIAMETER");
     ProTestToolParamGet(fp, p_tool, "DRILL_LENGTH");
     ProTestToolParamGet(fp, p_tool, "CUTTING_OFFSET");
     ProTestToolParamGet(fp, p_tool, "BODY_DIAMETER");
     ProTestToolParamGet(fp, p_tool, "INSERT_LENGTH");
     ProTestToolParamGet(fp, p_tool, "SIDE_WIDTH");
     ProTestToolParamGet(fp, p_tool, "SHANK_DIAM");

     fclose(fp);
     status = ProInfoWindowDisplay(ProStringToWstring(w_filename, tool_name),
         NULL,NULL);      
     TEST_CALL_REPORT ("ProInfoWindowDisplay", "ProTestGetToolInfo",
         status, status != PRO_TK_NO_ERROR);
    
     return PRO_TK_NO_ERROR;
}

