配合APP界面使用:递归延申接口APP
Lib_Name: 模块库名字,用来添加模块,自行替换
curPort: 当前的模块,必须是输入Inport或输出Outport类型
Dist: 递归层数
PortName: 传输的信号名
isIn: 输入信号标志位
realName: 针对变体子系统输出做的优化,子系统未激活时,如果内部和外部信号名一致会导致仿真出错,这个信号名作为外部输入,PortName为内部
function Msg = Extend(curPort,Dist,PortName,isIn,realName)
Parent = get(curPort,'Parent');
ParHandle = getSimulinkBlockHandle(Parent);
index = strfind(Parent,'/');
Grand = Parent(1:index(end));
isNoCov = false;
isRoot = false;
isVs = false;
isVs_p = false;
Ports = get_param(Parent,'PortHandles');
Found = false;
try
isVs = strcmp(get_param(Grand,'Variant'),'on');
isVs_p = strcmp(get_param(Parent,'Variant'),'on');
catch
end
if isVs_p
isNoCov = strcmp(PortName,realName);
PortName = realName;
end
blkPath = strcat(Grand,PortName);
PortIndex = get(curPort,'Port');
line_handle = -1;
% 如果已经有这个输出接口直接return
if ~isIn && (getSimulinkBlockHandle(strcat(Parent(1:index(1)),PortName)) > 0)
Msg = '已经有这个输出接口了';
return;
end
if ~contains(Grand(1:end-1),'/')
isRoot = true;
end
if Dist <= 1 && ~isRoot
%到中间目的层
if isIn
[Found,FromTag,LineHandle] = PortSearch(Grand,PortName);
if Found
if isempty(FromTag)
[blkHandle,~] = GotoGen(Grand,LineHandle);
else
blkHandle = add_block('Lib_Name/Signals and systems/From',[blkPath,'_From'],'MakeNameUnique','on','GotoTag',FromTag);
end
else
blkHandle = add_block('Lib_Name/Signals and systems/From',[blkPath,'_From'],'MakeNameUnique','on','GotoTag',PortName);
end
line_handle = AddLine(Grand,blkHandle,'1',ParHandle,PortIndex);
if Found
Msg = 'Extend Successfully';
return
end
else
if strcmp(get_param(Parent,'Variant'),'on')
out_pos = get_param(get_param(Parent,'PortHandles').Outport(str2double(PortIndex)),'Position');
gotoHandle = add_block('Lib_Name/Signals and systems/Goto',[blkPath,'_Goto'],'MakeNameUnique','on','GotoTag',PortName, ...
'Position',[out_pos(1) + 300,out_pos(2) - 4,out_pos(1) + 320,out_pos(2) + 4]);
if isNoCov
line_handle = AddLine(Grand,ParHandle,PortIndex,gotoHandle,'1');
else
%先加convert
covHandle = add_block('Lib_Name/Signals and systems/Rescaler',[blkPath,'_SigCov'],'MakeNameUnique','on', ...
'Position',[out_pos(1) + 130,out_pos(2) - 7,out_pos(1) + 180,out_pos(2) + 7]);
line_handle = AddLine(Grand,ParHandle,PortIndex,covHandle,'1');
set(line_handle,'ShowPropagatedSignals',1);
%再加接口
line_handle = AddLine(Grand,covHandle,'1',gotoHandle,'1');
set(line_handle,'Name',PortName);
end
else
gotoHandle = add_block('Lib_Name/Signals and systems/Goto',[blkPath,'_Goto'],'GotoTag',PortName);
line_handle = AddLine(Grand,ParHandle,PortIndex,gotoHandle,'1');
end
end
try
set(line_handle,'ShowPropagatedSignals',1);
catch
end
Msg = 'Extend Successfully';
return;
elseif isVs
%到达中间变体子系统
if isIn
pos = get_param(Ports.Inport(str2double(PortIndex)),'Position');
add_block('Lib_Name/Signals and systems/In',blkPath,'BackgroundColor','black','ForegroundColor','black', ...
'Position',[pos(1) - 110,pos(2) - 5,pos(1) - 100,pos(2) + 5]);
else
pos = get_param(Ports.Outport(str2double(PortIndex)),'Position');
add_block('Lib_Name/Signals and systems/Out',blkPath,'BackgroundColor','black','ForegroundColor','black', ...
'Position',[pos(1) + 100,pos(2) - 5,pos(1) + 110,pos(2) + 5]);
end
else
%到中间层
if isIn
%Inport
if ~contains(Grand(1:end-1),'/')
blkHandle = add_block('Lib_Name/Signals and systems/global_input',blkPath);
isRoot = true;
else
[Found,FromTag,LineHandle] = PortSearch(Grand,PortName);
if Found
if isempty(FromTag)
[blkHandle,~] = GotoGen(Grand,LineHandle);
else
blkHandle = add_block('Lib_Name/Signals and systems/From',[blkPath,'_From'],'GotoTag',FromTag);
end
else
blkHandle = add_block('Lib_Name/Signals and systems/In',blkPath,'BackgroundColor','black','ForegroundColor','black');
end
end
line_handle = AddLine(Grand,blkHandle,'1',ParHandle,PortIndex);
if Found
set(line_handle,'ShowPropagatedSignals',1);
Msg = 'Extend Successfully';
return
end
else
%Outport
if ~contains(Grand(1:end-1),'/')
blkHandle = add_block('Lib_Name/Signals and systems/global_output',blkPath);
isRoot = true;
line_handle = AddLine(Grand,ParHandle,PortIndex,blkHandle,'1');
else
if strcmp(get_param(Parent,'Variant'),'on')
out_pos = get_param(get_param(Parent,'PortHandles').Outport(str2double(PortIndex)),'Position');
OutHandle = add_block('Lib_Name/Signals and systems/Out',blkPath,'BackgroundColor','black','ForegroundColor','black', ...
'Position',[out_pos(1) + 310,out_pos(2) - 5,out_pos(1) + 320,out_pos(2) + 5]);
if isNoCov
line_handle = AddLine(Grand,ParHandle,PortIndex,OutHandle,'1');
set(line_handle,'ShowPropagatedSignals',1);
else
%先加convert
covHandle = add_block('Lib_Name/Signals and systems/Rescaler',strcat(blkPath,'_SigCov'),...
'Position',[out_pos(1) + 130,out_pos(2) - 10,out_pos(1) + 180,out_pos(2) + 10]);
line_handle = AddLine(Grand,ParHandle,PortIndex,covHandle,'1');
set(line_handle,'ShowPropagatedSignals',1);
%再加接口
line_handle = AddLine(Grand,covHandle,'1',OutHandle,'1');
set(line_handle,'Name',PortName);
end
else
blkHandle = add_block('Lib_Name/Signals and systems/Out',blkPath, ...
'BackgroundColor','black','ForegroundColor','black');
line_handle = AddLine(Grand,ParHandle,PortIndex,blkHandle,'1');
end
end
end
end
if ~isRoot
try
set(line_handle,'ShowPropagatedSignals',1);
catch
end
Dist = Dist - 1;
Msg = Extend(getSimulinkBlockHandle(blkPath),Dist,PortName,isIn,realName);
else
Msg = 'Extend Successfully';
return;
end
end
function line_handle = AddLine(SubName,Outblk,OutPort,Inblk,InPort)
OutPort = str2double(OutPort);
InPort = str2double(InPort);
OutPortHandle = get(Outblk,'PortHandles');
OutPortHandle = OutPortHandle.Outport(OutPort);
InPortHandle = get(Inblk,'PortHandles');
InPortHandle = InPortHandle.Inport(InPort);
line_handle = add_line(SubName,OutPortHandle,InPortHandle);
end