BimRv SDK:检索计算的表格单元格数据

本文解释了如何从 OdBmDBViewSchedule 和 OdBmPanelScheduleView 元素中收集计算的单元格数据。

常规表具有一个架构,该架构定义了表中显示的元素的字段集和条件。这些字段可以是简单的(表示参数 ID)或可计算的(公式)。TableData 对象是根据表架构构建的。

OdBmDBViewSchedule 元素具有内部元数据,用于构建其单元格中的值。计划架构中定义的字段将表格单元格链接到字段指定的相应参数。这些参数与使用基于计划类别或类别和计划筛选规则的条件收集的元素相关。

OdBmPanelScheduleView 元素没有字段和模式,但面板明细表模板的每个单元格都有一个对应的参数。

简单的值检索过程包括两个步骤:

  1. 在从表中提取单元格值之前,您应该使用 OdBmDBViewSchedule 类的 OdBmDBViewSchedule::refreshData() 方法和 OdBmPanelScheduleView 类的 OdBmPanelScheduleView::refreshData() 方法重新计算它们。
  2. 要检索所需表部分的计算值,OdBmPanelScheduleView 和 OdBmDBViewSchedule 类会覆盖其祖先 OdBmTableView 的 getCellText() 方法:OdBmDBViewSchedule::getCellText() 和 OdBmPanelScheduleView::getCellText()。

表格单元格也可以与其相邻单元格合并,并且可以使用 getCellMergedArea() 方法检索有关合并区域的信息。要获取表格单元格样式,请使用 getTableCellStyle() 方法。OdBmTableCellStyle 元素具有与单元格文本字体相关的属性,可以通过以下示例中使用的方法进行访问。

示例

