/*
	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 <ProMechMaterialOrient.h>
#include <ProUtil.h>
#include <ProMessage.h>
#include <PTMechExamples.h>

/*=============================================================*\
  FUNCTION: PTMechExMatOrient 
  PURPOSE:  Add csys material orientation item 
\*=============================================================*/
  
ProError PTMechExMatOrient()
  
{
  ProName tk_matorient_wname;
  ProMechItem tk_constraint_item;
  ProMechMaterialOrientData orient_data;
  ProMechMaterialOrientCsys csys_data;
  ProMechStatus mech_status;
  int n_sels;
  ProSelection *sels;
  ProModelitem mech_item;
  ProMdl matorient_part;
  ProMechGeomref csys;
  ProMechMaterialOrientRotation rotation = {1.0, 2.0, 3.0};
  PTMechExGeomRef csys_ref; 

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

/*--------------------------------------------------------------------*\
  Select csys 
\*--------------------------------------------------------------------*/
  
  status = ProMessageDisplay(msgfile, "PTMechEx Select csys", msgbuf);
  PT_TEST_LOG_SUCC("ProMessageDisplay()");
  
  
  status =ProSelect ("csys",
                     1,
                     NULL,
                     NULL,
                     NULL,
                     NULL,
                     &sels,
                     &n_sels);
  PT_TEST_LOG_SUCC ("ProSelect()");
  
  status = ProSelectionModelitemGet (sels[0], &mech_item);
  PT_TEST_LOG_SUCC ("ProSelectionModelitemGet()");

/*--------------------------------------------------------------------*\
  Creating Surface Type Projected Closest-axis Mat. Orientation.
\*--------------------------------------------------------------------*/
  
  csys_ref.type    = PRO_MECH_COORD_SYSTEM;
  csys_ref.subtype = PRO_MECH_CSYS_CARTESIAN;
  csys_ref.id      = mech_item.id ;
  csys_ref.path    = NULL;

/*--------------------------------------------------------------------*\
  Create new mech item 
\*--------------------------------------------------------------------*/
  status = ProMechitemCreate ((ProSolid) matorient_part, PRO_SIMULATION_MATL_ORIENT,
                              &tk_constraint_item);
  PT_TEST_LOG_SUCC ("ProMechitemCreate");
  
  ProStringToWstring(tk_matorient_wname,"tk_created_mat_orient");
  
  status = ProMechitemNameSet ( &tk_constraint_item, tk_matorient_wname);
  PT_TEST_LOG_SUCC ("ProMechitemNameSet");
  
/*--------------------------------------------------------------------*\
  Set the Material orient Description 
\*--------------------------------------------------------------------*/
  status = ProMechmaterialorientDescriptionSet (&tk_constraint_item,
                                                L"Mat Orient - Srf Type - Proj Closest axis");
  PT_TEST_LOG_SUCC ("ProMechmaterialorientDescriptionSet");

/*--------------------------------------------------------------------*\
  Allocate new  Material oriention 
\*--------------------------------------------------------------------*/

  status = ProMechmaterialorientdataAlloc (&orient_data);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientdataAlloc");

/*--------------------------------------------------------------------*\
  Set the Material oriention data object type 
\*--------------------------------------------------------------------*/

  status = ProMechmaterialorientdataObjecttypeSet (orient_data,
                                                   PRO_MECH_MATLORI_SURFACE);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientdataObjecttypeSet");
  
/*--------------------------------------------------------------------*\
  Allocate csys data 
\*--------------------------------------------------------------------*/

  status = ProMechmaterialorientcsysAlloc (&csys_data);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientcsysAlloc");

/*--------------------------------------------------------------------*\
  Create references 
\*--------------------------------------------------------------------*/

  status = PTMechExGeomReferenceCreate (&csys_ref, &csys);
  PT_TEST_LOG_SUCC ("PTMechExConstraintCreate");

/*--------------------------------------------------------------------*\
  Poppulate csys data
\*--------------------------------------------------------------------*/

  status = ProMechmaterialorientcsysCsysSet (csys_data, csys);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientcsysCsysSet");
  
  status = ProMechmaterialorientcsysProjectiontypeSet (csys_data,
                                                       PRO_MECH_MATLORI_CSYS_PROJ_CLOSEST);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientcsysProjectiontypeSet");

/*--------------------------------------------------------------------*\  
  Poppulate Material orient data 
\*--------------------------------------------------------------------*/

  status = ProMechmaterialorientdataCsysdataSet (orient_data,
                                                 csys_data);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientdataCsysdataSet");
  
  status = ProMechmaterialorientdataSurfacerotationSet ( orient_data, rotation[2]);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientdataSurfacerotationSet");

/*--------------------------------------------------------------------*\
  Set the Material orient data to the new constraint 
\*--------------------------------------------------------------------*/

  status = ProMechmaterialorientDataSet (&tk_constraint_item, orient_data);
  PT_TEST_LOG_SUCC ("ProMechmaterialorientDataSet");

  status = ProMechitemUpdateComplete ( &tk_constraint_item);
  PT_TEST_LOG_SUCC ("ProMechitemUpdateComplete");

  status = ProMechitemStatusGet ( &tk_constraint_item, &mech_status);
  PT_TEST_LOG_SUCC ("ProMechitemStatusGet");

/*--------------------------------------------------------------------*\
  Print Message from the result
\*--------------------------------------------------------------------*/

  if (mech_status == PRO_MECH_ACTIVE)
    {
      status = ProMessageDisplay(msgfile, "PTMechEx Material orientation created successfully");
      PT_TEST_LOG_SUCC("ProMessageDisplay()");
    }
  else
    {
      status = ProMessageDisplay(msgfile, "PTMechEx Material orientation creation failed");
      PT_TEST_LOG_SUCC("ProMessageDisplay()");
    }
  
  return PRO_TK_NO_ERROR;
  
}