主方法
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