Files
ShrlAlgoToolkit/RvAddinTest/CheckCollision.cs
2024-09-22 11:05:41 +08:00

103 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
namespace RvAddinTest
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
internal class CheckCollision : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
//程序UI界面
UIApplication uiapp = commandData.Application;
//获取元素(选择) 显示元素 视图(活动视图)管理(对象)
UIDocument uidoc = uiapp.ActiveUIDocument;
//程序
Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
//获取位置和场地 视图(多个视图)管理 获取元素Revit 项目里的全部元素)
Document doc = uidoc.Document;
//获取所有打开文档
DocumentSet docset = uiapp.Application.Documents;
//当前视图
View view = doc.ActiveView;
List<Element> collector = new FilteredElementCollector(doc, view.Id)
.OfClass(typeof(MEPCurve))
.WhereElementIsNotElementType().ToList();
// 检查管线之间的碰撞
List<Tuple<Element, Element>> collisions = CheckPipeCollisionsParallel(doc, collector);
// 显示碰撞结果
ShowCollisionResults(collisions);
return Result.Succeeded;
}
public List<Tuple<Element, Element>> CheckPipeCollisionsParallel(Document doc, List<Element> pipes)
{
List<Tuple<Element, Element>> collisions = new List<Tuple<Element, Element>>();
// 使用 Parallel.For 并行检查碰撞
Parallel.For(0, pipes.Count, i =>
{
for (int j = i + 1; j < pipes.Count; j++)
{
Element pipe1 = pipes[i];
Element pipe2 = pipes[j];
// 使用 ElementIntersectsElementFilter 检查碰撞
ElementIntersectsElementFilter filter = new ElementIntersectsElementFilter(pipe1);
FilteredElementCollector collector = new FilteredElementCollector(doc, new ElementId[] { pipe2.Id });
collector.WherePasses(filter);
if (collector.Any())
{
// 如果检测到碰撞,记录下来
lock (collisions)
{
collisions.Add(new Tuple<Element, Element>(pipe1, pipe2));
}
}
}
});
return collisions;
}
public void ShowCollisionResults(List<Tuple<Element, Element>> collisions)
{
if (collisions.Count == 0)
{
TaskDialog.Show("碰撞检查", "未发现碰撞");
}
else
{
StringBuilder message = new StringBuilder();
message.AppendLine("发现碰撞:");
foreach (Tuple<Element, Element> collision in collisions)
{
message.AppendLine($"管线 {collision.Item1.Id} 与管线 {collision.Item2.Id} 发生碰撞");
}
ExportCollisionResultsToFile(message.ToString());
//TaskDialog.Show("碰撞检查", message.ToString());
}
}
public void ExportCollisionResultsToFile(string content)
{
// 获取桌面路径
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// 创建文件路径
string filePath = Path.Combine(desktopPath, "碰撞检查.txt");
// 写入文件
File.WriteAllText(filePath, content);
}
}
}