主要内容

本页采用了机器翻译。点击此处可查看英文原文。

分层报告系统

此示例显示如何创建其各节根据系统层次结构进行编号的报告。每个节包含一个系统快照和包含子系统快照的子节。要创建这样的节,请创建一个节对象,添加图快照,然后添加子系统节。要创建子系统节,再次创建一个节,添加子系统图快照,然后添加其子系统节。创建节的算法是递归的。此示例创建并使用一个名为 createSystemSection 的本地函数,该函数实现了递归算法。

使用 createSystemSection 函数创建分层报告

打开一个模型。

model = "sf_car";
open_system(model);

创建并打开报告对象。

% Change the output type from "pdf" to "docx" or "html" to create a 
% Word or HTML report, respectively.
rpt = slreportgen.report.Report("myreport", "pdf");
open(rpt);

添加标题页。

titlepage = mlreportgen.report.TitlePage();
titlepage.Title = "Hierarchical Report";
add(rpt, titlepage);

添加目录,并将层级数设置为最大值 6。

toc = mlreportgen.report.TableOfContents();
toc.TOCObj.NumberOfLevels = 6;
add(rpt, toc);

通过调用 createSystemSection 本地函数为模型创建系统节(见下文)。该函数递归调用自身来创建子系统的各个节。

section = createSystemSection(model);
add(rpt, section);

生成并显示报告。

close(rpt);
rptview(rpt);

定义 createSystemSection 本地函数

系统节由系统快照及其子节中的子系统组成。要创建系统节,请使用 slreportgen.finder.Diagram 查找器并以 SearchDepth 为 1 查找深达一层的所有系统。

function section = createSystemSection(sys)
    df = slreportgen.finder.DiagramFinder(sys);
    df.SearchDepth = 1;

    % Use the finder in iterator mode. The next function returns search results
    % one-by-one and the hasNext function determines when there are no more 
    % search results. To obtain the current system, call the next function 
    % once.
    sysResult = next(df);

    % Now, create a section using mlreportgen.report.Section with the system 
    % name as the title.
    section = mlreportgen.report.Section( ...
        "Title", mlreportgen.utils.normalizeString(sysResult.Name));

    % Add a system snapshot and a caption that shows the full diagram path. 
    % To include additional information about the system, add it to the 
    % section object.
    diag = slreportgen.report.Diagram(sysResult.Object);
    diag.Snapshot.appendCaption(sysResult.Path);
    add(section, diag);
    
    % To create subsections, loop through all subsystems and recursively call 
    % createSystemSection. Before calling createSystemSection, add a page break
    % so each system starts on a new page. Note that adding a page break right 
    % after the system snapshot would add a blank page at the end of the report.
    while hasNext(df)
        childSysResult = next(df);
        add(section, mlreportgen.dom.PageBreak());
        subSection = createSystemSection(childSysResult.Object);
        add(section, subSection);
    end
end