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.

sf_dist(dist,varargin)
function sf_dist(dist,varargin)
%SF_DIST bring stateflow objects to a specified distance


% 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 centre and horizontal centre

% 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

if nargin<2
    distType=1;
    cascade=0;
else
    distType=varargin{1};
    cascade=any(distType==[4 5 6]);
    if cascade
        distType=distType-3;
    end
end

% read the position of the last selected object
pos=getPosition(sfo(end));
% calculate horizontal and vertical centre
hv=[(pos(1)+0.5*pos(3)) (pos(2)+0.5*pos(4))];

for i=(length(sfo)-1):-1:1
    % read position of the current object
    currPos=getPosition(sfo(i));
    % calculate horizontal and vertical centre of the current object
    currhv=[(currPos(1)+0.5*currPos(3)) (currPos(2)+0.5*currPos(4))];
    % calculate the difference of the centres to decide the orientation
    deltaCentre=hv-currhv;
    if cascade
        % save the origin centre of the current object for the next
        % iteration
        hv=currhv;
    end
    
    if distType==1
        % automatic mode
        % find direction of higher misalignment
        [dummy,Idx]=max(abs(deltaCentre));
        % set distType to 2 -> horizontal or 3 -> vertical
        distType=Idx+1;
    end
    
    switch distType
        case 2,
            % horizontal distance
            isCurrLeft=deltaCentre(1)>0;
            if isCurrLeft
                currPos(1)=pos(1)-dist-currPos(3);
            else
                currPos(1)=pos(1)+pos(3)+dist;
            end
        case 3,
            % vertical distance
            isCurrTop=deltaCentre(2)>0;
            if isCurrTop
                currPos(2)=pos(2)-dist-currPos(4);
            else
                currPos(2)=pos(2)+pos(4)+dist;
            end
        otherwise
            error('unknown distance type');
    end
    if any(currPos(1:2)<0)
        error('negative position');
    end

    % copy to stateflow object
    setPosition(sfo(i),currPos);
    if cascade
        % change reference position to the new position of the current object
        pos=currPos;
    end
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

Contact us