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



/*---------------------- Pro/Toolkit Includes ------------------------*/
#include "ProToolkit.h"
#include "ProFeature.h"
#include "ProElemId.h"
#include "ProExtrude.h"
#include "ProModFeat.h"
#include "ProStdSection.h"
#include "ProElement.h"
#include "ProElempath.h"
#include "ProFeatType.h"
#include "ProFeatForm.h"
#include "ProSelection.h"
#include "ProSection.h"
#include "ProModelitem.h"

#include "ProSelbuffer.h"
#include "ProCollect.h"
#include "ProSrfcollection.h"
#include "ProCrvcollection.h"

#include "ProDraft.h"
#include "ProDirection.h"

static ProFileName message_file; 

#define C_LOG(a) ProTKPrintf ("Status for %s is = %d\n", a, status ); \
	ERROR_CHECK( a, "UgOADraftCreate.c", status );

#define C_PRINT(a) ProTKPrintf ( "%s\n", a);

/*===============================================================*\
FUNCTION : UserOADraftCreate()
PURPOSE  : Function for OA creation of Draft features.
\*===============================================================*/
ProError UserOADraftCreate()
{
  ProErrorlist            errors;
  ProMdl                  model;
  ProSelection            model_sel;
  ProFeature              feature;
  ProFeatureCreateOptions *opts = 0;
  ProElempath             path;
  ProElempathItem         path_items[2];
  ProSection              section;
  ProAsmcomppath          comp_path;
  ProAsmcomppath          *p_comp_path = NULL;

  ProCollection srf_collection = NULL;
  ProCollection crv_collection = NULL;

  ProModelitem model_item;
  ProReference reference, direction_ref = NULL;
  ProSelection direction_ref_sel = NULL; 
  ProName wide_string; 

  ProSelection * selbuff_sels; 
  int selbuff_sels_size, selbuff_count; 

  ProElement pro_e_feature_tree;
  ProElement pro_e_feature_type;
  ProElement pro_e_std_feature_name;
  ProElement pro_e_draft_tweak_or_intersec;
  ProElement pro_e_draft_extend;
  ProElement pro_e_draft_split;
  ProElement pro_e_std_surf_collection_appl;
  ProElement pro_e_direction_compound;
  ProElement pro_e_direction_reference;
  ProElement pro_e_direction_flip;
  ProElement pro_e_std_section;
  ProElement pro_e_std_sec_method;
  ProElement pro_e_std_sec_select;
  ProElement pro_e_std_curve_collection_appl;
  ProElement pro_e_surf_chain_cmpnd;
  ProElement pro_e_surf_chain_method;
  ProElement pro_e_surf_chain_ref_surfs;
  ProElement pro_e_surf_chain_surf;
  ProElement pro_e_std_sec_setup_plane;
  ProElement pro_e_std_sec_plane_view_dir;
  ProElement pro_e_std_sec_plane_orient_dir;
  ProElement pro_e_draft_constant_or_variable;
  ProElement pro_e_draft_include_tangent;
  ProElement pro_e_draft_side_1;
  ProElement pro_e_draft_neutral_object_type_1;
  ProElement pro_e_draft_dependent_1;
  ProElement pro_e_draft_angle_1;
  ProElement pro_e_draft_angles;
  ProElement pro_e_draft_side_2;
  ProElement pro_e_draft_neutral_object_type_2;
  ProElement pro_e_draft_dependent_2;
  ProElement pro_e_draft_angle_2;

  ProStringToWstring ( message_file, "msg_ugfeatcreat.txt" ); 

  status = ProMdlCurrentGet (&model);
  if ( status != PRO_TK_NO_ERROR ) return ( status );

  /*---------------------------------------------------------------*\
    Processing the contents of the selection buffer 
    + Allows only 3 entries in the selection buffer as follows -
      One selection, One curve collection & one surface collection
    + The 3 entries can be in any order, the code is capable of 
      extracting the right-ones to get assigned appropriately
      i.e. selection for draft direction, surface collection for 
      draft surface and curve collection for draft hinge elements
      in the final elemenet tree
  \*---------------------------------------------------------------*/

  status = ProSelbufferSelectionsGet( &selbuff_sels );
  C_LOG( " ProSelbufferSelectionsGet " );    
  if ( status != PRO_TK_NO_ERROR )
    return status; 

  status = ProArraySizeGet ( selbuff_sels, &selbuff_sels_size ); 
  C_LOG (" ProArraySizeGet")

    ProTKPrintf ( "Selections in selbuff = %d\n", selbuff_sels_size );
  /*---------------------------------------------------------------*\
    allowing exactly 3 entities in the selection buffer
  \*---------------------------------------------------------------*/
    if ( selbuff_sels_size != 3 )
      {
	status = ProMessageDisplay(message_file,
		  "USER Invalid selection buffer context:");
	C_LOG (" ProMessageDisplay")

	return PRO_TK_BAD_CONTEXT; 
      }


  for ( selbuff_count = 0; selbuff_count < selbuff_sels_size; selbuff_count++ )
    {
      ProCollection collection; 
      ProCollectionType coll_type; 
      ProTKPrintf ( "Count in selbuff = %d\n", selbuff_count ); 
      status = ProSelectionCollectionGet ( selbuff_sels[selbuff_count], 
					   &collection ); 
      C_LOG (" ProSelectionCollectionGet")

  /*---------------------------------------------------------------*\
    Processing selection for draft direction
  \*---------------------------------------------------------------*/
	if ( status != PRO_TK_NO_ERROR )
	  {
	    if ( direction_ref_sel != NULL )
	      {
  /*---------------------------------------------------------------*\
    Only one selection allowed for this context
  \*---------------------------------------------------------------*/
		status = ProMessageDisplay(message_file,
			    "USER Invalid selection buffer context:");
		C_LOG (" ProMessageDisplay")

		return PRO_TK_BAD_CONTEXT; 
	      }

  /*---------------------------------------------------------------*\
    Extracting the selection in the selection buffer - draft direction
  \*---------------------------------------------------------------*/
	    status = ProSelectionCopy ( selbuff_sels[selbuff_count], 
					&direction_ref_sel ); 
	    C_LOG (" ProSelectionCopy")

  /*---------------------------------------------------------------*\
    Converting to ProReference as required by the elemtree
  \*---------------------------------------------------------------*/
	    status = ProSelectionToReference ( direction_ref_sel, 
					       &direction_ref ); 
	    C_LOG (" ProSelectionToReference")
	  }
	else
	  {
  /*---------------------------------------------------------------*\
     Checking for collection types
  \*---------------------------------------------------------------*/
	    status = ProCollectionTypeGet ( collection, &coll_type );
	    C_LOG (" ProCollectionTypeGet")

	      if ( coll_type == PRO_COLLECTION_SRFCOLL ) 
		{
		  if ( srf_collection != NULL )
		    {
		      status = ProMessageDisplay(message_file,
			    "USER Invalid selection buffer context:");
		      C_LOG (" ProMessageDisplay")
			return PRO_TK_BAD_CONTEXT; 
		    }

  /*---------------------------------------------------------------*\
     Surface collection for draft surface
  \*---------------------------------------------------------------*/
		  status = ProSrfcollectionCopy ( collection, &srf_collection ); 
		  C_LOG (" ProSrfcollectionCopy")
		    if ( status != PRO_TK_NO_ERROR )
		      return status; 		  
		}
	      else
		{
		  if ( crv_collection != NULL )
		    {
		      status = ProMessageDisplay(message_file,
			    "USER Invalid selection buffer context:");
		      C_LOG (" ProMessageDisplay")
		      return PRO_TK_BAD_CONTEXT; 
		    }

  /*---------------------------------------------------------------*\
     Curve collection for draft hinge
  \*---------------------------------------------------------------*/
		  status = ProCrvcollectionCopy ( collection, &crv_collection ); 
		  C_LOG (" ProCrvcollectionCopy")
		    if ( status != PRO_TK_NO_ERROR )
		      return status; 
		}
  /*---------------------------------------------------------------*\
     Freeing the temporary copy of collection
  \*---------------------------------------------------------------*/ 
	    status = ProCollectionFree ( &collection ); 
	    C_LOG (" ProCollectionFree")
  
	  }
    }

  /*---------------------------------------------------------------*\
     Confirming for the required contents processed
  \*---------------------------------------------------------------*/

  if ( ( direction_ref_sel == NULL ) || ( srf_collection == NULL ) ||
       ( crv_collection == NULL ) )
    {
      status = ProMessageDisplay(message_file,
			    "USER Invalid selection buffer context:");
      C_LOG (" ProMessageDisplay")
      return PRO_TK_BAD_CONTEXT; 
    }
     
  /*---------------------------------------------------------------*\
    Populating root element PRO_E_FEATURE_TREE 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_FEATURE_TREE *** " );
  status = ProElementAlloc ( PRO_E_FEATURE_TREE, &pro_e_feature_tree ); 
  C_LOG( " ProElementAlloc " );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_FEATURE_TYPE 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_FEATURE_TYPE *** " );
  status = ProElementAlloc ( PRO_E_FEATURE_TYPE, &pro_e_feature_type );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_feature_type, PRO_FEAT_DRAFT ); 
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_feature_type );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_EXTEND 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_EXTEND *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_EXTEND, &pro_e_draft_extend );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_draft_extend, PRO_DRAFT_UI_NO_EXTEND );
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_draft_extend );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_SPLIT 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_SPLIT *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_SPLIT, &pro_e_draft_split );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_draft_split, PRO_DRAFT_UI_SPLIT_NONE );
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_draft_split );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_STD_SURF_COLLECTION_APPL 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_STD_SURF_COLLECTION_APPL *** " );
  status = ProElementAlloc ( PRO_E_STD_SURF_COLLECTION_APPL, &pro_e_std_surf_collection_appl );
  C_LOG( " ProElementAlloc " );

  /*---------------------------------------------------------------*\
    Assigning the surface collection 
  \*---------------------------------------------------------------*/
  status = ProElementCollectionSet( pro_e_std_surf_collection_appl, srf_collection );
  C_LOG( " ProElementCollectionSet " );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_std_surf_collection_appl );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DIRECTION_COMPOUND 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DIRECTION_COMPOUND *** " );
  status = ProElementAlloc ( PRO_E_DIRECTION_COMPOUND, &pro_e_direction_compound );
  C_LOG( " ProElementAlloc " );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_direction_compound  );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DIRECTION_REFERENCE 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DIRECTION_REFERENCE *** " );
  status = ProElementAlloc ( PRO_E_DIRECTION_REFERENCE, &pro_e_direction_reference );
  C_LOG( " ProElementAlloc " );

  /* Assigning the direction reference */ 
  status = ProElementReferenceSet( pro_e_direction_reference, direction_ref );
  C_LOG( " ProElementReferenceSet " );
  status = ProElemtreeElementAdd ( pro_e_direction_compound, NULL, pro_e_direction_reference );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DIRECTION_FLIP 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DIRECTION_FLIP *** " );
  status = ProElementAlloc ( PRO_E_DIRECTION_FLIP, &pro_e_direction_flip );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_direction_flip, 
				  PRO_DIRECTION_FLIP_ALONG ); 
  /* ProDirectionFlipOption :: ProDirection.h */ 
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_direction_compound, NULL, pro_e_direction_flip );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_CONSTANT_OR_VARIABLE 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_CONSTANT_OR_VARIABLE *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_CONSTANT_OR_VARIABLE, &pro_e_draft_constant_or_variable );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_draft_constant_or_variable, PRO_DRAFT_UI_CONSTANT ); /* 1 */ 
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_draft_constant_or_variable );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_INCLUDE_TANGENT 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_INCLUDE_TANGENT *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_INCLUDE_TANGENT, &pro_e_draft_include_tangent );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_draft_include_tangent, PRO_DRAFT_UI_INC_TANG );
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_draft_include_tangent );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_SIDE_1 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_SIDE_1 *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_SIDE_1, &pro_e_draft_side_1 );
  C_LOG( " ProElementAlloc " );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_draft_side_1  );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_NEUTRAL_OBJECT_TYPE_1 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_NEUTRAL_OBJECT_TYPE_1 *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_NEUTRAL_OBJECT_TYPE_1, &pro_e_draft_neutral_object_type_1 );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_draft_neutral_object_type_1, PRO_DRAFT_UI_CURVE );
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_draft_side_1, NULL, pro_e_draft_neutral_object_type_1 );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_STD_CURVE_COLLECTION_APPL 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_STD_CURVE_COLLECTION_APPL *** " );
  status = ProElementAlloc ( PRO_E_STD_CURVE_COLLECTION_APPL, &pro_e_std_curve_collection_appl );
  C_LOG( " ProElementAlloc " );

  /*---------------------------------------------------------------*\
     Assigning the curve collection 
  \*---------------------------------------------------------------*/
  status = ProElementCollectionSet( pro_e_std_curve_collection_appl, crv_collection );
  C_LOG( " ProElementCollectionSet " );
  status = ProElemtreeElementAdd ( pro_e_draft_side_1, NULL, pro_e_std_curve_collection_appl );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_DEPENDENT_1 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_DEPENDENT_1 *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_DEPENDENT_1, &pro_e_draft_dependent_1 );
  C_LOG( " ProElementAlloc " );
  status = ProElementIntegerSet ( pro_e_draft_dependent_1, PRO_DRAFT_UI_INDEPENDENT );
  C_LOG( " ProElementIntegerSet" );
  status = ProElemtreeElementAdd ( pro_e_draft_side_1, NULL, pro_e_draft_dependent_1 );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Populating  element PRO_E_DRAFT_ANGLE_1 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_DRAFT_ANGLE_1 *** " );
  status = ProElementAlloc ( PRO_E_DRAFT_ANGLE_1, &pro_e_draft_angle_1 );
  C_LOG( " ProElementAlloc " );
  status = ProElementDoubleSet (  pro_e_draft_angle_1, 10.000000 );
  C_LOG( " ProElementDoubleSet" );
  status = ProElemtreeElementAdd ( pro_e_draft_side_1, NULL, pro_e_draft_angle_1 );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
     Writing the elemtree in XML format
  \*---------------------------------------------------------------*/
  status = ProElemtreeWrite ( pro_e_feature_tree, PRO_ELEMTREE_XML, 
			      L"draft_tree.out" ); 
  C_LOG( " ProElemtreeWrite" );

  /*---------------------------------------------------------------*\
    Creating feature in the current model.
  \*---------------------------------------------------------------*/
  status = ProMdlToModelitem( model, &model_item ); 
  status = ProSelectionAlloc (p_comp_path, &model_item,
			      &model_sel);
  

  status = ProArrayAlloc(1,sizeof(ProFeatureCreateOptions),
    1, (ProArray*)&opts);

  opts[0]= PRO_FEAT_CR_DEFINE_MISS_ELEMS;

  status = ProFeatureWithoptionsCreate (model_sel, pro_e_feature_tree,
    opts, PRO_REGEN_NO_FLAGS, &feature, &errors);
  C_LOG (" ProFeatureWithoptionsCreate"); 

  status = ProArrayFree((ProArray*)&opts);
  
/*---------------------------------------------------------------*\
    Freeing allocated memory.
\*---------------------------------------------------------------*/ 
  
  status = ProSelectionFree (&model_sel ); 
  C_LOG (" ProSelectionFree"); 

  status = ProElementFree (&pro_e_feature_tree ); 
  C_LOG (" ProElementFree"); 
  
  return (status);
}

#undef C_LOG
#undef C_PRINT