此示例展示了 BimRv/Examples/TB_Commands/Dump/TableDump 中示例的 OdBmTableViewToHTMLHelper 类的实现。

  1. 定义并实现稍后将使用的实用方法。
    // Gets the HTML string representing this horizontal alignment.
    static OdString getAlignString(OdBm::HorizontalAlignmentStyle::Enum hAlignment) {
      switch (hAlignment)
      {
      case OdBm::HorizontalAlignmentStyle::Left:
        return L" align=\"left\"";
      case OdBm::HorizontalAlignmentStyle::Center:
        return L" align=\"center\"";
      case OdBm::HorizontalAlignmentStyle::Right:
        return " align=\"right\"";
      }
      return OdString::kEmpty;
    }
    static OdString colorToHtml(OdBmCmColor color) {
      return OdString().format(L"\"#%2.2X%2.2X%2.2X\"", color.red(), color.green(), color.blue());
    }
    static bool isWhiteColor(const OdBmCmColor& color) {
      return color.red() == 255 && color.green() == 255 && color.blue() == 255;
    }
    OdBmTableViewToHTMLHelper::OdBmTableViewToHTMLHelper(const OdBmTableViewPtr& pTableView)
      : m_pTableView(pTableView) {
    }
    void replaceIllegalCharacters(OdString& sText) {
      sText.replace(L"&", L"&");
      sText.replace(L"<", L"<");
      sText.replace(L">", L">");
      sText.replace(L"\"", L""");
      sText.replace(L"'", L"'");
      if (sText.isEmpty())
        sText = L" ";
    }
  2. 定义类和必要的方法。
    class OdBmTableViewToHTMLHelper
    {
    public:
      /** \details
        Default constructor of OdBmTableViewToHTMLHelper class.
      */
      OdBmTableViewToHTMLHelper(const OdBmTableViewPtr&);
      /** \details
        Exports the table to the HTML file.
        \returns String representing the result of the export
      */
      OdString exportTable() const;
    private:
      OdString exportCell(OdBm::SectionType::Enum sectionId, OdUInt32 row, OdUInt32 col) const;
      OdString exportSection(OdBm::SectionType::Enum) const;
      OdBmTableViewPtr m_pTableView;
      mutable std::set<std::pair<OdUInt32, OdUInt32>> writtenCells;
    };

    构造函数接受指向 OdBmTableView 对象及其后代的智能指针。exportTable() 方法只是在表视图(标题、正文、摘要和页脚)中的所有可能部分上运行 exportSection(OdBm::SectionType::Enum)。

  3. 实现 exportSection() 方法。
    OdString OdBmTableViewToHTMLHelper::exportSection(OdBm::SectionType::Enum sectionId) const {
      OdBmTableSectionDataPtr pSection = m_pTableView->getData()->getSectionDataItem(sectionId);
      if (pSection.isNull())
        return OdString::kEmpty;
      OdString sSectionLeader = L"<table border=\"1\">\n    ";
      OdString sSectionTrailer = L"\n  </table>";
      OdString sRowLeader = L"<tr>\n";
      OdString sRowTrailer = L"\n    </tr>";
      OdString sSection;
      for (OdUInt32 row = 0; row < pSection->getNumberOfRows(); row++) {
        OdString sSectionRow = sRowLeader+L"      ";
        for (OdUInt32 col = pSection->getFirstColumnNumber(); col < pSection->getNumberOfColumns(); col++) {
          if (std::find(writtenCells.begin(), writtenCells.end(), std::make_pair(row, col)) != writtenCells.end())
            continue;
          sSectionRow += exportCell(sectionId, row, col);
        }
        sSection += sSectionRow + sRowTrailer;
      }
      writtenCells.clear();
      return sSectionLeader + sSection + sSectionTrailer;
    }
    
    
  4. 实现 exportCell() 方法。
    
    OdString OdBmTableViewToHTMLHelper::exportCell(OdBm::SectionType::Enum sectionId, OdUInt32 row, OdUInt32 col) const {
      OdString sCellLeader = L"<td";
      OdString sCellTrailer = L"</td>";
      OdString sCellLeaderTrailer = L">";
      OdString sText = m_pTableView->getCellText(sectionId, row, col);
      replaceIllegalCharacters(sText);
      OdBmTableSectionDataPtr pSection = m_pTableView->getData()->getSectionDataItem(sectionId);
      OdBmTableCellStylePtr pCellStyle;
      OdBmTableCellMergedArea mergedArea;
      OdString sCellStyle;
      OdString sColSpan;
      OdString sRowSpan;
      OdString sFontStyleLeader;
      OdString sFontStyleTrailer;
      if (eOk == pSection->getCellMergedArea(row, col, mergedArea)) {
        // 如果合并单元格跨多列
        if (mergedArea.getLeft() != mergedArea.getRight()) {
          sColSpan = OdString().format(L" colspan=\"%d\" ", mergedArea.getRight() - mergedArea.getLeft() + 1);
        }
        // 如果合并单元格跨多行
        if (mergedArea.getTop() != mergedArea.getBottom()) {
          sColSpan = OdString().format(L" rowspan=\"%d\" ", mergedArea.getBottom() - mergedArea.getTop() + 1);
        }
        // 记住所有与合并相关的已写入单元格
        for (OdUInt32 iMergedRow = mergedArea.getTop(); iMergedRow <= mergedArea.getBottom(); iMergedRow++) {
          for (OdUInt32 iMergedCol = mergedArea.getLeft(); iMergedCol <= mergedArea.getRight(); iMergedCol++) {
            writtenCells.insert(std::make_pair(iMergedRow, iMergedCol));
          }
        }
      }
      if (eOk == pSection->getTableCellStyle(row, col, pCellStyle)) {
        // 水平对齐
        sCellStyle += getAlignString(pCellStyle->getHorizontalAlignment());
        // 为即将到来的单元格写入格式属性
        // 背景颜色
        if (!isWhiteColor(pCellStyle->getBackgroundColor())) {
          sCellStyle += L" bgcolor=" + colorToHtml(pCellStyle->getBackgroundColor());
        }
        // 为单元格写入子标签
        // 下划线
        if (pCellStyle->isFontUnderline()) {
          sFontStyleLeader += L"<u>";
          sFontStyleTrailer += L"</u>";
        }
        // 斜体
        if (pCellStyle->isFontItalic()) {
          sFontStyleLeader += L"<i>";
          sFontStyleTrailer += L"</i>";
        }
        // 粗体
        if (pCellStyle->isFontBold()) {
          sFontStyleLeader += L"<b>";
          sFontStyleTrailer += L"</b>";
        }
      }
      return sCellLeader + sColSpan + sRowSpan + sCellStyle + sCellLeaderTrailer + sFontStyleLeader + sText + sFontStyleTrailer + sCellTrailer;
    }

今天就开始行动

免费试用 ODA 软件 60 天。
无风险,无需信用卡。

免费试用