function newmodel = slvnvextract(sys, showModel)
%SLVNVEXTRACT - Extract subsystem or subchart contents into new model
%
%   NEWMODEL = SLVNVEXTRACT(SUBSYSTEM) extracts the contents of the
%   Atomic Subsystem block SUBSYSTEM and creates a new model. Returns the
%   name of the new model in NEWMODEL. SLVNVEXTRACT uses the subsystem
%   name for the model name, appending a numeral to the model name if that
%   model name already exists.
%
%   NEWMODEL = SLVNVEXTRACT(SUBCHART) extracts the contents of the
%   Atomic Subchart SUBCHART and creates a new model. SUBCHART should
%   specify the full path of the Atomic Subchart. SLVNVEXTRACT uses the
%   subchart name for the model name, appending a numeral to the model
%   name if that model name already exists.
%
%   NEWMODEL = SLVNVEXTRACT(EXPORTFCNMODEL) extracts a scheduler model for the 
%   Export Function model EXPORTFCNMODEL for the Simulink Design Verifier 
%   software to analyze.  Returns the name of the new model in NEWMODEL. 
%   SLDVEXTRACT uses the Export Function model name with a suffix
%   SldvScheduler for the extracted model name, appending a numeral to the model 
%   name if that model name already exists.
%
%   NEWMODEL = SLVNVEXTRACT(MODELBLOCK) extracts the contents of the
%   model block MODELBLOCK and creates a model for the Simulink
%   Design Verifier software to analyze. Returns the name of the new model
%   in NEWMODEL. SLDVEXTRACT uses the model block name for the model name,
%   appending a numeral to the model name if that model name already exists.
%
%   NEWMODEL = SLVNVEXTRACT(MODELMISSINGSLFUNCTIONDEF) extracts a stub model
%   for the model MODELMISSINGSLFUNCTIONDEF which has missing Simulink
%   Function definitions for the Simulink Design Verifier software to
%   analyze. Returns the name of the new model in NEWMODEL. SLDVEXTRACT
%   uses the input model name with a suffix SldvStub for the extracted
%   model name, appending a numeral to the model name if that model name
%   already exists.
%
%   NEWMODEL = SLVNVEXTRACT(SUBSYSTEM, SHOWMODEL)
%   NEWMODEL = SLVNVEXTRACT(SUBCHART, SHOWMODEL)
%   NEWMODEL = SLVNVEXTRACT(EXPORTFCNMODEL, SHOWMODEL)
%   NEWMODEL = SLVNVEXTRACT(MODELBLOCK, SHOWMODEL)
%   NEWMODEL = SLVNVEXTRACT(MODELMISSINGSLFUNCTIONDEF, SHOWMODEL) opens the extracted
%   model if you set SHOWMODEL to true. Extracted model will be only loaded
%   if SHOWMODEL is set to false.

%   Copyright 2010-2021 The MathWorks, Inc.


    narginchk(1, 2);
    
    if nargin<2
        showModel = true;
    end

    try
        % Determine the kind of extraction
        extractObj = sldvshareprivate('createMdlExtractor', mfilename, sys);
        % Perform extraction
        [status, modelH, err_msg] = extractObj.extract(sys, ...
            showModel);
    catch Mex
        Sldv.Token.get.release;
        baseEID = 'Slvnv:EXTRACT:Failed';
        baseMex = MException(baseEID, message(baseEID).getString);
        baseMex = baseMex.addCause(Mex);
        throw(baseMex);
    end
    
    if ~status 
        if isempty(err_msg)
            error(message('Slvnv:EXTRACT:Failed'));                
        elseif ~isstruct(err_msg)
            error('Slvnv:EXTRACT:Failed',err_msg);                
        else
            error('Slvnv:EXTRACT:Failed',err_msg.msg);                
        end
    else
    	newmodel = get_param(modelH,'Name');
    end
end

% LocalWords: NEWMODEL SHOWMODEL SLVNV EXTRACT EXPORTFCNMODEL MODELBLOCK
% LocalWords: MODELMISSINGSLFUNCTIONDEF
