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

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

/*===============================================================*\
FUNCTION : UserSimpleDraftCreate()
PURPOSE  : Function for creation of Simple Draft features.
\*===============================================================*/
ProError UserSimpleDraftCreate()
{
  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;

  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, "utilities.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_STD_FEATURE_NAME 
    - Optional 
  \*---------------------------------------------------------------*/
  C_PRINT( " *** Processing Element PRO_E_STD_FEATURE_NAME *** " );
  status = ProElementAlloc ( PRO_E_STD_FEATURE_NAME, &pro_e_std_feature_name );
  C_LOG( " ProElementAlloc " );
  ProStringToWstring ( wide_string, "DRAFT_ID_XXX" );
  status = ProElementWstringSet (  pro_e_std_feature_name, wide_string );
  C_LOG( " ProElementWstringSet" );
  status = ProElemtreeElementAdd ( pro_e_feature_tree, NULL, pro_e_std_feature_name );
  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" );
  status = ProSrfcollinstrAlloc ( 1, PRO_B_FALSE, &srf_instr );
  C_LOG( " ProSrfcollinstrAlloc" );
  status = ProSrfcollinstrIncludeSet ( srf_instr, PRO_B_TRUE );
  C_LOG( " ProSrfcollinstrIncludeSet" );
  {
    ProSelection *sel_array;
    int n_sel_array;

    ProTKPrintf(" Please select surface,sldface,qltface SURFACE to be drafted \n"); 
    status = ProMessageDisplay ( message_file, "USER Select reference." );
    C_LOG( " ProMessageDisplay" );
    status = ProSelect ( "surface,sldface,qltface", 1, NULL, NULL, NULL, NULL,
        &sel_array, &n_sel_array );
    C_LOG( " ProSelect" );

    if ( n_sel_array <= 0 )
      return PRO_TK_GENERAL_ERROR;
  
    status = ProSelectionToReference ( sel_array[0], &reference );
    C_LOG( " ProSelectionToReference" );
  }
  status = ProSrfcollrefAlloc ( PRO_SURFCOLL_REF_SINGLE, reference, &isntr_ref );
  C_LOG( " ProSrfcollrefAlloc" );
  status = ProSrfcollinstrReferenceAdd ( srf_instr, isntr_ref );
  C_LOG( " ProSrfcollinstrReferenceAdd" );
  status = ProSrfcollectionInstructionAdd ( collection, srf_instr );
  C_LOG( " ProSrfcollectionInstructionAdd" );
  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" );

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

  {
    ProSelection *sel_array;
    int n_sel_array;

    ProTKPrintf(" Please select edge,curve,axis DRAFT DIRECTION \n"); 
    status = ProMessageDisplay ( message_file, "USER Select reference." );
    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;
  
    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" );

  /*---------------------------------------------------------------*\
    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 " );
  status = ProCrvcollectionAlloc ( &collection );
  C_LOG( " ProCrvcollectionAlloc" );
  status = ProCrvcollinstrAlloc ( PRO_CURVCOLL_ADD_ONE_INSTR, &crv_instr );
  C_LOG( " ProCrvcollinstrAlloc" );
  status = ProCrvcollinstrAttributeSet ( crv_instr, PRO_CURVCOLL_NO_ATTR );
  C_LOG( " ProCrvcollinstrAttributeSet" );
  {
    ProSelection *sel_array;
    int n_sel_array;

    ProTKPrintf(" Please select edge,curve,axis DRAFT HINGE \n"); 
    status = ProMessageDisplay ( message_file, "USER Select reference." );
    C_LOG( " ProMessageDisplay" );
    status = ProSelect ( "edge,curve", 1, NULL, NULL, NULL, NULL,
        &sel_array, &n_sel_array );
    C_LOG( " ProSelect" );

    if ( n_sel_array <= 0 )
      return PRO_TK_GENERAL_ERROR;
  
    status = ProSelectionToReference ( sel_array[0], &reference );
    C_LOG( " ProSelectionToReference" );
  }
  status = ProCrvcollinstrReferenceAdd ( crv_instr, reference );
  C_LOG( " ProCrvcollinstrReferenceAdd" );
  status = ProCrvcollectionInstructionAdd ( collection, crv_instr );
  C_LOG( " ProCrvcollectionInstructionAdd" );
  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" );

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

  /*---------------------------------------------------------------*\
    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 = ProElementFree (&pro_e_feature_tree ); 
  C_LOG (" ProElementFree"); 
  
  return (status);
}

#undef C_LOG
#undef C_PRINT