Files
SzmediTools/Szmedi.RevitToolkit.Approval/ViewModels/FillArchiParametersViewModel.cs

306 lines
13 KiB
C#
Raw Normal View History

2025-09-16 16:06:41 +08:00
using System.ComponentModel;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Xml.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Szmedi.RevitToolkit.Approval.Models;
namespace Szmedi.RevitToolkit.Approval.ViewModels
{
public partial class FillArchiParametersViewModel : ObservableObject
{
public FillArchiParametersViewModel(Document doc)
{
// 初始化专业列表
SelectedMajor = doc.GetMajor(out string major);
var fileContent = IOAssists.GetMvdLiteContent(SelectedMajor);
mvdData = MvdLiteAssist.Parse(fileContent);
Properties = MvdLiteAssist.GetProperties(mvdData);
PropertyAssists.Load();
foreach (var property in Properties)
{
var defaultValue = PropertyAssists.GetDefaultValue(property.DefinitionName, property.Name);
if (!string.IsNullOrEmpty(defaultValue))
{
property.DefaultValue = defaultValue;
}
else
{
// 如果没有默认值,则使用约束中的第一个值作为默认值
property.DefaultValue = property.ValueConstraints.FirstOrDefault();
}
}
//Properties = allProps;
//CollectionViewSource.GetDefaultView(Properties);
Collection = CollectionViewSource.GetDefaultView(Properties);
//new CollectionViewSource() { Source = Properties }.View;
Collection.GroupDescriptions.Add(new PropertyGroupDescription("DefinitionName"));
Collection.Filter = item =>
{
if (string.IsNullOrEmpty(FilterText))
{
return true;
}
if (item is AfcaArchiProperty param)
{
return !string.IsNullOrEmpty(param.DefinitionName) && param.DefinitionName.Contains(FilterText);
}
return false;
};
}
[ObservableProperty]
private bool isManual;
private MvdData mvdData;
[ObservableProperty]
public partial Major SelectedMajor { get; set; }
[ObservableProperty]
//[NotifyCanExecuteChangedFor(nameof(ModifyParameterValueCommand))]
public partial List<AfcaArchiProperty> Properties { get; set; }
[ObservableProperty]
public partial string FilterText { get; set; }
[ObservableProperty]
public partial ICollectionView Collection { get; set; } = null;
partial void OnSelectedMajorChanged(Major value)
{
CheckedForFilter = false;
var fileContent = IOAssists.GetMvdLiteContent(value);
mvdData = MvdLiteAssist.Parse(fileContent);
Properties = MvdLiteAssist.GetProperties(mvdData);
Collection = new CollectionViewSource() { Source = Properties }.View;
Collection.GroupDescriptions.Add(new PropertyGroupDescription("DefinitionName"));
PropertyAssists.Load();
foreach (var property in Properties)
{
var defaultValue = PropertyAssists.GetDefaultValue(property.DefinitionName, property.Name);
if (!string.IsNullOrEmpty(defaultValue))
{
property.DefaultValue = defaultValue;
}
else
{
// 如果没有默认值,则使用约束中的第一个值作为默认值
property.DefaultValue = property.ValueConstraints.FirstOrDefault();
}
}
//Properties = allProps;
//collectionView = CollectionViewSource.GetDefaultView(Properties);
//foreach (var prop in Properties)
//{
// prop.DefaultValue = prop.ValueConstraints?.FirstOrDefault();
// if (prop.ParameterType == ParameterType.Number)
// {
// prop.DefaultValue = "0";
// }
//}
}
partial void OnFilterTextChanged(string value)
{
Collection.Filter = item =>
{
if (string.IsNullOrEmpty(FilterText))
{
return true;
}
if (item is AfcaArchiProperty param)
{
return new[] { param.DefinitionName, param.Name, param.DefaultValue }.Any(
t => t != null && t.Contains(FilterText));
}
return false;
};
//collectionView.Refresh();
}
/// <summary>
/// 全选过滤后的参数
/// </summary>
[ObservableProperty]
public partial bool CheckedForFilter { get; set; }
//[RelayCommand]
//private void ToggleAllEnabled()
//{
// foreach (var config in Configs)
// {
// config.IsEnabled = areAllEnabled;
// }
//}
partial void OnCheckedForFilterChanged(bool value)
{
//只选中
var filteredItems = Collection.SourceCollection
.Cast<AfcaArchiProperty>()
.Where(
param =>
{
if (string.IsNullOrEmpty(FilterText))
return true;
return new[] { param.DefinitionName, param.Name, param.DefaultValue }.Any(
t => t?.Contains(FilterText) ?? false);
})
.ToList(); // 转为 List 或其他集合
foreach (var param in filteredItems)
{
param.IsChecked = value;
}
}
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(ModifyParameterValueCommand))]
public partial bool IsIdling { get; set; } = true;
//[ObservableProperty]
//public bool CanModify()
//{
// return PropertyDictionary != null && PropertyDictionary.Any(p => p.IsChecked) && !IsRunning;
//}
[RelayCommand(CanExecute = nameof(IsIdling))]
private void ModifyParameterValue()
{
if (Properties == null || (Properties != null && !Properties.Any(p => p.IsChecked)))
{
MessageBox.Show("请勾选要修改参数值的参数");
return;
}
StringBuilder sb = new StringBuilder();
RvApp.Handler
.Raise(
uiapp =>
{
IsIdling = false;
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
IList<Element> elements = null;
//e.OfParentModelCollector
try
{
doc.Invoke(
ts =>
{
var paramsToFill = Properties.Where(p => p.IsChecked).ToList();
var definitions = paramsToFill.GroupBy(p => p.DefinitionName)
.Select(p => p.Key)
.Select(d => MvdLiteAssist.GetDefinitionByName(mvdData, d))
.ToList();
//foreach (var def in definitions)
//{
// var entity = MvdLiteAssist.GetDefinitionByName(mvdData, def);
//}
if (IsManual)
{
elements = uidoc.Selection
.PickElementsByRectangle(
new FuncFilter(
e => e.get_BoundingBox(null) != null &&
e.Category is { CategoryType: CategoryType.Model }), "请选择要填写参数的构件");
}
else
{
elements = doc.OfParentModelCollector()
.Where(
e =>
{
foreach (var definition in definitions)
{
return definition.TypeLabels
.Contains(e.LookupParameter("深圳构件标识").AsString());
}
return false;
//return definitions.Contains(e.LookupParameter("深圳构件标识").AsString());
})
.ToList();
}
foreach (var elem in elements)
{
foreach (var prop in paramsToFill)
{
var param = elem.LookupParameter(prop.Name);
if (param == null)
{
sb.AppendLine(
$"{elem.Id},{elem.Name},{prop.Name},请检查参数是否存在");
continue;
}
var result = param.SetValueString(prop.DefaultValue);
if (!result)
{
result = param.Set(prop.DefaultValue);
}
if (!result)
{
sb.AppendLine(
$"{elem.Id},{elem.Name},{prop.Name},请检查类型是否匹配");
}
//if (!result)
//{
// MessageBox.Show(
// $"构件{elem.Id}的参数{prop.Name}设置失败,请检查参数是否存在或类型是否匹配",
// "错误",
// MessageBoxButton.OK,
// MessageBoxImage.Error);
//}
}
}
},
"填写建筑工程参数");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
//CanModify = true;
}
IsIdling = true;
if (sb.Length > 0)
{
var result = MessageBox.Show($"{sb}", "设置失败", MessageBoxButton.OK, MessageBoxImage.Error);
// if (result == MessageBoxResult.OK)
// {
// sb.Insert(0, "构件Id,构件名称,参数名称,错误信息\r\n");
// File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\填写" + ".csv",
//sb.ToString(), Encoding.UTF8);
// }
}
else
{
MessageBox.Show($"已为{elements.Count()}个构件填写参数");
}
});
}
}
}