www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@modeldev/mv2sl.m

    function [sys_out, ok, msg] = mv2sl(md, Xmodels, file, DO_PEV,DO_CONSTRAINTS)
%MVSL build a SIMULINK version of model
%
%  [SYS, OK, MSG] = MV2SL(MDEV, MODELS, FILE, PEV)

%  Copyright 2000-2016 The MathWorks, Inc. and Ford Global Technologies, Inc.


ok = true;
msg = '';
sys_out = [];
if nargin<5
    DO_CONSTRAINTS = true;
end
% test each model in the list to see if it can be exported to Simulink
for i = 1:length(Xmodels)
    ok(i) = canExportToSimulink(Xmodels{i});
end

% error out if any of the models in the list has a problem
if ~all(ok)
    ok = false;
    msg= 'Export to Simulink is not supported for this model type (or one of its component models).';
    return
end

try
    hSys = load_system(file);
catch  %#ok<CTCH>
    [path, filename, ext] = fileparts(file);
    filename = mbcMakeValidName(filename);
    
    % Check a system called filename is not already open
    S = find_system('SearchDepth', 0, 'Name', filename);
    if ~isempty(S)
        ok = false;
        msg = sprintf(['A Simulink block diagram called %s is already open.  Please', ...
            ' choose another name or close the open diagram before exporting.'], filename);
    else
        file = fullfile(path,[filename,ext]);
        hSys = new_system(filename);
        try
            save_system(hSys,file);
        catch ME
            close_system(hSys, 0);
            ok = false;
            msg = ME.message;
        end
    end
end
if ~ok
    return
end

parentsys = get(hSys, 'Name');
mlist= Xmodels;

if ~isa(mlist,'cell')
    mlist = {mlist};
end

for i=1:length(mlist)
    if i > 1
        [oldc,oldw,~] = LTRB2Centre(get_param(blk,'position'));
    end
    % Build the model
    [blk,blk_name,mod] = mv2sl(mlist{i},DO_PEV && pevcheck(mlist{i}),parentsys);
    if i > 1
        % Ensure next block doesn't overlap previous
        [~,neww,newh] = LTRB2Centre(get_param(blk,'position'));
        centre = oldc + [ceil(oldw/2 + neww/2 + 30), 0];
        set_param(blk,'position',Centre2LTRB(centre,neww,newh));
    end

    if DO_CONSTRAINTS
        % Build model constraints
        
        % export the boundary constraint
        testplan = mdevtestplan(md);
        boundaryConstraint = BoundaryModel(testplan,mlist{i});

        modBlockName = slblock(boundaryConstraint, blk_name);
        mod_const = [blk_name, '/', modBlockName];
        
        % replace interior point outport with a terminator
        pos = get_param([mod_const, '/Interior Point'], 'Position');
        delete_line(mod_const, 'switch/1', 'Interior Point/1');
        delete_block([mod_const,'/Interior Point']);
        % add terminator
        add_block('built-in/Terminator', [mod_const, '/Interior Point'], 'Position', pos)
        add_line(mod_const, 'switch/1', 'Interior Point/1');
        
        % Position the constraints sub-system within the model block
        [centre,width,height] = LTRB2Centre(get_param(mod,'position'));
        centre(2) = centre(2) + height + 40;
        set_param(mod_const,'position',Centre2LTRB(centre,width,height));
        
        if ~strcmp(blk_name,parentsys)
            % Add line between model input and constraints input
            add_line(blk_name,'Mx1/1', [modBlockName,'/1'], 'Autorouting', 'on');
            % Add a new outport
            out = add_block('built-in/Outport',[blk_name, '/Constraints']);
            centre = get_param(mod_const,'OutputPorts') + [80 0];
            set_param(out,'position',Centre2LTRB(centre,20,20));
            add_line(blk_name,[modBlockName,'/1'],'Constraints/1');
        end
    end
end

% save system and open it on screen
save_system(hSys,file);
set_param(hSys,'open','on');

sys_out = blk;