调整代码
This commit is contained in:
311
ShrlAlgoToolkit.RevitAddins/Standardizer/RenameTypeViewModel.cs
Normal file
311
ShrlAlgoToolkit.RevitAddins/Standardizer/RenameTypeViewModel.cs
Normal file
@@ -0,0 +1,311 @@
|
||||
using System.Collections;
|
||||
using Autodesk.Revit.DB;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using ShrlAlgoToolkit.RevitAddins.RvFamily;
|
||||
using ShrlAlgoToolkit;
|
||||
using ShrlAlgoToolkit.RevitAddins;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Standardizer;
|
||||
|
||||
public partial class RenameTypeViewModel : ObservableObject
|
||||
{
|
||||
public RenameTypeViewModel(UIApplication uiApplication)
|
||||
{
|
||||
doc = uiApplication.ActiveUIDocument.Document;
|
||||
allCategories = GetCategoriesFromDocument();
|
||||
Collection = allCategories;
|
||||
//ICollectionView cv = CollectionViewSource.GetDefaultView(Categories);
|
||||
//cv.Filter = item => string.IsNullOrEmpty(SearchText) ||
|
||||
// ((KeyValuePair<BuiltInCategory, string>)item).Value.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) >= 0;
|
||||
//TaskDialog.Show("数量", CategoriesDictionary.Count.ToString());
|
||||
}
|
||||
|
||||
//[ObservableProperty]
|
||||
//private List<BuiltInCategory> selectedBuiltInCategories;
|
||||
private readonly Dictionary<string, List<ElementType>> allCategories;
|
||||
|
||||
//public ICollectionView CategoriesCollectionView { get; set; }
|
||||
private readonly Document doc;
|
||||
/// <summary>
|
||||
/// 列表中元素
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(ModifyNameCommand))]
|
||||
public partial List<RenameTypeItem> RenameItems { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial bool CanInput { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial bool IsRunning { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 选中正在使用中的条目
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
public partial bool IsUsed { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial Dictionary<string, List<ElementType>> Collection { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial int CategoryCount { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial int ModifyType { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string FoundText { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string PrefixText { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string ReplaceText { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string SearchText { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string Separator { get; set; } = "-";
|
||||
|
||||
[ObservableProperty]
|
||||
public partial string SuffixText { get; set; }
|
||||
[ObservableProperty]
|
||||
public partial bool ByFamilyName { get; set; }
|
||||
//[ObservableProperty]
|
||||
//private IList selectedItems;
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
||||
public bool CanModify => RenameItems != null && RenameItems.Any<RenameTypeItem>(item => item.IsSelected);
|
||||
//[ObservableProperty]
|
||||
//private bool? isAllItemsSelected;
|
||||
|
||||
//partial void OnIsAllItemsSelectedChanged(bool? value)
|
||||
//{
|
||||
// if (value.HasValue)
|
||||
// {
|
||||
// SelectAll(value.Value, RenameItems);
|
||||
// }
|
||||
//}
|
||||
|
||||
public bool? IsAllItemsSelected
|
||||
{
|
||||
get
|
||||
{
|
||||
if (RenameItems == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var selected = Enumerable.Distinct<bool>(RenameItems?.Select(item => item.IsSelected)).ToList();
|
||||
return selected?.Count == 1 ? selected.Single() : null;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value.HasValue)
|
||||
{
|
||||
SelectAll(value.Value, RenameItems);
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<string, List<ElementType>> GetCategoriesFromDocument()
|
||||
{
|
||||
var dict = new FilteredElementCollector(doc)
|
||||
.OfClass(typeof(ElementType))
|
||||
.Cast<ElementType>()
|
||||
.Where(t => t.CanBeRenamed && t.Category is { CategoryType: CategoryType.Model or CategoryType.Annotation, AllowsBoundParameters: true })
|
||||
.GroupBy(t => t.Category.Id)
|
||||
.ToDictionary(g => Category.GetCategory(doc, g.Key).Name, g => g.ToList());
|
||||
CategoryCount = dict.Count;
|
||||
return dict;
|
||||
//foreach (BuiltInCategory builtInCategory in Enum.GetValues(typeof(BuiltInCategory)))
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// var types = new FilteredElementCollector(doc).OfCategory(builtInCategory).WhereElementIsElementType().Cast<ElementType>().ToList();
|
||||
// if (types.Any() && types.Any(t => t.CanBeRenamed))
|
||||
// {
|
||||
// var category = types.FirstOrDefault()!.Category;
|
||||
// if (category is { CategoryType: CategoryType.Model or CategoryType.Annotation, AllowsBoundParameters: true })
|
||||
// {
|
||||
// var name = category.Name;
|
||||
// items.Add(builtInCategory, name);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// catch (Exception)
|
||||
// {
|
||||
// //string name = Enum.GetName(typeof(BuiltInCategory), builtInCategory);
|
||||
// //TaskDialog.Show( "错误", $"{name}");
|
||||
// }
|
||||
//}
|
||||
|
||||
//CategoryCount = items.Count;
|
||||
//return items.OrderBy(kv => kv.Value).ToDictionary(kv => kv.Key, kv => kv.Value);
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanModify))]
|
||||
private async Task ModifyName()
|
||||
{
|
||||
await Task.Delay(1_000);
|
||||
ModifyTypeName();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取列表中所有条目的新名称
|
||||
/// </summary>
|
||||
private void GetNewNames()
|
||||
{
|
||||
var selectedItems = Enumerable.Where<RenameTypeItem>(RenameItems, item => item.IsSelected);
|
||||
foreach (var renameItem in selectedItems)
|
||||
{
|
||||
string tempName = renameItem.OldTypeName;
|
||||
if (ByFamilyName)
|
||||
{
|
||||
tempName = renameItem.FamilyName;
|
||||
}
|
||||
////手动修改的新名称不处理
|
||||
//if (!string.IsNullOrEmpty(renameItem.NewTypeName))
|
||||
//{
|
||||
// continue;
|
||||
//}
|
||||
if (!string.IsNullOrEmpty(FoundText))
|
||||
{
|
||||
tempName = tempName.Replace(FoundText, string.IsNullOrEmpty(ReplaceText) ? string.Empty : ReplaceText);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(PrefixText))
|
||||
{
|
||||
tempName = tempName.Insert(0, $"{PrefixText}{Separator}");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(SuffixText))
|
||||
{
|
||||
tempName += $"{Separator}{SuffixText}";
|
||||
}
|
||||
|
||||
renameItem.NewTypeName = tempName;
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void GetRenameItems(IList items)
|
||||
{
|
||||
if (items.Count == 0)
|
||||
{
|
||||
RenameItems = null!;
|
||||
CanInput = false;
|
||||
return;
|
||||
}
|
||||
|
||||
List<RenameTypeItem> symbolItems = [];
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item is KeyValuePair<string, List<ElementType>> categoryItems)
|
||||
{
|
||||
foreach (var type in categoryItems.Value)
|
||||
{
|
||||
RenameTypeItem renameItem = new()
|
||||
{
|
||||
OldTypeName = type.Name,
|
||||
ElementType = type,
|
||||
CategoryName = categoryItems.Key,
|
||||
FamilyName = type.FamilyName
|
||||
};
|
||||
|
||||
symbolItems.Add(renameItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CanInput = symbolItems.Count > 0;
|
||||
RenameItems = symbolItems;
|
||||
|
||||
foreach (var renameItem in RenameItems)
|
||||
{
|
||||
renameItem.PropertyChanged += (_, args) =>
|
||||
{
|
||||
if (args.PropertyName == nameof(RevitAddins.RvFamily.RenameTypeItem.IsSelected))
|
||||
{
|
||||
OnPropertyChanged(nameof(IsAllItemsSelected));
|
||||
//foreach (RenameTypeItem item in SelectedItems)
|
||||
//{
|
||||
// item.IsSelected = renameItem.IsSelected;
|
||||
//}
|
||||
ModifyNameCommand.NotifyCanExecuteChanged();
|
||||
if (string.IsNullOrEmpty(renameItem.NewTypeName))
|
||||
{
|
||||
GetNewNames();
|
||||
}
|
||||
}
|
||||
//if (args.PropertyName == nameof(RenameTypeItem.NewTypeName))
|
||||
//{
|
||||
// ModifyTypeName();
|
||||
//}
|
||||
};
|
||||
}
|
||||
|
||||
//IsAllItemsSelected = true;
|
||||
}
|
||||
|
||||
private void ModifyTypeName()
|
||||
{
|
||||
var needToRename = Enumerable.Where<RenameTypeItem>(RenameItems, item => item.IsSelected);
|
||||
using Transaction ts = new(doc, "修改类型名称");
|
||||
ts.Start();
|
||||
|
||||
foreach (var renameItem in needToRename)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(renameItem.NewTypeName))
|
||||
{
|
||||
renameItem.ElementType.Name = renameItem.NewTypeName;
|
||||
renameItem.OldTypeName = renameItem.NewTypeName;
|
||||
}
|
||||
}
|
||||
|
||||
ts.Commit();
|
||||
}
|
||||
|
||||
partial void OnFoundTextChanged(string value)
|
||||
{
|
||||
GetNewNames();
|
||||
}
|
||||
|
||||
partial void OnPrefixTextChanged(string value)
|
||||
{
|
||||
GetNewNames();
|
||||
}
|
||||
|
||||
partial void OnReplaceTextChanged(string value)
|
||||
{
|
||||
GetNewNames();
|
||||
}
|
||||
|
||||
partial void OnSearchTextChanged(string value)
|
||||
{
|
||||
Collection = allCategories.Where(kv => string.IsNullOrEmpty(value) || kv.Key.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
.ToDictionary(k => k.Key, v => v.Value);
|
||||
CategoryCount = Collection.Count;
|
||||
//CategoriesCollectionView.Refresh();
|
||||
}
|
||||
|
||||
partial void OnSeparatorChanged(string value)
|
||||
{
|
||||
GetNewNames();
|
||||
}
|
||||
|
||||
partial void OnSuffixTextChanged(string value)
|
||||
{
|
||||
GetNewNames();
|
||||
}
|
||||
|
||||
private static void SelectAll(bool select, IEnumerable<RenameTypeItem> models)
|
||||
{
|
||||
foreach (var model in models)
|
||||
{
|
||||
model.IsSelected = select;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user