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


/*--------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*--------------------------------------------------------------------*/
#include "ProToolkit.h"
#include "ProMdl.h"
#include "ProSolid.h"
#include "ProSurface.h"
#include "ProSheetmetal.h"
#include "ProMode.h"

/*--------------------------------------------------------------------*\
C System includes
\*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*\
Application includes
\*--------------------------------------------------------------------*/
#include <TestError.h>
#include <TestFiletypes.h>
#include <UtilMessage.h>
#include <UtilNames.h>
#include <UtilString.h>
#include <UtilTypes.h>
#include <UtilVisit.h>
#include <UtilFiles.h>
#include <PTApplsUnicodeUtils.h>
#include <UtilCollect.h>
#include <ProTKRunTime.h>
#include <ProUtil.h>

/*--------------------------------------------------------------------*\
Application macros
\*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*\
Application data types
\*--------------------------------------------------------------------*/

typedef struct testsmtlsurf
{
    ProMdl  *model;
    FILE    *fp;
} ProTestSmtlSurf;

/*--------------------------------------------------------------------*\
Application prototypes
\*--------------------------------------------------------------------*/
ProError ProTestCurveCompAct( ProCurve , ProCurve , int, ProBoolean,
    						   ProError, ProAppData);
ProError ProTestCurveAct( ProModelitem* , ProError, ProAppData);
int ProTestGeomTraverse(ProMdl *, int);




/*====================================================================*\
    FUNCTION :  ProTestSheetmetalTraverse()
    PURPOSE  :  Traverse all sheetmetal surfaces.
\*====================================================================*/
int ProTestSheetmetalTraverse(
    ProMdl *model)
{
  ProUtilCname fname;
  ProTestSmtlSurf app_data;
  ProError   status;
  FILE       *fp;
  ProSurface *surfaces;
  int surfaces_num, i;

  ProError ProTestShtmtlSurfAct(ProSurface surface, ProError instatus,
                                ProAppData tmp_app_data);

/*--------------------------------------------------------------------*\
    Get the name of the output file
\*--------------------------------------------------------------------*/
    ProTestQcrName(model, (char*)TRAVERSAL, fname);
    fp = PTApplsUnicodeFopen(fname,"w");

    if (fp == NULL)
        return(-1);

/*--------------------------------------------------------------------*\
    Set up the general data with the model and the file
\*--------------------------------------------------------------------*/
    app_data.fp = fp;
    app_data.model = model;

/*--------------------------------------------------------------------*\
        Set up a header for the output file
\*--------------------------------------------------------------------*/
        ProTKFprintf(fp,"SHEETMETAL SURFACES:\n\n");
        ProTKFprintf(fp,"  Id      Surface Type    \n");

/*--------------------------------------------------------------------*\
    Visit all the solid surfaces
\*--------------------------------------------------------------------*/
                               					 
    status = ProUtilCollectSolidSurfaces ((ProSolid) *model, &surfaces);
    if (status == PRO_TK_NO_ERROR)
    {
        status = ProArraySizeGet ((ProArray)surfaces, &surfaces_num);
        TEST_CALL_REPORT( "ProArraySizeGet()", "ProTestShtmtlSurfAct()", 
            status, status != PRO_TK_NO_ERROR );
        for (i = 0; i < surfaces_num; i++)
        {
            status = ProTestShtmtlSurfAct (surfaces[i],
	        PRO_TK_NO_ERROR, (ProAppData)&app_data);
        }
        status = ProArrayFree ((ProArray*)&surfaces);
        TEST_CALL_REPORT( "ProArrayFree()", "ProTestShtmtlSurfAct()", 
            status, status != PRO_TK_NO_ERROR );
    }

    if (status == PRO_TK_E_NOT_FOUND)
            ProTKFprintf(fp, " -- (No surfaces found) --\n");

    fclose(fp);

  return(0);
}


