修改命名空间
This commit is contained in:
143
ShrlAlgoToolkit.RevitCore/Extensions/ScheduleExtensions.cs
Normal file
143
ShrlAlgoToolkit.RevitCore/Extensions/ScheduleExtensions.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
using Autodesk.Revit.DB;
|
||||
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitCore.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 明细表转换类
|
||||
/// </summary>
|
||||
public static class ScheduleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 明细表转换为datable
|
||||
/// </summary>
|
||||
/// <param name="colNames"></param>
|
||||
/// <returns></returns>
|
||||
public static DataTable ToDataTable(this ViewSchedule view, params string[] colNames)
|
||||
{
|
||||
if (view is null)
|
||||
throw new ArgumentNullException(nameof(view));
|
||||
|
||||
if (view is null)
|
||||
throw new NullReferenceException(nameof(view));
|
||||
|
||||
var table = view.GetTableData();
|
||||
|
||||
var body = table.GetSectionData(SectionType.Body);
|
||||
|
||||
var colNum = body.NumberOfColumns;
|
||||
|
||||
var rowNum = body.NumberOfRows;
|
||||
|
||||
var result = new DataTable { TableName = view.GetCellText(SectionType.Header, 0, 0) };
|
||||
|
||||
var colIndexs = new List<int>();
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
for (var j = 0; j < colNum; j++)
|
||||
{
|
||||
if (colIndexs.Contains(j))
|
||||
continue;
|
||||
|
||||
var cellText = view.GetCellText(SectionType.Body, i, j);
|
||||
|
||||
if (colNames.Contains(cellText))
|
||||
{
|
||||
colIndexs.Add(j);
|
||||
|
||||
result.Columns.Add(cellText);
|
||||
}
|
||||
|
||||
if (result.Columns.Count == colNames.Length)
|
||||
break;
|
||||
}
|
||||
|
||||
if (result.Columns.Count == colNames.Length)
|
||||
break;
|
||||
}
|
||||
|
||||
for (var i = 0; i < rowNum; i++)
|
||||
{
|
||||
var row = result.NewRow();
|
||||
|
||||
for (var j = 0; j < colIndexs.Count; j++)
|
||||
row[j] = view.GetCellText(SectionType.Body, i, colIndexs[j]);
|
||||
|
||||
result.Rows.Add(row);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 明细表转换为datable
|
||||
/// </summary>
|
||||
/// <param name="view"></param>
|
||||
/// <returns></returns>
|
||||
public static DataTable ToDataTable(this ViewSchedule view)
|
||||
{
|
||||
if (view is null)
|
||||
throw new ArgumentNullException(nameof(view));
|
||||
|
||||
var table = view.GetTableData();
|
||||
|
||||
var body = table.GetSectionData(SectionType.Body);
|
||||
|
||||
var colNum = body.NumberOfColumns;
|
||||
|
||||
var rowNum = body.NumberOfRows;
|
||||
|
||||
var result = new DataTable { TableName = view.GetCellText(SectionType.Header, 0, 0) };
|
||||
|
||||
for (var i = 0; i < colNum; i++)
|
||||
result.Columns.Add();
|
||||
|
||||
for (var i = 0; i < rowNum; i++)
|
||||
{
|
||||
var row = result.NewRow();
|
||||
|
||||
for (var j = 0; j < colNum; j++)
|
||||
row[j] = view.GetCellText(SectionType.Body, i, j);
|
||||
|
||||
result.Rows.Add(row);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 明细表转DataTable
|
||||
/// </summary>
|
||||
/// <param name="viewSchedule"></param>
|
||||
/// <returns></returns>
|
||||
private static DataTable ViewScheduleToDataTable(this ViewSchedule viewSchedule)
|
||||
{
|
||||
var dt = new DataTable();
|
||||
|
||||
var rowCount = viewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfRows;
|
||||
var colCount = viewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfColumns;
|
||||
var headers = viewSchedule.GetTableData().GetSectionData(SectionType.Header);
|
||||
for (var i = 0; i < colCount; i++)
|
||||
{
|
||||
var header = viewSchedule.GetCellText(SectionType.Header, 0, i);
|
||||
dt.Columns.Add(header);
|
||||
}
|
||||
|
||||
for (var i = 0; i < rowCount; i++)
|
||||
{
|
||||
var dr = dt.NewRow();
|
||||
for (var j = 0; j < colCount; j++)
|
||||
{
|
||||
dr[j] = viewSchedule.GetCellText(SectionType.Body, i, j);
|
||||
}
|
||||
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user