????????作為模型工程師,在Simulink中建模時,總是希望把模塊排列整齊從而更加美觀。但是模型的反復修改使得我們沒有太多的精力去做這些整理工作。因此筆者開發(fā)了如下腳本來自動實現Simulink模塊的整理工作,效果如下圖所示:
? ? ? ? 大家可以自取使用,希望可以給大家的開發(fā)工作帶來一點便利。使用方法是,在模型中點擊想要被整理的模塊(比如一個subsystem,使得gcbh為當前選中模塊的句柄),再運行函數 arrangeit() 即可。
?代碼如下:
% 整理與所選模塊的輸入和輸出連接的模塊的位置,使之排布整齊
% 操作方法:先在simulink中點擊選中目標模塊,再運行該函數
function arrangeit()
lineh = get(gcbh, 'LineHandles');
porth = get(gcbh, 'PortHandles');
blkDistance = 150;
%% 設置輸入接口模塊
for i = 1:1:length(porth.Inport)
% 如果當前port口上懸空,沒有連線,則跳過
if isequal(lineh.Inport(i), -1)
continue;
end
% 獲取當前port口的位置
[portx, porty, ~, ~] = lgetpos(porth.Inport(i));
% 獲取當前port口連線的源模塊的句柄
curtLineSrcBlkH = get_param(lineh.Inport(i), 'SrcBlockHandle');
% 如果源模塊是subsystem,則不對該subsystem的位置進行設置
if isequal('SubSystem', get_param(curtLineSrcBlkH, 'BlockType'))
continue;
end
% 獲取源模塊的寬度和高度值
[~, ~, width, height] = lgetpos(curtLineSrcBlkH);
% 重新設置源模塊的位置,保持其寬度和高度值不變
newx = portx - blkDistance;
newy = porty - height / 2;
lsetpos(curtLineSrcBlkH, newx, newy, width, height);
end
%% 設置輸出接口模塊
for i = 1:1:length(porth.Outport)
% 如果當前port口上懸空,沒有連線,則跳過
if isequal(lineh.Outport(i), -1)
continue;
end
% curtPortPos = get_param(porth.Outport(i), 'Position');
% 獲取當前port口的位置
[portx, porty, ~, ~] = lgetpos(porth.Outport(i));
% 獲取當前port口連線的目標模塊的句柄
curtLineDstBlkH = get_param(lineh.Outport(i), 'DstBlockHandle');
% 如果目標模塊是subsystem,則不對該subsystem的位置進行設置
if isequal('SubSystem', get_param(curtLineDstBlkH, 'BlockType'))
continue;
end
% 如果目標模塊是 swithc, 則不對該subsystem的位置進行設置
if isequal('Switch', get_param(curtLineDstBlkH, 'BlockType'))
continue;
end
% 當前port連線的目標模塊可能有多個模塊,因此挨個處理
for j = 1:1:length(curtLineDstBlkH)
% 獲取源模塊的寬度和高度值
[~, ~, width, height] = lgetpos(curtLineDstBlkH(j));
% newx = curtPortPos(1) + blkDistance;
% newy = curtPortPos(2) - height / 2 + height * (j - 1);
newx = portx + blkDistance;
newy = porty - height / 2 + height * (j - 1);
lsetpos(curtLineDstBlkH(j), newx, newy, width, height);
end
end
%% 設置 enable 或 trigger 接口,兩種類型的port合并后處理
EnableTriggerPortH = -1;
EnableTriggerLineH = -1;
if (~isequal(lineh.Enable, -1)) && (~isempty(lineh.Enable))
EnableTriggerPortH = porth.Enable;
EnableTriggerLineH = lineh.Enable;
end
if (~isequal(lineh.Trigger, -1)) && (~isempty(lineh.Trigger))
EnableTriggerPortH = porth.Trigger;
EnableTriggerLineH = lineh.Trigger;
end
if ~isequal(EnableTriggerPortH, -1)
% 獲取當前port口的位置
[portx, porty, ~, ~] = lgetpos(EnableTriggerPortH);
% 獲取當前port口連線的源模塊的句柄
curtLineSrcBlkH = get_param(EnableTriggerLineH, 'SrcBlockHandle');
% 只有當enable port的源模塊是 inport 類型的模塊時,才對其位置進行優(yōu)化
if isequal('Inport', get_param(curtLineSrcBlkH, 'BlockType'))
% 獲取源模塊的寬度和高度值
[~, ~, width, height] = lgetpos(curtLineSrcBlkH);
% 重新設置源模塊的位置,保持其寬度和高度值不變
newx = portx - width * 3;
newy = porty - height * 2;
lsetpos(curtLineSrcBlkH, newx, newy, width, height);
end
end
end
函數 arrangeit() 使用到了位置設置和位置讀取的兩個函數的代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-600985.html
function lsetpos(blockH, x, y, width, height)
% lsetpos(blockH, x, y, width, height)
% blockH 可以是 目標模塊句柄,或者是 目標模塊路徑
% blockH 不能是 port 或者 line 的句柄
% width 和 height 必須 ≥ 0
% 入參合法性檢查
% 如果 blockH 是句柄,則維度必須是1
% 如果 blockH 是目標模塊路徑,則是字符串
if ((length(blockH) > 1) && (~ischar(blockH)))
errordlg('函數 lsetpos 的入參 blockH 的長度大于1','錯誤','creatmode');
end
if (width < 0)
errordlg('錯誤信息:函數 lsetpos 的入參 width 小于0 ! 應 ≥ 0 ','錯誤','creatmode');
end
if (height < 0)
errordlg('錯誤信息:函數 lsetpos 的入參 height 小于0 ! 應 ≥ 0 ','錯誤','creatmode');
end
set_param(blockH, 'Position', [x, y, x + width, y + height]);
end
function [x, y, width, height] = lgetpos(blockH)
% [x, y, width, height] = lgetpos(目標模塊句柄)
% 如果 目標模塊句柄 是一個 port 的句柄,則 width 和 height 返回-1
% 入參合法性檢查
if (length(blockH) > 1)
errordlg('函數 lgetpos 的入參 blockH 的長度大于1','錯誤','creatmode');
end
pos = get_param(blockH, 'Position');
x = pos(1);
y = pos(2);
% 如果目標句柄是port的句柄,則pos只有兩維,此時寬度和高度返回-1
if length(pos) == 4
width = pos(3) - pos(1);
height = pos(4) - pos(2);
else
width = -1;
height = -1;
end
end
如果大家覺得有點用處,還請點個贊,歡迎留言討論~文章來源地址http://www.zghlxwxcb.cn/news/detail-600985.html
到了這里,關于用Matlab腳本實現Simulink模塊的自動整理的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!