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

FILE    : UgCreoSweepCreate.c
PURPOSE : This example shows how to create Sweep using new element tree introduced
          in Creo 1.0. Element tree is documented in ProSweep.h
		  Example uses a dashboard to take necessary inputs required for
		  elements. Initially all non-sketcher elements are filled to create
		  incomplete feature & then feature is redefined with section added to
		  element PRO_E_SKECTHER
		  To run this example model must be current part or assembly
		  


#include <UgCreoSweepCreate.h>

#define sweep_create_log(call)  ProTKPrintf ("Status for %s is = %d\n", call, status);

static OtkSweepElemtreeValues elemValues;
static FILE* sweep_create_logfile=NULL;
static ProFileName sweep_msgfilename;
/*********************************************/
ProError UgCreoSweepCreate ()
{	
    ProError status=PRO_TK_NO_ERROR;
	ProUIDashboardShowOptions options;
	ProMdl currModel;
	ProMdlType mdlType;
	   
	sweep_create_logfile = fopen("Create_Creo_Sweep.log","w");	 

	status = ProMdlCurrentGet(&currModel);
	sweep_create_log("ProMdlCurrentGet");	  	  
	
	if(!currModel)
     {
	   ProStringToWstring(sweep_msgfilename,"msg_ugfeatcreat.txt");
		 
	   status = ProMessageDisplay(sweep_msgfilename,"USER Bad Context:");
	   sweep_create_log("ProMessageDisplay");
	   return PRO_TK_BAD_CONTEXT;             
	 }
	  
	status = ProMdlTypeGet(currModel,&mdlType);
	sweep_create_log("ProMdlTypeGet");
	 
	if((mdlType != PRO_MDL_ASSEMBLY) && (mdlType != PRO_MDL_PART))
	   {
	     status = ProMessageDisplay(sweep_msgfilename,"USER Invalid Type :");
	     sweep_create_log("ProMessageDisplay");
	      return PRO_TK_BAD_CONTEXT;             
	   }
	  
	else
	 {		
		/*Fill default values*/
		elemValues.curvesCollected=0;
		elemValues.dirReferselected=1;
		strcpy(elemValues.PRO_E_STD_FEATURE_NAME_value ,"Creo Sweep");
		elemValues.PRO_E_EXT_SURF_CUT_SOLID_TYPE_value = PRO_SWEEP_FEAT_TYPE_SOLID;
		elemValues.PRO_E_FRM_NORMAL_value = PRO_FRAME_NORM_ORIGIN;
		elemValues.PRO_E_FRM_USER_X_value = 0;

		status = UgSweepAppDashboardOptionsSetup (L"TK_Dashboard", 2, UgSweepAppSetupAllPagesFromFile,NULL,&options);	    
        
		if(status==PRO_TK_NO_ERROR)
	    status = ProUIDashboardShow (options);		
	  }		

fclose(sweep_create_logfile);
	
 return PRO_TK_NO_ERROR;

}

