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


/*------------------------------------------------------------------*\
Pro/Toolkit includes
\*------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProObjects.h>
#include <ProFeature.h>
#include <ProDimension.h>
#include <PTApplsUnicodeUtils.h>
/*------------------------------------------------------------------*\
Application includes 
\*------------------------------------------------------------------*/
#include <TestError.h>

/*==================================================================*\
FUNCTION : UserDimensionChange()
PURPOSE  : Change a selected model dimension.
\*==================================================================*/
int UserDimensionChange()
{
    ProMdl model ;               /* current model */
    ProSelection *p_sel_feat ;   /* selected feature */
    ProSelection *p_sel_dim ;    /* selected dimension */
    ProModelitem dim_mdlitem ;   /* dimension Modelitem */
    int n_sel_feat ;             /* no. of selected features */
    int n_sel_dim ;              /* no. of selected dims */
    ProFileName msg_file ;       /* message file */
    int err ;                    /* return status */
    double value;                /* dimension value */
    double newdimval ;           /* user-entered dim value */
    ProCharLine str1 ;           /* message string */

    ProStringToWstring(msg_file, "msg_ugfund.txt") ;

/*------------------------------------------------------------------*\
    Check for a model.
\*------------------------------------------------------------------*/
    err = ProMdlCurrentGet(&model);
    ERROR_CHECK("UserDimensionChange()", "ProMdlCurrentGet()", err); 
    if (err != PRO_TK_NO_ERROR)
    {
       ProMessageDisplay(msg_file, "USER %0s F", 
           "Error getting current model."); 
       return(err) ; 
    }

/*------------------------------------------------------------------*\
    Get a feature to modify.
\*------------------------------------------------------------------*/
    ProMessageDisplay(msg_file, "USER %0s F", "Select a feature."); 
           
    err = ProSelect("feature", 1, NULL, NULL, NULL, NULL, 
        &p_sel_feat, &n_sel_feat) ; 
    ERROR_CHECK("UserDimensionChange()", "ProSelect()", err); 
    if ( err != PRO_TK_NO_ERROR )
    {
        ProMessageDisplay(msg_file, "USER %0s F", 
            "Error or abort during selection.") ; 
        return(err) ; 
    }

    ProSelectionUnhighlight (p_sel_feat [0]);

/*------------------------------------------------------------------*\
    Show the dimensions on the feature.
\*------------------------------------------------------------------*/
    err = ProFeatureParamsDisplay (p_sel_feat [0], PRO_DIM_PARAM);
    ERROR_CHECK ("UserDimensionChange()", "ProFeatureParamsDisplay()", err); 
 
/*------------------------------------------------------------------*\
    Get the dimension to be changed.
\*------------------------------------------------------------------*/
    ProMessageDisplay(msg_file, "USER %0s F", "Select a dimension."); 

    err = ProSelect("dimension", 1, NULL, NULL, NULL, NULL, 
        &p_sel_dim, &n_sel_dim) ; 
    ERROR_CHECK("UserDimensionChange()", "ProSelect()", err); 
    if ( err != PRO_TK_NO_ERROR )
    {
        ProMessageDisplay(msg_file, "USER %0s F", 
            "Error or abort during selection.") ; 
        return(err) ; 
    }

    err = ProSelectionModelitemGet(p_sel_dim[0], &dim_mdlitem); 
    ERROR_CHECK("UserDimensionChange()", "ProSelectionModelitemGet()", 
        err); 

    err = ProDimensionValueGet(&dim_mdlitem,
			       &value ); 
    ERROR_CHECK("UserDimensionChange()", "prodim_get_dimension()", err); 

/*------------------------------------------------------------------*\
    Change the dimension.
\*------------------------------------------------------------------*/
    ProTKSprintf(str1, "Dimension value: %lf ... Enter new value: ", 
        value) ; 
    ProMessageDisplay(msg_file, "USER %0s F", str1) ; 
    ProMessageDoubleRead(NULL, &newdimval) ;    

    err = ProDimensionValueSet ( &dim_mdlitem, newdimval); 
    ERROR_CHECK("UserDimensionChange()", "ProDimensionValueSet()", err); 

    err = ProDimensionDisplayUpdate ( &dim_mdlitem );

    ProMessageDisplay(msg_file, "USER %0s F", 
        "Dimension changed. Regenerate model to see changes."); 

    return(PRO_TK_NO_ERROR) ; 
}