Files
SzmediTools/Szmedi.RvKits/InfoManager/PropEditor/ReplaceParamValueViewModel.cs
2025-09-16 16:06:41 +08:00

88 lines
3.6 KiB
C#

using System.Linq;
using System.Windows;
using Autodesk.Revit.DB;
using Nice3point.Revit.Toolkit.External.Handlers;
namespace Szmedi.RvKits.InfoManager
{
public partial class ReplaceParamValueViewModel : ObservableObject
{
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(ReplaceCommand))]
private string sourceStr;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(ReplaceCommand))]
private string targetStr;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(ReplaceCommand))]
private string propertyName;
readonly ActionEventHandler handler;
[ObservableProperty]
private bool isSelectedItems;
private bool CanRepalce() => !string.IsNullOrEmpty(SourceStr) && !string.IsNullOrEmpty(TargetStr) && !string.IsNullOrEmpty(PropertyName);
public ReplaceParamValueViewModel()
{
handler = new ActionEventHandler();
}
[RelayCommand(CanExecute = nameof(CanRepalce))]
private void Replace()
{
handler.Raise(
uiapp =>
{
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
doc.Invoke(
_ =>
{
ICollection<ElementId> collection;
if (IsSelectedItems)
{
collection = uidoc.Selection.GetElementIds();
if (collection.Count == 0)
{
MessageBox.Show("请先选中元素");
return;
}
}
else
{
collection = doc.ActiveView.OfCollector().ToElementIds();
}
foreach (var id in collection)
{
var elem = doc.GetElement(id);
var param = elem.GetParameters(PropertyName).FirstOrDefault();
if (param != null && !param.IsReadOnly)
{
var sourceValue = param.AsString();
if (string.IsNullOrEmpty(sourceValue))
{
sourceValue = param.AsValueString();
if (string.IsNullOrEmpty(sourceValue))
{
continue;
}
var targetStrValue = sourceValue.Replace(SourceStr, TargetStr);
param.SetValueString(targetStrValue);
continue;
}
var targetValue = sourceValue.Replace(SourceStr, TargetStr);
param.Set(targetValue);
}
}
},
"查找替换属性");
MessageBox.Show("替换完成");
});
}
}
}