Code covered by the BSD License  

Highlights from
AutoConnect

image thumbnail

AutoConnect

by

 

Automatically connects MISO, MIMO, and Sink blocks when block is dropped over a line in Simulink.

AutoConnect
% AutoConnect.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.

function AutoConnect
%
% Add a call to this function in they CopyFcn and MoveFcn callback of any block.
% The block will automatically connect when dropped on a line in a model.
% It is STRONGLY suggested that this function is not used on SISO blocks,
% and that Simulink's built in auto-connecting functionality is used for those
% blocks. This will provide similar (but not as robust) functionality for
% MISO, MIMO and Sink blocks.
%
% Dependencies: This function calls SinkAutoConnect
%
% Limitations: This will not autoconnect blocks on vertical signal lines.

% See if the block is connected
lineh = get_param(gcb,'LineHandles');

% If the block is not connected, look to make an autoconnection
if ~any(lineh.Inport ~= -1)
    
    % Find the outport position
    porth = get_param(gcb,'PortHandles');
    
    % If there's no outport, call SinkAutoConnect
    if isempty(porth.Outport)
        SinkAutoConnect;
    else
        for numo = 1:length(porth.Outport)
            % Find the outport position
            oport_pos = get_param(porth.Outport(numo),'Position');
            
            % Check for a co-linear inport
            for i = 1:length(porth.Inport)
                iport_pos = get_param(porth.Inport(i),'Position');
                
                % Check for the same y position of an inport
                if iport_pos(2) == oport_pos(2)
                    % Create a line origin position
                    delta = 10;
                    line_pos = iport_pos;
                    line_pos(1) = line_pos(1)-delta;
                    
                    % Add a line
                    temp_line = add_line(gcs,[line_pos; iport_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(temp_line,'SrcPortHandle');
                    
                    if sph ~= -1
                        in_line = get_param(temp_line,'LineParent');
                        
                        pts = get_param(in_line,'Points');
                        % Detemine signal orientation
                        for ind = 2:length(pts)
                            if pts(ind,2) == iport_pos(2) & pts(ind-1,2) == iport_pos(2)
                                if pts(ind,1) > pts(ind-1,1)
                                    orient = 'right';
                                else
                                    orient = 'left';
                                end
                            end
                        end
                        
                        % On a vertical line, run away!
                        if ~exist('orient','var')
                            delete_line(temp_line);
                            return;
                        end
                            
                        delete_line(temp_line);
                        line_pos = iport_pos;
                        if strcmp(orient,'left')
                            line_pos(1) = line_pos(1)+delta;
                        elseif strcmp(orient,'right')
                            line_pos(1) = line_pos(1)-delta;
                        end
                            
                        % flip the block
                        set_param(gcb,'Orientation',orient);
                        
                        % grab port positions again
                        iport_pos = get_param(porth.Inport(i),'Position');
                        oport_pos = get_param(porth.Outport(numo),'Position');
                        
                        % Add a new temp line
                        temp_line = add_line(gcs,[line_pos; iport_pos]);
                        
                        % Get the input line
                        in_line = get_param(temp_line,'LineParent');
                        
                        % Find all the lines related to the one to be
                        % connected to
                        kids = get_param(in_line,'LineChildren');
                        ind = find(kids ~= temp_line);
                        out_line = kids(ind);
                        lines = CheckConnect(out_line);
                        
                        % out_line's pts should be last cell in lines.
                        % replace the first point with the outport pos
                        lines{end}(1,:)= oport_pos;
                                          
                        % Delete the lines to the Dst block(s)
                        delete_line(gcs,oport_pos);
                        
                        % Connect everything back up
                        add_line(gcs,lines);
                        
                    else %the block wasn't on a line
                        %delete the temporary line
                        delete_line(temp_line);
                    end
                end
            end
        end
    end
end
end

function SinkAutoConnect
% SinkAutoConnect.m
% Mike Anthony
% MathWorks
% Copyright (c) 2010, The MathWorks, Inc.
% All rights reserved.
%
% 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
end

function all_lines = CheckConnect(lh)

all_lines = {};
kids = get_param(lh,'LineChildren');
if ~isempty(kids)
    for i = 1:length(kids)
        temp = CheckConnect(kids(i));
        all_lines = {all_lines{:} temp{:}};
    end
end
all_lines{end+1} = get_param(lh,'Points');
end

Contact us