
function geom_slice_model_gui()
%
% geom_slice_model_gui()  -  a wrapper function for geom_slice_model
%
% Exports fea from current FEATool session, prompts for an axis-orthogonal
% slice plane selection ('XY', 'XZ', or 'YZ'), an invariant axis coordinate 
% variable value, and other argument(s) from a popup window. After checking
% the validity of the user input, it calls "geom_slice_model" which slices 
% the fea.geom geometry along the specified plane.  If a normal status is 
% returned, it copies the returned GEO to the MATLAB base workspace with
% the name fea_geom_sliced_SUFFIX, wher SUFFIX indicates the slice plane
% and invariant axis coordinate, val.(e.g.,'YZ' plane at x=1.2 --> YZ_1_2)
% 
%     orthoplane:	'XY', 'YZ', or 'XZ' to denote orientation of the 
%                       slice plane
%     val:          The value of the invariant coordinate in the plane
%                     (e.g., for 'XY', z is invariant) e.g., 0.5
%     do_plot:      If true, the resulting geometry model is plotted before
%                       and after slice processing.
%
%     See also:     geom_slice_model   geom_bounds_and_size

% A yet unimplemented option is to use fea from an already exported FEATool
% session. 

% This flag could become an input option, but for now is set true.
do_export = true;

% If fea does not exist in the base workspace, then export it from 
% current FEATool session. (possible future option)

if( do_export == true )
    fprintf("Info: geom_slice_model_gui:, fea "+ ...
        "exported from current FEATool session,\n      "+ ...
        "fea.geom used as Model to be sliced.\n");
	h_menu_item = findall( 0, 'Label', 'Export FEA Struct To MATLAB' );
	cbf = get( h_menu_item, 'Callback' );
	cbf{1}( h_menu_item, [], cbf{2:end} );
end

% After fea is assured to exists in base workspace, retrieve it into 
% local_fea.
local_fea = evalin('base','fea');

% In order to process the do_plot option, set it true initially. It is
% reversed later on depending on user input.
do_plot   = true;

% Get input from user as to which cut plane and what value and whether or
% not to display plots.

font_size = '12';
prompts = {'\fontsize{12}Slice plane ( XY | XZ | YZ )', ...
           '\fontsize{12}Invariant coordinate value (number)', ...
           '\fontsize{12}Display plot ( true | false )'};
dialog_title = 'Slice Plane Parameters';
input_window_dimensions = [1 50];
default_input = {'XY','0.0', 'true'};
options.Interpreter = 'tex';

slice_data_validated = false;

% make multiple inqiries until valid data (or cancel) is obtained.
while (~slice_data_validated)
    
    slice_parameters = inputdlg( ...
                prompts, ...
                dialog_title, ...
                input_window_dimensions, ...
                default_input, ...
                options);
            
    % Exit if 'Cancel'
            
    if (isempty(slice_parameters))
         info_dlg = warndlg( ...
            'geom_slice_model_gui: Processing canceled by user.', ...
            'Input Canceled');
        waitfor(info_dlg);
        disp ("geom_slice_model_gui: "+ ...
            "Input dialog canceled, Processing canceled by user.");
        stat = -1;
        return;
    end

    % disp ("Slice parameters input:"+slice_parameters);
    disp ("Slice/Cut plane input parameters:");
    disp ("   Slice Plane : "+slice_parameters{1});
    disp ("   Slice Loc'n : "+slice_parameters{2});
    disp ("   Display Plot: "+slice_parameters{3});

    orthoplane  = slice_parameters{1};
    val         = str2double(slice_parameters{2});
    val_str     = strrep(slice_parameters{2},'.','_');
    do_plot_str = slice_parameters{3};

    % check input values for validity

    % begin by hoping for the best, set data validated = true
    slice_data_validated = true;

    % Now check each piece and set invalid on condition
    
    if isnan(val)
        slice_data_validated = false;
    end

    if (~isequal (orthoplane, 'XY')) & ...
       (~isequal (orthoplane, 'YZ')) & ...
       (~isequal (orthoplane, 'XZ'))

        slice_data_validated = false; 
    end

    if (~isequal(do_plot_str, 'true') & ...
        ~isequal(do_plot_str, 'false'))                                                         

        slice_data_validated = false; 
    end

    if (~slice_data_validated)

           err_msg = sprintf( ...
               "geom_slice_model_gui: INVALID SLICE PARAMETER(s),\n"+ ...
               "   Cut plane:         %s\n"+ ...
               "   Plane location:  %s\n"+ ...
               "   Display plots:     %s\nTry again...", ...
               slice_parameters{1}, ...
               slice_parameters{2}, ...
               slice_parameters{3});
           fprintf (err_msg);
           err_dlg = errordlg(err_msg, 'Invalid Parameter(s)');
           waitfor(err_dlg);
           default_input = slice_parameters; %{'XY','0.0', 'true'};

    else
        %  do_plot_str is either 'true' or 'false' so set the logical
        %  do_plot.
        
        % This test actually implements 'true' or not 'true' :-)
        if isequal(do_plot_str, 'true')
            do_plot = true;
        else
            do_plot = false;
        end
    end
% end of user input while loop
end

% Call geom model slice function, passing in the fea geom struct and
% options.
[GEO, stat] = geom_slice_model (local_fea.geom, orthoplane, val, do_plot);

% Set output file name to indicate the slice plane and location.
output_file_name = sprintf('fea_geom_sliced_%s_%s', orthoplane, val_str);

if stat == 0
    % Create or overwrite workspace variable with sliced model result
    assignin ('base', output_file_name, GEO);
                                                                                                                                                                 
    disp ("Sliced geom was copied to Workspace: '"+output_file_name+"'");
    disp ("   To import into FEATool:");
    disp ("   Select Geometry-->Import Geometry-->From MATLAB Workspace...");
    success_msg = sprintf ( ...
        "Sliced geom file:\n  '%s' %s.\n\n%s\n%s", ...
        output_file_name, ...
        "was copied to MATLAB Workspace", ...
        "To import into FEATool:", ...
        "  Select Geometry-->Import Geometry-->From MATLAB Workspace...");
    uiwait(msgbox(success_msg, ...
        'Model slice completed successfully','modal'));
else
    disp ("geom_slice_model returned non-zero status: "+stat);
    disp ("   Workspace file '"+output_file_name+"' is unchanged");
end
% end function geom_slice_model_guiF
end