ProError  UgSweepAppBuildElementTree ()
{	
	
   ProError status=PRO_TK_NO_ERROR;
   ProElement elem_0_0,elem_1_1,elem_1_2,elem_1_3;
   ProElement elem_2_0,elem_3_0,elem_4_0,elem_4_1,elem_5_0,elem_2_1;
   ProElement elem_3_1,elem_3_2 = NULL,elem_4_2,elem_5_1,elem_5_2;
   ProElement elem_1_4,elem_1_5,elem_1_6,elem_1_7,elem_1_8;
   ProElement elem_2_2,elemtree,elem_3_3,elem_3_4,elem_3_5;
   ProFeature tk_feat;
   ProMdl currMdl;
   ProSelection mdlSel;
   ProModelitem currMdlitem;
   ProFeatureCreateOptions *cr_opts=NULL;
   ProFeatureCreateOptions complete_options[] = {PRO_FEAT_CR_INCOMPLETE_FEAT};
   ProErrorlist p_errors;
   ProElempath elemPath;
   ProElempathItem sktElempathitems[]=   { 
		                                    {PRO_ELEM_PATH_ITEM_TYPE_ID,PRO_E_SWEEP_PROF_COMP},
                                            {PRO_ELEM_PATH_ITEM_TYPE_ID,PRO_E_SWEEP_SECTION},
                                            {PRO_ELEM_PATH_ITEM_TYPE_ID,PRO_E_SKETCHER}
	                                      };
   ProSection sketch;
   ProElement sketch_elem;    
   ProBoolean is_incomplete=PRO_B_FALSE;
   wchar_t temp_feat_name[PRO_NAME_SIZE];
				
   /*PRO_E_FEATURE_TREE */
   status = ProElementAlloc(PRO_E_FEATURE_TREE,&elem_0_0);
   sweep_create_log("ProElementAlloc");

   /*PRO_E_FEATURE_FORM*/
    status = ProElementAlloc(PRO_E_FEATURE_FORM,&elem_1_1);
	sweep_create_log("ProElementAlloc");
	
	status = ProElementIntegerSet(elem_1_1,4);
	sweep_create_log("ProElementIntegerSet");
	
	status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_1);
	sweep_create_log("ProElemtreeElementAdd");

    /*PRO_E_SWEEP_TYPE*/
	status = ProElementAlloc(PRO_E_SWEEP_TYPE,&elem_1_2);
    sweep_create_log("ProElementAlloc");

	status = ProElementIntegerSet(elem_1_2,PRO_SWEEP_TYPE_MULTI_TRAJ);
	sweep_create_log("ProElementIntegerSet");

	status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_2);
	sweep_create_log("ProElemtreeElementAdd");
	
	/*PRO_E_SWEEP_FRAME_COMP*/
	status = ProElementAlloc(PRO_E_SWEEP_FRAME_COMP,&elem_1_3);
    sweep_create_log("ProElementAlloc");

    status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_3);
	sweep_create_log("ProElemtreeElementAdd");
	
	/*PRO_E_FRM_OPT_TRAJ*/
	status = ProElementAlloc(PRO_E_FRM_OPT_TRAJ,&elem_2_0);
    sweep_create_log("ProElementAlloc");

    status = ProElemtreeElementAdd(elem_1_3,NULL,elem_2_0);
	sweep_create_log("ProElemtreeElementAdd");

	/*PRO_E_OPT_TRAJ*/
	status = ProElementAlloc(PRO_E_OPT_TRAJ,&elem_3_0);
    sweep_create_log("ProElementAlloc");

    status = ProElemtreeElementAdd(elem_2_0,NULL,elem_3_0);
	sweep_create_log("ProElemtreeElementAdd");
	
	/*PRO_E_STD_SEC_METHOD*/
	status = ProElementAlloc(PRO_E_STD_SEC_METHOD,&elem_4_0);
    sweep_create_log("ProElementAlloc");
    
	status = ProElementIntegerSet(elem_4_0,PRO_SEC_SELECT);
	sweep_create_log("ProElementIntegerSet");

    status = ProElemtreeElementAdd(elem_3_0,NULL,elem_4_0);
	sweep_create_log("ProElemtreeElementAdd");

	/*PRO_E_STD_SEC_SELECT*/
	status = ProElementAlloc(PRO_E_STD_SEC_SELECT,&elem_4_1);
    sweep_create_log("ProElementAlloc");    

    status = ProElemtreeElementAdd(elem_3_0,NULL,elem_4_1);
	sweep_create_log("ProElemtreeElementAdd");

	/*PRO_E_STD_CURVE_COLLECTION_APPL*/
	status = ProElementAlloc(PRO_E_STD_CURVE_COLLECTION_APPL,&elem_5_0);
	sweep_create_log("ProElemtreeElementAdd");

	status = ProElementCollectionSet(elem_5_0,elemValues.PRO_E_FRM_OPT_TRAJ_crvColl);
	sweep_create_log("ProElementCollectionSet");

	status = ProElemtreeElementAdd(elem_4_1,NULL,elem_5_0);
	sweep_create_log("ProElemtreeElementAdd");

	status = ProCollectionFree(&(elemValues.PRO_E_FRM_OPT_TRAJ_crvColl));
	sweep_create_log("ProCollectionFree");

	/*PRO_E_FRAME_SETUP*/
    status = ProElementAlloc(PRO_E_FRAME_SETUP,&elem_2_1);
	sweep_create_log("ProElemtreeElementAdd");

	status = ProElemtreeElementAdd(elem_1_3,NULL,elem_2_1);
	sweep_create_log("ProElemtreeElementAdd");

	/*PRO_E_FRM_NORMAL*/
	status = ProElementAlloc(PRO_E_FRM_NORMAL,&elem_3_1);
	sweep_create_log("ProElemtreeElementAdd");

	status = ProElementIntegerSet(elem_3_1,elemValues.PRO_E_FRM_NORMAL_value);
	sweep_create_log("ProElementIntegerSet");

	status = ProElemtreeElementAdd(elem_1_3,NULL,elem_3_1);
	sweep_create_log("ProElemtreeElementAdd");

	if((elemValues.PRO_E_FRM_NORMAL_value==PRO_FRAME_PIVOT_DIR) || (elemValues.PRO_E_FRM_NORMAL_value==PRO_FRAME_CONST_Z_DIR))
	  {				
			  
		 if(elemValues.PRO_E_FRM_NORMAL_value==PRO_FRAME_PIVOT_DIR)
		  {
			 /*PRO_E_FRM_PIVOT_DIR*/
			  status = ProElementAlloc(PRO_E_FRM_PIVOT_DIR,&elem_3_2);
	          sweep_create_log("ProElementAlloc");
		      
			  status = ProElemtreeElementAdd(elem_1_3,NULL,elem_3_2);
	          sweep_create_log("ProElemtreeElementAdd");
		   }

		 else if(elemValues.PRO_E_FRM_NORMAL_value==PRO_FRAME_CONST_Z_DIR)
		   {
			  /*PRO_E_FRM_CONST_Z*/
			  status = ProElementAlloc(PRO_E_FRM_CONST_Z,&elem_3_2);
	          sweep_create_log("ProElementAlloc");
		      
			  status = ProElemtreeElementAdd(elem_1_3,NULL,elem_3_2);
	          sweep_create_log("ProElemtreeElementAdd");
		    }

			 /*PRO_E_DIRECTION_COMPOUND*/
		     status = ProElementAlloc(PRO_E_DIRECTION_COMPOUND,&elem_4_2);
	         sweep_create_log("ProElementAlloc");

			 status = ProElemtreeElementAdd(elem_3_2,NULL,elem_4_2);
	         sweep_create_log("ProElemtreeElementAdd");
			 
			/*PRO_E_DIRECTION_REFERENCE*/
            status = ProElementAlloc(PRO_E_DIRECTION_REFERENCE,&elem_5_1);
	        sweep_create_log("ProElementAlloc");

			status = ProElementReferenceSet(elem_5_1,elemValues.PRO_E_DIRECTION_REFERENCE_value);
			sweep_create_log("ProElementReferenceSet");

			status = ProElemtreeElementAdd(elem_4_2,NULL,elem_5_1);
			sweep_create_log("ProElemtreeElementAdd");

			 /*PRO_E_DIRECTION_FLIP*/
			status = ProElementAlloc(PRO_E_DIRECTION_REFERENCE,&elem_5_2);
	        sweep_create_log("ProElementAlloc");

			status = ProElementIntegerSet(elem_5_1,PRO_DIRECTION_FLIP_ALONG );
			sweep_create_log("ProElementReferenceSet");

			status = ProElemtreeElementAdd(elem_4_2,NULL,elem_5_2);
			sweep_create_log("ProElemtreeElementAdd");
   	    }

	    /*PRO_E_FRM_ORIENT*/
	    status = ProElementAlloc(PRO_E_FRM_ORIENT,&elem_3_3);
	    sweep_create_log("ProElementAlloc");

		status = ProElemtreeElementAdd(elem_1_3,NULL,elem_3_3);
		sweep_create_log("ProElemtreeElementAdd");

		if(elemValues.PRO_E_FRM_NORMAL_value=PRO_FRAME_PIVOT_DIR)
		{
		  status = ProElementIntegerSet(elem_3_3,PRO_FRAME_CONSTANT);
	      sweep_create_log("ProElementReferenceSet");
		}

		else
		{
	      status = ProElementIntegerSet(elem_3_3,PRO_FRAME_MIN);
	      sweep_create_log("ProElementReferenceSet");

		  //PRO_E_FRM_USER_X
		  status = ProElementAlloc(PRO_E_FRM_USER_X,&elem_3_4);
	      sweep_create_log("ProElementAlloc");

		  status = ProElementIntegerSet(elem_3_4,elemValues.PRO_E_FRM_USER_X_value);
		  sweep_create_log("ProElementIntegerSet");

		  status = ProElemtreeElementAdd(elem_1_3,NULL,elem_3_4);
		  sweep_create_log("ProElemtreeElementAdd");
		}	

		/*PRO_E_STD_FEATURE_NAME*/
	    status = ProElementAlloc(PRO_E_STD_FEATURE_NAME,&elem_1_4);
	    sweep_create_log("ProElementAlloc");

		ProStringToWstring(temp_feat_name,elemValues.PRO_E_STD_FEATURE_NAME_value);		

		status = ProElementWstringSet(elem_1_4,temp_feat_name);
		sweep_create_log("ProElementWstringSet");

		status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_4);
		sweep_create_log("ProElemtreeElementAdd");

		/*PRO_E_EXT_SURF_CUT_SOLID_TYPE*/
		status = ProElementAlloc(PRO_E_EXT_SURF_CUT_SOLID_TYPE,&elem_1_5);
	    sweep_create_log("ProElementAlloc");

		status = ProElementIntegerSet(elem_1_5,elemValues.PRO_E_EXT_SURF_CUT_SOLID_TYPE_value);
		sweep_create_log("ProElementWstringSet");

		status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_5);
		sweep_create_log("ProElemtreeElementAdd");

		/*PRO_E_REMOVE_MATERIAL*/
		status = ProElementAlloc(PRO_E_REMOVE_MATERIAL,&elem_1_6);
	    sweep_create_log("ProElementAlloc");

		status = ProElementIntegerSet(elem_1_6,elemValues.PRO_E_REMOVE_MATERIAL_value);
		sweep_create_log("ProElementWstringSet");

		status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_6);
		sweep_create_log("ProElemtreeElementAdd");
		
		/*PRO_E_FEAT_FORM_IS_THIN*/
		status = ProElementAlloc(PRO_E_FEAT_FORM_IS_THIN,&elem_1_7);
	    sweep_create_log("ProElementAlloc");

		status = ProElementIntegerSet(elem_1_7,elemValues.PRO_E_FEAT_FORM_IS_THIN_value);
		sweep_create_log("ProElementWstringSet");

		status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_7);
		sweep_create_log("ProElemtreeElementAdd");

		/*PRO_E_SWP_ATTR*/
		status = ProElementAlloc(PRO_E_SWP_ATTR,&elem_1_8);
	    sweep_create_log("ProElementAlloc");

		status = ProElemtreeElementAdd(elem_0_0,NULL,elem_1_8);
		sweep_create_log("ProElemtreeElementAdd");

		if(elemValues.PRO_E_EXT_SURF_CUT_SOLID_TYPE_value=PRO_SWEEP_FEAT_TYPE_SURFACE )
		{
		  /*PRO_E_END_SRF_ATTR*/
		  status = ProElementAlloc(PRO_E_END_SRF_ATTR,&elem_2_2);
	      sweep_create_log("ProElementAlloc");

		  status = ProElementIntegerSet(elem_2_2,elemValues.PRO_E_END_SRF_ATTR_value);
		  sweep_create_log("ProElementIntegerSet");

		  status = ProElemtreeElementAdd(elem_1_8,NULL,elem_2_2);
		  sweep_create_log("ProElemtreeElementAdd");
	    }

		/*Create Incomplete feature */
		status = ProArrayAlloc(0,sizeof(ProFeatureCreateOptions),1,(ProArray*)&cr_opts);
	    sweep_create_log("ProArrayAlloc");

        status = ProArrayObjectAdd((ProArray*)&cr_opts,PRO_VALUE_UNUSED,1,(void*)&(complete_options[0])); 
	    sweep_create_log("ProArrayObjectAdd");

		status = ProMdlCurrentGet(&currMdl);
	    sweep_create_log("ProMdlCurrentGet");

		status = ProMdlToModelitem(currMdl,&currMdlitem);
		sweep_create_log("ProMdlToModelitem");

		status = ProSelectionAlloc(NULL,&currMdlitem,&mdlSel);
		sweep_create_log("ProSelectionAlloc");

		status = ProFeatureWithoptionsCreate(mdlSel,elem_0_0,cr_opts,PRO_REGEN_NO_FLAGS,&tk_feat,&p_errors);
		sweep_create_log("ProFeatureWithoptionsCreate");
		
	 
	/*Fill PRO_E_SKETCHER & Redfine feature*/
	status = ProFeatureElemtreeExtract(&tk_feat,NULL,PRO_FEAT_EXTRACT_NO_OPTS,&elemtree);
	sweep_create_log("ProFeatureElemtreeExtract");

	status = ProElempathAlloc(&elemPath);
	sweep_create_log("ProFeatureElemtreeExtract ");
    
	status = ProElempathDataSet(elemPath,sktElempathitems,3); 
	sweep_create_log("ProFeatureElemtreeExtract ");

    status = ProElemtreeElementGet(elemtree,elemPath,&sketch_elem);     
	sweep_create_log("ProFeatureElemtreeExtract ");
    
	status = ProElementSpecialvalueGet(sketch_elem,NULL,(ProAppData*)&sketch);
	sweep_create_log("ProFeatureElemtreeExtract ");

	/* Adding a Circle and a Rectangle */
	UgSweepAppAddRectangle (sketch,0);
	
	UgSweepAppAddCircle (sketch); 

	 status = ProFeatureWithoptionsRedefine( NULL,&tk_feat,elemtree,cr_opts,PRO_REGEN_NO_FLAGS,&p_errors);
	sweep_create_log("ProFeatureWithoptionsRedefine ");

	/*Add one more Rectangle */
	UgSweepAppAddRectangle (sketch,30);

	 status = ProFeatureWithoptionsRedefine( NULL,&tk_feat,elemtree,cr_opts,PRO_REGEN_NO_FLAGS,&p_errors);
	sweep_create_log("ProFeatureWithoptionsRedefine ");


	/* Add a Ellipse */	
	UgSweepAppAddEllipse (sketch);
    
    status = ProElementSpecialvalueSet(sketch_elem,(ProAppData)sketch);
	sweep_create_log("ProElementSpecialvalueSet ");

	 status = ProFeatureWithoptionsRedefine( NULL,&tk_feat,elemtree,cr_opts,PRO_REGEN_NO_FLAGS,&p_errors);
	sweep_create_log("ProFeatureWithoptionsRedefine ");
    
    status = ProFeatureElemtreeExtract(&tk_feat,NULL,PRO_FEAT_EXTRACT_NO_OPTS,&elemtree);
	sweep_create_log("ProFeatureElemtreeExtract ");
     
    status = ProFeatureIsIncomplete(&tk_feat,&is_incomplete);
	sweep_create_log("ProFeatureIsIncomplete ");
    
	if(is_incomplete)      
    status = ProElemtreeWrite(elemtree,PRO_ELEMTREE_XML,L"./incomplete_tree.xml");
	sweep_create_log("ProElemtreeWrite ");
     
    status = ProElempathFree(&elemPath);
	sweep_create_log("ProElempathFree ");

    status = ProSectionFree(&sketch);    
	sweep_create_log("ProSectionFree ");
    
	status = ProElementFree(&elemtree);
	sweep_create_log("ProElementFree ");
    
	status = ProArrayFree((ProArray*)&cr_opts);
	sweep_create_log("ProElemtreeWrite ");	

	return status;
}




