2024-09-22 11:05:41 +08:00
|
|
|
|
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;
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
using ShrlAlgoToolkit.Core.Assists;
|
2026-02-23 16:49:34 +08:00
|
|
|
|
using ShrlAlgoToolkit.RevitCore.Base;
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
namespace ShrlAlgo.Addin.Test;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
[Transaction(TransactionMode.Manual)]
|
|
|
|
|
|
[Regeneration(RegenerationOption.Manual)]
|
2026-02-23 16:49:34 +08:00
|
|
|
|
public class RevitAddin : BaseCommand
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
2026-02-20 15:31:44 +08:00
|
|
|
|
|
2026-02-23 16:49:34 +08:00
|
|
|
|
protected override void ExecuteCommand()
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
2026-02-23 16:49:34 +08:00
|
|
|
|
// 获取文档中所有的墙
|
|
|
|
|
|
var walls = new FilteredElementCollector(Document)
|
|
|
|
|
|
.OfClass(typeof(Wall))
|
|
|
|
|
|
.ToElements();
|
|
|
|
|
|
|
|
|
|
|
|
if (walls.Count == 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 直接调用我们的进度条管理器!
|
|
|
|
|
|
ProgressManager.Run("批量修改墙参数", (reporter, token) =>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
2026-02-23 16:49:34 +08:00
|
|
|
|
// 利用之前封装的 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("提示", "操作结束或已被取消!");
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|