function SinkAutoConnect
% SinkAutoConnect.m
% Mike Anthony
% MathWorks
% Copyright (c) 2010, The MathWorks, Inc.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
% * Neither the name of the The MathWorks, Inc. nor the names
% of its contributors may be used to endorse or promote products derived
% from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
% Add a call to this function in they CopyFcn and MoveFcn callback of any block
% with signle or multiple inports and no outport. The block will automatically
% connect when dropped on a signal line in a model.
% See if the block is connected
lineh = get_param(gcb,'LineHandles');
% If the block is not connected, look to make an autoconnection
if lineh.Inport(1) == -1
% Find the port position
porth = get_param(gcb,'PortHandles');
for numi = 1:length(porth.Inport)
port_pos = get_param(porth.Inport(numi),'Position');
% Find the block position
pos = get_param(gcb,'Position');
% Check if block is on a signal line
connect = false;
% Create a line origin position
line_pos = port_pos;
line_pos(1) = line_pos(1)-10;
% Add a line
nl = add_line(gcs,[line_pos; port_pos]);
% Check to see if the line you jsut added connected
% (It's MUCH easier to add the line and see if it connected than check to
% see if you should add the line in the first place)
sph = get_param(nl,'SrcPortHandle');
if sph ~= -1
connect = true;
slh = get_param(nl,'LineParent');
end
% If drawing this line connected the block, then move it
if connect
% Shift the block downward
delta = pos(4)-pos(2);
% Prevent Recursion warning
try
% Move the block
set_param(gcb,'Position',[pos(1) pos(2)+delta pos(3) pos(4)+delta]);
catch
end
else %else get rid of the temporary line
delete_line(gcs,port_pos);
end
end
end