/*****************************************************************************\
FILE    : UgNoteCreate.c
PURPOSE : Pro/TOOLKIT User Guide Example - Create a drawing note
HISTORY..
DATE      BUILD   AUTHOR    MODIFICATIONS
04-Dec-97 H-02-02 Philippe  $$1    Created
\*****************************************************************************/
/*-------------------------- Pro/Develop includes ---------------------------*/
#include <prodevelop.h>
#include <select3d.h>

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

/*-------------------------- Application includes ---------------------------*/
#include <TestError.h>



/*---------------------------Function Prototypes-----------------------------*/
ProError UserPickPosition ();
Prodtl_note * UserFreeNoteSetup();

/*****************************************************************************\
Function : UserNoteCreate() 
Purpose :  Create a drawing note
\*****************************************************************************/
ProError UserNoteCreate()
{
    int			status;
    int			note_id;
    ProMdl		p_draw;
    Prodtl_note 	*p_note;
    ProCharLine     	text;
    ProLine     	w_text;
    ProPoint3d		pos;
    Select3d    	*leaders;
    int			n_leaders;
    ProFileName WMSGFIL = {'m','s','g','_','u','g','d','w','g','.','t','x','t','\0'};

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

    /*----------------------------------------------------------*\
    Ask the user for the note position.
    \*----------------------------------------------------------*/
    status = UserPickPosition(WMSGFIL,"Pick the position of the new note.", pos);
    ERROR_CHECK("UserNoteCreate","UserPickPosition",status);
    if (status != PRO_TK_NO_ERROR) return(status);
    
    /*----------------------------------------------------------*\
    Ask the user for a line of note text.
    \*----------------------------------------------------------*/
    status = ProMessageDisplay(WMSGFIL, "USER %0s", 
				"Enter note text [QUIT] : ");
    ERROR_CHECK("UserNoteCreate","ProMessageDisplay",status);

    status = ProMessageStringRead( PRO_LINE_SIZE, w_text);
    ERROR_CHECK("UserNoteCreate","ProMessageStringRead",status);
    if (status != PRO_TK_NO_ERROR) return(status);

    ProWstringToString (text, w_text);

    /*----------------------------------------------------------*\
    Ask the user to pick the leader points (edges of models in
    drawing views).
    \*----------------------------------------------------------*/
    status = ProMessageDisplay(WMSGFIL, "USER %0s", "Select leader points");
    ERROR_CHECK("UserNoteCreate","ProMessageDisplay",status);

    n_leaders = pro_select ("edge", -1, &leaders, NULL, NULL);

    /*----------------------------------------------------------*\
    Set up the Prodtl_note structure.
    \*----------------------------------------------------------*/
    p_note = UserFreeNoteSetup ( p_draw, pos, text,
             			n_leaders, leaders );
    ERROR_CHECK("UserNoteCreate","UserFreeNoteSetup", status);
    if (status != PRO_TK_NO_ERROR) return(status);
     
    /*----------------------------------------------------------*\
    Add the note to the drawing.
    \*----------------------------------------------------------*/
    note_id = prodtl_create ((Prohandle) p_draw, NULL,
                             (Prodtl_item*) p_note);
    ERROR_CHECK("UserNoteCreate","prodtl_create",(note_id<0));
    if (note_id < 0) return(PRO_TK_GENERAL_ERROR);

    status = prodtl_display ((Prohandle) p_draw, PRODTL_NOTE,
                                note_id, PRODTL_SHOW);
    ERROR_CHECK("UserNoteCreate","prodtl_display",(status == 0));
    if (status <=0) return(PRO_TK_GENERAL_ERROR);

    return(PRO_TK_NO_ERROR);
}

