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

#include <ProToolkit.h>
#include <ProSelection.h>
#include <ProGeomitem.h>
#include <ProSolid.h>
#include <UtilMath.h>

#include <TestError.h>

/*============================================================================*\
	Function: UserOutlineCompute()
	Purpose: Display extents of solid in space defined by CSYS
\*============================================================================*/
int UserOutlineCompute()
{
    int sel_count;
    ProError status;
    ProFileName msgfile;
    ProSelection *psels=NULL;
    ProModelitem csys_feat;
    ProGeomitemdata *geom_data=NULL;
    ProCsysdata *p_csys=NULL;
    ProMdl solid;
    Pro3dPnt outline[2];
    ProMatrix transf, itranf;
    ProSolidOutlExclTypes excludes[] = {PRO_OUTL_EXC_DATUM_PLANE, 
                                        PRO_OUTL_EXC_DATUM_POINT,
                                        PRO_OUTL_EXC_DATUM_CSYS};

/*----------------------------------------------------------------------------*\
   Request Csys as reference point and orientation to report outline points
\*----------------------------------------------------------------------------*/

    ProStringToWstring(msgfile,"msg_ugsolid.txt");
    status = ProMessageDisplay(msgfile,"USER Select CSYS as reference point");
    ERROR_CHECK("UserOutlineCompute","ProMessageDisplay",status);
    if((ProSelect("csys",1,NULL,NULL,NULL,NULL,&psels, &sel_count) != 
       PRO_TK_NO_ERROR) || (sel_count < 1))
    {
       status = ProMessageDisplay(msgfile,"USER Invalid Selection!");
       ERROR_CHECK("UserOutlineCompute","ProMessageDisplay(Invalid Selection)",status);
       return((int)PRO_TK_GENERAL_ERROR);
    }

/*----------------------------------------------------------------------------*\
	Retrieve Csys Data 
\*----------------------------------------------------------------------------*/
    status = ProSelectionModelitemGet(psels[0], &csys_feat);
    ERROR_CHECK("UserOutlineCompute","ProSelectionModelitemGet",status);
    status = ProGeomitemdataGet(&csys_feat,&geom_data);
    ERROR_CHECK("UserOutlineCompute","ProGeomitemdataGet",status);
    if(geom_data->obj_type != PRO_CSYS)
    {
       status = ProMessageDisplay(msgfile,
                         "USER Invalid Feature Selected as reference point");
	ERROR_CHECK("UserOutlineCompute","ProMessageDisplay",status);
       return((int)PRO_TK_GENERAL_ERROR);
    }

    p_csys = geom_data->data.p_csys_data;
    ProUtilVectorsToTransf(p_csys->x_vector, p_csys->y_vector, p_csys->z_vector,
                          p_csys->origin,transf);
    ProUtilMatrixInvert(transf, itranf);
    status = ProMdlCurrentGet(&solid);
    ERROR_CHECK("UserOutlineCompute","ProMdlCurrentGet",status);
    status = ProSolidOutlineCompute(solid, transf, excludes, 3, outline);
    ERROR_CHECK("UserOutlineCompute","ProSolidOutlineCompute",status);
    status = ProMessageDisplay(msgfile,"USER Outline points: (%0f %1f %2f) (%3f %4f %5f)",
                      &outline[0][0],&outline[0][1],&outline[0][2],
                      &outline[1][0],&outline[1][1],&outline[1][2]);
    ERROR_CHECK("UserOutlineCompute","ProMessageDisplay(Outline points)",status);
    return((int)status);
}