123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Architecture;
- using Autodesk.Revit.DB.Mechanical;
- using FWindSoft.Revit;
- using FWindSoft.Revit.Extension;
- using FWindSoft.SystemExtensions;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace LRH.Tool
- {
- public static class RoomToSpaceUtils
- {
- /// <summary>
- /// 获取指定的元素信息
- /// </summary>
- /// <param name="view"></param>
- /// <param name="filter"></param>
- /// <returns></returns>
- public static List<Element> GetElements(View view, ElementFilter filter)
- {
- //删除当前视图下的所有空间和空间分割
- var doc = view.Document;
- var elements = doc.GetElements(filter);
- return elements.Where(e => e.LevelId == view.GenLevel.Id).ToList();
- }
- /// <summary>
- /// 房间转换成空间
- /// </summary>
- /// <param name="view"></param>
- /// <param name="option"></param>
- /// <returns></returns>
- public static List<RoomRelationItem> RoomToSpace(View view, RoomToSpaceTransOption option,Transaction tran)
- {
- //删除当前视图下的所有空间和空间分割
- var doc = view.Document;
- var spaces = GetElements(view, new SpaceFilter());
- if (option.DeleteOriginalSpace)
- {
- try
- {
- tran.Start();
- doc.Delete(spaces.Select(e => e.Id).ToList());
- tran.Commit();
- }
- catch (Exception)
- {
- tran.RollBack();
- }
- }
- var spaceSeps = GetElements(view, new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaceSeparationLines)).OfType<ModelLine>().ToList();
- if (option.DeleteOriginalSpaceSq)
- {
- try
- {
- tran.Start();
- doc.Delete(spaceSeps.Select(e => e.Id).ToList());
- tran.Commit();
- }
- catch (Exception)
- {
- tran.RollBack();
- }
- }
- CreateSpaces(view, tran);
-
- List<RoomRelationItem> relationItems = new List<RoomRelationItem>();
- try
- {
- tran.Start();
- #region 根据房间分割创建空间分割
- if (option.RoomSqToSpaceSq)
- {
- var roomSeps = GetElements(view, new ElementCategoryFilter(BuiltInCategory.OST_RoomSeparationLines))
- .OfType<ModelLine>().ToList();
- CurveArray curveArray = new CurveArray();
- foreach (var roomSep in roomSeps)
- {
- curveArray.Append(roomSep.GeometryCurve);
- }
- if (!curveArray.IsEmpty)
- {
- Plane p = Plane.CreateByNormalAndOrigin(view.ViewDirection, view.Origin);
- var sp = SketchPlane.Create(doc, p);
- doc.Create.NewSpaceBoundaryLines(sp, curveArray, view);
- }
- }
- #endregion
- relationItems = RoomDataToSpace(view);
- tran.Commit();
- }
- catch (Exception)
- {
- tran.RollBack();
- }
- return relationItems;
- }
- /// <summary>
- /// 房间数据转移到空间数据
- /// </summary>
- /// <param name="view"></param>
- /// <returns></returns>
- public static List<RoomRelationItem> RoomDataToSpace(View view)
- {
- var doc = view.Document;
- var rooms = GetElements(view, new RoomFilter()).OfType<Room>();
- var offset = XYZ.BasisZ * 0.1;
- HashSet<int> handledSpaces = new HashSet<int>();
- List<RoomRelationItem> relations = new List<RoomRelationItem>();
- foreach (Room room in rooms)
- {
- var relationItem = new RoomRelationItem() { RoomId = room.Id.ToString() };
- relations.Add(relationItem);
- using (SubTransaction subTran = new SubTransaction(doc))
- {
- Space useSpcae = null;
- try
- {
- subTran.Start();
- var location = room.Location.ConvertToPoint();
- if (location == null )
- {
- //|| room.Area.IsEqual(0)
- relationItem.Messages.Add("房间信息无效");
- continue;
- }
- useSpcae = doc.GetSpaceAtPoint(location.Add(offset));
- if (useSpcae == null)
- {
- relationItem.Messages.Add("未找到对应空间");
- continue;
- }
- InfoTrans(room, useSpcae, handledSpaces.Contains(useSpcae.Id.IntegerValue));
- subTran.Commit();
- //成功
- handledSpaces.Add(useSpcae.Id.IntegerValue);
- relationItem.SpaceId = useSpcae.Id.ToString();
- relationItem.IsSuccess = true;
- }
- catch (Exception ex)
- {
- relationItem.Messages.Add("赋值异常"+ex.Message);
- subTran.RollBack();
- }
- }
- }
- return relations;
- }
- public static List<Space> CreateSpaces(View view)
- {
- List<Space> spaces = new List<Space>();
- Document doc = view.Document;
- var level = view.GenLevel;
- Parameter para = view.GetParameter(BuiltInParameter.VIEW_PHASE);
- ElementId phaseId = para.AsElementId();
- var phase = doc.GetElement(phaseId) as Phase;
- var points = GetSpacesPoints(view);
- foreach (var point in points)
- {
- var oldSpace = doc.GetSpaceAtPoint(point, phase);
- if (oldSpace == null)
- {
- var tempSapce = doc.Create.NewSpace(level, phase, new UV(point.X, point.Y));
- spaces.Add(tempSapce);
- }
- }
- return spaces;
- }
- public static void CreateSpaces(View view,Transaction tran)
- {
- Document doc = view.Document;
- FailuresPreprocessor preprocessor = new FailuresPreprocessor(true);
- {
- try
- {
- FailureHandlingOptions fho = tran.GetFailureHandlingOptions();
- fho.SetFailuresPreprocessor(preprocessor);
- tran.SetFailureHandlingOptions(fho);
- #region 创建空间
- tran.Start();
- var spaces=CreateSpaces(view);
- tran.Commit(); //提交获取冲突代码
- #endregion
- #region 冲突处理代码
- FailureHandlingOptions failureOptions = tran.GetFailureHandlingOptions();
- failureOptions.SetFailuresPreprocessor(new FailuresPreprocessor(false));
- tran.SetFailureHandlingOptions(failureOptions);
- tran.Start();
- //返回值临时使用
- var failItems = preprocessor.Items;
- foreach (var failuresItem in failItems)
- {
- var firstId = failuresItem.Items.FirstOrDefault();
- if (firstId == null)
- {
- continue;
- }
- for (int i = 1; i < failuresItem.Items.Count; i++)
- {
- var currentId = failuresItem.Items[i];
- if (firstId.IntegerValue > currentId.IntegerValue)
- {
- doc.Delete(new List<ElementId>() { firstId });
- firstId = currentId;
- }
- } // doc.GetElement(failuresItem.Items.FirstOrDefault());
- }
- tran.Commit();
- #endregion
- }
- catch (Exception ex)
- {
- tran.RollBack();
- }
- }
- }
- /// <summary>
- /// 获取视图中创建空间的点的信息
- /// </summary>
- /// <param name="view"></param>
- /// <returns></returns>
- public static List<XYZ> GetSpacesPoints(View view)
- {
- Document doc = view.Document;
- var level = view.GenLevel;
- Parameter para = view.GetParameter(BuiltInParameter.VIEW_PHASE);
- ElementId phaseId = para.AsElementId();
- var phase = doc.GetElement(phaseId) as Phase;
- #region 创建新的节点
- List<XYZ> points = new List<XYZ>();
- using (SubTransaction sub = new SubTransaction(doc))
- {
- try
- {
- sub.Start();
- var spaceSeps = GetElements(view, new ElementCategoryFilter(BuiltInCategory.OST_MEPSpaceSeparationLines)).OfType<ModelLine>().ToList();
- CurveArray curveArray = new CurveArray();
- foreach (var modelLine in spaceSeps)
- {
- curveArray.Append(modelLine.GeometryCurve);
- }
- #region 创建房间分割
- Plane p = Plane.CreateByNormalAndOrigin(view.ViewDirection, view.Origin);
- var sp = SketchPlane.Create(doc, p);
- if (!curveArray.IsEmpty)
- {
- doc.Create.NewRoomBoundaryLines(sp, curveArray, view);
- }
- #endregion
- #region 获取封闭区域
- var topology = doc.get_PlanTopology(level, phase);
- foreach (PlanCircuit topologyCircuit in topology.Circuits)
- {
- var point = topologyCircuit.GetPointInside();
- var xyz = new XYZ(point.U, point.V, level.ProjectElevation + 1); //适当升高一些
- points.Add(xyz);
- }
- #endregion
- }
- catch (Exception e)
- {
- }
- finally
- {
- sub.RollBack();
- }
- }
- #endregion
- return points;
- }
- public static void InfoTrans(Room room,Space space,bool isAppend)
- {
- var name = room.GetParameterString(BuiltInParameter.ROOM_NAME) ?? string.Empty;
- if (isAppend)
- {
- var oldName = space.GetParameterString(BuiltInParameter.ROOM_NAME)??string.Empty;
- if (!string.IsNullOrWhiteSpace(name)&& !oldName.Contains(name))
- {
- name = oldName + "," + name;
- }
- }
- space.SetParameter(BuiltInParameter.ROOM_NAME, name);
- }
- public static StringBuilder CreateResult(List<RoomRelationItem> items)
- {
- StringBuilder builder = new StringBuilder();
- int errorCount = 0;
- foreach (var item in items)
- {
- if (item.IsSuccess)
- {
- continue;
- }
- builder.AppendLine(item.ToString());
- errorCount++;
- }
- builder.AppendLine($"总共{items.Count}房间,转换过程出现问题{errorCount}个");
- return builder;
- }
- }
- #region 扩展选项类
- public class RoomRelationItem
- {
- public string RoomId { get; set; }
- public string SpaceId { get; set; }
- /// <summary>
- /// 是否成功
- /// </summary>
- public bool IsSuccess { get; set; }
- /// <summary>
- /// 错误消息
- /// </summary>
- public List<string> Messages { get; private set; } = new List<string>();
- public override string ToString()
- {
- var message = string.Join(";", Messages);
- return $"RoomId:{RoomId} SpaceId:{SpaceId} Result:{message}";
- }
- }
- /// <summary>
- /// 导出控制选项
- /// </summary>
- public class RoomToSpaceTransOption
- {
- /// <summary>
- /// 是否删除原始空间
- /// </summary>
- public bool DeleteOriginalSpace { get; set; } = true;
- /// <summary>
- /// 是否删除原始空间分割
- /// </summary>
- public bool DeleteOriginalSpaceSq { get; set; }
- /// <summary>
- /// 是否将房间分割转换成空间分割
- /// </summary>
- public bool RoomSqToSpaceSq { get; set; }
- }
- public class FailuresItem
- {
- public FailuresItem()
- {
- }
- public FailuresItem(IEnumerable<ElementId> ids)
- {
- Items.AddRange(ids);
- }
- public List<ElementId> Items { get; private set; } = new List<ElementId>();
- }
- /// <summary>
- /// 空间错误机制
- /// </summary>
- public class FailuresPreprocessor : IFailuresPreprocessor
- {
- /*
- * 创建警告时,如果一个区域已经存在多个空间,创建警告时会把所有警告空间进行捕获
- */
- #region 构造函数
- public FailuresPreprocessor()
- {
- Items = new List<FailuresItem>();
- }
- public FailuresPreprocessor(bool append) : this()
- {
- Append = append;
- }
- #endregion
- #region 相关操纵选项
- /// <summary>
- /// 是否追加捕获信息
- /// </summary>
- private bool Append { get; set; }
- public void SetOperate(bool continueAdd)
- {
- Append = continueAdd;
- }
- public List<FailuresItem> Items { get; private set; }
- #endregion
- public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
- {
- IList<FailureMessageAccessor> messages = failuresAccessor.GetFailureMessages();
- if (messages.Count == 0)
- return FailureProcessingResult.Continue;
- foreach (FailureMessageAccessor message in messages)
- {
- var currentSeverity = message.GetSeverity();
- switch (currentSeverity)
- {
- case FailureSeverity.Error:
- {
- if (message.HasResolutions())
- failuresAccessor.ResolveFailure(message);
- break;
- }
- case FailureSeverity.Warning:
- {
- if (Append)
- {
- var elementIds = message.GetFailingElementIds();
- if (elementIds.Any())
- {
- Items.Add(new FailuresItem(elementIds.ToList()));
- }
- }
- failuresAccessor.DeleteWarning(message);
- break;
- }
- }
- }
- return FailureProcessingResult.ProceedWithCommit;
- }
- }
- #endregion
- }
|