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

/*---------------------- Application Includes ------------------------*/
#include <TestError.h>
#include <ProCrvcollection.h>
/*---------------------- Function Prototypes -------------------------*/
ProError UserCurveLength();
ProError UserCrvcollectionFilter (ProSelection   cur_sel,
				  ProAppData   app_data);

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

static int crv_entry_count; 

/*============================================================================*\
	Function: UserCurveLength()
	Purpose: Compute Area of the Surface Collection
\*============================================================================*/
ProError UserCurveLength()
{
  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_length = 0; 

  ProSelection * coll_sels; 
  int coll_sels_size; 
  ProCollectioninstrType coll_flags[6] = { 
    PRO_CURVCOLL_ONE_BY_ONE, 
    PRO_CURVCOLL_TAN_CHAIN,
    PRO_CURVCOLL_CURVE_CHAIN,
    PRO_CURVCOLL_BNDRY_CHAIN,
    PRO_CURVCOLL_SURF_CHAIN,
    PRO_CURVCOLL_LOG_EDGE }; 
    
  crv_entry_count++; 

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

  status = ProMessageDisplay(msgfile,"USER Collect Curves to find total length:");
  ERROR_CHECK("UserCurveLength","ProMessageDisplay",status);

  status = ProCrvcollectionAlloc ( &collection );
  ERROR_CHECK( "UserCurveLength", "ProCrvcollectionAlloc", status );

/*----------------------------------------------------------------------------*\
	Interactive Curve Collection
\*----------------------------------------------------------------------------*/ 
  status = ProCurvesCollect ( (ProChaincollUIControl *)coll_flags, 
			      6, 
			      ( ProCrvcollFilter ) UserCrvcollectionFilter, 
			      ( ProAppData ) NULL, 
			      &collection, 
			      &coll_sels, 
			      &coll_sels_size ); 
 
  ERROR_CHECK( "UserCurveLength", "ProCurvesCollect", status );

/*----------------------------------------------------------------------------*\
       Regenerating the returned collection to get the list of 
       constituent curves
\*----------------------------------------------------------------------------*/  
  status = ProCrvcollectionRegenerate ( collection, 
					&regen_sels, &n_regen_sels ); 
  ERROR_CHECK( "UserCurveLength", "ProCrvcollectionRegenerate", status );

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

/*----------------------------------------------------------------------------*\
       Evaluating the overall length from edge collection
\*----------------------------------------------------------------------------*/ 
  for ( sel_count = 0; sel_count < n_regen_sels; sel_count++ )
    {
      ProEdge edge; 
      double length; 

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

      status = ProGeomitemToEdge ( &model_item, &edge ); 
      ERROR_CHECK( "UserCurveLength", "ProGeomitemToEdge", status );

/*----------------------------------------------------------------------------*\
       Evaluating the length of individual curve
\*----------------------------------------------------------------------------*/ 
      status = ProEdgeLengthEval ( edge, &length ); 
      ERROR_CHECK( "UserCurveLength", "ProSurfaceAreaEval", status );

      total_length = total_length + length; 

    }

  status = ProMessageDisplay(msgfile,"USER Total Curve Length is %0f",
			     &total_length);
  ERROR_CHECK( "UserCurveLength", "ProMessageDisplay", status );

/*----------------------------------------------------------------------------*\
       Freeing Memory
\*----------------------------------------------------------------------------*/ 

  status = ProCollectionFree ( &collection ); 
  ERROR_CHECK( "UserCurveLength", "ProCollectionFree", status );

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

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

  ProMessageClear();
  return(status);
}

/*============================================================================*\
	Function: UserCrvcollectionFilter()
	Purpose: Filter function for curve collection. 
	         Filters only PRO_EDGE type of selections
\*============================================================================*/
ProError UserCrvcollectionFilter (ProSelection   cur_sel,
				  ProAppData   app_data)
{
  ProError status; 
  ProModelitem model_item; 
  
  status = ProSelectionModelitemGet ( cur_sel, &model_item ); 
  ERROR_CHECK( "UserCrvcollectionFilter", "ProSelectionModelitemGet", status );

  if ( model_item.type == PRO_EDGE ) 
    return PRO_TK_NO_ERROR;
  else
    return PRO_TK_GENERAL_ERROR; 
}