simulink自动建模及端口对齐
程序员文章站
2024-02-23 22:52:22
...
工作中需要开发一个小工具,实现端口及子系统端口识别自动连接信号线,并对齐信号线,初步完成demo,并贴上代码。
需求为:子系统中的端口名和顶层的端口名对应相连,即需要识别端口名称是否一致,然后信号线相连,如图1为处理之前:
子系统内部逻辑无需处理,简单直连,见图2:
处理之后效果见图3:
贴上代码:
% 找到子系统的句柄
aa = find_system(bdroot,'FindAll','on','SearchDepth','1','BlockType','SubSystem');
% 返回子系统的名称
bb = get_param(aa,'Name');
% 拼接子系统路径
cc = [bdroot,'/',bb];
% 找出外部输入端口
dd = find_system(bdroot,'FindAll','on','SearchDepth','1','BlockType','Inport');
% 找出外部输出端口
ee = find_system(bdroot,'FindAll','on','SearchDepth','1','BlockType','Outport');
% 找出子系统输入端口
ff = find_system(cc,'FindAll','on','SearchDepth','1','BlockType','Inport');
% 找出子系统输出端口
gg = find_system(cc,'FindAll','on','SearchDepth','1','BlockType','Outport');
% 输入端口连接
for k = 1:length(ff)%子系统循环在外
for i = 1:length(dd)%外部端口循环在内
if (get_param(dd(i),'Name') == get_param(ff(k),'Name'))
hh = get_param(dd(i),'Name');
add_line(bdroot,[hh,'/1'],[bb,'/',num2str(k)]);
end
end
end
%输出端口连接
for y = 1:length(gg)%子系统循环在外
for x = 1:length(ee)%外部端口循环在内
if (get_param(ee(x),'Name') == get_param(gg(y),'Name'))
ii = get_param(ee(x),'Name');
add_line(bdroot,[bb,'/',num2str(y)],[ii,'/1']);
end
end
end
% 信号线平直
aaa = get_param(cc,'PortConnectivity');
for k=1:length(aaa)
bbb = aaa(k).Position;
if(~isempty(aaa(k).SrcBlock))
set_param(aaa(k).SrcBlock,'Position',[bbb(1)-60,bbb(2)-5,bbb(1)-40,bbb(2)+5])
end
if(~isempty(aaa(k).DstBlock))
set_param(aaa(k).DstBlock,'Position',[bbb(1)+40,bbb(2)-5,bbb(1)+60,bbb(2)+5])
end
end
the end