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


/*====================================================================*\
FILE    : PTMechExCopy.c
PURPOSE : Test Pro/TOOLKIT functions for Mechanica 
\*====================================================================*/

/*--------------------------------------------------------------------*\
  Pro/Toolkit includes -- include this first
\*--------------------------------------------------------------------*/
#include <ProMechLoad.h>
#include <ProUtil.h>
#include <ProMessage.h>
#include <ProMechValue.h>
#include <PTMechExamples.h>

/*================================================================*\
  FUNCTION: PTMechExLoadPostfilter 
  PURPOSE:  Selection filter to ensure that only pressure and force loads are selected
\*================================================================*/
static ProError PTMechExLoadPostfilter (ProSelection selection, ProAppData app_data)
{
	ProModelitem mech_item;
	ProMechLoadType load_type;

	status = ProSelectionModelitemGet (selection, (ProModelitem*)&mech_item);
    PT_TEST_LOG_SUCC ("ProSelectionModelitemGet()");

	if (mech_item.type != PRO_SIMULATION_LOAD)
		return (PRO_TK_CONTINUE);
   
    status =ProMechloadTypeGet (&mech_item,&load_type);
    PT_TEST_LOG_SUCC("=ProMechloadTypeGet");

    if (((load_type != PRO_MECH_LOAD_FORCE) && (load_type != PRO_MECH_LOAD_PRESSURE)) || (status != PRO_TK_NO_ERROR))
  		return (PRO_TK_CONTINUE);

	return (PRO_TK_NO_ERROR);
}

/*=============================================================*\
  FUNCTION: PTMechExMagChange  
  PURPOSE:  Increases the magnitude of force/pressure 
\*=============================================================*/

