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


/*---------------------- Pro/Toolkit Includes ------------------------*/
#include <ProToolkit.h>
#include <ProCollect.h>
#include <ProParameter.h>
#include <ProParamval.h>
#include <ProSrfcollection.h>

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

/*---------------------- Function Prototypes -------------------------*/
ProError UserSurfArea();

/*---------------------- Static variables -------------------------*/

static int srf_entry_count; 

/*============================================================================*\
	Function: UserSurfArea()
	Purpose: Compute Area of the Surface Collection
\*============================================================================*/
ProError UserSurfArea()
{
  int sel_count;
  ProError status;
  ProCharName name;
  ProModelitem model_item;
  ProFileName msgfile;
  ProName w_name;
  ProCollection collection; 
  ProSelection * regen_sels; 
  int n_regen_sels; 
  double total_area = 0; 

  ProParameter param; 
  ProParamvalue param_value; 
  ProName param_name; 
  char param_name_s[PRO_NAME_SIZE]; 
  ProSelection * coll_sels; 
  int coll_sels_size; 
  ProUnititem units;
  ProCollectioninstrType coll_filters[] = { PRO_SURFCOLL_SINGLE_SURF }; 
    
  srf_entry_count++; 

/*----------------------------------------------------------------------------*\
	Prompt user for Surface Collection
\*----------------------------------------------------------------------------*/
  ProStringToWstring(msgfile,"msg_uggeom.txt");

  status = ProMessageDisplay(msgfile,"USER Collect Surfaces to find total area:");
  ERROR_CHECK("UserSurfArea","ProMessageDisplay",status);
  
  status = ProSrfcollectionAlloc ( &collection );
  ERROR_CHECK( "UserSurfArea", "ProSrfcollectionAlloc", status );
 
/*----------------------------------------------------------------------------*\
	Interactive Surface Collection
\*----------------------------------------------------------------------------*/    
  status = ProSurfacesCollect ( coll_filters, 
				0, 
				( ProCollFilter ) NULL, 
				( ProAppData ) NULL, 
				collection, 
				&coll_sels, 
				&coll_sels_size ); 

  ERROR_CHECK( "UserSurfArea", "ProSurfacesCollect", status );

/*----------------------------------------------------------------------------*\
       Regenerating the returned collection to get the list of 
       constituent surfaces
\*----------------------------------------------------------------------------*/   
  status = ProSrfcollectionRegenerate ( collection, 
					&regen_sels, &n_regen_sels ); 

  if ( ( status != PRO_TK_NO_ERROR ) || ( n_regen_sels <= 0 ) )
    return PRO_TK_GENERAL_ERROR; 

/*----------------------------------------------------------------------------*\
       Evaluating the overall area from surface collection & assigning parameters
       to the individual surfaces
\*----------------------------------------------------------------------------*/ 
  for ( sel_count = 0; sel_count < n_regen_sels; sel_count++ )
    {
      ProSurface surface; 
      double area; 

      status = ProSelectionModelitemGet ( regen_sels[sel_count], 
					  &model_item ); 
      ERROR_CHECK( "UserSurfArea", "ProSelectionModelitemGet", status );

      status = ProGeomitemToSurface ( &model_item, &surface ); 
      ERROR_CHECK( "UserSurfArea", "ProGeomitemToSurface", status );

/*----------------------------------------------------------------------------*\
       Evaluating the area of individual surface
\*----------------------------------------------------------------------------*/  
      status = ProSurfaceAreaEval ( surface, &area ); 
      ERROR_CHECK( "UserSurfArea", "ProSurfaceAreaEval", status );

      total_area = total_area + area; 

/*----------------------------------------------------------------------------*\
       Initializing Parameter value
\*----------------------------------------------------------------------------*/  
      param_value.type = PRO_PARAM_DOUBLE; 
      param_value.value.d_val = area; 
	  ProStringToWstring(units.name,"meter");
      ProTKSprintf ( param_name_s, "%s_%d", "srf_dbl", 
		srf_entry_count+sel_count ); 
      ProStringToWstring ( param_name, param_name_s ); 

      status = ProModelitemNameSet ( &model_item, param_name ); 
      ERROR_CHECK( "UserSurfArea", "ProModelitemNameSet", status );

/*----------------------------------------------------------------------------*\
       Creating Parameter for surface
\*----------------------------------------------------------------------------*/ 
      status = ProParameterWithUnitsCreate( &model_item, param_name, &param_value,
				    &units,&param ); 
      ERROR_CHECK( "UserSurfArea", "ProParameterWithUnitsCreate", status );

    }

  status = ProMessageDisplay(msgfile,"USER Total Surface Area is %0f",
			     &total_area);
  ERROR_CHECK( "UserSurfArea", "ProMessageDisplay", status );

/*----------------------------------------------------------------------------*\
       Freeing Memory
\*----------------------------------------------------------------------------*/ 
  status = ProCollectionFree ( &collection ); 
  ERROR_CHECK( "UserSurfArea", "ProCollectionFree", status );

  status = ProSelectionarrayFree ( regen_sels ); 
  ERROR_CHECK( "UserSurfArea", "ProSelectionarrayFree", status );

  status = ProSelectionarrayFree ( coll_sels ); 
  ERROR_CHECK( "UserSurfArea", "ProSelectionarrayFree", status );

  ProMessageClear();
  return(status);
}