Files
ShrlAlgoToolkit/ShrlAlgo.Addin.Test/RevitAddin.cs

64 lines
2.1 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.IO;
using System.Reflection;
using System.Text;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Nice3point.Revit.Toolkit.External;
using ShrlAlgoToolkit.Core.Assists;
using ShrlAlgoToolkit.RevitCore.Base;
namespace ShrlAlgo.Addin.Test;
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class RevitAddin : BaseCommand
{
protected override void ExecuteCommand()
{
// 获取文档中所有的墙
var walls = new FilteredElementCollector(Document)
.OfClass(typeof(Wall))
.ToElements();
if (walls.Count == 0) return;
// 直接调用我们的进度条管理器!
ProgressManager.Run("批量修改墙参数", (reporter, token) =>
{
// 利用之前封装的 TransactionExtensions 开启事务
Document.InvokeInTransaction(() =>
{
for (int i = 0; i < walls.Count; i++)
{
// 1. 检查用户是否点击了“取消”
token.ThrowIfCancellationRequested();
var wall = walls[i];
// 2. 你的 Revit API 操作 (例如修改参数)
var commentsParam = wall.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
if (commentsParam != null && !commentsParam.IsReadOnly)
{
commentsParam.Set($"自动写入 - {i}");
}
// 3. 报告进度 (内置了防卡顿机制,每秒调用几万次都不会卡)
reporter.Report(i + 1, walls.Count, $"正在处理墙: {wall.Name}");
// 如果逻辑非常非常耗时可以考虑在这里短暂休眠让出一点点CPU通常不用加
// Thread.Sleep(1);
}
}, "Batch Update Walls");
});
// 任务结束(无论完成还是取消),后续代码继续执行
TaskDialog.Show("提示", "操作结束或已被取消!");
}
}