//
// (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.IO;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
namespace Revit.SDK.Samples.PanelSchedule.CS
{
///
/// Translate the panel schedule view data from Revit to CSV.
///
class CSVTranslator : Translator
{
///
/// create a CSVTranslator instance for a PanelScheduleView instance.
///
/// the exporting panel schedule view instance.
public CSVTranslator(PanelScheduleView psView)
{
m_psView = psView;
}
///
/// export to a CSV file that contains the PanelScheduleView instance data.
///
/// the exported file path
public override string Export()
{
string asemblyName = System.Reflection.Assembly.GetExecutingAssembly().Location;
string panelScheduleCSVFile = asemblyName.Replace("PanelSchedule.dll", ReplaceIllegalCharacters(m_psView.Name) + ".csv");
if (File.Exists(panelScheduleCSVFile))
{
File.Delete(panelScheduleCSVFile);
}
using (StreamWriter sw = File.CreateText(panelScheduleCSVFile))
{
//sw.WriteLine("This is my file.");
DumpPanelScheduleData(sw);
sw.Close();
}
return panelScheduleCSVFile;
}
///
/// dump PanelScheduleData to comma delimited.
///
///
private void DumpPanelScheduleData(StreamWriter sw)
{
DumpSectionData(sw, m_psView, SectionType.Header);
DumpSectionData(sw, m_psView, SectionType.Body);
DumpSectionData(sw, m_psView, SectionType.Summary);
DumpSectionData(sw, m_psView, SectionType.Footer);
}
///
/// dump SectionData to comma delimited.
///
/// exporting file stream
/// the PanelScheduleView instance is exporting.
/// which section is exporting, it can be Header, Body, Summary or Footer.
private void DumpSectionData(StreamWriter sw, 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)
{
StringBuilder oneRow = new StringBuilder();
for (int jj = 0; jj < nCols_Section; ++jj)
{
try
{
oneRow.AppendFormat("{0},", m_psView.GetCellText(sectionType, ii, jj));
}
catch (Exception)
{
// do nothing.
}
}
sw.WriteLine(oneRow.ToString());
}
}
}
}