function saveGlobalParamsToModel(this, cli)
%

% Copyright 2005-2017 The MathWorks, Inc.

if nargin < 2
cli = this.getCLI;
end
% Get the CLI attached to the hdlcc object. All params are sourced directly
% off of this except for few.
this.pushPropsToCLI;

mdlName = this.getModelName;
if ~isempty(mdlName)
if ~(strcmp(get_param(mdlName, 'LibraryType'), 'BlockLibrary') && ...
strcmp(get_param(mdlName, 'Lock'), 'on'))
setNonDefParams(mdlName, cli);
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function setNonDefParams(mdlName, cli)
nonDefParamNames = cli.getNonDefaultProps;
nonDefParamNames = sort(nonDefParamNames);

if length(nonDefParamNames) == 1 && strcmpi(nonDefParamNames{1}, 'HDLSubsystem')
mdlsnn = hdlget_param(mdlName, 'HDLSubsystem');
dlgsnn = cli.HDLSubsystem;
nonDefParams = {'HDLSubsystem', dlgsnn};
setParamsOnModel = true;

hd = get_param(mdlName, 'HDLParams');
if ~isempty(hd)
hdp = hd.getCurrentMdlPropNames;
if (length(hdp) == 1 && strcmpi(hdp{1}, 'HDLSubsystem')) && strcmpi(mdlsnn, dlgsnn)
nonDefParams = {};
setParamsOnModel = false;
end
end
else
nonDefParams = {};
oldP = get_param(mdlName, 'HDLParams');
% We only want to set params on the model when there is an actual
% change.
if isempty(oldP)
setParamsOnModel = true;
oldProps = {};
else
setParamsOnModel = false;
oldProps = oldP.getCurrentMdlProps;
% We cannot see difference in the objects, so force a write when
% objects are involved.
if any(strcmp(oldProps, 'FloatingPointTargetConfiguration')) || ...
any(strcmp(oldProps, 'HDLCodingStandard'))
setParamsOnModel = true;
end
if numel(oldProps)/2 ~= numel(nonDefParamNames)
% If the number of existing properties and the number of new
% properties are different, we need to write the new props to
% the model.
setParamsOnModel = true;
end
end
if ~isempty(nonDefParamNames)
for ii=1:length(nonDefParamNames)
pName = nonDefParamNames{ii};
pValue = cli.(pName);
idx = find(strcmp(oldProps, pName));
if isempty(idx)
% Param is not on model currently
setParamsOnModel = true;
else
idx = idx + 1;
oldVal = oldProps{idx};
if ischar(oldVal) || isstring(oldVal) || iscellstr(oldVal)
% String-like param, add to set if different
setParamsOnModel = setParamsOnModel || any(~strcmp(oldVal, pValue));
elseif isnumeric(oldVal)
% Numeric param, add to set if different
setParamsOnModel = setParamsOnModel || any(oldVal ~= pValue);
else
% Coding std and NFP objects will always get added,
% and dirty the model. Since they are object based, it
% is the identical object in pValue and objVal. Change
% one, both change. So there is no possibility for
% change detection.
setParamsOnModel = true;
end
end
nonDefParams{end+1} = pName; %#ok<AGROW>
nonDefParams{end+1} = pValue; %#ok<AGROW>
end
end
end

if setParamsOnModel && ~isempty(nonDefParams)
newP = slprops.hdlmdlprops(nonDefParams);
set_param(mdlName, 'HDLParams', newP);
end
end