ProError UgSweepAppDashboardOptionsSetup (ProName dashboard_name,int num_slideup_panels,
					                         UgSweepAppDashboardPageSetupProcedure procedure, 
					                         ProAppData app_data,ProUIDashboardShowOptions* p_options)
{  
	
  ProUIDashboardPageOptions main_page;
  ProUIDashboardPageOptions *slideup_pages=NULL;
  ProUIDashboardShowOptions options;
  int ii=0;
  ProError status = PRO_TK_NO_ERROR;

  /* Setup main page */
  (*procedure) (dashboard_name, 0, app_data, &main_page);
  
   /* Setup slideup pages */  
    ProArrayAlloc (2, sizeof (ProUIDashboardPageOptions),2, (ProArray*)&slideup_pages);
   (*procedure) (dashboard_name, 1, app_data, &(slideup_pages[0]));
   (*procedure) (dashboard_name,2, app_data, &(slideup_pages[1]));
  
   status = ProUIDashboardshowoptionsAlloc (main_page, slideup_pages,  NULL, &options);
   sweep_create_log("ProUIDashboardshowoptionsAlloc ");

   status = ProUIDashboardshowoptionsTitleSet(options,L"Creo Sweep");
   sweep_create_log("ProUIDashboardshowoptionsTitleSet ");

  *p_options = options;

  return (PRO_TK_NO_ERROR); 
}


