No BSD License  

Highlights from
Stateflow Alignment Tool

image thumbnail

Stateflow Alignment Tool

by

 

06 Jan 2005 (Updated )

Stateflowalign is a simple GUI to align and modify graphical stateflow objects.

al
function al
%AL align stateflow objects
% The last selected stateflow object is the anchor all other selected
% objects are aligned to. The alignment is done relating to the best
% matching alignment line of the objects. These lines are top, bottom,
% left, right, vertical center and horizontal center

% get all selected stateflow objects
allSfo=gcsfo;

% remove all transitions:
% preallocate array
isTransition=zeros(1,length(allSfo));
% search for Transitions
for i=1:length(allSfo)
    isTransition(i)=isa(allSfo(i), 'Stateflow.Transition');
end
% remove transition object pointers in sfo
sfo=allSfo(~isTransition);

if length(sfo)<2
    error(['You must select at least two objects!' 10 ...
        'Transitions are irrelevant.']);
end


% read the position of the last selected object
[pos isJunction]=getPosition(sfo(end));
% calculate left, right, top, bottom, horizontal and vertical center
lrtbhv=calcLRBTHV(pos,isJunction);

for i=1:(length(sfo)-1)
    % read position of the current object
    [currPos currIsJunction]=getPosition(sfo(i));
    % calculate left, right, top, bottom, horizontal and vertical center
    currLrtbhv=calcLRBTHV(currPos,currIsJunction);
    % calculate misalignment
    delta=lrtbhv-currLrtbhv;
    % search the index of the closest distance
    [dummy,Idx]=min(abs(delta));
    if any(Idx==[1 2 5])
        % misalignment in x direction
        % correct misalignment
        currPos(1)=currPos(1)+delta(Idx);
    else
        % misalignment in y direction
        % correct misalignment
        currPos(2)=currPos(2)+delta(Idx);
    end
    % assure non-negative positions
    if any(currPos(1:2)<0)
        error('negative position');
    end
    % copy to stateflow object
    setPosition(sfo(i), currPos);
end

function [pos, varargout]=getPosition(sfObject)
% return the position of a stateflow object as vector [left top width height]
p=sfObject.Position;
isJunction=isa(p,'Stateflow.JunctionPosition');
if isJunction
    pos=[p.Center(1)-p.Radius p.Center(2)-p.Radius 2*p.Radius 2*p.Radius];
else
    pos=p;
end
% return isJunction if requested
if nargout>1
    varargout(1)={isJunction};
end

function setPosition(sfObject,p)
% set the position of a stateflow object to vector p = [left top width height]
isJunction=isa(sfObject,'Stateflow.Junction');
if isJunction
    pos=sfObject.Position;
    pos.Radius=p(3)*0.5;
    pos.Center=[(p(1)+pos.Radius) (p(2)+pos.Radius)];
else
    sfObject.Position=p;
end


function lrtbhv=calcLRBTHV(pos,varargin)
% calculate left, right, top, bottom, horizontal and vertical center

if (nargin>1)
    mode=varargin{1};
else
    mode=0;
end
switch mode
    case 0,
        lrtbhv=[pos(1) (pos(1)+pos(3)) pos(2) (pos(2)+pos(4)) ...
            (pos(1)+0.5*pos(3)) (pos(2)+0.5*pos(4))];
    case 1,
        % collapse to a point in the centre
        cx=pos(1)+0.5*pos(3);
        cy=pos(2)+0.5*pos(4);
        lrtbhv=[cx cx cy cy cx cy];
    otherwise
        error(['unknown option ' num2str(varargin{1})]);
end

Contact us