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


/*---------------------- Pro/Toolkit Includes ------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProMessage.h>
#include <ProGraphic.h>
#include <ProWchar.h>
#include <ProUtil.h>
#include <PTApplsUnicodeUtils.h>

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

/*---------------------- Function Prototypes -------------------------*/
ProError UserScreen2DrwExample();
ProError UserSolid2ScreenExample();

/*------------------------- External Data ----------------------------*/
extern void UserScreenToDwg();
extern void UserSolidToScreen();
extern int UserMakeSelections();


ProError UserSolid2ScreenExample()
{
   ProError err;
   ProMdl model;
   ProMdlType type;
   ProSurface surface;
   ProModelitem p_mdl_item;
   ProPoint3d solid_point, screen_point;
   ProSolid solid;
   FILE *fp;
   ProPath path;

/*---------------------------------------------------------------------*\
  Get the current model and check if it is a solid 
\*---------------------------------------------------------------------*/
   err = ProMdlCurrentGet( &model );
   ERROR_CHECK( "UserSolid2ScreenExample", "ProMdlCurrentGet", err );
   if ( err != PRO_TK_NO_ERROR ) return ( err );
   err = ProMdlTypeGet( model, &type );
   ERROR_CHECK( "UserSolid2ScreenExample", "ProMdlTypeGet", err );
   if ( (type != PRO_MDL_PART) && (type != PRO_MDL_ASSEMBLY) ) return ( err );
   solid = (ProSolid)model;

/*---------------------------------------------------------------------*\
   Let the user select a surface and extract the selected point 
\*---------------------------------------------------------------------*/
   UserMakeSelections( &surface, &p_mdl_item, solid_point );

/*---------------------------------------------------------------------*\
   Transform from Solid to Screen Coordinates 
\*---------------------------------------------------------------------*/
   UserSolidToScreen( solid, solid_point, screen_point );

/*---------------------------------------------------------------------*\
  Write the results to a file and display it 
\*---------------------------------------------------------------------*/
   fp = PTApplsUnicodeFopen( "coord_trans.dat", "w" );
   if ( fp != NULL ) {
      ProTKFprintf( fp, "You picked a point with these coordinates:\n");
      ProTKFprintf( fp, "\tX: %f\n", solid_point[0]);
      ProTKFprintf( fp, "\tY: %f\n", solid_point[1]);
      ProTKFprintf( fp, "\tZ: %f\n", solid_point[2]);
      ProTKFprintf( fp, "\n" );
      ProTKFprintf( fp, "Transformed to screen coordinates:\n");
      ProTKFprintf( fp, "\tX: %f\n", screen_point[0]);
      ProTKFprintf( fp, "\tY: %f\n", screen_point[1]);
      ProTKFprintf( fp, "\tZ: %f\n", screen_point[2]);
      fclose (fp);

      ProStringToWstring( path, "coord_trans.dat" );
      err = ProInfoWindowDisplay( path, NULL, NULL );
      ERROR_CHECK( "UserScreen2DrwExample", "ProInfoWindowDisplay", err );
   }

   return( PRO_TK_NO_ERROR );
}

ProError UserScreen2DrwExample() 
{
   ProError err;
   ProMdl model;
   ProMdlType type;
   ProDrawing drawing;
   ProFileName msgfil;
   ProPoint3d position, drw_point;
   ProMouseButton button_pressed;
   FILE *fp;
   ProPath path;

/*---------------------------------------------------------------------*\
  Get the current model and check if it is a drawing 
\*---------------------------------------------------------------------*/
   err = ProMdlCurrentGet( &model );
   ERROR_CHECK( "UserScreen2DrwExample", "ProMdlCurrentGet", err );
   if ( err != PRO_TK_NO_ERROR ) return ( err );
   err = ProMdlTypeGet( model, &type );
   ERROR_CHECK( "UserScreen2DrwExample", "ProMdlTypeGet", err );
   if ( type != PRO_MDL_DRAWING ) return ( err );
   drawing = (ProDrawing)model; 

/*---------------------------------------------------------------------*\
  Prompt the user for a menu pick and get coordinates 
\*---------------------------------------------------------------------*/
   ProStringToWstring( msgfil, "msg_ugfund.txt" );

   err = ProMessageDisplay( msgfil, "USER Pick point on the screen" );
   ERROR_CHECK( "UserScreen2DrwExample", "ProMessageDisplay", err );

   err = ProMousePickGet( PRO_ANY_BUTTON, &button_pressed, position );  
   ERROR_CHECK( "UserScreen2DrwExample", "ProMousePickGet", err );

/*---------------------------------------------------------------------*\
  Transform the screen coordinates to drawing coordinates 
\*---------------------------------------------------------------------*/
   UserScreenToDwg( drawing, position, drw_point );

/*---------------------------------------------------------------------*\
  Write the results to a file and display it 
\*---------------------------------------------------------------------*/
   fp = PTApplsUnicodeFopen( "coord_trans.dat", "w" );
   if ( fp != NULL ) {
      ProTKFprintf( fp, "You picked a point with these coordinates:\n");
      ProTKFprintf( fp, "\tX: %f\n", position[0]);
      ProTKFprintf( fp, "\tY: %f\n", position[1]);
      ProTKFprintf( fp, "\tZ: %f\n", position[2]);
      ProTKFprintf( fp, "\n" );
      ProTKFprintf( fp, "Transformed to drawing coordinates:\n");
      ProTKFprintf( fp, "\tX: %f\n", drw_point[0]);
      ProTKFprintf( fp, "\tY: %f\n", drw_point[1]);
      ProTKFprintf( fp, "\tZ: %f\n", drw_point[2]);
      fclose (fp);

      ProStringToWstring( path, "coord_trans.dat" );
      err = ProInfoWindowDisplay( path, NULL, NULL );
      ERROR_CHECK( "UserScreen2DrwExample", "ProInfoWindowDisplay", err );
   }
   
   return ( PRO_TK_NO_ERROR );
}