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


#include <ProToolkit.h>
#include <ProParameter.h>
#include <ProSelection.h>
#include <ProUtil.h>

#include <TestError.h>
 
/*=====================================================================*\
Function : UserLabelFeature
Purpose  : Function labels a feature with a string parameter.
\*=====================================================================*/
int UserLabelFeature()
{
  ProModelitem  feature;
  ProSelection  *sel;
  ProParameter  param;
  ProParamvalue value;
  ProFileName   msgfil;
  ProName       name;
  ProLine       line;
  ProError      err;
  int           nsel;

/*---------------------------------------------------------------------*\
  Setup the name of the message file
\*---------------------------------------------------------------------*/
    ProStringToWstring(msgfil,"msg_ugparam.txt");

/*---------------------------------------------------------------------*\
   Select a feature to label. If nothing is selected exit function.
\*---------------------------------------------------------------------*/
    err = ProMessageDisplay(msgfil,"USER Select a feature to label");
    ERROR_CHECK("UserLabelFeature","ProMessageDisplay",err);

    err = ProSelect("feature",1, NULL,NULL,NULL,NULL, &sel, &nsel);
    ERROR_CHECK("UserLabelFeature","ProSelect",err);

    if(nsel <= 0)
        return(0);

/*---------------------------------------------------------------------*\
   Get the model item from selection.
\*---------------------------------------------------------------------*/
    err = ProSelectionModelitemGet(sel[0], &feature);
    ERROR_CHECK("UserLabelFeature","ProSelectionModelitemGet",err);

/*---------------------------------------------------------------------*\
   Get the name of the parameter.
\*---------------------------------------------------------------------*/
    err = ProMessageDisplay(msgfil,"USER %0s", "Enter the name of the parameter: ");
    err = ProMessageStringRead(PRO_NAME_SIZE, name);
  
/*---------------------------------------------------------------------*\
   Set parameter type and value.
\*---------------------------------------------------------------------*/
    value.type = PRO_PARAM_STRING;

    err = ProMessageDisplay(msgfil,"USER %0s", "Enter the parameter string line: ");
    err = ProMessageStringRead(PRO_LINE_SIZE, value.value.s_val);

    
/*---------------------------------------------------------------------*\
  If the parameter exists, set its new value. Otherwise, create it.
\*---------------------------------------------------------------------*/
    err = ProParameterInit(&feature, name, &param);
    ERROR_CHECK("UserLabelFeature","ProParameterInit", err);
   
    if( err == PRO_TK_E_NOT_FOUND)
    {
         err = ProParameterWithUnitsCreate(&feature, name, &value,NULL, &param);
         ERROR_CHECK("UserLabelFeature","ProParameterWithUnitsCreate",err);
    }
    else
    {
         err = ProParameterValueWithUnitsSet(&param, &value,NULL);
         ERROR_CHECK("UserLabelFeature","ProParameterValueWithUnitsSet",err);
    }

  return(PRO_TK_NO_ERROR);
}