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

#include <ProToolkit.h>
#include <ProDtlsymdef.h>

int UsrSymdefLineAdd();
int UsrSymdefNoteAdd();
 
/*====================================================================*\ 
FUNCTION : UsrBoxSymdefCreate()
 PURPOSE  : To create a symbol definition with the specified name,
             containing a box and a note with the specified text 
\*====================================================================*/ 
int UsrBoxSymdefCreate(
     ProDrawing drawing,
     ProCharName name,
     ProCharLine text)
 {
     ProDtlsymdefdata sdata;
     ProPath path;
     ProVector origin, e1, e2;
     ProDtlsymdefattach attach;
     ProDtlsymdef symdef;
     double defheight;
     ProName w_size;
     ProMatrix matrix;
     int 	cur_sheet;
     ProError err;

/*--------------------------------------------------------------------*\ 
    Allocate symbol definition description data 
\*--------------------------------------------------------------------*/     
     ProDtlsymdefdataAlloc(drawing, &sdata);

/*--------------------------------------------------------------------*\ 
    Set the name of the symbol 
\*--------------------------------------------------------------------*/     
     ProStringToWstring(path, name);
     ProDtlsymdefdataPathSet(sdata, path);

/*--------------------------------------------------------------------*\ 
    Set the height type 
\*--------------------------------------------------------------------*/     
     ProDtlsymdefdataHeighttypeSet(sdata, PRODTLSYMDEFHGHTTYPE_FIXED);

/*--------------------------------------------------------------------*\ 
    Set a FREE attachment at the origin of the symbol 
\*--------------------------------------------------------------------*/     
     memset(origin, '\0', sizeof(ProVector));
     ProDtlsymdefattachAlloc(PROSYMDEFATTACHTYPE_FREE, 0, 0.0,
                 origin, &attach);

     ProDtlsymdefdataAttachAdd(sdata, attach);
     ProDtlsymdefattachFree(attach);

/*--------------------------------------------------------------------*\ 
    Create the empty symbol, and free the description 
\*--------------------------------------------------------------------*/     
     if(ProDtlsymdefCreate(drawing, sdata, &symdef) != PRO_TK_NO_ERROR)
         return(0);

     ProDtlsymdefdataFree(sdata);

/*--------------------------------------------------------------------*\ 
    Calculate the default text height for the drawing 
\*--------------------------------------------------------------------*/     
     err = ProSolidDefaulttextheightGet ((ProSolid)drawing, &defheight);
     err = ProDrawingCurrentSheetGet(drawing, &cur_sheet);
     err = ProDrawingSheetTrfGet(drawing, cur_sheet, w_size, matrix);
     defheight /= matrix[0][0];

/*--------------------------------------------------------------------*\ 
    Create four lines to form a box, twice the default text height,
     around the origin 
\*--------------------------------------------------------------------*/ 
     e1[0] = -defheight;
     e1[1] = -defheight;
     e1[2] =   0.0;
     e2[0] =  defheight;
     e2[1] = -defheight;
     e2[2] =   0.0;

     UsrSymdefLineAdd(&symdef, e1, e2, PRO_COLOR_ERROR);
     memcpy(e1, e2, sizeof(ProVector));
     e2[1]=defheight;
 
     UsrSymdefLineAdd(&symdef, e1, e2, PRO_COLOR_ERROR);
     memcpy(e1, e2, sizeof(ProVector));
     e2[0]=-defheight;

     UsrSymdefLineAdd(&symdef, e1, e2, PRO_COLOR_ERROR);
     memcpy(e1, e2, sizeof(ProVector));
     e2[1]=-defheight;

     UsrSymdefLineAdd(&symdef, e1, e2, PRO_COLOR_ERROR);

/*--------------------------------------------------------------------*\ 
    Add a note with the specified text at the origin 
\*--------------------------------------------------------------------*/ 
     origin[1] -=  defheight / 2.0;
     UsrSymdefNoteAdd(&symdef, text, origin);
     
     return(1);
 }

