Matlab M 代码实现 Stateflow 节点连线 & 添加 LabelString

先看效果:

演示代码:

function Test

SFPath = 'Test/Test';
rt = sfroot;
Chart = rt.find('-isa', 'Stateflow.Chart', '-and', 'Path', SFPath);

Junc1 = Stateflow.Junction(Chart);
Junc1.Position.Center = [100, 200];

Junc2 = Stateflow.Junction(Chart);
Junc2.Position.Center = [350, 200];

Junc3 = Stateflow.Junction(Chart);
Junc3.Position.Center = [100, 300];

Junc4 = Stateflow.Junction(Chart);
Junc4.Position.Center = [350, 300];

AddTran([], Junc1);  % 第一个参数为空就是给第二个参数添加默认转移

LabelStr = '[bFirstRunFlag == true]{bFirstRunFlag = false;}';
AddTran(Junc1, Junc2, LabelStr);

LabelStr = '[gTest_u16Test == 300]';
AddTran(Junc1, Junc3, LabelStr);

LabelStr = '{Test = 1;}';
AddTran(Junc2, Junc4, LabelStr, true);

end

AddTran 函数定义如下:

LabelString 只会放置在线的上,右 或 左边,只有在 DownLeftFlag 为 true 时竖向连线的 LabelString 才会放置在线的左边。

%% AddTran
function AddTran(varargin)

    if nargin == 4
        [SourceJunc, DestinationJunc, String, DownLeftFlag] = varargin{[1, 2, 3, 4]};
    elseif nargin == 3
        [SourceJunc, DestinationJunc, String] = varargin{[1, 2, 3]};
        DownLeftFlag = false;
    elseif nargin == 2
        [SourceJunc, DestinationJunc] = varargin{[1, 2]};
        DownLeftFlag = false;
        String = '';

    end
    Tran = Stateflow.Transition(DestinationJunc.Subviewer);
    Tran.Source = SourceJunc;
    Tran.Destination = DestinationJunc;
    DestinationPos = DestinationJunc.Position.Center;

    if isempty(SourceJunc)
        SourcePos = [DestinationPos(1), DestinationPos(2) - 60];
        Direction = 'Down';
        Tran.SourceEndpoint = SourcePos;
    else
        SourcePos = SourceJunc.Position.Center;
        DiffX = SourcePos(1) - DestinationPos(1);
        DiffY = SourcePos(2) - DestinationPos(2);

        if DiffX == 0
            if DiffY < 0
                Direction = 'Down';
            else
                Direction = 'Up';
            end
        else
            if DiffX < 0
                Direction = 'Right';
            else
                Direction = 'Left';
            end
        end
    end

    if strcmp(Direction, 'Right')
        Tran.SourceOClock = 3;
        Tran.DestinationOClock = 9;
        WidthOrHeight = DestinationPos(1) - SourcePos(1);
    elseif strcmp(Direction, 'Down')
        Tran.SourceOClock = 6;
        Tran.DestinationOClock = 12;
        WidthOrHeight = DestinationPos(2) - SourcePos(2);
    elseif strcmp(Direction, 'Left')
        Tran.SourceOClock = 9;
        Tran.DestinationOClock = 3;
        WidthOrHeight = SourcePos(1) - DestinationPos(1);
    elseif strcmp(Direction, 'Up')
        Tran.SourceOClock = 12;
        Tran.DestinationOClock = 6;
        WidthOrHeight = SourcePos(2) - DestinationPos(2);
    end

    Tran.FontSize = 8;
    Tran.LabelString = String;
    LabelPos = Tran.LabelPosition;
    TranPos = Get_LabelPos(SourcePos, WidthOrHeight, ...
        LabelPos, Direction, DownLeftFlag);
    Tran.LabelPosition = TranPos;

end

%%
function Pos = Get_LabelPos(varargin)

    [SourceJuncPos, WidthOrHeight, LabelPos, Direction, DownLeftFlag] = varargin{[1, 2, 3, 4, 5]};
    if strcmp(Direction, 'Right')
        Pos = [SourceJuncPos(1) + (WidthOrHeight - LabelPos(3)) / 2, ...
            SourceJuncPos(2) - LabelPos(4) - 1, LabelPos(3), LabelPos(4)];
    elseif strcmp(Direction, 'Down')
        if DownLeftFlag
            Pos = [SourceJuncPos(1) - 10 - LabelPos(3), SourceJuncPos(2) + (WidthOrHeight - LabelPos(4)) / 2, ...
                LabelPos(3), LabelPos(4)];
        else
            Pos = [SourceJuncPos(1) + 10, SourceJuncPos(2) + (WidthOrHeight - LabelPos(4)) / 2, ...
                LabelPos(3), LabelPos(4)];
        end
    elseif strcmp(Direction, 'Left')
        Pos = [SourceJuncPos(1) - LabelPos(3) - (WidthOrHeight - LabelPos(3)) / 2, ...
            SourceJuncPos(2) - LabelPos(4) - 1, LabelPos(3), LabelPos(4)];
    else
        Pos = [SourceJuncPos(1) + 10, SourceJuncPos(2) - LabelPos(4) - ...
            (WidthOrHeight - LabelPos(4)) / 2, LabelPos(3), LabelPos(4)];
    end

end

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值