Files
SzmediTools/Szmedi.Test/MappingSystemCmd.cs
2026-01-16 17:07:43 +08:00

66 lines
2.5 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.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
namespace Szmedi.Test
{
/// <summary>
/// Revit执行命令
/// </summary>
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class MappingSystemCmd : 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;
var systems=new FilteredElementCollector(doc)
.OfClass(typeof(MEPSystem))
.WhereElementIsNotElementType().Cast<MEPSystem>().ToList();
using (Transaction tx = new Transaction(doc, "Mapping System Names"))
{
tx.Start();
foreach (var sys in systems)
{
var typeId = sys.GetTypeId();
var type = doc.GetElement(typeId) as MEPSystemType;
if (sys is MechanicalSystem mechanicalSystem)
{
var elementSet = mechanicalSystem.DuctNetwork;
foreach (Element elem in elementSet)
{
elem.LookupParameter("三级系统名称")?.Set(type.Name);
}
}
if (sys is PipingSystem pipingSystem)
{
var elementSet = pipingSystem.PipingNetwork;
foreach (Element elem in elementSet)
{
elem.LookupParameter("三级系统名称")?.Set(type.Name);
}
}
}
tx.Commit();
}
return Result.Succeeded;
}
}
}