ProError UgSweepAppSetupAllPagesFromFile (ProName dashboard_name, 
					     int page_no, 
					     ProAppData appdata, 
					     ProUIDashboardPageOptions* p_options)
{
  ProCharLine page_name;
  ProCharLine sec_resource_name;
  ProUIDashboardPageOptions options;
  ProError status = PRO_TK_NO_ERROR;

  
  UgSweepAppDashboardPageData* page_data = (UgSweepAppDashboardPageData*)calloc (1, sizeof (UgSweepAppDashboardPageData));
  
  page_data->action = (ProUIDashboardpageCallbackFunction)UgSweepAppPageWriteAction;
  page_data->create_action = (ProUIDashboardpageCallbackFunction)NULL;
  
  switch(page_no)
  { 
   case 0:
   page_data->create_action = (ProUIDashboardpageCallbackFunction)UgSweepDashboardMainPageSetup;  
   status = ProUIDashboardpageoptionsAlloc ("dashboard_main_page", "MainPage", page_data, &options);
   sweep_create_log("ProUIDashboardpageoptionsAlloc ");
   break;

   case 1:  
   page_data->create_action = (ProUIDashboardpageCallbackFunction)UgSweepAppDashboardSlideupPageSetup;      
   status = ProUIDashboardpageoptionsAlloc ("dashboard_reference_page", "referenceSlidepage", page_data, &options);
   sweep_create_log("ProUIDashboardpageoptionsAlloc ");
   break;

   case 2:
   page_data->create_action = (ProUIDashboardpageCallbackFunction)UgSweepAppDashboardOptionPageSetup;   
   status = ProUIDashboardpageoptionsAlloc ("dashboard_options_page", "optionSlidepage", page_data, &options);
   sweep_create_log("ProUIDashboardpageoptionsAlloc ");
   break;  
  }
  
   status = ProUIDashboardpageoptionsNotificationSet (options, (ProUIDashboardpageCallbackFunction)UgSweepStdDashboardPageNotification, appdata);
   sweep_create_log("ProUIDashboardpageoptionsNotificationSet ");
  
  *p_options = options;

  return (PRO_TK_NO_ERROR);
}


