www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgfeaturenode/importstrategy.m

    function featnode = importstrategy( featnode, mdlfile, blockname )
%IMPORTSTRATEGY Imports a Simulink model into a feature
%
%  FEATNODE = IMPORTSTRATEGY( FEATNODE, <MDLFILE>, <BLOCKNAME> )

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

CGBH = cgbrowser;
d=CGBH.getViewData;

if ~isempty(d) && ~isempty(d.sys) && ishandle(d.sys)
    close_system(d.sys,0);
end


if nargin==1
    filename = i_choosesystem;
    if isnumeric( filename) && (filename==0)
        return;
    end
else
    % we were given a file name
    filename = mdlfile;
end

[path, name] = fileparts( filename );
if isempty(path)
    path = pwd;
end

try
    sys = loadSystem(name,path);
catch ME
    i_error(sprintf('Couldn''t open Simulink model %s\n%s\n', name, ME.message));
    i_resetPointer( CGBH );
    return
end
if strcmp(get_param(sys,'Lock'),'on')
    i_error('CAGE cannot parse locked library files');
    close_system(sys,0);
    i_resetPointer( CGBH );
    return
end

if CGBH.GUIExists
   set([d.Handles.LibMenu d.Handles.ParseMenu],'Enable','on');
end

% Create a SL window the same size and position as the display frame
browserWidth = 160;
set_param(sys,'modelbrowservisibility','on',...
    'modelbrowserwidth',browserWidth,...
    'autozoom','on',...
    'toolbar','off',...
    'statusbar','off',...
    'zoomfactor','fitsystem');
d.sysCloseImportListener = ...
    handle.listener(handle(sys), 'CloseEvent', @i_SLClose);
CGBH.setViewData(d);

% try and parse only available block
try
    if nargin==3
        outports = find_system(sys,'searchdepth',1,'BlockType','Outport', 'Name', blockname);
        button = 'Automatic';
    else
        outports = find_system(sys,'searchdepth',1,'BlockType','Outport');
        switch length(outports) 
            case 0
                hError = errordlg('An outport is required in the top level of a Simulink model.','Import Strategy');
                uiwait(hError)
                return
            case 1
                button = 'Automatic';
            otherwise
                button = questdlg('There is more than one importable signal at the top level of this strategy. Automatically import all outports, or manually choose another from the diagram.', ...
                    'Import Strategy', ...
                    'All','Manual','Manual');
        end
    end

catch ME
    i_error(ME.message)
    return
end

% clear any user data in blocks
blocks=find_system(get_param(sys,'Handle'),'findall','on','lookundermasks','all',...
    'Type','block');
set(blocks,'UserData','');

NewName = get_param(outports(1),'Name');

switch button
    case 'Automatic'
        % featnode = parseAutomaticMode(featnode,d,sys,CGBH,block);
        [featnode,OK] = parseSLOutports(featnode,sys,outports);
    case 'Manual'
        parseManual(CGBH,sys)
        OK = false;
    case 'All'
        [featnode,OK] = parseSLOutports(featnode,sys,outports);
    otherwise
        OK = false;
end


featnode = info(featnode);
if OK
    % rename top level node
    featnode = setname(featnode, uniquename( info(project(featnode)), NewName) );
    % update feature view
    d=CGBH.getViewData;
    d.sys = [];
    CGBH.setViewData(d);
    close_system(sys,0);
    CGBH.ViewNode;
    CGBH.doDrawTree(address(featnode),'refresh');
    i_resetPointer( CGBH )
else
    CGBH.ViewNode;
end
featnode = info(featnode);

function i_SLClose(~,~)
Callbacks(cgfeaturenode,'i_SLClose',[],[]);

%--------------------------------------------------------------------------
function filename = i_choosesystem
%--------------------------------------------------------------------------
[filename,path]=uigetfile({'*.mdl;*.slx','Simulink Model (*.mdl,*.slx)'}, 'Choose Simulink strategy file...',...
    mbcGetPath('cage', 'Strategies'));
if isa(filename,'double')
    %Cancel was pushed
    return
else
    if ~exist(path,'dir')
        return
    end
    filename = fullfile(path, filename);
end

%---------------------------------------------------------------------------------------
function i_resetPointer( CGBH )
%---------------------------------------------------------------------------------------
if CGBH.GUIExists
    set(CGBH.Figure,'Pointer','arrow');
end

%---------------------------------------------------------------------------------------
function i_error(str)
%---------------------------------------------------------------------------------------
CGBH = cgbrowser;
if CGBH.GUIExists
    uiwait(errordlg(str,'CAGE Error','modal'));
else
    error( 'mbc:importstrategy:FailedToImportStrategy', str );
end

function sys = loadSystem(name,path)
if exist(name,'file') ~= 4
    % Simulink model not in current directory or on path
    % now need to load that file
    % we can't load_system with the fullpath,
    % need to cd to the directory first, and then load the model
    originaldir = cd( path );
    changeDir = true;
else
    changeDir = false;
end
ws = warning('off','all');
restoreWS = onCleanup(@() warning(ws));
load_system( name );
if changeDir
    cd(originaldir);
end
sys=get_param(name,'handle');

function parseManual(CGBH,sys)

% manual
i_resetPointer( CGBH );
d=CGBH.getViewData;

% make each outport blue, with correct openfcn

setOutportOpenFcn(sys);

% open system and wait for user to select outport
d.sys = get_param(sys,'handle');
open_system(sys);

if CGBH.GUIExists
    d = pMessage(d, 'Double click on a blue outport to parse the required strategy');
    CGBH.setViewData(d);
end
CGBH.ViewNode;

function setOutportOpenFcn(sys)


outports = find_system(sys,'searchdepth',1,'BlockType','Outport');

openfcn='Callbacks(cgfeaturenode,''i_SLUpdate'',[],[]);';
set(outports,'OpenFcn',openfcn,'BackGroundColor','blue');