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


/*---------------------- Pro/Toolkit Includes ------------------------*/
#include <ProToolkit.h>
#include <ProSurface.h>
#include <ProXsec.h>
#include <ProAsmcomp.h>
#include <ProUtil.h>

#include <PTApplsUnicodeUtils.h>


/*---------------------- Application Includes ------------------------*/
#include <TestError.h>

/*---------------------- Function Prototypes -------------------------*/
void user_List_Xsecs();
void user_member_path ();

ProError user_xsec_contour_visit(ProContour  p_contour,
                                  ProError    status,
                                  ProAppData  app_data)
{
  static int n_conts;
  FILE *fp = (FILE*)app_data;
  ProTKFprintf(fp, "            Contour %d\n", n_conts++);
  return PRO_TK_NO_ERROR;
}

ProError user_xsec_visit(ProXsec *p_xsec,
                         ProAppData app_data)
{
  ProError status;
  FILE *fp = (FILE*)app_data;
  ProCharName cname;
  ProXsecGeometry  *geom_arr;
  int n_xsec_items, ii;
  char        path[200];
  ProXsecMdlname p_xsecmdlname;
  ProMdlName p_xsec_name;
  ProXSectionItem *xsec_item_arr;

  ProWstringToString (cname, p_xsec->xsec_name);  
  ProTKFprintf(fp,"Cross section %s\n", cname);
    
  status = ProXsecRegenerate(p_xsec);
  
  ProWstringCopy(p_xsec->xsec_name, p_xsec_name, PRO_VALUE_UNUSED);
  
  status = ProXsecMdlnameAlloc(p_xsec->solid_owner, p_xsec_name, &p_xsecmdlname);
  
  status = ProXSectionItemsCollect(p_xsecmdlname, NULL, &xsec_item_arr);
  if (status != PRO_TK_NO_ERROR)
	  return (status);

  status = ProArraySizeGet((ProArray)xsec_item_arr, &n_xsec_items);

  /*-----------------------------------------------------------*\
    For each body/component of the cross section
  \*-----------------------------------------------------------*/
  for (ii = 0; ii < n_xsec_items; ii++)
  {
    int n_faces;
	ProSurface h_face;
    double area;
	ProAsmcomppath comp_path;
	ProType id_type;
	int id;
	ProGeom geom;
	
	status = ProXSectionItemDataGet(xsec_item_arr[ii], &comp_path, &id_type, &id, &geom);
	if (status != PRO_TK_NO_ERROR)
	  return (status);

    /*-----------------------------------------------------------*\
       Print the assembly path of the component.
    \*-----------------------------------------------------------*/    
	user_member_path (p_xsec->solid_owner, &comp_path, path);
    ProTKFprintf(fp,"    Component %2d : %s\n", ii, path);

    /*-----------------------------------------------------------*\
       List the component faces and contours.
    \*-----------------------------------------------------------*/
	
	 h_face = (ProSurface)geom;
     n_faces = 1;
     do {
         ProSurface next_face;
         status = ProSurfaceAreaEval(h_face, &area);
         ProTKFprintf(fp, "        Face %d, area %f\n", n_faces++, area);
         status = ProSurfaceContourVisit(h_face, 
                       (ProSurfaceContourVisitAction)user_xsec_contour_visit, 
                       (ProSurfaceContourFilterAction)NULL, (ProAppData)fp);
         status = ProSurfaceNextGet(h_face, &next_face);
         h_face = next_face;
     } while (h_face != NULL);
  }
  
  ProXSectionItemsArrFree( (ProXSectionItem **)&xsec_item_arr );

  return status;
}

void user_List_Xsecs()
{
    ProCharName filename = {'x','s','e','c','s','.','l','s','t','\0'};
    ProFileName wfilename;
    ProError    status;
    ProMdl      h_obj;

    FILE	*fp;

    ProStringToWstring(wfilename, filename);
    fp = PTApplsUnicodeFopen(filename, "w");

    status = ProMdlCurrentGet(&h_obj);
/*-----------------------------------------------------------*\
    For each cross section...
\*-----------------------------------------------------------*/
    status = ProSolidXsecVisit((ProSolid)h_obj, 
                    (ProSolidXsecVisitAction)user_xsec_visit, 
                    (ProAppData)fp);

    ProInfoWindowDisplay(wfilename, NULL, NULL);
}


void user_member_path (h_obj, comp_path, path)
ProMdl        h_obj;
ProAsmcomppath  *comp_path;
char           path[];
{
    ProMdl            assembly;
    char              name[PRO_MDLNAME_SIZE], type[PRO_MDLEXTENSION_SIZE],buff[200];
    int               m;    
    ProError          err;
	ProMdlName modelName;
	ProMdlExtension modelExtension;
	    
	err = ProMdlMdlnameGet((ProMdl)h_obj, modelName);    
 
	err = ProMdlExtensionGet((ProMdl)h_obj, modelExtension);    

    ProWstringToString(name,modelName);
    ProWstringToString(type,modelExtension);

    strcpy (path, name);
    strcat (path, ".");
    strcat (path, type);

    assembly = h_obj;
    for (m = 0; m < comp_path->table_num; m++)
    {
        ProMdl comp_model;
        ProAsmcomp component;

        component.type = PRO_FEATURE;
        component.id = comp_path->comp_id_table[m];
        component.owner = assembly;

        err = ProAsmcompMdlGet(&component, &comp_model);        

		err = ProMdlMdlnameGet((ProMdl)comp_model, modelName);
		 
		err = ProMdlExtensionGet((ProMdl)comp_model, modelExtension);
		
        ProTKSprintf(buff, "/%s.%s[%d]", 
                      ProWstringToString (name, modelName),
                      ProWstringToString (type, modelExtension), 
                      comp_path->comp_id_table[m] );
        strcat (path, buff);
    }
}