/* Write the page details. */
ProError UgSweepAppPageWriteAction (ProUIDashboardPage page,ProUIDashboardPageEventType event_type,
	                            ProAppData appdata)
{
  ProAppData page_app_data;
  UgSweepAppDashboardPageData* page_data=NULL;  
  ProAppData user_data;
  ProUIDashboard dashboard;  
  ProError status = PRO_TK_NO_ERROR;

  status = ProUIDashboardpageUserdataGet (page, &page_app_data);
  sweep_create_log("ProUIDashboardpageUserdataGet ");
 

  page_data = (UgSweepAppDashboardPageData*)page_app_data;

  if (event_type == PRO_UI_DASHBOARD_PAGE_CREATE || event_type == PRO_UI_DASHBOARD_PAGE_SHOW)
    {
      if (page_data->create_action != (ProUIDashboardpageCallbackFunction)NULL)
	     (*(page_data->create_action)) (page, event_type, appdata);
    }

  if (event_type == PRO_UI_DASHBOARD_PAGE_DESTROY && appdata!=NULL)
  { 
      if(page_data)	
	  {
	   free(page_data);	  
	  }
  }
	  

  return (PRO_TK_NO_ERROR);
}

ProError UgSweepStdDashboardPageNotification (ProUIDashboardPage page,ProUIDashboardPageEventType event_type,
			 	  ProAppData appdata)
{
  ProAppData page_app_data;
  UgSweepAppDashboardPageData* page_data;
  ProError status;

  status = ProUIDashboardpageUserdataGet (page, &page_app_data);
  sweep_create_log("ProUIDashboardpageUserdataGet ")

  page_data = (UgSweepAppDashboardPageData*)page_app_data;

  (*page_data->action) (page, event_type, appdata);

  return (PRO_TK_NO_ERROR);
}

