この記事では、OdBmDBViewScheduleおよびOdBmPanelScheduleView要素から計算されたセルデータを収集する方法について説明します。
通常のテーブルには、テーブルに表示される要素のフィールドと条件のセットを定義するスキーマがあります。これらのフィールドは単純なもの(パラメータIDを表す)または計算可能なもの(数式)です。TableDataオブジェクトは、テーブルスキーマに基づいて構築されます。
OdBmDBViewSchedule要素には、そのセルの値を構築するために使用される内部メタデータがあります。スケジュールスキーマで定義されたフィールドは、テーブルセルをフィールドで指定されたそれぞれのパラメータにリンクします。これらのパラメータは、スケジュールカテゴリまたはカテゴリとスケジュールフィルター規則に基づく条件を使用して収集された要素に関連しています。
OdBmPanelScheduleView要素にはフィールドとスキーマはありませんが、パネルスケジュールテンプレートの各セルには対応するパラメータがあります。
値の取得の簡単なプロセスは、次の2つのステップで構成されます。
- テーブルからセル値を抽出する前に、OdBmDBViewScheduleクラスの場合はOdBmDBViewSchedule::refreshData()メソッドを、OdBmPanelScheduleViewクラスの場合はOdBmPanelScheduleView::refreshData()メソッドを使用して、それらを再計算する必要があります。
- 目的のテーブルセクションの計算値を取得するには、OdBmPanelScheduleViewクラスとOdBmDBViewScheduleクラスは、それらの祖先であるOdBmTableViewのgetCellText()メソッドをオーバーライドします: OdBmDBViewSchedule::getCellText()とOdBmPanelScheduleView::getCellText()。
テーブルセルは、隣接するセルと結合することもでき、結合された領域に関する情報はgetCellMergedArea()メソッドを使用して取得できます。テーブルセルスタイルを取得するには、getTableCellStyle()メソッドを使用します。OdBmTableCellStyle要素には、セルテキストフォントに関連するプロパティがあり、以下の例で使用されているメソッドによってアクセスできます。
例
この例は、BimRv/Examples/TB_Commands/Dump/TableDumpにあるサンプルからのOdBmTableViewToHTMLHelperクラスの実装を示しています。
- 後で使用するユーティリティメソッドを定義し、実装します。
// 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" "; } - クラスと必要なメソッドを定義します。
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) を実行するだけです。
- 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; } - 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 merged cell spans multiple columns if (mergedArea.getLeft() != mergedArea.getRight()) { sColSpan = OdString().format(L" colspan=\"%d\" ", mergedArea.getRight() - mergedArea.getLeft() + 1); } // If merged cell spans multiple rows if (mergedArea.getTop() != mergedArea.getBottom()) { sColSpan = OdString().format(L" rowspan=\"%d\" ", mergedArea.getBottom() - mergedArea.getTop() + 1); } // Remember all written cells related to the merge 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)) { // Horizontal alignment sCellStyle += getAlignString(pCellStyle->getHorizontalAlignment()); // Write formatting attributes for the upcoming cell // Background color if (!isWhiteColor(pCellStyle->getBackgroundColor())) { sCellStyle += L" bgcolor=" + colorToHtml(pCellStyle->getBackgroundColor()); } // Write subtags for the cell // Underline if (pCellStyle->isFontUnderline()) { sFontStyleLeader += L"<u>"; sFontStyleTrailer += L"</u>"; } //Italic if (pCellStyle->isFontItalic()) { sFontStyleLeader += L"<i>"; sFontStyleTrailer += L"</i>"; } //Bold if (pCellStyle->isFontBold()) { sFontStyleLeader += L"<b>"; sFontStyleTrailer += L"</b> } } return sCellLeader + sColSpan + sRowSpan + sCellStyle + sCellLeaderTrailer + sFontStyleLeader + sText + sFontStyleTrailer + sCellTrailer; }