RevitTableDrawContext.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Autodesk.Revit.DB;
  2. using FWindSoft.Modules.Tables;
  3. using FWindSoft.Revit.Extension;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FWindSoft.Revit.RevitTable
  10. {
  11. public class RevitTableDrawContext : ITableDrawContext
  12. {
  13. /// <summary>
  14. /// 绘图上下文
  15. /// </summary>
  16. /// <param name="view">绘制发生的view</param>
  17. public RevitTableDrawContext(View view)
  18. {
  19. ActiveView = view;
  20. }
  21. public View ActiveView { get; set; }
  22. /*
  23. * 绘图上下文,表格绘制提供相关功能
  24. */
  25. /// <summary>
  26. /// 计算原始的定位点
  27. /// </summary>
  28. /// <param name="table"></param>
  29. public void CalcOriginLocation(TTable table)
  30. {
  31. int rowCount = table.Rows.Count;
  32. int columnCount = table.Columns.Count;
  33. double tempTop = 0;
  34. for (int i = 0; i < rowCount; i++)
  35. {
  36. if (i != 0)
  37. {
  38. tempTop = tempTop + table.Rows[i - 1].Height;
  39. }
  40. double tempLeft = 0;
  41. for (int j = 0; j < columnCount; j++)
  42. {
  43. if (j != 0)
  44. {
  45. tempLeft = tempLeft + table.Columns[j - 1].Width;
  46. }
  47. var tempCell = table.GetCell(i,j);
  48. if (tempCell != null)
  49. {
  50. tempCell.Top = tempTop;
  51. tempCell.Left = tempLeft;
  52. }
  53. }
  54. }
  55. }
  56. public void Draw(Modules.Tables.Point location, TTable table)
  57. {
  58. throw new NotImplementedException();
  59. }
  60. public void DrawCellValue(TCell cell)
  61. {
  62. var value = cell.Value;
  63. if (value == null)
  64. return;
  65. switch(cell.ContextType)
  66. {
  67. case CellContentType.Text:
  68. {
  69. #region 创建textNode,并配置对齐方式
  70. #endregion
  71. break;
  72. }
  73. case CellContentType.Image:
  74. {
  75. break;
  76. }
  77. case CellContentType.Custom:
  78. {
  79. break;
  80. }
  81. }
  82. //do
  83. //{
  84. // #region 标签数据
  85. // TextNote textNote = Value as TextNote;
  86. // XYZ tempOrigion = GetLocation();
  87. // if (textNote != null)
  88. // {
  89. // View tempView = ExternalDataWrapper.Current.Doc.GetElement(textNote.OwnerViewId) as View;
  90. // if (tempView == null)
  91. // {
  92. // return;
  93. // }
  94. // #region 处理标签宽度
  95. // double scale = tempView.Scale;
  96. // textNote.Width = Width.ToApi() / scale;
  97. // #endregion
  98. // //标签宽度,与表格同宽
  99. // #region 处理水平对齐偏移
  100. // double tempOffsetWidth = Width.ToApi() / 2;
  101. // int hashcode = textNote.GetParameterInteger(BuiltInParameter.TEXT_ALIGN_HORZ);
  102. // if (hashcode == TextAlignFlags.TEF_ALIGN_LEFT.GetHashCode())
  103. // {
  104. // tempOffsetWidth = 0;
  105. // }
  106. // else if (hashcode == TextAlignFlags.TEF_ALIGN_RIGHT.GetHashCode())
  107. // {
  108. // tempOffsetWidth = Width.ToApi();
  109. // }
  110. // XYZ result = tempOrigion.OffsetPoint(XYZ.BasisX, tempOffsetWidth);
  111. // #endregion
  112. // BoundingBoxXYZ bb = textNote.GetBoundingBoxExt(tempView, false);
  113. // if (bb != null)
  114. // {
  115. // double textHeight = bb.Max.Y - bb.Min.Y;
  116. // double offset = (Height.ToApi() - textHeight) / 2;
  117. // result = result.OffsetPoint(-XYZ.BasisY, offset);
  118. // }
  119. // textNote.Document.MoveElementExt(textNote.Id, result.Subtract(textNote.Coord));
  120. // break;
  121. // }
  122. // #endregion
  123. // #region 图片数据
  124. // var tempElement = Value as Element;
  125. // if (tempElement != null &&
  126. // tempElement.Category.GetBuiltInCategoryExt() == BuiltInCategory.OST_RasterImages)
  127. // {
  128. // View tempView = ExternalDataWrapper.Current.Doc.GetElement(tempElement.OwnerViewId) as View;
  129. // if (tempView == null)
  130. // {
  131. // return;
  132. // }
  133. // BoundingBoxXYZ bb = tempElement.GetBoundingBoxExt(tempView, false);
  134. // XYZ old = XYZ.Zero;
  135. // XYZ newLocation = tempOrigion.OffsetPoint(XYZ.BasisX, Width.ToApi() / 2)
  136. // .OffsetPoint(-XYZ.BasisY, Height.ToApi() / 2);
  137. // #endregion
  138. // if (bb != null)
  139. // {
  140. // double width = (bb.Max.X - bb.Min.X);
  141. // #region 计算对齐点,-1左偏,1右偏
  142. // int alignFlag = 0;
  143. // if (Format != null && int.TryParse(Format.ToString(), out alignFlag))
  144. // {
  145. // }
  146. // if (alignFlag != 0)
  147. // {
  148. // var offsetDirection = alignFlag * XYZ.BasisX;
  149. // double offsetValue = (Width.ToApi() - width) / 2;
  150. // newLocation = newLocation.OffsetPoint(offsetDirection, Math.Abs(offsetValue));
  151. // }
  152. // //左上角计算
  153. // //double textHeight = bb.Max.Y - bb.Min.Y;
  154. // //old = new XYZ(bb.Min.X, bb.Max.Y, tempOrigion.Z);
  155. // //图片的定位点是中心点
  156. // old = (bb.Max + bb.Min) / 2;
  157. // }
  158. // tempElement.Document.MoveElementExt(tempElement.Id, newLocation.Subtract(old));
  159. // break;
  160. // }
  161. // #endregion
  162. //} while (false);
  163. }
  164. public List<Modules.Tables.Line> DrawPreview(TTable table)
  165. {
  166. List<Modules.Tables.Line> lines = new List<Modules.Tables.Line>();
  167. CalcOriginLocation(table);
  168. int rowCount = table.Rows.Count;
  169. int columnCount = table.Columns.Count;
  170. #region 表格线框
  171. for (int i = 0; i < rowCount; i++)
  172. {
  173. for (int j = 0; j < columnCount; j++)
  174. {
  175. var tempCell = table.GetCell(i, j);
  176. if (tempCell == null)
  177. continue;
  178. int baseRow = i, baseColumn = j,lastRow = i, lastColumn = j;
  179. var range = tempCell.CellRange;
  180. if(range!=null)
  181. {
  182. baseRow = range.StartRowIndex;
  183. baseColumn = range.StartColumnIndex;
  184. lastRow = range.EndRowIndex;
  185. lastColumn = range.EndColumnIndex;
  186. }
  187. if (baseRow == i && baseColumn == j)
  188. {
  189. var curves = GetBorders(tempCell);
  190. lines.Add(curves[0]);
  191. lines.Add(curves[1]);
  192. if (lastColumn == columnCount - 1)
  193. {
  194. lines.Add(curves[2]);
  195. }
  196. if (lastRow == rowCount - 1)
  197. {
  198. lines.Add(curves[3]);
  199. }
  200. }
  201. }
  202. }
  203. #endregion
  204. return lines;
  205. }
  206. public List<Modules.Tables.Line> GetBorders(TCell cell)
  207. {
  208. List<Modules.Tables.Line> curves = new List<Modules.Tables.Line>();
  209. double width = cell.Width.MmToFt(), height = cell.Height.MmToFt();
  210. XYZ tempBase = new XYZ(cell.Left.MmToFt(), cell.Top.MmToFt(), 0);
  211. XYZ leftBottom = tempBase.Offset(-XYZ.BasisY* height);
  212. XYZ rightTop = tempBase.Offset(XYZ.BasisX* width);
  213. XYZ rightBottom = rightTop.Offset(-XYZ.BasisY * height);
  214. curves.Add(new Modules.Tables.Line(ConverterToPoint(leftBottom), ConverterToPoint(tempBase)));
  215. curves.Add(new Modules.Tables.Line(ConverterToPoint(tempBase), ConverterToPoint(rightTop)));
  216. curves.Add(new Modules.Tables.Line(ConverterToPoint(rightTop), ConverterToPoint(rightBottom)));
  217. curves.Add(new Modules.Tables.Line(ConverterToPoint(rightBottom), ConverterToPoint(leftBottom)));
  218. return curves;
  219. }
  220. private Modules.Tables.Point ConverterToPoint(XYZ xyz)
  221. {
  222. return new Modules.Tables.Point() { X = xyz.X, Y = xyz.Y };
  223. }
  224. }
  225. }