ProError UgSweepDashboardMainPageSetup (ProUIDashboardPage page,ProUIDashboardEventType event_type,
					       ProAppData appdata)
{
  char* device_name=NULL;
  ProUIDashboard dashboard;
  char* std_layout=NULL;
  ProUIGridopts grid_opts;
  ProError status = PRO_TK_NO_ERROR;
  char* comp_dashname=NULL;
  
 

  status = ProUIDashboardpageDevicenameGet (page, &device_name);
  sweep_create_log("ProUIDashboardpageDevicenameGet ");

  status = ProUIDashboardpageDashboardGet (page, &dashboard);  
  sweep_create_log("ProUIDashboardpageDashboardGet ");

  status = ProUIDashboardStdlayoutGet (dashboard, &std_layout);
  sweep_create_log("ProUIDashboardStdlayoutGet ");

  elemValues.sweepDashboard = dashboard;

  status = ProUIDashboardpageComponentnameGet(page,"SldorSrf",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");
  
  status = ProUIRadiogroupSelectActionSet (device_name,comp_dashname,Select_SldorSrf_CB, appdata);
  sweep_create_log("ProUIRadiogroupSelectActionSet ");  
  
  status = ProUIDashboardpageComponentnameGet(page,"RemoveMat",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");  
  
  status = ProUICheckbuttonActivateActionSet (device_name,comp_dashname,Activate_RemoveMat_CB, appdata); 
  sweep_create_log("ProUICheckbuttonActivateActionSet ");
  
  status = ProUIDashboardpageComponentnameGet(page,"ThinFeat",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");
  
  status = ProUICheckbuttonActivateActionSet (device_name,comp_dashname,Activate_ThinFeat_CB, appdata);
  sweep_create_log("ProUICheckbuttonActivateActionSet ");

  PRO_UI_GRIDOPTS_DEFAULT(grid_opts);
  grid_opts.row = 0;
  grid_opts.column = 0;
  grid_opts.top_offset = 18;
  grid_opts.left_offset = 13;

  status = ProUILayoutPushbuttonAdd (device_name, std_layout, "DONE_BTN",&grid_opts);
  sweep_create_log("ProUILayoutPushbuttonAdd ");
  
  status = ProUIPushbuttonBitmapSet(device_name,"DONE_BTN","dash_done");
  sweep_create_log("ProUIPushbuttonBitmapSet ");
  
  status = ProUIPushbuttonDisable(device_name,"DONE_BTN");
  sweep_create_log("ProUIPushbuttonDisable ");
  
  status = ProUIPushbuttonActivateActionSet (device_name,"DONE_BTN",Activate_DONE_CB,appdata);
  sweep_create_log("ProUIPushbuttonActivateActionSet ");
  
  PRO_UI_GRIDOPTS_DEFAULT(grid_opts);
  grid_opts.row = 0;
  grid_opts.column = 1;
  grid_opts.top_offset = 18;
  grid_opts.left_offset = 13;
  grid_opts.right_offset = 13;
 
  status = ProUILayoutPushbuttonAdd (device_name, std_layout, "CANCEL",&grid_opts);
  sweep_create_log("ProUILayoutPushbuttonAdd ");
  
  status = ProUIPushbuttonBitmapSet(device_name,"CANCEL","dash_quit");
  sweep_create_log("ProUIPushbuttonBitmapSet ");
  
  status = ProUIPushbuttonActivateActionSet (device_name,"CANCEL",Activate_Cancel_CB, appdata);
  sweep_create_log("ProUIPushbuttonActivateActionSet ");
 
 return PRO_TK_NO_ERROR;
}


ProError UgSweepAppDashboardSlideupPageSetup (ProUIDashboardPage page,ProUIDashboardEventType event_type,
					                             ProAppData appdata)

{
  char* comp_dashname=NULL;
  ProError status=PRO_TK_NO_ERROR;
  char* device_name; 
  ProUIDashboard dashboard;
  
  
  status = ProUIDashboardpageDashboardGet (page, &dashboard);
  sweep_create_log("ProUIDashboardpageDashboardGet ");
 
  status = ProUIDashboardpageDevicenameGet (page, &device_name);
  sweep_create_log("ProUIDashboardpageDevicenameGet ");
	
  status = ProUIDashboardpageComponentnameGet(page,"PRO_E_FRM_NORMAL",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");
  
  status = ProUIOptionmenuSelectActionSet (device_name, comp_dashname,Select_PRO_E_FRM_NORMAL_CB, appdata);
  sweep_create_log("ProUIOptionmenuSelectActionSet ");

  status = ProUIDashboardpageComponentnameGet(page,"CollElemPb",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");

  status = ProUIPushbuttonActivateActionSet (device_name, comp_dashname,Activate_PushButton3_CB, appdata);
  sweep_create_log("ProUIPushbuttonActivateActionSet ");

  status = ProUIDashboardpageComponentnameGet(page,"DirElemList",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");
  
  status = ProUIListSelectActionSet (device_name, comp_dashname,Activate_DirElemList_CB, appdata);
  sweep_create_log("ProUIListSelectActionSet ");

  status = ProUIDashboardpageComponentnameGet(page,"PBRemove",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");

  status = ProUIPushbuttonActivateActionSet (device_name,comp_dashname,Activate_PBRemove_CB, appdata);
  sweep_create_log("ProUIPushbuttonActivateActionSet ");
  
  return PRO_TK_NO_ERROR;

}



 ProError UgSweepAppDashboardOptionPageSetup (ProUIDashboardPage page,
					       ProUIDashboardEventType event_type,
					       ProAppData appdata)

{
  char* comp_dashname=NULL;
  ProError status=PRO_TK_NO_ERROR;
  char* device_name;
  ProUIDashboard dashboard;  
  
  status = ProUIDashboardpageDashboardGet (page, &dashboard);
  sweep_create_log("ProUIDashboardpageDashboardGet ");

  status = ProUIDashboardpageDevicenameGet (page, &device_name);
  sweep_create_log("ProUIDashboardpageDevicenameGet ");

  status = ProUIDashboardpageComponentnameGet(page,"CapEndCB",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");

  status = ProUICheckbuttonActivateActionSet (device_name, comp_dashname,
                                           Activate_CheckButton1_CB, appdata);
  sweep_create_log("ProUICheckbuttonActivateActionSet ");
   
  status = ProUIDashboardpageComponentnameGet(page,"FeatNameIP",&comp_dashname);
  sweep_create_log("ProUIDashboardpageComponentnameGet ");
	
  status = ProUIInputpanelActivateActionSet (device_name, comp_dashname,
                                          Activate_InputPanel1_CB, appdata);
  sweep_create_log("ProUIInputpanelActivateActionSet ");


  return PRO_TK_NO_ERROR;

}

 void Select_SldorSrf_CB (char *dialog,char *component,ProAppData  appdata)
{
	char** sel_names=NULL;
	ProError status=PRO_TK_NO_ERROR;
	int num_sels=0;
	ProUIDashboardPage page;
	char* comp_dashname=NULL;
	char* deviceName=NULL;

	status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_options_page",&page);
	sweep_create_log("ProUIDashboardPageGet ");
		  
	status = ProUIDashboardpageComponentnameGet(page,"CapEndCB",&comp_dashname);
	sweep_create_log("ProUIDashboardpageComponentnameGet ");

	status = ProUIDashboardpageDevicenameGet(page,&deviceName);
	sweep_create_log("ProUIDashboardpageDevicenameGet ");

	status = ProUIRadiogroupSelectednamesGet(dialog,component,&num_sels,&sel_names);
	sweep_create_log("ProUIRadiogroupSelectednamesGet ");

	if(!status)
	{
		if (strcmp (sel_names[0],"PRO_SWEEP_FEAT_TYPE_SOLID") == 0)
		{
          elemValues.PRO_E_EXT_SURF_CUT_SOLID_TYPE_value = PRO_SWEEP_FEAT_TYPE_SOLID;

		  status = ProUICheckbuttonDisable(deviceName,comp_dashname);
		  sweep_create_log("ProUICheckbuttonEnable ");

		}

		else if (strcmp (sel_names[0],"PRO_SWEEP_FEAT_TYPE_SURFACE") == 0)
		{ 
		  elemValues.PRO_E_EXT_SURF_CUT_SOLID_TYPE_value = PRO_SWEEP_FEAT_TYPE_SURFACE;

		  status = ProUICheckbuttonEnable(deviceName,comp_dashname);
		  sweep_create_log("ProUICheckbuttonEnable ");
  
		}
	}

	return;
}


 void Activate_RemoveMat_CB (char *dialog,char *component,ProAppData  appdata)

{
	ProBoolean checked = PRO_B_FALSE;
	ProError status=PRO_TK_NO_ERROR;
		
	status = ProUICheckbuttonGetState (dialog, component, &checked);
	sweep_create_log("ProUICheckbuttonGetState ");

	if(checked)
    	elemValues.PRO_E_REMOVE_MATERIAL_value = PRO_SWEEP_MATERIAL_REMOVE;

	else
		elemValues.PRO_E_REMOVE_MATERIAL_value = PRO_SWEEP_MATERIAL_ADD;
}



 void Activate_ThinFeat_CB (char *dialog,char *component,ProAppData  appdata)

{
	ProBoolean checked = PRO_B_FALSE;
	ProError status=PRO_TK_NO_ERROR;
		
	status = ProUICheckbuttonGetState (dialog, component, &checked);
	sweep_create_log("ProUICheckbuttonGetState ");

	if(checked)
    elemValues.PRO_E_FEAT_FORM_IS_THIN_value = PRO_SWEEP_FEAT_FORM_THIN;

	else
	elemValues.PRO_E_FEAT_FORM_IS_THIN_value = PRO_SWEEP_FEAT_FORM_NO_THIN;

	return;
}

 void Activate_DONE_CB (char *dialog,char *component,ProAppData  appData)

{
	ProError status=PRO_TK_NO_ERROR;

	status = UgSweepAppBuildElementTree();	
	sweep_create_log("UgSweepAppBuildElementTree");

	status = ProUIDashboardDestroy(elemValues.sweepDashboard);
	sweep_create_log("ProUIDashboardDestroy "); 
}

 void Activate_Cancel_CB (char *dialog,char *component,ProAppData  appData)

{
	ProError status=PRO_TK_NO_ERROR;
	
	status = ProUIDashboardDestroy(elemValues.sweepDashboard);
	sweep_create_log("ProUIDashboardDestroy ");

}

void Select_PRO_E_FRM_NORMAL_CB (char *dialog, char *component,ProAppData  appdata)

{
	ProError status = PRO_TK_NO_ERROR;
    int num_sels=0;
	char** sel_names=NULL;
	ProUIDashboardPage page;
	char* comp_dashname=NULL;
	
	status = ProUIOptionmenuSelectednamesGet (dialog, component, &num_sels, &sel_names);
	sweep_create_log("ProUIDashboardDestroy ");

	if (strcmp (sel_names[0],"PRO_FRAME_NORM_ORIGIN") == 0)
	elemValues.PRO_E_FRM_NORMAL_value = PRO_FRAME_NORM_ORIGIN;

	else if (strcmp (sel_names[0],"PRO_FRAME_PIVOT_DIR") == 0)     
	elemValues.PRO_E_FRM_NORMAL_value = PRO_FRAME_PIVOT_DIR;
	
	else if (strcmp (sel_names[0],"PRO_FRAME_CONST_Z_DIR") == 0)
	elemValues.PRO_E_FRM_NORMAL_value = PRO_FRAME_CONST_Z_DIR;	

	status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_reference_page",&page);
	sweep_create_log("ProUIDashboardPageGet ");
    
	status = ProUIDashboardpageComponentnameGet(page,"DirElemList",&comp_dashname);
	sweep_create_log("ProUIDashboardpageComponentnameGet ");

	if(strcmp (sel_names[0],"PRO_FRAME_PIVOT_DIR") == 0 || strcmp (sel_names[0],"PRO_FRAME_CONST_Z_DIR") == 0)	    
  	{
	   status = ProUIListEnable(dialog,comp_dashname);

	   status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_main_page",&page);	 
	   sweep_create_log("ProUIDashboardPageGet ");

	   elemValues.dirReferselected=0;
	   EnableDonebutton(&page);
	}


	else
	{
     status = ProUIListDisable(dialog,comp_dashname);	
	 elemValues.dirReferselected=1;
	 
	 status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_main_page",&page);	 
	 sweep_create_log("ProUIDashboardPageGet ");

	  EnableDonebutton(&page);
	}

}


 void Activate_PushButton3_CB (char* dialog, char* component, ProAppData appData)
{


 ProChaincollUIControl collection_types [] = {PRO_CHAINCOLLUI_ALLOW_ALL};
 ProSelection *crvSels=NULL;
 int n_crvSels=0;
 ProError status=PRO_TK_NO_ERROR;
 char device_name[PRO_NAME_SIZE];
 ProUIDashboardPage page;

 status = ProCrvcollectionAlloc(&(elemValues.PRO_E_FRM_OPT_TRAJ_crvColl));
 sweep_create_log("ProCrvcollectionAlloc ");

 status = ProSelbufferClear();
 sweep_create_log("ProSelbufferClear ");
	 
 status = ProCurvesCollect(collection_types,1,NULL,NULL,&(elemValues.PRO_E_FRM_OPT_TRAJ_crvColl),&crvSels,&n_crvSels);
 sweep_create_log("ProCurvesCollect ")

 if(!status) 
 {
	 elemValues.curvesCollected = 1;
	 
	 status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_main_page",&page);
	 sweep_create_log("ProUIDashboardPageGet ");

	 EnableDonebutton(&page);
 }

 status = ProSelectionarrayFree(crvSels);
 sweep_create_log("ProSelectionarrayFree ");
}

 void Activate_DirElemList_CB (char *dialog, char *component, ProAppData  appData)
{
	ProError status = PRO_TK_NO_ERROR;
 	wchar_t w_name[PRO_NAME_SIZE+15];
	char label[PRO_NAME_SIZE+15]="\0";
	char ItemName[PRO_NAME_SIZE]="\0";
	ProUIDashboardPage page;
	ProSelection *sels=NULL;
	int num_sels=0;
	ProModelitem mdlItem;
	wchar_t *label_arr=NULL;
	
	status = ProSelect("edge,curve,csys,surface,axis",1,NULL,NULL,NULL,NULL,&sels,&num_sels);
	sweep_create_log("ProSelect");

	if(!status)
	{
	   status = ProSelectionToReference(sels[0],&(elemValues.PRO_E_DIRECTION_REFERENCE_value));
	  sweep_create_log("ProSelectionToReference");
		 
	  status = ProSelectionModelitemGet(sels[0],&mdlItem);
      sweep_create_log("ProSelectionModelitemGet");

	  status = ProModelitemNameGet(&mdlItem,w_name);
	  sweep_create_log("ProModelitemNameGet");

	  if(!status)
	  ProWstringToString(ItemName,w_name);

	  sprintf(label,"Type: %d Id: %d Name:%s",(int)mdlItem.type,mdlItem.id,ItemName);

	  ProStringToWstring(w_name,label);

	  label_arr= w_name;

	  status = ProUIListLabelsSet(dialog,component,1,&label_arr);
	  sweep_create_log("ProUIListLabelsSet ")

	  elemValues.dirReferselected=1;

	  status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_main_page",&page);
	  sweep_create_log("ProUIDashboardPageGet ")

	  EnableDonebutton(&page);

    }
   
}

 void Activate_CheckButton1_CB (char *dialog,char *component,ProAppData  appData)
{

	ProBoolean checked = PRO_B_FALSE;
	ProError status=PRO_TK_NO_ERROR;
	
	status = ProUICheckbuttonGetState (dialog, component, &checked);
	sweep_create_log("ProUICheckbuttonGetState ");

	if(checked)
    elemValues.PRO_E_END_SRF_ATTR_value = PRO_SWEEP_END_SRF_CAPPED;

	else
	elemValues.PRO_E_END_SRF_ATTR_value = PRO_SWEEP_END_SRF_OPEN;

}
 void Activate_InputPanel1_CB (char *dialog,char *component, ProAppData  appData)
{
	ProError status=PRO_TK_NO_ERROR;
	wchar_t* w_name=NULL;

	status = ProUIInputpanelWidestringGet(dialog,component,&w_name);
	sweep_create_log("ProUIInputpanelWidestringGet ");

	if(!status)
	{
	 ProWstringToString(elemValues.PRO_E_STD_FEATURE_NAME_value,w_name);
	 
	 status = ProWstringFree(w_name);
	 sweep_create_log("ProWstringFree ");
	}

}

 void Activate_PBRemove_CB (char *dialog,char *component, ProAppData  appData)
{
	ProError status=PRO_TK_NO_ERROR;
	ProUIDashboardPage page;
	char* comp_dashname=NULL;
	wchar_t* w_label = L"<iB1> Select 1 item";

	elemValues.PRO_E_DIRECTION_REFERENCE_value=0;
    
	status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_reference_page",&page);
	sweep_create_log("ProUIDashboardPageGet ");

	status = ProUIDashboardpageComponentnameGet(page,"DirElemList",&comp_dashname);
    sweep_create_log("ProUIDashboardpageComponentnameGet ");
	
	status = ProUIListLabelsSet(dialog,comp_dashname,1,&w_label);
	sweep_create_log("ProUIListLabelsSet ");

	status = ProUIDashboardPageGet(elemValues.sweepDashboard,"dashboard_main_page",&page);
	sweep_create_log("ProUIDashboardPageGet ");

	elemValues.dirReferselected=0;

	EnableDonebutton(&page);
}

 void EnableDonebutton(ProUIDashboard* page)
{
	ProError status=PRO_TK_NO_ERROR;
	char* device_name=NULL;
	char* comp_name=NULL;
	ProUIDashboard dashboard;

	status = ProUIDashboardpageDashboardGet(*page,&dashboard);
    sweep_create_log("ProUIDashboardpageDashboardGet ");
	
	status = ProUIDashboardpageDevicenameGet(*page,&device_name);
	sweep_create_log("ProUIDashboardpageDevicenameGet ");
	
	if(elemValues.curvesCollected && elemValues.dirReferselected)
	{
		status = ProUIPushbuttonEnable(device_name,"DONE_BTN");
		sweep_create_log("ProUIPushbuttonEnable ");
	}
	
	else
	{
		status = ProUIPushbuttonDisable(device_name,"DONE_BTN");
		sweep_create_log("ProUIPushbuttonEnable ");

	}

	return;

}


ProError UgSweepAppAddRectangle (ProSection nsketch,int offset)
{
	Pro2dLinedef rect_entity1,rect_entity2,rect_entity3,rect_entity4;
	int ent_id=0;

	rect_entity1.type = PRO_2D_LINE;
    rect_entity1.end1[0] = -50 + (-offset);
	rect_entity1.end1[1] = 40 + offset;
	rect_entity1.end2[0] = 50 + offset;
	rect_entity1.end2[1] = 40 + offset;
	
	rect_entity2.type = PRO_2D_LINE;
    rect_entity2.end1[0] = 50 + offset;
	rect_entity2.end1[1] = 40 + offset;
	rect_entity2.end2[0] = 50 + offset;
	rect_entity2.end2[1] = -40 + (-offset);
	
	rect_entity3.type = PRO_2D_LINE;
    rect_entity3.end1[0] = 50 + offset;
	rect_entity3.end1[1] = -40 + (-offset);
	rect_entity3.end2[0] = -50 + (-offset);
	rect_entity3.end2[1] = -40 + (-offset);
	
	rect_entity4.type = PRO_2D_LINE;
    rect_entity4.end1[0] = -50 + (-offset);
	rect_entity4.end1[1] = -40 + (-offset);
	rect_entity4.end2[0] = -50 + (-offset);
	rect_entity4.end2[1] = 40 + offset;	     

	status = ProSectionEntityAdd(nsketch,(Pro2dEntdef*)&(rect_entity1),&ent_id);
	sweep_create_log("ProSectionEntityAdd ");

	status = ProSectionEntityAdd(nsketch,(Pro2dEntdef*)&(rect_entity2),&ent_id);
	sweep_create_log("ProSectionEntityAdd ");

	status = ProSectionEntityAdd(nsketch,(Pro2dEntdef*)&(rect_entity3),&ent_id);
	sweep_create_log("ProSectionEntityAdd ");

	status = ProSectionEntityAdd(nsketch,(Pro2dEntdef*)&(rect_entity4),&ent_id);
	sweep_create_log("ProSectionEntityAdd ");

	status = ProSectionIntentManagerModeSet(nsketch, PRO_B_TRUE);
	sweep_create_log("ProSectionIntentManagerModeSet ");

	status = ProSectionIntentManagerModeSet(nsketch, PRO_B_FALSE);
	sweep_create_log("ProSectionIntentManagerModeSet ");

	return;

}

ProError UgSweepAppAddCircle (ProSection nsketch)
{
	Pro2dCircledef circle_entity;
    int ent_id=0;

	circle_entity.type = PRO_2D_CIRCLE;
    circle_entity.center[0] = 0.0;
    circle_entity.center[1] = 0.0;
    circle_entity.radius = 30.00;

	status = ProSectionEntityAdd(nsketch,(Pro2dEntdef*)&(circle_entity),&ent_id);
	sweep_create_log("ProSectionEntityAdd ");

	status = ProSectionIntentManagerModeSet(nsketch, PRO_B_TRUE);
	sweep_create_log("ProSectionIntentManagerModeSet ");

	status = ProSectionIntentManagerModeSet(nsketch, PRO_B_FALSE);
	sweep_create_log("ProSectionIntentManagerModeSet ");

	return;

}

ProError UgSweepAppAddEllipse (ProSection nsketch)
{
	Pro2dEllipsedef ellipse_entity;
	int ent_id=0;

	ellipse_entity.type = PRO_2D_ELLIPSE;
	ellipse_entity.origin[0] = 0;
	ellipse_entity.origin[1] = 0;
	ellipse_entity.x_radius = 150;
	ellipse_entity.y_radius = 100;

	status = ProSectionEntityAdd(nsketch,(Pro2dEntdef*)&(ellipse_entity),&ent_id);
	sweep_create_log("ProSectionEntityAdd ");

	status = ProSectionIntentManagerModeSet(nsketch, PRO_B_TRUE);
	sweep_create_log("ProSectionIntentManagerModeSet ");

	status = ProSectionIntentManagerModeSet(nsketch, PRO_B_FALSE);
	sweep_create_log("ProSectionIntentManagerModeSet ");

	return;

}