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

    function sys = makeProjectLibrary( featnode )
%MAKEPROJECTLIBRARY Create simulink diagram for strategy editing
%
%  SYS = MAKEPROJECTLIBRARY(NODE) creates a Simulink system that contains a
%  library of all of the CAGE blocks that are available for creating
%  strategies.

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


% get interesting things from the featurenode
F = getdata( featnode );
P = project( featnode );
D = getdd( P );

width = 5; % number of blocks wide in the library
modelBrowserWidth = 200;
horizspacer = 140;
vertspacer = 40;
ncount = 0;
ccount = 0;
l2count = 0;
l1count = 0;
fcount = 0;
vcount = 0;

sys = 'CAGE_Project';
alreadyExist = find_system('type','block_diagram','name',sys);
try
    for i = 1:length(alreadyExist)
        bdclose(alreadyExist{i});
    end
end
new_system(sys,'Library');
varDictionaryBlock = [sys,'/Variable', char(10),  'Dictionary'];
hL2 = add_block('built-in/Subsystem',[sys,'/Tables'],'position',[10 10 50 70]);
hL1 = add_block('built-in/Subsystem',[sys,'/Functions'],'position',[70 10 110 70]);
hN = add_block('built-in/Subsystem',[sys,'/Normalizers'],'position',[130 10 170 70]);
hC = add_block('built-in/Subsystem',[sys,'/Cal. constants'],'position',[200 10 240 70]);
hF = add_block('built-in/Subsystem',[sys,'/Features'],'position',[260 10 300 70]);
hVD = add_block('built-in/Subsystem',varDictionaryBlock,'position',[320 10 360 70]);

% First add data dictionary items
pVars = D.listptrs;
featModel = F.get('model');
if ~isempty(featModel)
    pMdlInp = featModel.getdditems;
else
    pMdlInp = mbcpointer(0);
end
% sort the model variables alphabetically
modelVarNames = pveceval(pMdlInp, @getname);
[unused, alphasortindex] = sort(upper(modelVarNames));
pMdlInp = pMdlInp(alphasortindex);
modelVarNames = modelVarNames(alphasortindex);
numMdlInp = length(pMdlInp);

% sort the rest of the variables alphabetically
VarNames = pveceval(pVars, @getname);
[UpperVarNames, alphasortindex] = sort(upper(VarNames));
pVars = pVars(alphasortindex);
VarNames = VarNames(alphasortindex);

% Add model variables
for n = 1:numMdlInp
    left = horizspacer*mod(n-1,width)+10;
    top = vertspacer*floor((n-1)/width);
    i_AddTag(hVD, pMdlInp(n), modelVarNames{n}, left, top, 'red');
    vcount = vcount + 1;
end
MdlVarSpace = max(0, vertspacer*(1+floor((numMdlInp-1)/width)));

% if we have a large variable dictionary we'll partion it into blocks of
% about 50.
if length(pVars) > 100
    % we want max maxBlocks vars per subsystem
    maxBlocks = 50;
    
    ranges = [1 length(UpperVarNames)];
    letters = 'AZ';
    
    n = 1;
    keepGoing = true;
    while keepGoing
        if (ranges(n,1) + maxBlocks)>length(UpperVarNames)
            keepGoing = false;
        else
            letters(n,2)  = UpperVarNames{ranges(n,1) + maxBlocks-1}(1);

            % find the last occurrence of the letter
            ranges(n,2) = find(strncmp(letters(n,2), UpperVarNames, 1), 1, 'last');

            if ranges(n,2)>=length(UpperVarNames)
                keepGoing = false;
            else
                % Add another sub-block
                n = n+1;
                ranges(n,1) = ranges(n-1,2) + 1;
                ranges(n,2) = length(UpperVarNames);
                letters(n,1) = char(letters(n-1,2)+1);
                letters(n,2) = 'Z';
            end
        end
    end
    
    % Now create subsystems that contain variable items
    for subI = 1:size(ranges,1)
        SSleft = 100*mod(subI-1,width)+10;
        SStop  = MdlVarSpace+80*floor((subI-1)/width);
        if strcmp(letters(subI,1), letters(subI,2))
            label = sprintf('%s/%s', varDictionaryBlock, letters(subI,1));
        else
            label = sprintf('%s/%s-%s', varDictionaryBlock, letters(subI,1), letters(subI,2));
        end
        hB = add_block('built-in/Subsystem', label, ...
            'position',[SSleft SStop+40 SSleft+60  SStop+100]);
        vcount = vcount + 1;
        thisStart = ranges(subI,1);
        for subJ = thisStart:ranges(subI,2)
            left = horizspacer * mod( subJ-thisStart , width ) + 10;
            top  = vertspacer  * floor( ( subJ-thisStart ) / width );
            i_AddTag(hB, pVars(subJ), VarNames{subJ}, left, top, 'black');
        end
    end
