/*
	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 <ProDrawing.h>

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



/*================================================================*\
FUNCTION  : UserSheetDelete
PURPOSE   : Deletes a drawing sheet from the current drawing-type
            object.
\*================================================================*/
int UserSheetDelete ()
{
    int		   status;
    int 	   i_range[2], num_sheets, sheet_num;
    ProDrawing     p_draw;
    ProCharLine    astr;
    int 	   win_id;
    ProFileName WMSGFIL = {'m','s','g','_','u','g','d','w','g','.','t','x','t','\0'};

    /*------------------------------------------------------------*\
      Get the current model
    \*------------------------------------------------------------*/
    status = ProMdlCurrentGet((ProMdl*)&p_draw);
    ERROR_CHECK("UserSheetDelete","ProMdlCurrentGet",status);
    if (status != PRO_TK_NO_ERROR) return(status);

    /*----------------------------------------------------------------*\
      Get the total number of sheets of the current drawing-type object
    \*----------------------------------------------------------------*/
    status = ProDrawingSheetsCount(p_draw, &num_sheets);
    ERROR_CHECK("UserSheetDelete","ProDrawingSheetsCount",status);
    if (num_sheets <= 0) return(PRO_TK_GENERAL_ERROR);

    /*----------------------------------------------------------------*\
      Prompt for a sheet number or use the current one
    \*----------------------------------------------------------------*/
    i_range[0] = 1;
    i_range[1] = num_sheets;
    ProTKSprintf (astr, "Enter sheet number [1-%d] [CUR] : ", num_sheets);
    status = ProMessageDisplay(WMSGFIL, "USER %0s", astr);
    ERROR_CHECK("UserSheetDelete","ProMessageDisplay",status);

    status = ProMessageIntegerRead(i_range, &sheet_num);
    ERROR_CHECK("UserSheetDelete","ProMessageIntRead",status);
    if (status != PRO_TK_NO_ERROR)
      {
    status = ProDrawingCurrentSheetGet(p_draw, &sheet_num);
   	ERROR_CHECK("UserSheetDelete","ProDrawingCurrentSheetGet",status);
	if (sheet_num < 1) return (PRO_TK_GENERAL_ERROR);
      } 

    /*----------------------------------------------------------------*\
      Delete the specified sheet from the current drawing-type object.
    \*----------------------------------------------------------------*/
    status = ProDrawingSheetDelete(p_draw, sheet_num);
    ERROR_CHECK("UserSheetDelete","ProDrawingSheetDelete",status);
    if (status !=PRO_TK_NO_ERROR) return (PRO_TK_GENERAL_ERROR);

 
    ProTKSprintf(astr, "Sheet No. %0d deleted from drawing.",  sheet_num);
    status = ProMessageDisplay(WMSGFIL, "USER %0s", astr);
    ERROR_CHECK("UserSheetDelete","ProMessageDisplay",status);
    
    /*------------------------------------------------------------*\
      Update the changes by repainting the window
    \*------------------------------------------------------------*/
    status = ProWindowCurrentGet(&win_id);
    ERROR_CHECK("UserSheetDelete","ProWindowCurrentGet",status);

    status = ProWindowRepaint (win_id);
    ERROR_CHECK("UserSheetDelete","ProWindowRepaint",status);

    return (sheet_num);
}