/*====================================================================*\ 
FUNCTION : UsrSymdefNoteAdd() PURPOSE  : To add a note with the specified
 text and location to a symbol definition. 
\*====================================================================*/ 
int UsrSymdefNoteAdd (
     ProDtlsymdef *symdef,
     ProCharLine ntext,
     ProVector pos)
 {
     ProDtlnotetext text;
     ProName font;
     ProDtlnoteline line;
     ProModelitem modelitem;
     ProName wname;
     ProCharName name;
     ProLine wstr;
     ProCharLine str;
     ProDtlnotedata ndata;
     ProView view;
     ProDtlattach attach, leader;
     ProDtlnote note;
     ProTextStyle style1,style2;
     ProColor color;

/*--------------------------------------------------------------------*\ 
    Allocate a text item, set value and style.
\*--------------------------------------------------------------------*/     
     ProDtlnotetextAlloc(&text);
     style1 = UserCreateTextStyle(L"cgomg", -1.0, 0.8, 0.0, 0.0);
     ProStringToWstring(wstr, ntext);
     ProDtlnotetextStringSet(text, wstr);
     ProDtlnotetextStyleSet(text, style1);
/*--------------------------------------------------------------------*\ 
    Allocate a new text line, and add the text item to it 
\*--------------------------------------------------------------------*/     
     ProDtlnotelineAlloc(&line);
     ProDtlnotelineTextAdd(line, text);

/*--------------------------------------------------------------------*\ 
    Allocate a note description, and add the line to it. Apply style to whole note.
\*--------------------------------------------------------------------*/     
     ProDtlnotedataAlloc(symdef->owner, &ndata);
     ProDtlnotedataLineAdd(ndata, line);
     ProTextStyleAlloc(&style2);
     color.method = PRO_COLOR_METHOD_TYPE;
     color.value.type = PRO_COLOR_HIGHLITE;
     ProTextStyleColorSetWithDef(style2, &color);
     ProTextStyleJustificationSet(style2, PRO_TEXT_HRZJUST_CENTER);
     ProTextStyleVertJustificationSet(style2, PRO_VERTJUST_MIDDLE);
     ProDtlnotedataTextStyleSet(ndata, style2);

/*--------------------------------------------------------------------*\ 
    Set the attachment of the note itself 
\*--------------------------------------------------------------------*/     
     ProDtlattachAlloc(PRO_DTLATTACHTYPE_FREE, NULL, pos, NULL, &attach);
     ProDtlnotedataAttachmentSet(ndata, attach);

/*--------------------------------------------------------------------*\ 
    Create the note 
\*--------------------------------------------------------------------*/     
     ProDtlnoteCreate(symdef->owner, symdef, ndata, &note);

/*--------------------------------------------------------------------*\ 
    Free the memory for the note description 
\*--------------------------------------------------------------------*/     
     ProDtlnotedataFree(ndata);
     ProTextStyleFree(&style1);
     ProTextStyleFree(&style2);
     return(1);
 }

/*====================================================================*\ 
FUNCTION : UsrSymdefLineAdd() 
PURPOSE  : Utility to add a line entity to a symbol definition 
\*====================================================================*/ 
int UsrSymdefLineAdd(
     ProDtlsymdef *symdef,
     ProVector start,
     ProVector end,
     ProColortype color)
 {
     ProDtlentity entity;
     ProDtlentitydata edata;
     ProCurvedata *curve;
     ProColor entity_color;

/*--------------------------------------------------------------------*\ 
    Allocate data for the draft entity 
\*--------------------------------------------------------------------*/     
     ProDtlentitydataAlloc(symdef->owner, &edata);

/*--------------------------------------------------------------------*\ 
    Allocate and initialize a curve description 
\*--------------------------------------------------------------------*/     
     ProCurvedataAlloc(&curve);
     ProLinedataInit(start, end, curve);

/*--------------------------------------------------------------------*\ 
    Use the curve description to set the entity data curve 
\*--------------------------------------------------------------------*/     
     ProDtlentitydataCurveSet(edata, curve);

/*--------------------------------------------------------------------*\ 
    Set the color to the specified Pro/ENGINEER predefined color 
\*--------------------------------------------------------------------*/     
     entity_color.method = PRO_COLOR_METHOD_TYPE;     
     entity_color.value.type = color;
     ProDtlentitydataColorSet(edata, &entity_color);

/*--------------------------------------------------------------------*\ 
    Create the entity 
\*--------------------------------------------------------------------*/     
     ProDtlentityCreate(symdef->owner, symdef, edata, &entity);

/*--------------------------------------------------------------------*\ 
    Release the entity data memory 
\*--------------------------------------------------------------------*/     
     ProDtlentitydataFree(edata);

     return(1);
 }

int UsrBoxSymdefCreateWrapper ()
{
  ProError err;
  ProName input_name;
  ProCharName name;
  ProLine input_line;
  ProCharLine line;
  ProDrawing drawing;
  ProMdlType mdl_type;
  ProFileName msgfil;

  ProStringToWstring (msgfil, "msg_ugdrawing.txt");

  err = ProMdlCurrentGet ((ProMdl*)&drawing);
  if (err != PRO_TK_NO_ERROR)
    return (err);
  err = ProMdlTypeGet ((ProMdl)drawing, &mdl_type);
  if (mdl_type != PRO_MDL_DRAWING)
    return (PRO_TK_INVALID_TYPE);

  ProMessageDisplay (msgfil, "USER Enter symbol definition name");
  err = ProMessageStringRead (PRO_NAME_SIZE, input_name);
  if (err != PRO_TK_NO_ERROR)
    return (err);
  
  ProWstringToString (name, input_name);
  
  ProMessageDisplay (msgfil, "USER Enter symbol definition text line");
  err = ProMessageStringRead (PRO_LINE_SIZE, input_line);
  if (err != PRO_TK_NO_ERROR)
    return (err);

  ProWstringToString (line, input_line);
  
  return (UsrBoxSymdefCreate (drawing, name, line));
}