/*
	Copyright (c) 2019 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_components, ii;
  char        path[200];

  ProTKFprintf(fp,"Cross section %s\n", ProWstringToString (cname,
               p_xsec->xsec_name));

  status = ProXsecRegenerate(p_xsec);

  status = ProXsecGeometryCollect(p_xsec, NULL, &geom_arr);
  status = ProArraySizeGet((ProArray)geom_arr, &n_components);

  /*-----------------------------------------------------------*\
    For each component of the cross section
  \*-----------------------------------------------------------*/
  for (ii = 0; ii < n_components; ii++)
  {
    int *memb_id_tab = geom_arr[ii].memb_id_tab;
    int memb_num, n_faces;
    ProSurface h_face = (ProSurface)geom_arr[ii].geom_ptr;
    double area;

    /*-----------------------------------------------------------*\
       Print the assembly path of the component.
    \*-----------------------------------------------------------*/
    status = ProArraySizeGet((ProArray)memb_id_tab, &memb_num);

    user_member_path (p_xsec->solid_owner, memb_num, memb_id_tab, path);
    ProTKFprintf(fp,"    Component %2d : %s\n", ii, path);

    /*-----------------------------------------------------------*\
       List the component faces and contours.
    \*-----------------------------------------------------------*/
     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);
  }

  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, memb_num, memb_id_tab, path)
ProMdl        h_obj;
int memb_num, *memb_id_tab;
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 < memb_num; m++)
    {
        ProMdl comp_model;
        ProAsmcomp component;

        component.type = PRO_FEATURE;
        component.id = memb_id_tab[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), 
                      memb_id_tab[m]);
        strcat (path, buff);
    }
}