Message 1 of 10
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Running into an issue where the txt file is unable to be accessed but i can still push things to the folder. I have admin rights on everything and pushed admin rights to the application. Not sure if it is code related.
JtWindowHandle:
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace ScheduleData
{
/// <summary>
/// Wrapper class for converting
/// IntPtr to IWin32Window.
/// </summary>
public class JtWindowHandle : IWin32Window
{
IntPtr _hwnd;
public JtWindowHandle(IntPtr h)
{
Debug.Assert(IntPtr.Zero != h,
"expected non-null window handle");
_hwnd = h;
}
public IntPtr Handle
{
get
{
return _hwnd;
}
}
}
}
Command:
#region Namespaces
using System.IO;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Autodesk;
using Autodesk.Revit;
using Autodesk.Revit.DB.Structure;
using TpMechanical.TpMechanical.Forms;
using System.Linq.Expressions;
#endregion
namespace ScheduleData
{
[Transaction(TransactionMode.ReadOnly)]
public class ScheduleDataCommand : IExternalCommand
{
/// <summary>
/// Define the schedule export folder.
/// All existing files will be overwritten.
/// </summary>
const string _export_folder_name
= "C:\\tmp\\schedule_data";
/// <summary>
/// Schedule export filename extension.
/// </summary>
const string _ext = ".txt";
const string _caption_prefix
= "Schedule Data - ";
/// <summary>
/// Select a text file in the gioven folder.
/// </summary>
/// <param name="folder">Initial folder.</param>
/// <param name="filename">Selected filename on success.</param>
/// <returns>Return true if a file was successfully selected.</returns>
static bool FileSelect(
string folder,
out string filename)
{
folder = Path.GetDirectoryName(folder);
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Select Schedule Data File";
dlg.CheckFileExists = true;
dlg.CheckPathExists = true;
//dlg.RestoreDirectory = true;
dlg.InitialDirectory = folder;
dlg.Filter = ".txt Files (*.txt)|*.txt";
bool rc = (DialogResult.OK == dlg.ShowDialog());
filename = dlg.FileName;
return rc;
}
void DisplayScheduleData(
string filename,
IWin32Window owner)
{
ScheduleDataParser parser
= new ScheduleDataParser(filename);
System.Windows.Forms.Form form
= new System.Windows.Forms.Form();
form.Size = new Size(400, 400);
form.Text = _caption_prefix + parser.Name;
DataGridView dg = new DataGridView();
dg.AllowUserToAddRows = false;
dg.AllowUserToDeleteRows = false;
dg.AllowUserToOrderColumns = true;
dg.Dock = System.Windows.Forms.DockStyle.Fill;
dg.Location = new System.Drawing.Point(0, 0);
dg.ReadOnly = true;
dg.TabIndex = 0;
dg.DataSource = parser.Table;
dg.Parent = form;
dg.IsAccessible= true;
form.ShowDialog(owner);
}
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
IWin32Window revit_window
= new JtWindowHandle(
Process.GetCurrentProcess().MainWindowHandle);
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
FilteredElementCollector col
= new FilteredElementCollector(doc)
.OfClass(typeof(ViewSchedule));
ViewScheduleExportOptions opt
= new ViewScheduleExportOptions();
foreach (ViewSchedule vs in col)
{
Directory.CreateDirectory(
_export_folder_name);
vs.Export(_export_folder_name,
vs.Name + _ext, opt);
}
string filename;
while (FileSelect(_export_folder_name,
out filename))
{
DisplayScheduleData(filename,
revit_window);
}
return Result.Succeeded;
}
}
}
DataParse:
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
namespace ScheduleData
{
class ScheduleDataParser
{
/// <summary>
/// Default schedule data file field delimiter.
/// </summary>
static char[] _tabs = new char[] { '\t' };
/// <summary>
/// Strip the quotes around text strings
/// in the schedule data file.
/// </summary>
static char[] _quotes = new char[] { '"' };
string _name = null;
DataTable _table = null;
/// <summary>
/// Schedule name
/// </summary>
public string Name
{
get { return _name; }
}
/// <summary>
/// Schedule columns and row data
/// </summary>
public DataTable Table
{
get { return _table; }
}
public ScheduleDataParser(string filename)
{
StreamReader stream = File.OpenText(filename);
string line;
string[] a;
while (null != (line = stream.ReadLine()))
{
a = line
.Split(_tabs)
.Select<string, string>(s => s.Trim(_quotes))
.ToArray();
// First line of text file contains
// schedule name
if (null == _name)
{
_name = a[0];
continue;
}
// Second line of text file contains
// schedule column names
if (null == _table)
{
_table = new DataTable();
foreach (string column_name in a)
{
DataColumn column = new DataColumn();
column.DataType = typeof(string);
column.ColumnName = column_name;
_table.Columns.Add(column);
}
_table.BeginLoadData();
continue;
}
// Remaining lines define schedula data
DataRow dr = _table.LoadDataRow(a, true);
}
_table.EndLoadData();
}
}
}
Solved! Go to Solution.