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


/*---------------------- Pro/Toolkit Includes ------------------------*/
#include "ProToolkit.h"
#include "ProAxis.h"
#include "ProFeature.h"
#include "ProFeatType.h"
#include "ProGeomitem.h"
#include "ProMdl.h"
#include "ProModelitem.h"
#include "ProObjects.h"
#include "ProSelection.h"
#include "ProSizeConst.h"
#include "ProSolid.h"
#include "ProUtil.h"

/*---------------------- Application Includes ------------------------*/
#include "TestError.h"


/*---------------------- Global Data ------------------------*/
typedef struct surface_visit_data
{
    FILE *fp;
    ProSolid part;
} AxisVisitData_t;

/*---------------------- Function Prototypes -------------------------*/
ProError UserDemoAxisAct();
ProError UserDemoHoleList();
int UserVisitDemo();


/*====================================================================*\
    FUNCTION :  UserDemoAxisAct()
    PURPOSE  :  Axis-visit action function, to write the
		axis name to a file.
\*====================================================================*/
ProError UserDemoAxisAct(
    ProAxis axis,
    ProError filt_status,
    ProAppData app_data)
{
    ProError status;
    AxisVisitData_t *p_data = (AxisVisitData_t*)app_data;
    ProSolid part=p_data->part;
    FILE *fp=p_data->fp;
    int id;
    ProModelitem modelitem;
    ProFeature feature;
    ProFeattype ftype;
    ProName wname;
    char name[PRO_NAME_SIZE];
    ProSelection selection;

/*--------------------------------------------------------------------*\
    Get the axis id
\*--------------------------------------------------------------------*/
    status = ProAxisIdGet(axis, &id);
    if(status != PRO_TK_NO_ERROR)
        return(status);

/*--------------------------------------------------------------------*\
    Make a Modelitem handle for the axis
\*--------------------------------------------------------------------*/
    status = ProModelitemInit(part, id, PRO_AXIS, &modelitem);
    if(status != PRO_TK_NO_ERROR)
        return(status);

/*--------------------------------------------------------------------*\
    Get the feature the axis belongs to
\*--------------------------------------------------------------------*/
    status = ProGeomitemFeatureGet(&modelitem, &feature);
    if(status != PRO_TK_NO_ERROR)
        return(status);

/*--------------------------------------------------------------------*\
    Get the type of the feature
\*--------------------------------------------------------------------*/
    status = ProFeatureTypeGet(&feature, &ftype);
    if(status != PRO_TK_NO_ERROR)
        return(status);
    
/*--------------------------------------------------------------------*\
    If the type was not HOLE, skip
\*--------------------------------------------------------------------*/
    if(ftype != PRO_FEAT_HOLE)
        return(PRO_TK_NO_ERROR);

/*--------------------------------------------------------------------*\
    Get the name of the axis
\*--------------------------------------------------------------------*/
    status = ProModelitemNameGet(&modelitem, wname);
    if(status != PRO_TK_NO_ERROR)
        return(status);
    ProWstringToString(name,wname);

/*--------------------------------------------------------------------*\
    Print out the axis name and hole id
\*--------------------------------------------------------------------*/
    ProTKFprintf(fp, "Axis %s belongs to hole feature %d\n", name, feature.id);

/*--------------------------------------------------------------------*\
    Highlight the owning hole
\*--------------------------------------------------------------------*/
    ProSelectionAlloc(NULL, &feature, &selection);
    ProSelectionHighlight(selection, PRO_COLOR_HIGHLITE);
    ProSelectionFree(&selection);

    return(PRO_TK_NO_ERROR);
}


/*====================================================================*\
    FUNCTION :  UserDemoHoleList()
    PURPOSE  :  Function to list the axes which belong to hole features
                in a part, and report their names
\*====================================================================*/
ProError UserDemoHoleList(
    ProSolid part)
{
    ProError status;
    AxisVisitData_t data;

    data.part = part;

/*--------------------------------------------------------------------*\
    Open the text file
\*--------------------------------------------------------------------*/
    data.fp = PTApplsUnicodeFopen("visit_test.dat","w");

/*--------------------------------------------------------------------*\
    Visit all the axes using the visit and filter functions above.
    Pass the owning sold, and the text file pointer using the app_data argument.
\*--------------------------------------------------------------------*/
    status = ProSolidAxisVisit(part, UserDemoAxisAct,
                                NULL, (ProAppData)&data);
    ERROR_CHECK("UserDemoHoleList","ProSolidAxisVisit",status);
/*--------------------------------------------------------------------*\
    Close the file
\*--------------------------------------------------------------------*/
    fclose(data.fp);

    return( PRO_TK_NO_ERROR );
}

/*====================================================================*\
    FUNCTION :  UserVisitDemo
    PURPOSE  :  call UserDemoHoleList on current model
\*====================================================================*/

int UserVisitDemo( )
{
  ProMdl model;
  ProError status;
  
  status = ProMdlCurrentGet( &model );
  ERROR_CHECK( "UserDemoVisit", "ProMdlCurrentGet", status );

  if ( status == PRO_TK_NO_ERROR )
    status = UserDemoHoleList( (ProSolid)model );

  return( (int)status );
}