%% Example of an Atomic Box
% This example shows how to use a linked atomic box to reuse a graphical
% function across multiple charts and models. 

% Copyright 2018-2019 The MathWorks, Inc.

%%
% The function |GetTime| is defined in a chart in the library model
% |sf_timer_utils_lib|. The graphical function returns the simulation time
% in C charts where the equivalent MATLAB(R) function |getSimulationTime|
% is not available. 

model = 'sf_timer_utils_lib';
load_system(model)
set_param(model,'Lock','off');

% Delete all annotations at the top level of the model
ah = find_system(model, 'FindAll', 'on', 'type', 'annotation');
for i = 1:length(ah)
   ao = get_param(ah(i), 'Object');
   ao.delete;
end

% Export chart functions to turn off edit-time check
rt = sfroot;
ch = rt.find('-isa','Stateflow.Chart','Name','TimerUtilities');
ch.ExportChartFunctions = true;

open_system([model '/TimerUtilities']);

%%
bdclose(model)

%% 
% The model |sf_timer_function_calls| consists of two charts with a similar
% structure. Each chart contains a pair of states (|A| and |B|) and an
% atomic box (|Time|) linked from the library chart. The entry action in
% state |A| calls the function |GetTime| and stores its value as |t0|. The
% condition guarding the transition from |A| to |B| calls the function
% again and compares its output with the parameter |T|. 

model = 'sf_timer_function_calls';
load_system(model)
open_system([model '/Chart1'],'force')

%%
close_system(model)

%%
% The top model |sf_timer_modelref| reuses the timer function in multiple
% referenced blocks. Because there are no exported functions, you can use
% more than one instance of the referenced block in the top model.

model = 'sf_timer_function_modelref';
load_system(model)

% Delete all annotations at the top level of the model
ah = find_system(model, 'FindAll', 'on', 'type', 'annotation');
for i = 1:length(ah)
   ao = get_param(ah(i), 'Object');
   ao.delete;
end

open_system(model)