/*
	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 "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, "UgIntcollectionDraftCreate.c", status );

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


/*===============================================================*\
FUNCTION : UserIntcollectionDraftCreate()
PURPOSE  : Function for creation of Draft features.
\*===============================================================*/
ProError UserIntcollectionDraftCreate()
{
  ProError                status;
  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 collection;
  ProCrvcollinstr crv_instr;
  ProModelitem model_item;
  ProReference reference;
  ProName wide_string; 
  ProSrfcollinstr srf_instr;
  ProSrfcollref isntr_ref;

  ProSelection * coll_sels; 
  int coll_sels_size; 
  ProSelection *sel_array;
  int n_sel_array;
  ProCollection tmp_collection; 
  ProCollectioninstrType srf_coll_filters[] = { PRO_SURFCOLL_SINGLE_SURF }; 
  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 }; 

  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 );

  /*---------------------------------------------------------------*\
    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 " );

  status = ProSrfcollectionAlloc ( &collection );
  C_LOG( " ProSrfcollectionAlloc" );

/*----------------------------------------------------------------------------*\
	Interactive Surface Collection
\*----------------------------------------------------------------------------*/ 
  status = ProMessageDisplay(message_file, "USER Collect surfaces :");
  C_LOG (" ProMessageDisplay")
  status = ProSurfacesCollect ( srf_coll_filters, 
				0, 
				( ProCollFilter ) NULL, 
				( ProAppData ) NULL, 
				collection, 
				&coll_sels, 
				&coll_sels_size ); 
				 
  C_LOG( " ProSurfacesCollect" );

  status = ProElementCollectionSet( pro_e_std_surf_collection_appl, collection );
  C_LOG( " ProElementCollectionSet " );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_std_surf_collection_appl );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Freeing Memory
  \*---------------------------------------------------------------*/
  status = ProCollectionFree ( &collection ); 
  C_LOG( " ProCollectionFree" );
  status = ProSelectionarrayFree ( coll_sels ); 
  C_LOG( " ProSelectionarrayFree" );

  /*---------------------------------------------------------------*\
    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 " );

  ProTKPrintf(" Please select edge,curve,axis DRAFT DIRECTION \n"); 
  status = ProMessageDisplay ( message_file, "USER Select draft direction :" );
  C_LOG( " ProMessageDisplay" );
  status = ProSelect ( "edge,curve,axis", 1, NULL, NULL, NULL, NULL,
		       &sel_array, &n_sel_array );
  C_LOG( " ProSelect" );

  if ( n_sel_array <= 0 )
    return PRO_TK_GENERAL_ERROR;
  
  /*---------------------------------------------------------------*\
    Converting ProSelection to ProReference  
  \*---------------------------------------------------------------*/
  status = ProSelectionToReference ( sel_array[0], &reference );
  C_LOG( " ProSelectionToReference" );

  status = ProElementReferenceSet( pro_e_direction_reference, reference );
  C_LOG( " ProElementReferenceSet " );
  status = ProElemtreeElementAdd ( pro_e_direction_compound, NULL, pro_e_direction_reference );
  C_LOG( " ProElemtreeElementAdd" );

  status = ProReferenceFree ( reference );
  C_LOG( " ProReferenceFree" );

  /*---------------------------------------------------------------*\
    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 " );


/*----------------------------------------------------------------------------*\
	Interactive Curve Collection
\*----------------------------------------------------------------------------*/ 
  status = ProCrvcollectionAlloc ( &tmp_collection );
  C_LOG( " ProCrvcollectionAlloc" );

  status = ProMessageDisplay(message_file, "USER Collect curves :");
  C_LOG (" ProMessageDisplay")

  status = ProCurvesCollect ( (ProChaincollUIControl *)coll_flags, 
			      6, 
			      ( ProCrvcollFilter ) NULL, 
			      ( ProAppData ) NULL, 
			      &tmp_collection, 
			      &coll_sels, 
			      &coll_sels_size ); 
  C_LOG( " ProCurvesCollect" );

/*----------------------------------------------------------------------------*\
       Converting collection from PRO_COLLECTION_LEGACY to PRO_COLLECTION_CRVCOLL 
       type as supported by Wildfire 2.0 feature element trees 
\*----------------------------------------------------------------------------*/ 
  status = ProCrvcollectionCopy ( tmp_collection, &collection ); 
  C_LOG( " ProCrvcollectionCopy" );

  status = ProElementCollectionSet( pro_e_std_curve_collection_appl, collection );
  C_LOG( " ProElementCollectionSet " );
  status = ProElemtreeElementAdd ( pro_e_draft_side_1, NULL, pro_e_std_curve_collection_appl );
  C_LOG( " ProElemtreeElementAdd" );

  /*---------------------------------------------------------------*\
    Freeing Memory
  \*---------------------------------------------------------------*/
  status = ProCollectionFree ( &tmp_collection ); 
  C_LOG( " ProCollectionFree" );
  status = ProCollectionFree ( &collection ); 
  C_LOG( " ProCollectionFree" );
  status = ProSelectionarrayFree ( coll_sels ); 
  C_LOG( " ProSelectionarrayFree" );

  /*---------------------------------------------------------------*\
    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);
  
/*---------------------------------------------------------------*\
    Free up the 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