Files
SzmediTools/Szmedi.RvKits/MEPTools/CableLayoutViewModel.cs
2025-09-16 16:06:41 +08:00

107 lines
3.0 KiB
C#

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace Szmedi.RvKits.MEPTools
{
public partial class CableLayoutViewModel : ObservableValidator
{
private Document doc;
public CableLayoutViewModel(Document doc)
{
this.doc = doc;
specifications = new FilteredElementCollector(doc).OfClass(typeof(ConduitType)).Cast<ConduitType>().ToList();
}
[ObservableProperty]
private ConduitType selectedConduitType;
private int count;
[Range(1, 20, ErrorMessage = "输入值有误!")]
public int Count
{
get => count;
set => SetProperty(ref count, value, true);
}
[ObservableProperty]
private List<ConduitType> specifications;
[ObservableProperty]
private ConduitSize size;
[ObservableProperty]
private Dictionary<string, ConduitSize> sizes;
//public void m(doc doc)
//{
// var set = ConduitSizeSettings.GetConduitSizeSettings(doc);
// set.GetSizeCount();
//}
private RelayCommand<object> closeWin;
public ICommand CloseWin
{
get
{
closeWin ??= new RelayCommand<object>(PerformCloseWin);
return closeWin;
}
}
private void PerformCloseWin(object obj)
{
if (obj is Window && Count > 0 && Size != null && SelectedConduitType != null)
{
Window window = (Window)obj;
window.DialogResult = true;
}
}
private RelayCommand selectionType;
public ICommand SelectionType
{
get
{
selectionType ??= new RelayCommand(PerformSelectionType);
return selectionType;
}
}
private void PerformSelectionType()
{
IEnumerator<KeyValuePair<string, ConduitSizes>> enumerator = ConduitSizeSettings.GetConduitSizeSettings(doc).GetEnumerator();
var standardName = SelectedConduitType.get_Parameter(BuiltInParameter.CONDUIT_STANDARD_TYPE_PARAM).AsValueString();
ConduitSizes sizes = null;
Dictionary<string, ConduitSize> dictionary = new();
while (enumerator.MoveNext())
{
var current = enumerator.Current;
if (current.Key == standardName)
{
sizes = current.Value;
break;
}
}
if (sizes != null)
{
foreach (var size in sizes)
{
string key = (size.NominalDiameter * 304.8).ToString();
dictionary.Add(key, size);
}
}
Sizes = dictionary;
}
}
}