www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@conboolean/pslAddBlock.m

    function hBlock = pslAddBlock(c, hSystem)
%PSLADDBLOCK Add a Simulink block for the constraint to the given system.
%
%   HBLOCK = PSLADDBLOCK(CON, HSYSTEM)
%
%  See also CONBOOLEAN, CONBASE/PSLADDBLOCK.

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

% number of constraints
nc = length( c.Constraints ); 

% Set up subsystem
hBlock = add_block( ...
           'built-in/Subsystem', [getfullname( hSystem ), '/Boolean'],        'MakeNameUnique', 'On' );
add_block( 'built-in/Inport',    [getfullname( hBlock ),  '/Xin'],            'MakeNameUnique', 'On' );
add_block( 'built-in/Outport',   [getfullname( hBlock ),  '/Distance'],       'MakeNameUnique', 'On' );
add_block( 'built-in/Outport',   [getfullname( hBlock ),  '/Interior Point'], 'MakeNameUnique', 'On' );

% Build the constraint
switch lower( c.Op ),
    case 'none',
        if nc ~= 1,
            warning(message('mbc:conboolean:InvalidState'));
        end
        error(message('mbc:conboolean:InvalidState7'));

    case 'and',
        i_buildAnd( getfullname( hBlock ), c.Constraints );

    case 'or',
        error(message('mbc:conboolean:InvalidState8'));

    case 'xor',
        if nc ~= 2,
            warning(message('mbc:conboolean:InvalidState'));
        end
        error(message('mbc:conboolean:InvalidState10'));

    otherwise
        error(message('mbc:conboolean:InvalidState'));
end

%------------------------------------------------------------------------------|
function i_buildAnd( blk, cons )
BI = 'built-in/';

ncons = length( cons );
conBlk  = cell( 1, ncons );

% 'pos' will be updated as each constraint block is added. 
pos = [15, 15, 35, 35];
set_param( [blk, '/Xin'], 'position', pos + [0, 30, 0, 30] );

% Add a block for the first constraint
[junk, conBlk{1}] = slblock( cons{1}, blk );
pos = pos + [40, 0, 140, 50];
set_param( conBlk{1}, 'position', pos );

% Add the remaining constraints 
for i = 2:ncons,
    [junk, conBlk{i}] = slblock( cons{i}, blk );
    pos = pos + [150, 40, 150, 40];
    set_param( conBlk{i}, 'position', pos );

end

% Add a 'max' block to determine which distance to use
maxBlk = add_block( [BI, 'MinMax'], [blk, '/max'], ...
    'Function', 'max', ...
    'Inputs', sprintf( '%d', ncons ) );
set_param( maxBlk, 'position', pos + [170, -pos(2)+15, 70, -50] );

% Position the outports
vpos = -floor( 0.5 * (15+pos(2)) );
set_param( [blk, '/Distance'],       'position', pos + [220, vpos, 120, vpos-50] );
set_param( [blk, '/Interior Point'], 'position', pos + [220,   45, 120,  -5] );

%
% Wire everything up
%
conNames = cell( size( conBlk ) );
for i = 1:ncons,
    conNames{i} = get( conBlk{i}, 'Name' ) ;
end

% Wire the "distance" of each constraint block to the "max" block
for i = 1:ncons,
     add_line( blk, [conNames{i}, '/1'], sprintf( 'max/%d', i ), ...
         'autorouting', 'on' );
end

% Wire up the max block to the distance output port
add_line( blk, 'max/1', 'Distance/1', 'autorouting', 'on' );

% Wire up the path of the point to constrain
add_line( blk, 'Xin/1', [conNames{1}, '/1'], 'autorouting', 'on' );
for i = 2:ncons,
     add_line( blk, [conNames{i-1}, '/2'], [conNames{i}, '/1'], ...
         'autorouting', 'on' );
end
add_line( blk, [conNames{ncons}, '/2'], 'Interior Point/1', 'autorouting', 'on' );

%------------------------------------------------------------------------------|
% EOF
%------------------------------------------------------------------------------|