// // (C) Copyright 2003-2019 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Xml; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Electrical; using Autodesk.Revit.UI; namespace Revit.SDK.Samples.PanelSchedule.CS { /// /// Translate the panel schedule view data from Revit to HTML table. /// class HTMLTranslator : Translator { /// /// create a Translator instance for a PanelScheduleView instance. /// /// the exporting panel schedule view instance. public HTMLTranslator(PanelScheduleView psView) { m_psView = psView; } /// /// export to a HTML page that contains the PanelScheduleView instance data. /// /// the exported file path public override string Export() { string asemblyName = System.Reflection.Assembly.GetExecutingAssembly().Location; string tempFile = asemblyName.Replace("PanelSchedule.dll", "template.html"); if (!System.IO.File.Exists(tempFile)) { TaskDialog messageDlg = new TaskDialog("Warnning Message"); messageDlg.MainIcon = TaskDialogIcon.TaskDialogIconWarning; messageDlg.MainContent = "Can not find 'template.html', please make sure the 'template.html' file is in the same folder as the external command assembly."; messageDlg.Show(); return null; } string panelScheduleFile = asemblyName.Replace("PanelSchedule.dll", ReplaceIllegalCharacters(m_psView.Name) + ".html"); XmlDocument doc = new XmlDocument(); XmlTextWriter tw = new XmlTextWriter(panelScheduleFile, null); doc.Load(tempFile); XmlNode psTable = doc.DocumentElement.SelectSingleNode("//div/table[1]"); DumpPanelScheduleData(psTable, doc); doc.Save(tw); return panelScheduleFile; } /// /// dump PanelScheduleData to a 'table' node in HTML. /// /// a 'table' node in HTML. /// private void DumpPanelScheduleData(XmlNode panelScheduleDataNode, XmlDocument doc) { DumpSectionData(panelScheduleDataNode, doc, m_psView, SectionType.Header); DumpSectionData(panelScheduleDataNode, doc, m_psView, SectionType.Body); DumpSectionData(panelScheduleDataNode, doc, m_psView, SectionType.Summary); DumpSectionData(panelScheduleDataNode, doc, m_psView, SectionType.Footer); } /// /// dump SectionData to the 'tr' nodes in HTML. /// /// a 'table' node in HTML. /// HTML page /// the PanelScheduleView instance is exporting. /// which section is exporting, it can be Header, Body, Summary or Footer. private void DumpSectionData(XmlNode panelScheduleDataNode, XmlDocument doc, PanelScheduleView psView, SectionType sectionType) { int nRows_Section = 0; int nCols_Section = 0; getNumberOfRowsAndColumns(m_psView.Document, m_psView, sectionType, ref nRows_Section, ref nCols_Section); for (int ii = 0; ii < nRows_Section; ++ii) { // add a node for each row XmlElement trNode = doc.CreateElement("tr"); panelScheduleDataNode.AppendChild(trNode); for (int jj = 0; jj < nCols_Section; ++jj) { // add node for each cell XmlElement tdNode = doc.CreateElement("td"); try { tdNode.InnerText = m_psView.GetCellText(sectionType, ii, jj); } catch (Exception) { // do nothing. } trNode.AppendChild(tdNode); } } } } }