ProError PTMechExMagChange ()
{
  ProSelection* sels;
  ProSelFunctions sel_funcs;
  ProMechItem mech_item;
  int n_sels, i=0;
  ProMdl constraint_part;
  ProMechLoadType load_type;
  ProMechVectoredValue force_vect_value;
  ProMechValue force_value_mag;
  ProMechForceData force_data;
  ProMechStatus mech_status;
  ProMechPressureData press_data;
  ProMechValue press_mech_val;
  double mag;
  ProLine val,val_5times;
  char curr_mag1[PRO_LINE_SIZE];
  char curr_mag2[PRO_LINE_SIZE];

  status = ProMdlCurrentGet(&constraint_part);
  PT_TEST_LOG_SUCC ("ProMdlCurrentGet");

  sel_funcs.pre_filter = NULL;
  sel_funcs.post_filter = PTMechExLoadPostfilter;
  sel_funcs.post_selact = NULL;
  sel_funcs.app_data = NULL;

  while (1)
  {
	status = ProMessageDisplay(msgfile, "PTMechEx Select the load", msgbuf);
	PT_TEST_LOG_SUCC("ProMessageDisplay()");


    status =ProSelect ("sim_load",
		       1,
		       NULL, 
		       &sel_funcs,
		       NULL,
		       NULL,
		       &sels,
		       &n_sels);
    PT_TEST_LOG_SUCC ("ProSelect()");
    
    if( status != PRO_TK_NO_ERROR || n_sels <= 0 )
      break; 
    
    status = ProSelectionModelitemGet (sels[i], (ProModelitem*)&mech_item);
    PT_TEST_LOG_SUCC ("ProSelectionModelitemGet()");
    
    status =ProMechloadTypeGet (&mech_item,&load_type);
    PT_TEST_LOG_SUCC("=ProMechloadTypeGet");

/*--------------------------------------------------------------------*\
  Get the force data 
\*--------------------------------------------------------------------*/

    if (load_type == PRO_MECH_LOAD_FORCE)
      {

        status = ProMechloadForcedataGet (&mech_item, &force_data);
	PT_TEST_LOG_SUCC ("ProMechloadForcedataGet");

	status = ProMechforcedataForceGet (force_data,&force_vect_value);
	PT_TEST_LOG_SUCC("ProMechforcedataForceGet");

	status = ProMechvectoredvalueMagnitudeGet (force_vect_value,&force_value_mag);
	PT_TEST_LOG_SUCC("ProMechvectoredvalueMagnitudeGet");
/*--------------------------------------------------------------------*\
  Check if the  magnitude is required field 
\*--------------------------------------------------------------------*/

	if (status == PRO_TK_E_NOT_FOUND)
	  {
	    status = ProMessageDisplay(msgfile, "PTMechEx Magnitude is not used for this load.");
	    PT_TEST_LOG_SUCC("ProMessageDisplay()");
	    continue;
	  }

/*--------------------------------------------------------------------*\
  Get the magnitude value 
\*--------------------------------------------------------------------*/
	
	status = ProMechvalueValueGet (force_value_mag,val);
	PT_TEST_LOG_SUCC("ProMechvalueValueGet");


/*--------------------------------------------------------------------*\
  Increase the magnitude 
\*--------------------------------------------------------------------*/
	
	if ( status == PRO_TK_NO_ERROR)
	  {

            ProWstringToString(curr_mag1,val);
	    PTMechExMagValueIncrease(val,val_5times);

/*--------------------------------------------------------------------*\
  Set the magnitude value 
\*--------------------------------------------------------------------*/
	    
	    status = ProMechvalueValueSet (force_value_mag,val_5times);
	    PT_TEST_LOG_SUCC ("ProMechvalueValueSet");
	    
	    status = ProMechvectoredvalueMagnitudeSet (force_vect_value, force_value_mag);
	    PT_TEST_LOG_SUCC ("ProMechvectoredvalueMagnitudeSet");
	    
	    status = ProMechforcedataForceSet (force_data, force_vect_value);
	    PT_TEST_LOG_SUCC ("ProMechforcedataForceSet");
	    
	  }
/*--------------------------------------------------------------------*\   
  Set the force data 
\*--------------------------------------------------------------------*/

	status = ProMechloadForcedataSet (&mech_item,force_data);
	PT_TEST_LOG_SUCC("ProMechloadForcedataSet");

/*--------------------------------------------------------------------*\
 Confirm the changed value 
\*--------------------------------------------------------------------*/

        status = ProMechloadForcedataGet (&mech_item, &force_data);
        PT_TEST_LOG_SUCC ("ProMechloadForcedataGet");

        status = ProMechforcedataForceGet (force_data,&force_vect_value);
        PT_TEST_LOG_SUCC("ProMechforcedataForceGet");

        status = ProMechvectoredvalueMagnitudeGet (force_vect_value,&force_value_mag);
        PT_TEST_LOG_SUCC("ProMechvectoredvalueMagnitudeGet");

        status = ProMechvalueValueGet (force_value_mag,val);
        PT_TEST_LOG_SUCC("ProMechvalueValueGet");

        ProWstringToString(curr_mag2,val);
      }
    
    if (load_type == PRO_MECH_LOAD_PRESSURE)
      
/*--------------------------------------------------------------------*\
  Get the pressure data 
\*--------------------------------------------------------------------*/
      {
	status = ProMechloadPressuredataGet (&mech_item,&press_data);
	PT_TEST_LOG_SUCC("ProMechloadPressuredataGet");
	
	status = ProMechpressuredataValueGet (press_data,&press_mech_val);
	PT_TEST_LOG_SUCC("ProMechpressuredataValueGet");
	
/*--------------------------------------------------------------------*\
  Get the value 
\*--------------------------------------------------------------------*/

	status = ProMechvalueValueGet (press_mech_val,val);
	PT_TEST_LOG_SUCC("ProMechvalueValueGet");
	
/*--------------------------------------------------------------------*\
  Increase the magnitude
\*--------------------------------------------------------------------*/
	
	if ( status == PRO_TK_NO_ERROR)
	  
	 {

           ProWstringToString(curr_mag1,val);
	   PTMechExMagValueIncrease(val,val_5times);
	    
	   status = ProMechvalueValueSet (press_mech_val,val_5times);
	   PT_TEST_LOG_SUCC ("ProMechvalueValueSet");
	   
	   status = ProMechpressuredataValueSet (press_data,press_mech_val);
	   PT_TEST_LOG_SUCC("ProMechpressuredataValueSet");
	 }

/*--------------------------------------------------------------------*\
  Set the pressure data
\*--------------------------------------------------------------------*/
	
	status = ProMechloadPressuredataSet (&mech_item,press_data);
	PT_TEST_LOG_SUCC("ProMechloadPressuredataSet");

/*--------------------------------------------------------------------*\
 Confirm the changed value
\*--------------------------------------------------------------------*/

        status = ProMechloadPressuredataGet (&mech_item,&press_data);
        PT_TEST_LOG_SUCC("ProMechloadPressuredataGet");

        status = ProMechpressuredataValueGet (press_data,&press_mech_val);
        PT_TEST_LOG_SUCC("ProMechpressuredataValueGet");

        status = ProMechvalueValueGet (press_mech_val,val);
        PT_TEST_LOG_SUCC("ProMechvalueValueGet");

        ProWstringToString(curr_mag2,val);
      }
    
    status = ProMechitemUpdateComplete (&mech_item);
    PT_TEST_LOG_SUCC ("ProMechitemUpdateComplete");
    
    status = ProMechitemStatusGet (&mech_item, &mech_status);
    PT_TEST_LOG_SUCC ("ProMechitemStatusGet");
    
/*--------------------------------------------------------------------*\
  Print Message from the result
\*--------------------------------------------------------------------*/
    if (mech_status == PRO_MECH_ACTIVE)
      {
	status = ProMessageDisplay(msgfile, "PTMechEx Magnitude changed successfuly",curr_mag1,curr_mag2);
	PT_TEST_LOG_SUCC("ProMessageDisplay()");
      }
    else
      {
	status = ProMessageDisplay(msgfile, "PTMechEx Magnitude modification failed", msgbuf);
	PT_TEST_LOG_SUCC("ProMessageDisplay()");
      }
  
  }
  return PRO_TK_NO_ERROR;
}