Simulink生成GotoFrom

主方法

LineHandles = find_system(gcs,'FindAll','on','SearchDepth',1,'Selected','on','type','line','SegmentType','trunk');
SubHandles = find_system(gcs,'FindAll','on','SearchDepth',1,'Selected','on','BlockType','SubSystem');
SubHandles = setdiff(SubHandles,get_param(gcs,'Handle'));
Handles = [LineHandles;SubHandles];
Ls = [];
for i = 1 : length(Handles)
    [~,LineHandles] = GotoGen(gcs,Handles(i));
    if LineHandles > 0
        Ls = union(Ls,LineHandles);
    end
end
try
    Simulink.BlockDiagram.routeLine(Ls);
catch
end

配合函数

function [FromHandle,LineHandle] = GotoGen(Subsys,Handle)
    warning('off');
    if strcmp(get(Handle,'type'),'line')
        CurLine = get(Handle);
        if isempty(CurLine.Name)
            SigName =  get(CurLine.SrcPortHandle,'PropagatedSignals');
        else
            SigName = CurLine.Name;
        end
        head = CurLine.Points(1,1);
        headHeight = CurLine.Points(1,2);
        tail = CurLine.Points(end,1);
        tailHeight = CurLine.Points(end,2);
        if any(CurLine.DstBlockHandle < 0)
            LineHandle = -1;
            CurLine = get(getLeaf(Handle));
            tail = CurLine.Points(end,1);
            tailHeight = CurLine.Points(end,2);
            addBlock(Subsys,'Goto',[tail + 7,tailHeight - 4,tail + 27,tailHeight + 4],SigName);
            [~,FromHandle] = addBlock(Subsys,'From',[tail + 37,tailHeight - 4,tail + 57,tailHeight + 4],SigName);
        else
            [GotoC,~] = addBlock(Subsys,'Goto',[tail - 20,tailHeight - 25,tail,tailHeight - 17],SigName);
            [~,FromHandle] = addBlock(Subsys,'From',[tail + 10,tailHeight - 25,tail + 30,tailHeight - 17],SigName);
            midPoints = round((head + tail) / 2);
            pos = get(GotoC.Inport(1),'Position');
            LineHandle = add_line(Subsys,CurLine.SrcPortHandle,GotoC.Inport(1));
            set(LineHandle,'Points',[midPoints,headHeight;midPoints,pos(2);pos]);
        end
        
    elseif strcmp(get(Handle,'BlockType'),'SubSystem')
        LineHandle = -1;
        FromHandle = -1;
        CurBlock = get(Handle);
        for i = 1 : length(CurBlock.LineHandles.Outport)
            if CurBlock.LineHandles.Outport(i) < 0
                pos = get(CurBlock.PortHandles.Outport(i),'Position');
                if ~isempty(CurBlock.OutputSignalNames{i})
                    [GotoPortC,~] = addBlock(CurBlock.Parent,'Goto',[pos(1) + 200,pos(2) - 4,pos(1) + 220,pos(2) + 4],CurBlock.OutputSignalNames{i});
                    LineHandle = add_line(Subsys,CurBlock.PortHandles.Outport(i),GotoPortC.Inport(1),'autorouting','smart');
                    [~,FromHandle] = addBlock(CurBlock.Parent,'From',[pos(1) + 230,pos(2) - 4,pos(1) + 250,pos(2) + 4],CurBlock.OutputSignalNames{i});
                else
                    fprintf('接口%d内部未赋予名字\n',i)
                end
            end
        end
    end
end

function LeafHandle = getLeaf(Handle)
    LeafHandle = -1;
    CurLine = get(Handle);
    Childs = CurLine.LineChildren;
    if isempty(Childs)
        if CurLine.DstPortHandle < 0
            LeafHandle = Handle;
        end
    else
        for i = 1 : length(Childs)
            LeafHandle = getLeaf(Childs(i));
            if LeafHandle > 0
                return
            end
        end
    end
end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值