FloorSystemItem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:FloorSystemItems
  3. * 作者:xulisong
  4. * 创建时间: 2019/1/4 17:50:04
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text.RegularExpressions;
  10. using Autodesk.Revit.DB;
  11. using Autodesk.Revit.DB.Mechanical;
  12. using Autodesk.Revit.DB.Plumbing;
  13. using SAGA.DotNetUtils.Data;
  14. using SAGA.Models;
  15. using SAGA.RevitUtils;
  16. using SAGA.RevitUtils.Extends;
  17. using SAGA.RevitUtils.MEP;
  18. namespace SAGA.GplotRelationComputerManage
  19. {
  20. public class FloorSystemItem
  21. {
  22. #region 构造函数
  23. /// <summary>
  24. /// 初始化解析类
  25. /// </summary>
  26. /// <param name="doc"></param>
  27. public FloorSystemItem(Document doc)
  28. {
  29. Document = doc;
  30. InitData();
  31. }
  32. #endregion
  33. #region 属性相关
  34. /// <summary>
  35. /// 关联模型信息
  36. /// </summary>
  37. public Document Document { get; private set; }
  38. /// <summary>
  39. /// 使用关联标高
  40. /// </summary>
  41. public Level UseLevel { get; private set; }
  42. private bool m_IsInited;
  43. /// <summary>
  44. /// 初始化数据
  45. /// </summary>
  46. private void InitData()
  47. {
  48. if (m_IsInited)
  49. return;
  50. UseLevel = Document.GetUseView()?.GenLevel;
  51. m_IsInited = true;
  52. }
  53. #endregion
  54. #region 开始节点获取
  55. /// <summary>
  56. /// 缓存信息
  57. /// </summary>
  58. private List<StringFlag<ElementTreeNode>> m_StartNodes;
  59. /// <summary>
  60. /// 按系统获取开始节点
  61. /// </summary>
  62. /// <returns></returns>
  63. private List<StringFlag<ElementTreeNode>> GetStartNodes()
  64. {
  65. if (m_StartNodes != null)
  66. {
  67. //返回没有处理过的节点信息
  68. return m_StartNodes.Where(s=>!s.Handled).ToList();
  69. }
  70. List<StringFlag<ElementTreeNode>> startNodes = new List<StringFlag<ElementTreeNode>>();
  71. var doc = Document;
  72. #region 立管处理
  73. /*
  74. * 暂时认定,一个立管之关联一个系统
  75. */
  76. var verticalPipes = doc.GetElements<Pipe>().Where(p => SystemCalcUtil.IsStart(p) && p.IsVerticalPipe());
  77. foreach (var verticalPipe in verticalPipes)
  78. {
  79. ElementTreeNode node = new ElementTreeNode();
  80. node.Current = verticalPipe;
  81. var systemTypeName = verticalPipe.GetSystemTypeName();
  82. startNodes.Add(new StringFlag<ElementTreeNode>(systemTypeName, node));
  83. }
  84. #endregion
  85. #region 特殊阀门处理
  86. ElementCategoryFilter pipeFilter = new ElementCategoryFilter(BuiltInCategory.OST_PipeAccessory);
  87. ElementCategoryFilter ductFilter = new ElementCategoryFilter(BuiltInCategory.OST_DuctAccessory);
  88. ElementFilter filter = new LogicalOrFilter(pipeFilter, ductFilter);
  89. var specialValves = doc.FilterElements(filter).Where(f => SystemCalcUtil.IsStart(f));
  90. foreach (var familyInstance in specialValves)
  91. {
  92. var connector = familyInstance.GetAllConnectors().FirstOrDefault(c => Regex.IsMatch(c.Description, AppSetting.EndFlag));
  93. if (connector == null)
  94. continue;
  95. var node = SystemParseManager.CreateTreeNode(familyInstance, connector, e => e is MEPCurve);
  96. var useMepCurve = node.GetLeaves().FirstOrDefault()?.Current as MEPCurve;
  97. if (useMepCurve == null)
  98. continue;
  99. var systemTypeName = useMepCurve.GetSystemTypeName();
  100. startNodes.Add(new StringFlag<ElementTreeNode>(systemTypeName, node));
  101. }
  102. #endregion
  103. return m_StartNodes = startNodes;
  104. }
  105. #endregion
  106. #region 设备节点获取
  107. /// <summary>
  108. /// 模型中包含的设备
  109. /// </summary>
  110. public Dictionary<string, List<FamilyInstance>> RevitEquipments { get; private set; }
  111. /// <summary>
  112. /// 通过设备类型获取相关设备
  113. /// </summary>
  114. /// <returns></returns>
  115. private List<FamilyInstance> GetEquipments(string category)
  116. {
  117. if (RevitEquipments == null)
  118. {
  119. var doc = Document;
  120. var fies = doc.FilterElements<FamilyInstance>();
  121. List<StringFlag<FamilyInstance>> original = new List<StringFlag<FamilyInstance>>();
  122. foreach (var familyInstance in fies)
  123. {
  124. var familyName = familyInstance.GetFamily().Name;
  125. if (string.IsNullOrEmpty(familyName))
  126. continue;
  127. var match=Regex.Match(familyName, @"^(?:((?:[A-Z][A-Z]){2,3})\s*-\s*\S*)");
  128. if (match.Success)
  129. {
  130. original.Add(new StringFlag<FamilyInstance>(match.Groups[1].ToString(), familyInstance));
  131. }
  132. }
  133. RevitEquipments= original.GroupBy(f => f.Flag).ToDictionary(g=>g.Key,g=>g.ToList().Select(f=>f.Instance).ToList());
  134. }
  135. RevitEquipments.TryGetValue(category, out List<FamilyInstance> result);
  136. return result??new List<FamilyInstance>();
  137. }
  138. #endregion
  139. #region 数据解析方法
  140. /// <summary>
  141. /// 解析当前楼层系统数据
  142. /// </summary>
  143. /// <param name="context"></param>
  144. public void Parse(SystemComputerContext context)
  145. {
  146. var doc = Document;
  147. var relations = context.Relations;
  148. foreach (var relation in relations)
  149. {
  150. var shell = context.RelationShells.GetItem(relation);
  151. if (shell.IsPipeSystem())
  152. {
  153. ParsePipe(context, shell);
  154. }
  155. else
  156. {
  157. ParseDuct(context, shell);
  158. }
  159. }
  160. }
  161. #region 水系统解析
  162. public void ParsePipe(SystemComputerContext context,RelationTypeShell relationSetting)
  163. {
  164. List<FloorDrawRecord> drawRecords = new List<FloorDrawRecord>();
  165. List<FloorRelationRecord> relationRecords = new List<FloorRelationRecord>();
  166. #region 计算拓扑树形结构
  167. /*
  168. * 1、遍历开始点的限制:特殊阀门,指定标注的Connector;立管,已连接连接件;空间,相交风管元素;
  169. * 2、根据拓扑图关联的对象信息,找到系统关联的设备生成节点。如果没有遍历到要独立生成
  170. * 3、要不要显示端头管所在空间的位置
  171. */
  172. var startNodes = GetStartNodes();
  173. List<int> useIds = new List<int>();
  174. for (int i = 0; i < startNodes.Count; i++)
  175. {
  176. var startNode = startNodes[i];
  177. if (!relationSetting.IsMatchSystem(startNode.Flag))
  178. {
  179. continue;
  180. }
  181. if (useIds.Any(id => id == startNode.Instance.Current.Id.IntegerValue))
  182. {
  183. continue;
  184. }
  185. var tempIds=SystemParseManager.BuildSystemNode(startNode.Instance, Domain.DomainPiping,
  186. (m => !relationSetting.IsMatchSystem(m.GetSystemTypeName())));
  187. useIds.AddRange(tempIds);
  188. startNode.Handled = true;
  189. //可以清空Leaves的子节点
  190. #region 整理原始数据
  191. //判断是否有设备节点,不进行数据处理
  192. var useNode = startNode.Instance;
  193. var firstEquipment = useNode.FirstOrDefault(t => MBIInfoUtil.IsEquipment(t.Current));
  194. //要不要以这个为判定条件,图形可以画出,关系肯定是没有的
  195. if (firstEquipment != null)
  196. {
  197. //存在设备则保存信息
  198. var drawRecord = BuildFloorDrawRecord(relationSetting, useNode);
  199. drawRecord.SystemName = startNode.Flag;
  200. drawRecords.Add(drawRecord);
  201. var relationRecord = BuildFloorRelationRecord(relationSetting, useNode);
  202. relationRecords.Add(relationRecord);
  203. }
  204. #endregion
  205. }
  206. //获取没有使用过的对象节点
  207. List<FamilyInstance> singleInstances =
  208. relationSetting.RelationItem.Objects.SelectMany(o => GetEquipments(o.Type)).ToList();
  209. for (int i = 0; i < singleInstances.Count; i++)
  210. {
  211. var useInstance = singleInstances[i];
  212. if (useIds.Any(id => id == useInstance.Id.IntegerValue))
  213. {
  214. continue;
  215. }
  216. var startNode = new ElementTreeNode() {Current = useInstance};
  217. var tempIds = SystemParseManager.BuildSystemNode(startNode, Domain.DomainPiping,
  218. (m => !relationSetting.IsMatchSystem(m.GetSystemTypeName())));
  219. useIds.AddRange(tempIds);
  220. #region 生成绘图,计算数据
  221. //解析数据,找管道定系统,管道找不到,系统名为"关联类型-未知"
  222. var firstMepCurve = startNode.FirstOrDefault(t => t.Current is MEPCurve);
  223. if (firstMepCurve == null)
  224. continue;
  225. var useSystemName = (firstMepCurve.Current as MEPCurve)?.GetSystemTypeName() ?? GetUnknownSystemName(relationSetting);
  226. //存在设备则保存信息
  227. var drawRecord = BuildFloorDrawRecord(relationSetting, startNode);
  228. drawRecord.SystemName = useSystemName;
  229. drawRecords.Add(drawRecord);
  230. var relationRecord = BuildFloorRelationRecord(relationSetting, startNode);
  231. relationRecords.Add(relationRecord);
  232. #endregion
  233. }
  234. #endregion
  235. context.DrawRecords.AddRange(drawRecords);
  236. context.RelationRecords.AddRange(relationRecords);
  237. }
  238. #endregion
  239. #region 风系统解析
  240. public void ParseDuct(SystemComputerContext context, RelationTypeShell relationSetting)
  241. { }
  242. #endregion
  243. #endregion
  244. #region 解析立管
  245. /*
  246. * 1、解析立管需要考虑,竖直风管,水管,空间的情况;
  247. *2、将各层数据汇总,才能统一计算立管的相关关系数据
  248. */
  249. private void ComputerVerticalPipes(Level refLevel, List<Pipe> pipes, SystemComputerContext context)
  250. {
  251. var result = new List<VerticalPipe>();
  252. var spaces = Document.GetElements<SpatialElement>(BuiltInCategory.OST_MEPSpaces).OfType<Space>();
  253. var level = refLevel;
  254. var currentLevelName = MBIInfoUtil.GetFloorNameByLevel(level.Name);
  255. foreach (var pipe in pipes)
  256. {
  257. if (pipe == null)
  258. continue;
  259. var vp = new VerticalPipe
  260. {
  261. Id = pipe.Id.IntegerValue.ToString(),
  262. LevelName = currentLevelName,
  263. LevelElevation = level.Elevation,
  264. UpPoint3D = PointConverter.XYZToPoint3D(pipe.GetVerticalTopPoint()),
  265. DownPoint3D = PointConverter.XYZToPoint3D(pipe.GetVerticalBottomPoint()),
  266. PipeSytemType = pipe.GetSystemTypeName()
  267. };
  268. #region 获取立管关联空间
  269. var locationCurve = pipe.GetLocationCurve();
  270. var midPoint = (locationCurve.StartPoint() + locationCurve.EndPoint()) / 2;
  271. Space refSpace = spaces.FirstOrDefault(s => s.IsPointInSpace(midPoint));//关联
  272. var display = context.GetSpaceDisplay(refSpace, out string tip);
  273. vp.Display = $"{display}:{pipe.Id.IntegerValue.ToString()}";
  274. vp.DisplayTip = tip ?? string.Empty;
  275. #endregion
  276. result.Add(vp);
  277. }
  278. LevelData levelData = new LevelData()
  279. {
  280. Id = refLevel.Id.ToString(),
  281. Name = MBIInfoUtil.GetFloorNameByLevel(refLevel.Name)
  282. };
  283. context.VerticalPipes.AddRange(result);
  284. context.Levels.Add(levelData);
  285. }
  286. #endregion
  287. #region 保存数据类对象创建
  288. private string GetUnknownSystemName(RelationTypeShell relationShell)
  289. {
  290. return relationShell.RelationItem?.Name + "---未知";
  291. }
  292. private FloorDrawRecord BuildFloorDrawRecord(RelationTypeShell relationShell,ElementTreeNode node)
  293. {
  294. var floorDraw = new FloorDrawRecord();
  295. floorDraw.SetFloorLevel(UseLevel);
  296. floorDraw.RelationType = relationShell.RelationItem.Type;
  297. floorDraw.DrawIems = SystemParseManager.CreateDrawing(node)??new SystemDrawItems();
  298. return floorDraw;
  299. }
  300. private FloorRelationRecord BuildFloorRelationRecord(RelationTypeShell relationShell, ElementTreeNode node)
  301. {
  302. var floorRelation = new FloorRelationRecord();
  303. floorRelation.SetFloorLevel(UseLevel);
  304. floorRelation.RelationType = relationShell.RelationItem.Type;
  305. floorRelation.RelationItems = SystemParseManager.CreateRelation(node)??new List<BinaryRelationItem>();
  306. return floorRelation;
  307. }
  308. #endregion
  309. }
  310. }