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


/*-------------------------- Pro/Toolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProCsys.h>
#include <PTApplsUnicodeUtils.h>
/*-------------------------- Application includes ---------------------------*/
#include <TestError.h>



/*--------------------------- Function Prototypes----------------------------*/
extern ProError UserUtilNameTypeGet();

/*================================================================*\
FUNCTION  : UserModelAdd
PURPOSE   : Adds a model to the current drawing-type object.
\*================================================================*/
ProError UserModelAdd ()
{
    int                 status;
    ProFamilyMdlName    wname;
    ProMdlType 	 	type;
    ProMdl		p_draw, p_model;
    ProCharLine 	astr;
    char		name[PRO_FAMILY_MDLNAME_SIZE];
    ProFileName WMSGFIL = {'m','s','g','_','u','g','d','w','g','.','t','x','t','\0'};
    ProError    err;

    /*------------------------------------------------------------*\
      Get the current model
    \*------------------------------------------------------------*/
    err = ProMdlCurrentGet(&p_draw);
    ERROR_CHECK("UserModelAdd","ProMdlCurrentGet",err);
    if (err != PRO_TK_NO_ERROR) return (err);

    /*------------------------------------------------------------*\
      Get the name and type of the model to add.
    \*------------------------------------------------------------*/
    status = UserUtilNameTypeGet(WMSGFIL,wname, &type );
    ERROR_CHECK("UserModelAdd","UserUtilNameTypeGet",status);
    if (status != PRO_TK_NO_ERROR) return (status);
 
    /*------------------------------------------------------------*\
      Retrieve the specified object into memory.
    \*------------------------------------------------------------*/
    err = ProMdlnameRetrieve(wname, (ProMdlfileType)type, (&p_model));
    ERROR_CHECK("UserModelAdd","ProMdlnameRetrieve",err);
    if (err != PRO_TK_NO_ERROR) return (err);

    /*------------------------------------------------------------*\
      Add it into the current drawing-type object
    \*------------------------------------------------------------*/
    err = ProDrawingSolidAdd ((ProDrawing)p_draw, (ProSolid)p_model);
    ERROR_CHECK("UserModelAdd","ProDrawingSolidAdd", err);
 
    /*------------------------------------------------------------*\
      Print a message in the message window
    \*------------------------------------------------------------*/
    if (PRO_TK_NO_ERROR == err)
     {
	ProWstringToString(name, wname);
	ProTKSprintf(astr,"Model %s has been added to drawing", name);
	err = ProMessageDisplay (WMSGFIL,"USER %0s", astr);
	ERROR_CHECK("UserModelAdd","ProMessageDisplay",err);
	return (PRO_TK_NO_ERROR);
     }
    else 
     {
	ProTKSprintf(astr,"Error %d in UserModelAdd", err);
        err = ProMessageDisplay (WMSGFIL,"USER %0s", astr);
        ERROR_CHECK("UserModelAdd","ProMessageDisplay",err);
	return (PRO_TK_GENERAL_ERROR);
     }
}