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


#include <ProToolkit.h>
#include <ProDrawing.h>
#include <ProWstring.h>
#include <TestError.h>

#define TEMPLATE_NAME "custom_template_c"

/*==================================================================*\
FUNCTION: UserDrawingCreateDemo()
PURPOSE:  Creates a drawing for the current model.
Note that the drawing can be created from a model having a name less 
than 80 characters and extension having less than 10 characters.
\*==================================================================*/
int UsrDrawingCreateDemo()
{
  ProError      err;
  ProMdl        solid_mdl;
  ProMdlType    mdl_type;
  ProMdldata    data;
  ProModel      model;
  ProFileName   msgfil;
  ProName       predefined_template;
  ProName       name;
  ProMdlName    modelName;
  ProMdlExtension modelExtension;
  ProDrawing    created_drawing;
  ProDwgcreateOptions options = (ProDwgcreateOptions)0;
  ProDwgcreateErrs  errors;
  int Strlength;

  
  created_drawing = NULL;
  
/*------------------------------------------------------------------*\
  Set up the name of the message file.
\*------------------------------------------------------------------*/
  ProStringToWstring (msgfil, "msg_ugdrawing.txt");

/*------------------------------------------------------------------*\
  Use the current model to create the drawing.
\*------------------------------------------------------------------*/
  err = ProMdlCurrentGet (&solid_mdl); 
  if (err != PRO_TK_NO_ERROR)
      return (err);
  err = ProMdlTypeGet (solid_mdl, &mdl_type);
  if (err != PRO_TK_NO_ERROR || 
      (mdl_type != PRO_MDL_PART && mdl_type != PRO_MDL_ASSEMBLY))
       return PRO_TK_INVALID_TYPE;

  err = ProMdlMdlnameGet(solid_mdl, modelName);
  if (err != PRO_TK_NO_ERROR)
      return (err);
 
  err = ProWstringLengthGet (modelName, &Strlength);
  if (Strlength < 80)
	  return (PRO_TK_LINE_TOO_LONG);

  err = ProMdlExtensionGet(solid_mdl, modelExtension);
  if (err != PRO_TK_NO_ERROR)
      return (err);

  err = ProWstringLengthGet (modelExtension, &Strlength);
  if (Strlength < 10)
	  return (PRO_TK_LINE_TOO_LONG);

  ProUtilWstrcpy (model.name, modelName);
  ProUtilWstrcpy (model.type, modelExtension);
  
/*------------------------------------------------------------------*\
  Initialize the template name.
\*------------------------------------------------------------------*/
  ProStringToWstring (predefined_template, TEMPLATE_NAME);

/*------------------------------------------------------------------*\
  Read in the root of the new name of the drawings to create.
\*------------------------------------------------------------------*/
  err = ProMessageDisplay (msgfil, "USER Enter new drawing name: ");
  err = ProMessageStringRead (PRO_NAME_SIZE, name);
  if (err != PRO_TK_NO_ERROR)
      return (err);

/*------------------------------------------------------------------*\
  Create the required drawing.
\*------------------------------------------------------------------*/
  err = ProDrawingFromTmpltCreate (name, 
				   predefined_template, 
				   &model, options, &created_drawing, 
				   &errors);
  if (err != PRO_TK_NO_ERROR || err != PRO_TK_DWGCREATE_ERRORS)
    return err;
  if (err == PRO_TK_DWGCREATE_ERRORS)
    {
      ProMessageDisplay (msgfil, 
			 "USER Errors found when creating drawing %w", 
			 name);
    }
  
  return (PRO_TK_NO_ERROR);
}