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


/*-------------------------- Pro/Toolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProCsys.h>
#include <PTApplsUnicodeUtils.h>
/*-------------------------- Application includes ---------------------------*/
#include <TestError.h>


char* UserGetPlotSize (ProPlotPaperSize type)
{
    switch (type)
    {
   case A_SIZE_PLOT         : return "A";
   case B_SIZE_PLOT         : return "B";
   case C_SIZE_PLOT         : return "C";
   case D_SIZE_PLOT         : return "D";
   case E_SIZE_PLOT         : return "E";
   case A4_SIZE_PLOT        : return "A4";
   case A3_SIZE_PLOT        : return "A3";
   case A2_SIZE_PLOT        : return "A2";
   case A1_SIZE_PLOT        : return "A1";
   case A0_SIZE_PLOT        : return "A0";
   case F_SIZE_PLOT         : return "F";
   case VARIABLE_SIZE_PLOT  : return "VARIABLE SIZE";
   default                  : return "";
    }
}

/*---------------------------------------------------------------------------*\
Function : UserSheetsList() 
Purpose : List all the sheets in a drawing
\*---------------------------------------------------------------------------*/
ProError UserSheetsList(void * arg1, int arg2) 
{ 
   ProError    status;
   int         cur_sheet, n_sheets, count;
   double      matrix[4][4];
   ProDrawing  p_drawing;
   ProMdldata  drawing_data;
   char        drawing_name[PRO_MDLNAME_SIZE];
   char        drawing_type[PRO_MDLEXTENSION_SIZE];   
   ProCharName filename = {'s','h','e','e','t','s','.','l','s','t','\0'};
   ProFileName wfilename;
   FILE *      fp;
   ProMdlName modelName;
   ProMdlExtension modelExtension;

   ProStringToWstring(wfilename, filename);

   /*------------------------------------------*\
     get information about the current drawing
   \*------------------------------------------*/ 
   status = ProMdlCurrentGet((ProMdl*) &p_drawing);
   ERROR_CHECK( "UserSheetsList", "ProMdlCurrentGet", status );
   if (status != PRO_TK_NO_ERROR) return (status);
      
   status = ProMdlMdlnameGet(p_drawing, modelName);
   ERROR_CHECK( "UserSheetsList", "ProMdlMdlnameGet", status );
   if (status != PRO_TK_NO_ERROR) return (status);
	
   status = ProMdlExtensionGet(p_drawing, modelExtension);
   ERROR_CHECK( "UserSheetsList", "ProMdlExtensionGet", status );
   if (status != PRO_TK_NO_ERROR) return (status);


   status = ProDrawingSheetsCount(p_drawing, &n_sheets);
   ERROR_CHECK( "UserSheetsList", "ProDrawingSheetsCount", status);
   if (n_sheets <= 0) return (PRO_TK_GENERAL_ERROR);

   /*------------------------------------------*\
     store the current sheet number
   \*------------------------------------------*/ 
   status = ProDrawingCurrentSheetGet(p_drawing, &cur_sheet);
   ERROR_CHECK( "UserSheetsList", "ProDrawingCurrentSheetGet",status);
   if (cur_sheet<=0) return (PRO_TK_E_NOT_FOUND);

   /*------------------------------------------*\
     Open file and write drawing data as header
   \*------------------------------------------*/ 
   fp = PTApplsUnicodeFopen(filename, "w");
   ProWstringToString(drawing_name,modelName);
   ProWstringToString(drawing_type, modelExtension);
   ProTKFprintf(fp, "%s.%s contains the following %d sheets :\n",
		drawing_name, drawing_type, n_sheets); 

   /*------------------------------------------*\
     Visit all the sheets of the current drawing
   \*------------------------------------------*/ 
   for (count = 1; count <= n_sheets; count++) 
   {
        ProPlotPaperSize paper_type;
   
        status = ProDrawingCurrentSheetSet(p_drawing, count);
        ERROR_CHECK( "UserSheetsList", "ProDrawingCurrentSheetSet",status);
        if (status != PRO_TK_NO_ERROR) return (PRO_TK_GENERAL_ERROR);

        status = ProDrawingSheetSizeGet(p_drawing, count, &paper_type, NULL, NULL);
        ERROR_CHECK( "UserSheetsList", "ProDrawingSheetSizeGet", status);
        if (status != PRO_TK_NO_ERROR) return (status);
        ProTKFprintf(fp, "\t%d)\t%s\n", count, UserGetPlotSize(paper_type));
   }

   /*------------------------------------------*\
     restore the current sheet 
   \*------------------------------------------*/ 
   status = ProDrawingCurrentSheetSet (p_drawing, cur_sheet); 
   ERROR_CHECK( "UserSheetsList", "prodrw_set_current_sheet", status);
   if (status != PRO_TK_NO_ERROR) return (status);

   fclose(fp);

   /*------------------------------------------*\
     display the file in an information window 
   \*------------------------------------------*/ 
   status = ProInfoWindowDisplay(wfilename, NULL, NULL);
   ERROR_CHECK( "UserSheetsList", "ProInfoWindowDisplay", status );
   return (status);
}