using Autodesk.Revit.DB;
using FWindSoft.Modules.Tables;
using FWindSoft.Revit.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FWindSoft.Revit.RevitTable
{
public class RevitTableDrawContext : ITableDrawContext
{
///
/// 绘图上下文
///
/// 绘制发生的view
public RevitTableDrawContext(View view)
{
ActiveView = view;
}
public View ActiveView { get; set; }
/*
* 绘图上下文,表格绘制提供相关功能
*/
///
/// 计算原始的定位点
///
///
public void CalcOriginLocation(TTable table)
{
int rowCount = table.Rows.Count;
int columnCount = table.Columns.Count;
double tempTop = 0;
for (int i = 0; i < rowCount; i++)
{
if (i != 0)
{
tempTop = tempTop + table.Rows[i - 1].Height;
}
double tempLeft = 0;
for (int j = 0; j < columnCount; j++)
{
if (j != 0)
{
tempLeft = tempLeft + table.Columns[j - 1].Width;
}
var tempCell = table.GetCell(i,j);
if (tempCell != null)
{
tempCell.Top = tempTop;
tempCell.Left = tempLeft;
}
}
}
}
public void Draw(Modules.Tables.Point location, TTable table)
{
throw new NotImplementedException();
}
public void DrawCellValue(TCell cell)
{
var value = cell.Value;
if (value == null)
return;
switch(cell.ContextType)
{
case CellContentType.Text:
{
#region 创建textNode,并配置对齐方式
#endregion
break;
}
case CellContentType.Image:
{
break;
}
case CellContentType.Custom:
{
break;
}
}
//do
//{
// #region 标签数据
// TextNote textNote = Value as TextNote;
// XYZ tempOrigion = GetLocation();
// if (textNote != null)
// {
// View tempView = ExternalDataWrapper.Current.Doc.GetElement(textNote.OwnerViewId) as View;
// if (tempView == null)
// {
// return;
// }
// #region 处理标签宽度
// double scale = tempView.Scale;
// textNote.Width = Width.ToApi() / scale;
// #endregion
// //标签宽度,与表格同宽
// #region 处理水平对齐偏移
// double tempOffsetWidth = Width.ToApi() / 2;
// int hashcode = textNote.GetParameterInteger(BuiltInParameter.TEXT_ALIGN_HORZ);
// if (hashcode == TextAlignFlags.TEF_ALIGN_LEFT.GetHashCode())
// {
// tempOffsetWidth = 0;
// }
// else if (hashcode == TextAlignFlags.TEF_ALIGN_RIGHT.GetHashCode())
// {
// tempOffsetWidth = Width.ToApi();
// }
// XYZ result = tempOrigion.OffsetPoint(XYZ.BasisX, tempOffsetWidth);
// #endregion
// BoundingBoxXYZ bb = textNote.GetBoundingBoxExt(tempView, false);
// if (bb != null)
// {
// double textHeight = bb.Max.Y - bb.Min.Y;
// double offset = (Height.ToApi() - textHeight) / 2;
// result = result.OffsetPoint(-XYZ.BasisY, offset);
// }
// textNote.Document.MoveElementExt(textNote.Id, result.Subtract(textNote.Coord));
// break;
// }
// #endregion
// #region 图片数据
// var tempElement = Value as Element;
// if (tempElement != null &&
// tempElement.Category.GetBuiltInCategoryExt() == BuiltInCategory.OST_RasterImages)
// {
// View tempView = ExternalDataWrapper.Current.Doc.GetElement(tempElement.OwnerViewId) as View;
// if (tempView == null)
// {
// return;
// }
// BoundingBoxXYZ bb = tempElement.GetBoundingBoxExt(tempView, false);
// XYZ old = XYZ.Zero;
// XYZ newLocation = tempOrigion.OffsetPoint(XYZ.BasisX, Width.ToApi() / 2)
// .OffsetPoint(-XYZ.BasisY, Height.ToApi() / 2);
// #endregion
// if (bb != null)
// {
// double width = (bb.Max.X - bb.Min.X);
// #region 计算对齐点,-1左偏,1右偏
// int alignFlag = 0;
// if (Format != null && int.TryParse(Format.ToString(), out alignFlag))
// {
// }
// if (alignFlag != 0)
// {
// var offsetDirection = alignFlag * XYZ.BasisX;
// double offsetValue = (Width.ToApi() - width) / 2;
// newLocation = newLocation.OffsetPoint(offsetDirection, Math.Abs(offsetValue));
// }
// //左上角计算
// //double textHeight = bb.Max.Y - bb.Min.Y;
// //old = new XYZ(bb.Min.X, bb.Max.Y, tempOrigion.Z);
// //图片的定位点是中心点
// old = (bb.Max + bb.Min) / 2;
// }
// tempElement.Document.MoveElementExt(tempElement.Id, newLocation.Subtract(old));
// break;
// }
// #endregion
//} while (false);
}
public List DrawPreview(TTable table)
{
List lines = new List();
CalcOriginLocation(table);
int rowCount = table.Rows.Count;
int columnCount = table.Columns.Count;
#region 表格线框
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
var tempCell = table.GetCell(i, j);
if (tempCell == null)
continue;
int baseRow = i, baseColumn = j,lastRow = i, lastColumn = j;
var range = tempCell.CellRange;
if(range!=null)
{
baseRow = range.StartRowIndex;
baseColumn = range.StartColumnIndex;
lastRow = range.EndRowIndex;
lastColumn = range.EndColumnIndex;
}
if (baseRow == i && baseColumn == j)
{
var curves = GetBorders(tempCell);
lines.Add(curves[0]);
lines.Add(curves[1]);
if (lastColumn == columnCount - 1)
{
lines.Add(curves[2]);
}
if (lastRow == rowCount - 1)
{
lines.Add(curves[3]);
}
}
}
}
#endregion
return lines;
}
public List GetBorders(TCell cell)
{
List curves = new List();
double width = cell.Width.MmToFt(), height = cell.Height.MmToFt();
XYZ tempBase = new XYZ(cell.Left.MmToFt(), cell.Top.MmToFt(), 0);
XYZ leftBottom = tempBase.Offset(-XYZ.BasisY* height);
XYZ rightTop = tempBase.Offset(XYZ.BasisX* width);
XYZ rightBottom = rightTop.Offset(-XYZ.BasisY * height);
curves.Add(new Modules.Tables.Line(ConverterToPoint(leftBottom), ConverterToPoint(tempBase)));
curves.Add(new Modules.Tables.Line(ConverterToPoint(tempBase), ConverterToPoint(rightTop)));
curves.Add(new Modules.Tables.Line(ConverterToPoint(rightTop), ConverterToPoint(rightBottom)));
curves.Add(new Modules.Tables.Line(ConverterToPoint(rightBottom), ConverterToPoint(leftBottom)));
return curves;
}
private Modules.Tables.Point ConverterToPoint(XYZ xyz)
{
return new Modules.Tables.Point() { X = xyz.X, Y = xyz.Y };
}
}
}