123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- 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
- {
- /// <summary>
- /// 绘图上下文
- /// </summary>
- /// <param name="view">绘制发生的view</param>
- public RevitTableDrawContext(View view)
- {
- ActiveView = view;
- }
- public View ActiveView { get; set; }
- /*
- * 绘图上下文,表格绘制提供相关功能
- */
- /// <summary>
- /// 计算原始的定位点
- /// </summary>
- /// <param name="table"></param>
- 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<Modules.Tables.Line> DrawPreview(TTable table)
- {
- List<Modules.Tables.Line> lines = new List<Modules.Tables.Line>();
- 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<Modules.Tables.Line> GetBorders(TCell cell)
- {
- List<Modules.Tables.Line> curves = new List<Modules.Tables.Line>();
- 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 };
- }
- }
- }
|