/*====================================================================*\
    FUNCTION :  ProTestShtmtlSurfAct()
    PURPOSE  :  General action function for a surface
\*====================================================================*/
ProError ProTestShtmtlSurfAct(
    ProSurface surface,
    ProError instatus,
    ProAppData tmp_app_data)
{
  ProTestSmtlSurf *app_data = (ProTestSmtlSurf *) tmp_app_data;
  ProSmtSurfType  smt_surf_type;
  ProMdl *model;
  int surf_id;
  ProError status;
  FILE *fp;

    fp     = app_data->fp;
    model  = app_data->model;

/*--------------------------------------------------------------------*\
        Get the surface id.
\*--------------------------------------------------------------------*/
    status = ProSurfaceIdGet(surface, &surf_id);
    TEST_CALL_REPORT("ProSurfaceIdGet()", "ProTestShtmtlSurfAct()",
                      status, status != PRO_TK_NO_ERROR);

/*--------------------------------------------------------------------*\
        Get the sheetmetal surface type.
\*--------------------------------------------------------------------*/
    status = ProSmtSurfaceTypeGet ((ProPart)(*model), surface,
                                                   &smt_surf_type);
    TEST_CALL_REPORT("ProSmtSurfaceTypeGet()", "ProTestShtmtlSurfAct()",
                      status, status != PRO_TK_NO_ERROR);

/*--------------------------------------------------------------------*\
        Print the id and type of the sheetmetal surface.
\*--------------------------------------------------------------------*/
    switch (smt_surf_type)
    {
        case PRO_SMT_SURF_NON_SMT :
            ProTKFprintf(fp,"  %d \t %s\n", surf_id, 
                          "PRO_SMT_SURF_NON_SMT - surface created by solid feature.");
          break;
        case PRO_SMT_SURF_SIDE :
            ProTKFprintf(fp,"  %d \t %s\n", surf_id, 
                          "PRO_SMT_SURF_SIDE - side surface.");
          break;
        case PRO_SMT_SURF_FACE :
            ProTKFprintf(fp,"  %d \t %s\n", surf_id, 
                          "PRO_SMT_SURF_FACE - face (green) surface.");
          break;
        case PRO_SMT_SURF_OFFSET :
            ProTKFprintf(fp,"  %d \t %s\n", surf_id, 
                          "PRO_SMT_SURF_OFFSET - offset (white) surface.");
          break;
        default :;
    }

  return(PRO_TK_NO_ERROR);
}

/*====================================================================*\
    FUNCTION :  ProTestSheetmetalSurfs()
    PURPOSE  :  Command Traversal for listing of all surfaces in a
                sheetmetal model and evaluation of surface types.
\*====================================================================*/
int ProTestSheetmetalSurfs(
    ProMdl *model,
    int action)
{
  ProError status;
  ProMdldata mdata;
  ProMdlExtension modelExtension;
  ProMode  curr_mode;
  ProUtilCname type;

/*--------------------------------------------------------------------*\
    Find out the current mode (works only in the sheetmetal mode)
\*--------------------------------------------------------------------*/
    status = ProModeCurrentGet(&curr_mode);
    TEST_CALL_REPORT("ProModeCurrentGet()", "ProTestSheetmetalSurfs()",
                      status, status != PRO_TK_NO_ERROR);

    if (curr_mode != PRO_MODE_SHEET_METAL)
        return(-1);

/*--------------------------------------------------------------------*\
    Find out the model type
\*--------------------------------------------------------------------*/
    status = ProMdlExtensionGet(*model, modelExtension);
    TEST_CALL_REPORT("ProMdlExtensionGet()", "ProTestSheetmetalSurfs()",
                      status, status != PRO_TK_NO_ERROR);
    ProWstringToString(type, modelExtension);

/*--------------------------------------------------------------------*\
    Visit all surfaces in the sheetmetal model.
\*--------------------------------------------------------------------*/
    if(!ProUtilStrcmp(type, (char*)"PRT"))
        ProTestSheetmetalTraverse(model);

    return(0);
}