www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@contwostage/pslAddBlock.m
function hBlock = pslAddBlock(con, hSystem) %PSLADDBLOCK Add a Simulink block for the constraint to the given system % % HBLOCK = PSLADDBLOCK(CON, HSYSTEM) % % See also CONTWOSTAGE, CONBASE/PSLADDBLOCK, CONBASE/SLSETPARAMETERS. % Copyright 2005-2014 The MathWorks, Inc. % Load the library with the constraint blocks load_system( 'mbcSLConstraints' ); % % Start with the star-shaped constraint template block from the library. % % Form the string for the source block src = sprintf( 'mbcSLConstraints/%s', class( con ) ); % Form the string for the target block tgt = sprintf( '%s/%s - %s', getfullname( hSystem ), getName( con ), class( con ) ); % add the new block hBlock = add_block( src, tgt, 'MakeNameUnique', 'On', 'LinkStatus', 'None' ); % Parameters ngm = numel( con.Global ); % number of global models gi = getGlobalIndices( con ); li = getLocalIndices( con ); nf = nFactors( con ); blk = getfullname( hBlock ); % % Setup the selector blocks % set_param( [blk, '/Local Selector'], 'InputPortWidth', sprintf( '%g ', nf ),... 'Elements', ['[', sprintf( '%g ', li ), ']']); set_param( [blk, '/Global Selector'], 'InputPortWidth', sprintf( '%g ', nf ) ,... 'Elements', ['[', sprintf( '%g ', gi ), ']'] ); set_param( [blk, '/Mx1'], 'inputs', num2str( ngm ), 'displayoption', 'bar' ); % % Replace the "Local Constraint" block with a block for the actual local % constraint % localBlk = add_block( ... sprintf( 'mbcSLConstraints/Local %s', class( con.Local ) ), ... sprintf( '%s/Local - %s', blk, class( con.Local ) ),... 'MakeNameUnique', 'On', 'LinkStatus', 'None' ); i_replace_block( [blk, '/Local Constraint'], localBlk ); i_slSetLocalParameters( con.Local, localBlk ); % We need to put in some coding if we have a friend, i.e., if we have an % historical constraint. if ~isempty( con.LocalFriend ), localBlockInport1 = [sprintf( 'Local - %s', class( con.Local ) ), '/1']; delete_line( blk, 'Local Selector/1', localBlockInport1 ); codeBlock = codebuild( con.LocalFriend, blk, 'Local Code' ); set_param( codeBlock, 'Position', [215, 40, 295, 90] ); add_line( blk, 'Local Selector/1', 'Local Code/1', 'autorouting','on' ); add_line( blk, 'Local Code/1', localBlockInport1, 'autorouting','on' ); localBlockOutport2 = [sprintf( 'Local - %s', class( con.Local ) ), '/2']; delete_line( blk, localBlockOutport2, 'Mx2/1' ); invcodeBlock = codebuild( con.LocalFriend, blk, 'Local Invcode', true ); set_param( invcodeBlock, ... 'Position', [510, 205, 590, 255], ... 'ForegroundColor', 'red' ); add_line( blk, localBlockOutport2, 'Local Invcode/1', 'autorouting','on' ); add_line( blk, 'Local Invcode/1', 'Mx2/1', 'autorouting','on' ); end % % Add the global model blocks % pos = [155, 156, 265, 194]; for i = 1:ngm, nm = sprintf( 'Global (%d)', i ); globalBlk = modelbuild( con.Global{i}, blk, nm, false ); % Position below the last global model pos = pos + [0, 60, 0, 60]; set_param( globalBlk, 'position', pos ); % Wire up to the global selector and the mux block add_line( blk, 'Global Selector/1', [nm, '/1'], 'autorouting','on' ); add_line( blk, [nm, '/1'], ['Mx1/', sprintf( '%d', i )], 'autorouting','on' ); end %------------------------------------------------------------------------------- function pos = i_replace_block( oldblk, newblk ) pos = get_param( oldblk, 'position' ); delete_block( oldblk ); set_param( newblk, 'position', pos ); %-------------------------------------------------------------------------- function i_slSetLocalParameters(con, sys) %I_SLSETLOCALPARAMETERS Sets the parameters for a Simulink model of a Local constraint. % Get up user data structure for this constraint ud = pslGetLocalParameters( con ); if ~isempty( ud ), % The initialization command has to get all of the data from the user % structure and assign it to the variables that the block is expecting fn = fieldnames( ud ); nFields = length( fn ); maskInitialization = cell( nFields, 1 ); for i = 1:nFields, maskInitialization{i} = sprintf( '%s = ud.%s; ', fn{i}, fn{i} ); end maskInitialization = strcat( ... 'ud = get_param( gcb, ''UserData'' ); ', ... maskInitialization{:} ); % Assign the user data to the block set_param( sys, 'UserData', ud ); set_param( sys, 'UserDataPersistent', 'on' ); set_param( sys, 'MaskInitialization', maskInitialization ); end %-------------------------------------------------------------------------- % EOF %--------------------------------------------------------------------------