RevitTableManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //Copyright (c) 2015, 北京探索者软件公司
  3. //All rights reserved.
  4. //文件名称: RevitTableManager.cs
  5. //文件描述: Revit表格管理
  6. //创 建 者: xls
  7. //创建日期: 2016-10-13
  8. //版 本 号:1.0.0.0
  9. ////////////////////////////////////////////////////////////////////////////////
  10. using System;
  11. using System.Collections.Generic;
  12. using Autodesk.Revit.DB;
  13. namespace FWindSoft.Revit.Table
  14. {
  15. public class RevitTableManager
  16. {
  17. }
  18. #region 基础信息维护
  19. /// <summary>
  20. /// Revit表格
  21. /// </summary>
  22. public class RevitTalbe
  23. {
  24. private RevitCell[,] m_Cells;
  25. private List<RevitColumn> m_Columns;
  26. private List<RevitRow> m_Rows;
  27. public RevitTalbe(int rowCount,int columnCount)
  28. {
  29. #region 初始化表格 column,row
  30. m_Columns=new List<RevitColumn>();
  31. m_Rows=new List<RevitRow>();
  32. for (int i = 0; i < rowCount; i++)
  33. {
  34. m_Rows.Add(new RevitRow());
  35. }
  36. for (int i = 0; i < columnCount; i++)
  37. {
  38. m_Columns.Add(new RevitColumn());
  39. }
  40. #endregion
  41. #region 初始化表格 cell
  42. m_Cells = new RevitCell[rowCount, columnCount];
  43. double tempTop = 0;
  44. for (int i = 0; i < rowCount; i++)
  45. {
  46. if (i != 0)
  47. {
  48. tempTop = tempTop + m_Rows[i - 1].Height;
  49. }
  50. double tempLeft = 0;
  51. for (int j = 0; j < columnCount; j++)
  52. {
  53. if (j != 0)
  54. {
  55. tempLeft = tempLeft + m_Columns[j - 1].Width;
  56. }
  57. m_Cells[i, j] = new RevitCell(this) { BaseColumnIndex = j, BaseRowIndex = i, Top = tempTop, Left = tempTop };
  58. }
  59. }
  60. #endregion
  61. }
  62. #region 属性维护
  63. /// <summary>
  64. /// 列集合
  65. /// </summary>
  66. public List<RevitColumn> Columns { get { return m_Columns; } }
  67. /// <summary>
  68. /// 行集合
  69. /// </summary>
  70. public List<RevitRow> Rows { get { return m_Rows; } }
  71. #endregion
  72. #region 公开方法维护
  73. public void SetValue(int row, int column,object value)
  74. {
  75. m_Cells[row, column].Value = value;
  76. }
  77. public void SetValueMerge(int row1, int column1, int row2, int column2, object value)
  78. {
  79. int baseRowIndex = row1 <= row2 ? row1 : row2;
  80. int baseColumnIndex = column1 <= column2 ? column1 : column2;
  81. int rowRange = Math.Abs(row1 - row2);
  82. int columnRange = Math.Abs(column1 - column2);
  83. //暂时不修改成同一个引用对象
  84. for (int i = 0; i < rowRange+1; i++)
  85. {
  86. for (int j = 0; j < columnRange+1; j++)
  87. {
  88. var tempValue = m_Cells[baseRowIndex+i, baseColumnIndex+j];
  89. tempValue.BaseRowIndex = baseRowIndex;
  90. tempValue.BaseColumnIndex = baseColumnIndex;
  91. tempValue.Value = value;
  92. tempValue.ExtendRowRange = rowRange;
  93. tempValue.ExtendColumnRange = columnRange;
  94. }
  95. }
  96. }
  97. #endregion
  98. #region 私有方法,重新计算表格偏移量
  99. public void CalcCellsOFfset()
  100. {
  101. int rowCount = Rows.Count;
  102. int columnCount = Columns.Count;
  103. double tempTop = 0;
  104. for (int i = 0; i < rowCount; i++)
  105. {
  106. if (i != 0)
  107. {
  108. tempTop = tempTop + m_Rows[i - 1].Height;
  109. }
  110. double tempLeft = 0;
  111. for (int j = 0; j < columnCount; j++)
  112. {
  113. if (j != 0)
  114. {
  115. tempLeft = tempLeft + m_Columns[j - 1].Width;
  116. }
  117. var tempCell = m_Cells[i, j];
  118. if (tempCell != null)
  119. {
  120. tempCell.Top = tempTop;
  121. tempCell.Left = tempLeft;
  122. }
  123. }
  124. }
  125. }
  126. #endregion
  127. #region 绘制表格
  128. /// <summary>
  129. /// 表格所处位置
  130. /// </summary>
  131. public XYZ Location { get; set; }
  132. public void DrawTable(XYZ origion,View view)
  133. {
  134. CalcCellsOFfset();
  135. Location = origion;
  136. int rowCount = Rows.Count;
  137. int columnCount = Columns.Count;
  138. #region 表格线框
  139. for (int i = 0; i < rowCount; i++)
  140. {
  141. for (int j = 0; j < columnCount; j++)
  142. {
  143. var tempCell = m_Cells[i, j];
  144. if (tempCell == null)
  145. continue;
  146. if (tempCell.BaseRowIndex == i && tempCell.BaseColumnIndex == j)
  147. {
  148. var curves=tempCell.GetBorders();
  149. curves[0].NewDetailCurveExt(view);
  150. curves[1].NewDetailCurveExt(view);
  151. if ((tempCell.BaseColumnIndex + tempCell.ExtendColumnRange) == columnCount - 1)
  152. {
  153. curves[2].NewDetailCurveExt(view);
  154. }
  155. if ((tempCell.BaseRowIndex+tempCell.ExtendRowRange) == rowCount - 1)
  156. {
  157. curves[3].NewDetailCurveExt(view);
  158. }
  159. tempCell.DrawValue();
  160. }
  161. }
  162. }
  163. #endregion
  164. }
  165. #endregion
  166. }
  167. /// <summary>
  168. /// 表格列维护
  169. /// </summary>
  170. public class RevitColumn
  171. {
  172. public RevitColumn()
  173. {
  174. Width = 2000;
  175. }
  176. public RevitColumn(double width)
  177. {
  178. Width = width;
  179. }
  180. /// <summary>
  181. /// 列的宽度
  182. /// </summary>
  183. public double Width { get; set; }
  184. }
  185. /// <summary>
  186. /// 表格行维护
  187. /// </summary>
  188. public class RevitRow
  189. {
  190. public RevitRow()
  191. {
  192. Height = 2000;
  193. }
  194. public RevitRow(double height)
  195. {
  196. Height = height;
  197. }
  198. /// <summary>
  199. /// 列的高度
  200. /// </summary>
  201. public double Height { get; set; }
  202. }
  203. public class RevitCell
  204. {
  205. private RevitTalbe m_ReferenceTable;
  206. public RevitCell(RevitTalbe revitTalbe)
  207. {
  208. m_ReferenceTable = revitTalbe;
  209. }
  210. /// <summary>
  211. /// 相对于表格定位点的位置 上
  212. /// </summary>
  213. public double Top { get; set; }
  214. /// <summary>
  215. /// 相对于表格定位点的位置 左
  216. /// </summary>
  217. public double Left { get; set; }
  218. /// <summary>
  219. /// 当前cell的宽度
  220. /// </summary>
  221. public double Width
  222. {
  223. get
  224. {
  225. double tempWidth = 0;
  226. try
  227. {
  228. for (int i = 0; i < ExtendColumnRange + 1; i++)
  229. {
  230. tempWidth += m_ReferenceTable.Columns[BaseColumnIndex + i].Width;
  231. }
  232. }
  233. catch
  234. {
  235. // ignored
  236. }
  237. return tempWidth;
  238. }
  239. }
  240. /// <summary>
  241. /// 当前cell的高度
  242. /// </summary>
  243. public double Height
  244. {
  245. get
  246. {
  247. double tempHeight = 0;
  248. try
  249. {
  250. for (int i = 0; i < ExtendRowRange + 1; i++)
  251. {
  252. tempHeight += m_ReferenceTable.Rows[BaseRowIndex + i].Height;
  253. }
  254. }
  255. catch
  256. {
  257. // ignored
  258. }
  259. return tempHeight;
  260. }
  261. }
  262. /// <summary>
  263. /// 基准表格行位置
  264. /// </summary>
  265. public int BaseRowIndex { get; set; }
  266. /// <summary>
  267. /// 基准表格列位置
  268. /// </summary>
  269. public int BaseColumnIndex { get; set; }
  270. /// <summary>
  271. /// 向下合并行的数量
  272. /// </summary>
  273. public int ExtendRowRange { get; set; }
  274. /// <summary>
  275. /// 向右合并列的数量
  276. /// </summary>
  277. public int ExtendColumnRange { get; set; }
  278. /// <summary>
  279. /// 单元格的值
  280. /// </summary>
  281. public object Value { get; set; }
  282. #region 公开方法
  283. public XYZ GetLocation()
  284. {
  285. XYZ tempOrigion = m_ReferenceTable.Location??XYZ.Zero;
  286. return new XYZ(tempOrigion.X+Left.ToApi(),tempOrigion.Y-Top.ToApi(),tempOrigion.Z);
  287. }
  288. /// <summary>
  289. /// 获取cell Border,顺序左,上,右,下
  290. /// </summary>
  291. /// <returns></returns>
  292. public List<Curve> GetBorders()
  293. {
  294. List<Curve> curves=new List<Curve>();
  295. XYZ tempBase = GetLocation();
  296. XYZ leftBottom = tempBase.OffsetPoint(-XYZ.BasisY, Height.ToApi());
  297. XYZ rightTop = tempBase.OffsetPoint(XYZ.BasisX, Width.ToApi());
  298. XYZ rightBottom = rightTop.OffsetPoint(-XYZ.BasisY, Height.ToApi());
  299. curves.Add(tempBase.NewLine(leftBottom));
  300. curves.Add(tempBase.NewLine(rightTop));
  301. curves.Add(rightTop.NewLine(rightBottom));
  302. curves.Add(leftBottom.NewLine(rightBottom));
  303. return curves;
  304. }
  305. public void DrawValue()
  306. {
  307. TextNote textNote = Value as TextNote;
  308. XYZ tempOrigion = GetLocation();
  309. if (textNote != null)
  310. {
  311. View tempView= ExternalDataWrapper.Current.Doc.GetElement(textNote.OwnerViewId) as View;
  312. if (tempView == null)
  313. {
  314. return;
  315. }
  316. #region 处理标签宽度
  317. double scale = tempView.Scale;
  318. textNote.Width = Width.ToApi() / scale;
  319. #endregion
  320. //标签宽度,与表格同宽
  321. #region 处理水平对齐偏移
  322. double tempOffsetWidth = Width.ToApi() / 2;
  323. int hashcode = textNote.GetParameterInteger(BuiltInParameter.TEXT_ALIGN_HORZ);
  324. if (hashcode == TextAlignFlags.TEF_ALIGN_LEFT.GetHashCode())
  325. {
  326. tempOffsetWidth = 0;
  327. }
  328. else if (hashcode == TextAlignFlags.TEF_ALIGN_RIGHT.GetHashCode())
  329. {
  330. tempOffsetWidth = Width.ToApi();
  331. }
  332. XYZ result = tempOrigion.OffsetPoint(XYZ.BasisX, tempOffsetWidth);
  333. #endregion
  334. BoundingBoxXYZ bb = textNote.get_BoundingBox(tempView);
  335. if (bb != null)
  336. {
  337. double textHeight = bb.Max.Y - bb.Min.Y;
  338. double offset = (Height.ToApi() - textHeight) / 2;
  339. result = result.OffsetPoint(-XYZ.BasisY, offset);
  340. }
  341. textNote.Document.MoveElementExt(textNote.Id, result.Subtract(textNote.Coord));
  342. }
  343. }
  344. #endregion
  345. }
  346. #endregion
  347. }