/*===============================================================*\
    Function that gives the default text height for the
    specified drawing
\*===============================================================*/
ProError UserTextDefaultHeight (ProMdl p_draw, double * height)
{
    int 	status;
    ProName   	w_size;
    double    	matrix[4][4];

    /*----------------------------------------------------------*\
    Get the default text height, in drawing coordinates. 
    \*----------------------------------------------------------*/
    status = pro_get_drawing_text_height ((Prohandle) p_draw, height, NULL);
    ERROR_CHECK("UserTextDefaultHeight","pro_get_drawing_text_height",
		 (!status));
    if (!status) return (PRO_TK_GENERAL_ERROR);

    status = prodb_drawing_sheet_info ((Prohandle) p_draw, w_size, matrix);
    ERROR_CHECK("UserTextDefaultHeight","prodb_drawing_sheet_info", (!status));
    if (!status) return (PRO_TK_GENERAL_ERROR);

   *height /= matrix[0][0]; /* for unit conversion */
    return (PRO_TK_NO_ERROR);

}

/*===============================================================*\
    Function that sets up the C structure for a free note with
    parametric leaders
\*===============================================================*/
Prodtl_note * UserFreeNoteSetup(
	ProMdl		p_draw,
	double        	pos[3],
	char          	text[],
	int           	n_leaders,
	Select3d     	*leaders)
{
    int 	status;
    int         note_id, l;
    Prodtl_note *p_note;

    /*----------------------------------------------------------*\
    Claim and initialize the note record.
    \*----------------------------------------------------------*/
    p_note = (Prodtl_note*) calloc (1, sizeof (Prodtl_note));
    p_note->type = PRODTL_NOTE;
    p_note->id   = -1;

    /*----------------------------------------------------------*\
    Add the single text line record.
    \*----------------------------------------------------------*/
    p_note->n_lines = 1;
    p_note->text_lines = (Prodtl_text_line*) calloc (1,
            sizeof(Prodtl_text_line));

    /*----------------------------------------------------------*\
    Add a single text record to the text line.
    \*----------------------------------------------------------*/
    p_note->text_lines[0].n_texts = 1;
    p_note->text_lines[0].texts = (Prodtl_text*) calloc (1,
            sizeof(Prodtl_text));

    /*----------------------------------------------------------*\
    Set up the text with default properties.
    \*----------------------------------------------------------*/
    status = UserTextDefaultHeight(p_draw,
			 &(p_note->text_lines[0].texts[0].height));
    ERROR_CHECK("UserFreeNoteSetup","UserTextDefaultHeight",status);
    p_note->text_lines[0].texts[0].width_factor = 0.8;
    p_note->text_lines[0].texts[0].slant_angle = 0.0;
    p_note->text_lines[0].texts[0].thickness = 0.0;
    pro_str_to_wstr (p_note->text_lines[0].texts[0].font, "font");
    p_note->text_lines[0].texts[0].attributes = 0;
    pro_str_to_wstr (p_note->text_lines[0].texts[0].string, text);
    p_note->attributes = 0;

    /*----------------------------------------------------------*\
    Set the note to be the default color for text.
    \*----------------------------------------------------------*/
    p_note->color.type = PTC_COLOR_INDEX;
    p_note->color.color = LETTER_COLOR;

    /*----------------------------------------------------------*\
    Set the attachment to be FREE at the specified screen
    coordinates.
    \*----------------------------------------------------------*/
    p_note->leader.text_attach.type = PRODTL_ATT_TYPE_FREE;
    p_note->leader.text_attach.att_geom.select_pnt[0] = pos[0];
    p_note->leader.text_attach.att_geom.select_pnt[1] = pos[1];
    p_note->leader.text_attach.att_geom.select_pnt[2] = pos[2];

    /*----------------------------------------------------------*\
    Copy the selected leader points as PARAMETRIC leaders.
    \*----------------------------------------------------------*/
    p_note->leader.n_leaders = n_leaders;
    p_note->leader.leaders = (Prodtl_attach*) calloc (n_leaders,
          sizeof(Prodtl_attach));
    for (l = 0; l < n_leaders; l++)
    {
         p_note->leader.leaders[l].type =
                 PRODTL_ATT_TYPE_PARAMETRIC;
         memcpy (&p_note->leader.leaders[l].att_geom,
                &leaders[l], sizeof(Select3d));
    }
    return (p_note);
}
