给模块添加文字并且添加超链接,再次打开模型出现失效了。
原因是使用HTML方式添加出现失效,给修改成调用函数方式。
如果想给字体添加超链接,或者是字体颜色不能使用Simulink自带的属性,得使用HTML属性,这样会有导致连接失效风险。
可见下列代码添加模型的超链接。
toppaths = gcbp;
bplengths = getLength(toppaths);
blockpath = getBlock(toppaths,bplengths);
cellarray = convertToCell(toppaths);
systems = extractBefore(blockpath,'/');
% systems = inputdlg({'请输入顶层模型的名称:'},'Model Name.',[1 35],{'Top Model'});
% if isempty(systems)
% systems = gcs;
% else
% systems=systems{1};
% end
[models,blockpaths]=find_mdlrefs(systems,'AllLevels',0);
inports = find_system(systems,'BlockType','Inport');
pointsX = 1000;
pointsY = 500;
for jj=1:length(inports)
posit = get_param(inports(jj),'Position');
if pointsX >= posit{:}(1)
pointsX = posit{:}(1);
if pointsY >= posit{:}(2)
pointsY = posit{:}(2);
end
end
end
pointsX=pointsX-500;
pointsX1 = pointsX;
pointsY1 = pointsY;
maxlengts = 0;
for ii=1:length(blockpaths)
strs = extractAfter(blockpaths(ii),'/');
bp = ['Simulink.BlockPath([parpath'',{''' blockpaths{ii} '''}])'];
a = Simulink.Annotation(gcs,strs{:});
a.UseDisplayTextAsClickCallback='off';
a.Interpreter = 'rich';
a.ClickFcn = ['toppaths = gcbp;' newline 'cellarray = convertToCell(toppaths);'...
newline 'if length(cellarray)>1'...
newline ' parpath = cellarray(1:end-1);'...
newline 'else'...
newline ' parpath = {};'...
newline 'end'...
newline 'hilite_system(' bp ',''find'');'];
% 使用调用函数方式可以防止连接失效。
a.Name = strs{:};
% a.Text = ['<a href="matlab:hilite_system(' bp ',''find'')' '">' strs{:} '</a>.'];
numLe = length(strs{:});
if maxlengts < numLe
maxlengts=numLe;
end
a.MarkupType = 'markup';
a.FontSize = 28;
a.Position = [pointsX pointsY pointsX+numLe*9 pointsY + 14];
pointsY = pointsY + 48;
clear a
end
pointsY = pointsY + 48;
a= Simulink.Annotation(systems,'RemoveHighlighting');
a.UseDisplayTextAsClickCallback='off';
a.Interpreter = 'rich';
a.ClickFcn = ['SLStudio.Utils.RemoveHighlighting(get_param(''' systems ''',''handle''))'];
a.Name = 'RemoveHighlighting';
% a.Text = ['<a href="matlab:SLStudio.Utils.RemoveHighlighting(get_param(''' systems ''',''handle''))' '">RemoveHighlighting</a>.'];
numLe = length('RemoveHighlighting');
if maxlengts < numLe
maxlengts=numLe;
end
a.MarkupType = 'markup';
a.FontSize = 28;
a.Position = [pointsX pointsY pointsX+numLe*10 pointsY + 14];
are = add_block('built-in/Area',[systems '/All modules are connected'],'Position',[pointsX1-90,pointsY1-50,pointsX+330+maxlengts,pointsY+50]);
set_param(are,'FontSize','28');
% view(a2)
% % deleteannotation
% h = find_system('Application_SWA_sim','FindAll','on','Type','annotation');
% for pp=1:length(h)
% a = get_param(h(pp),'Object');
% delete(a)
% end