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


/*---------------------- Pro/Toolkit Includes ------------------------*/
#include "ProToolkit.h"
#include "ProSection.h"
#include "ProSecdim.h"
#include "ProSecerror.h"
#include "Pro2dEntdef.h"
#include "ProSecdimType.h"
#include "ProMdl.h"
#include "UtilMath.h"
#include "ProMenu.h"
#include "ProUtil.h"

/*---------------------- Application Includes ------------------------*/
#include "TestError.h"

/*---------------------- Function Prototypes -------------------------*/
ProError UserSectionCreateExample();
ProError ProDemoSectCreate();

/*------------------------- Global Data -----------------------------*/
typedef char ProUtilCname[PRO_NAME_SIZE];


/*===========================================================*\
FUNCTION :  ProDemoSectCreate()
PURPOSE  :  Creates a section model whose shape is a
            rectangle with a semi-circular "bite" from 
            the left side.
\*===========================================================*/

ProError ProDemoSectCreate(
    double         width,       /* (In)  The rectangle width */
    double         height,      /* (In)  The rectangle height */
    double         bite_radius, /* (In)  The radius of the bite */
    double         bite_height, /* (In)  The height of the bite */
    ProUtilCname   name,        /* (In)  The sketch name */
    ProBoolean     alloc,       /* (In)  mem Alloc specifier */
    ProSection    *p_section)   /* (In/Out) The section handle for the
                                         sketch */
{
    ProError            status, solve_status;
    ProSection          section;
    ProName             wname;
    Pro2dArcdef         arc;
    Pro2dLinedef        line;
    int                 bottom_id, right_id, top_id, 
                        left_top_id,left_bottom_id, arc_id; 
    int                 width_dim, height_dim, bite_height_dim, 
                        bite_radius_dim;
    ProSectionPointType pnt_types[2];
    Pro2dPnt            point;
    int                 n_errors, e;
    ProWSecerror        errors;
    ProMsg              wmsg;
    char                msg[PRO_PATH_SIZE];
    int                 dims[2];
/*-----------------------------------------------------------*\
    Check that the dimensions are possible.
\*-----------------------------------------------------------*/
    if (width < EPSM6 || height < EPSM6)
        return (-1);
    if (bite_height <= bite_radius)
        return (-1);
    if (bite_height + bite_radius > height)
        return (-1);
    if (bite_radius >= width)
        return (-1);
/*-----------------------------------------------------------*\
    Allocate the handle for the 2-D section.
\*-----------------------------------------------------------*/
    if (alloc == PRO_B_TRUE)
      {
	status = ProSection2DAlloc (&section);
      }
    else
      section = *p_section;
/*-----------------------------------------------------------*\
    Set the name of the section.
\*-----------------------------------------------------------*/
    pro_str_to_wstr (wname, name);
    status = ProSectionNameSet (section, wname);
/*-----------------------------------------------------------*\
    Set the epsilon value.
\*-----------------------------------------------------------*/
    status = ProSectionEpsilonSet (section, 0.5);
/*-----------------------------------------------------------*\
    Add a straight-line entity for the bottom of the rectangle.
\*-----------------------------------------------------------*/
    line.type    = PRO_2D_LINE;
    line.end1[0] = 0.0;
    line.end1[1] = 0.0;
    line.end2[0] = width + 0.1;
    line.end2[1] = 0.0;
    status = ProSectionEntityAdd (section,
             (Pro2dEntdef*)&line, &bottom_id);
/*-----------------------------------------------------------*\
    ...right
\*-----------------------------------------------------------*/
    line.type    = PRO_2D_LINE;
    line.end1[0] = width + 0.1;
    line.end1[1] = 0.0;
    line.end2[0] = width;
    line.end2[1] = height;
    status = ProSectionEntityAdd (section, 
             (Pro2dEntdef*)&line, &right_id);
/*-----------------------------------------------------------*\
    ...top
\*-----------------------------------------------------------*/
    line.type    = PRO_2D_LINE;
    line.end1[0] = width;
    line.end1[1] = height;
    line.end2[0] = 0.0;
    line.end2[1] = height;
    status = ProSectionEntityAdd (section, (Pro2dEntdef*)&line,
             &top_id);
/*-----------------------------------------------------------*\
    ...left above the bite
\*-----------------------------------------------------------*/
    line.type    = PRO_2D_LINE;
    line.end1[0] = 0.0;
    line.end1[1] = bite_height - bite_radius;
    line.end2[0] = 0.0;
    line.end2[1] = 0.0;
    status = ProSectionEntityAdd (section, (Pro2dEntdef*)&line, 
                                  &left_top_id);
/*-----------------------------------------------------------*\
    ...left below the bite
\*-----------------------------------------------------------*/
    line.type    = PRO_2D_LINE;
    line.end1[0] = 0.0;
    line.end1[1] = height;
    line.end2[0] = 0.0;
    line.end2[1] = bite_height + bite_radius;
    status = ProSectionEntityAdd (section, (Pro2dEntdef*)&line,
                                  &left_bottom_id);
/*-----------------------------------------------------------*\
    Add an arc entity for the bite itself.
\*-----------------------------------------------------------*/
    arc.type        = PRO_2D_ARC;
    arc.center[0]   = 0.0;
    arc.center[1]   = bite_height;
    arc.start_angle = PI * 1.5;   /* 270 degrees counterclockwise
                                     from the X axis */
    arc.end_angle   = PI / 2.0;   /* 90 degrees counterclockwise
                                     from the X axis */
    arc.radius      = bite_radius;
    status = ProSectionEntityAdd (section, (Pro2dEntdef*)&arc,
             &arc_id);
/*-----------------------------------------------------------*\
    Add a dimension for the width (the length of the top
    entity).
\*-----------------------------------------------------------*/
    point[0] = width/2.0;
    point[1] = height + 10.0;
    pnt_types[0] = PRO_ENT_WHOLE;
    status = ProSecdimCreate (section, &top_id, pnt_types, 1, 
             PRO_TK_DIM_LINE, point, &width_dim);
/*-----------------------------------------------------------*\
    Add a dimension for the height (the length of the 
    right entity).
\*-----------------------------------------------------------*/
    point[0] = width + 1.0;
    point[1] = height/2.0;
    pnt_types[0] = PRO_ENT_WHOLE;
    status = ProSecdimCreate (section, &right_id, pnt_types, 1,
             PRO_TK_DIM_LINE, point, &height_dim);
/*-----------------------------------------------------------*\
    Add a dimension for the height of the bite.
\*-----------------------------------------------------------*/
    point[0] = -1.0;
    point[1] = bite_height/2.0;
    dims[0]  = bottom_id;
    dims[1]  = arc_id;
    pnt_types[0] = PRO_ENT_WHOLE;
    pnt_types[1] = PRO_ENT_CENTER;
    status = ProSecdimCreate (section, dims, pnt_types, 2,
             PRO_TK_DIM_LINE_POINT, point, &bite_height_dim);
/*-----------------------------------------------------------*\
    Add a dimension for the radius of the bite.
\*-----------------------------------------------------------*/
    point[0] = bite_radius + 1.0;
    point[1] = bite_height;
    pnt_types[0] = PRO_ENT_WHOLE;
    status = ProSecdimCreate (section, &arc_id, pnt_types, 1,
             PRO_TK_DIM_RAD, point, &bite_radius_dim);
/*-----------------------------------------------------------*\
    Claim memory for the error structure.
\*-----------------------------------------------------------*/
    status = ProSecerrorAlloc (&errors);
/*-----------------------------------------------------------*\
    Solve the section.
\*-----------------------------------------------------------*/
    solve_status = ProSectionSolve (section, &errors);
/*-----------------------------------------------------------*\
    If the solve failed, report error messages and exit.
\*-----------------------------------------------------------*/
    if (solve_status != PRO_TK_NO_ERROR)
    {
        status = ProSecerrorCount (&errors, &n_errors);
        for (e = 0; e < n_errors; e++)
        {
             status = ProSecerrorMsgGet (errors, e, wmsg);
             ProWstringToString (msg, wmsg);
             ProTKPrintf ("Error %d message : %s\n",e, msg);
        }
        return (-1);
    }
    status = ProSecerrorFree (&errors);
/*-----------------------------------------------------------*\
    Save the section.
\*-----------------------------------------------------------*/
    status = ProMdlSave (section);
/*-----------------------------------------------------------*\
    Return the section handle
\*-----------------------------------------------------------*/
    *p_section = section;
    return (0);
}


/*===========================================================*\
FUNCTION :  UserSectionCreateExample()
PURPOSE  :  Invokes the function to create the Section model 
\*===========================================================*/

ProError UserSectionCreateExample()
{
    ProError status;
    double width;
    double height;
    double bite_radius;
    double bite_height;
    char *name = "test"; 
    ProBoolean alloc;
    char filename[PRO_NAME_SIZE];
    ProSection section;
    int win_id;
    ProMacro wmacro;
    char *macro = "#MODE;#SKETCHER;#SEARCH/RETR;#IN SESSION;#TEST.SEC;";

    width = 200;
    height = 150;
    bite_radius = 20;
    bite_height = 30;
    alloc = PRO_B_TRUE;

    status = ProDemoSectCreate( width, height, bite_radius, 
                       bite_height, name, alloc, &section );
    ERROR_CHECK( "UserSectionCreateExample", "ProDemoSectCreate", status );

    ProStringToWstring( wmacro, macro );
    ProMacroLoad(wmacro);

    return ( PRO_TK_NO_ERROR );
}