else
    % Don't duplicate the model variables in this case
    ToKeep = ~ismember(pVars, pMdlInp);
    pVarsToAdd = pVars(ToKeep);
    VarNames = VarNames(ToKeep);
    for n = 1:length(pVarsToAdd)
        left = horizspacer*mod(n-1,width)+10;
        top = MdlVarSpace+vertspacer*floor((n-1)/width);
        i_AddTag(hVD, pVarsToAdd(n), VarNames{n}, left, top, 'black');
        vcount = vcount + 1;
    end
end

% Now add all other items

% Running list of added cal constants.
pCalConst = mbcpointer(1,0);
pVars = pVars(:).';

pND = children(P);
ND = infoarray(pND);
for n = 1:length(pND)
    thisND = ND{n};
    if isa(thisND, 'cgfeaturenode')
        pF = getdata(thisND);
        if pF~=F
            % Add feature
            left = horizspacer*mod(fcount,width)+10;
            top = vertspacer*floor(fcount/width);
            i_AddTag(hF, pF, '', left, top);
            fcount = fcount+1;
        end
        % Add any constants that are in the feature
        pC = setdiff(pF.getsource, [pVars, pCalConst]);
        for m = 1:length(pC)
            left = horizspacer*mod(ccount,width)+10;
            top = vertspacer*floor(ccount/width);
            i_AddTag(hC, pC(m), '', left, top);
            ccount = ccount+1;
        end
        pCalConst = [pCalConst pC];
    elseif isa(thisND, 'cgtablenode')
        pT = getdata(thisND);
        if pT.getNumAxes==1
            psys = hL1;
            left = horizspacer*mod(l1count,width)+10;
            top = vertspacer*floor(l1count/width);
            l1count = l1count+1;
        else
            psys = hL2;
            left = horizspacer*mod(l2count,width)+10;
            top = vertspacer*floor(l2count/width);
            l2count = l2count+1;
        end
        i_AddTag(psys, pT, '', left, top,'darkGreen');
    elseif isa(thisND, 'cgnormnode')
        pN = getdata(thisND);
        left = horizspacer*mod(ncount,width)+10;
        top = vertspacer*floor(ncount/width);
        i_AddTag(hN, pN, '', left, top,'lightBlue');
        ncount = ncount+1;
    end

end

maxBlocks = max([ncount,ccount,l2count,l1count,fcount,vcount]);
height = min(max(maxBlocks*20+40,150),300);

slPos = pcalcSLPosition;
set_param(sys, ...
    'blockdescriptionStringDataTip','on', ...
    'dirty', 'off');

if maxBlocks > 0
    open_system(sys);
    set_param(sys,'Location',[slPos(1)+modelBrowserWidth slPos(2)-75-height slPos(3) slPos(2)-75])
    set_param(sys,'ModelBrowserVisibility','on','ModelBrowserWidth',modelBrowserWidth);
end



function i_AddTag(sys, ptr, objname, left, top, col)
if nargin<6
    col = 'black';
end

if isempty(objname)
    objname = ptr.getname;
end
blockname = sprintf('%s/%s/_TAG%s', get(sys, 'path'), get(sys, 'Name'), objname);
add_block('built-in/From',blockname,...
    'MakeNameUnique', 'on', ...
    'GotoTag',objname,...
    'Description',objname,...
    'ShowName','off',...
    'UserData',ptr,...
    'Position',[left,top,left+120,top+15],...
    'ForegroundColor',col);