351 lines
12 KiB
C#
351 lines
12 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using Autodesk.Revit.DB;
|
||
using Autodesk.Revit.UI;
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
using Microsoft.Win32;
|
||
using OfficeOpenXml;
|
||
using Sai.RvKits.IO;
|
||
using Sai.RvWrapper.Shared;
|
||
using SZBIM.StandardTools;
|
||
using SZBIM.StandardTools.ParameterManager;
|
||
|
||
namespace Sai.Common.Shared.ParameterManager
|
||
{
|
||
public partial class AddParamsViewModel : ObservableObject
|
||
{
|
||
public AddParamsViewModel(Autodesk.Revit.ApplicationServices.Application app)
|
||
{
|
||
this.app = app;
|
||
//项目参数
|
||
projectParamsHandler = new AddProjectParamsHandler();
|
||
projectParamsEvent = ExternalEvent.Create(projectParamsHandler);
|
||
//族文件添加族参数
|
||
familyFamilyParamsHandler = new AddFamilyFileFamilyParamsHandler();
|
||
familyFamilyParamsEvent = ExternalEvent.Create(familyFamilyParamsHandler);
|
||
//项目文件中的族添加族参数
|
||
projectFamilyParamsHandler = new AddProjectFileFamilyParamsHandler();
|
||
projectFamilyParamsEvent = ExternalEvent.Create(projectFamilyParamsHandler);
|
||
}
|
||
|
||
private readonly Autodesk.Revit.ApplicationServices.Application app;
|
||
|
||
/// <summary>
|
||
/// 添加到文件的外部事件(族参数)
|
||
/// </summary>
|
||
private readonly ExternalEvent familyFamilyParamsEvent;
|
||
|
||
/// <summary>
|
||
/// 添加族参数到族文件
|
||
/// </summary>
|
||
private readonly AddFamilyFileFamilyParamsHandler familyFamilyParamsHandler;
|
||
|
||
/// <summary>
|
||
/// 项目参数(绑定类别的外部事件)
|
||
/// </summary>
|
||
private readonly ExternalEvent projectFamilyParamsEvent;
|
||
|
||
/// <summary>
|
||
/// 项目文件的族参数
|
||
/// </summary>
|
||
private readonly AddProjectFileFamilyParamsHandler projectFamilyParamsHandler;
|
||
|
||
/// <summary>
|
||
/// 项目中的族添加族参数(共享,非共享)
|
||
/// </summary>
|
||
private readonly ExternalEvent projectParamsEvent;
|
||
|
||
/// <summary>
|
||
/// 添加参数为项目参数绑定类别
|
||
/// </summary>
|
||
private readonly AddProjectParamsHandler projectParamsHandler;
|
||
|
||
|
||
[ObservableProperty] private string excelPath;
|
||
|
||
private string excelWorkbook;
|
||
|
||
[ObservableProperty] private string familyPath;
|
||
|
||
[ObservableProperty] private BuiltInParameterGroup group;
|
||
|
||
private bool? isAddToFile;
|
||
|
||
[ObservableProperty] private bool isFamilyParam;
|
||
|
||
[ObservableProperty] private bool isSharedFamilyParam;
|
||
|
||
[ObservableProperty] [NotifyPropertyChangedFor(nameof(IsNotSharedProjectParam))] [NotifyCanExecuteChangedFor(nameof(AddParamsToFilesCommand))]
|
||
private bool isSharedProjectParam;
|
||
|
||
[ObservableProperty] private Parameter parameter;
|
||
|
||
//[ObservableProperty]
|
||
//private UnitType unitType;
|
||
|
||
[ObservableProperty] private System.Collections.IEnumerable workSheets;
|
||
|
||
public bool? IsAddToFile
|
||
{
|
||
get => isAddToFile;
|
||
set => SetProperty(ref isAddToFile, value);
|
||
}
|
||
|
||
private bool IsNotSharedProjectParam => !isSharedProjectParam;
|
||
|
||
|
||
public ObservableCollection<ParamModel> ParamsList { get; set; }
|
||
|
||
|
||
public List<TreeModel> Models { get; } = new();
|
||
|
||
[RelayCommand]
|
||
private void AddParamItem()
|
||
{
|
||
if (ParamsList == null)
|
||
{
|
||
ParamsList = new ObservableCollection<ParamModel>
|
||
{
|
||
new()
|
||
};
|
||
}
|
||
else
|
||
{
|
||
ParamsList.Add(new ParamModel());
|
||
}
|
||
}
|
||
|
||
[RelayCommand(CanExecute = nameof(IsNotSharedProjectParam))]
|
||
private void AddParamsToFiles()
|
||
{
|
||
familyFamilyParamsHandler.IsFamilyParam = IsFamilyParam;
|
||
familyFamilyParamsHandler.ParameterList = ParamsList.ToList();
|
||
familyFamilyParamsEvent.Raise();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加到项目中的族(包含绑定类别的共享参数和族参数)
|
||
/// </summary>
|
||
[RelayCommand]
|
||
private void AddParamsToProject()
|
||
{
|
||
if (IsSharedProjectParam)
|
||
{
|
||
projectParamsHandler.ParameterList = ParamsList.ToList();
|
||
projectParamsEvent.Raise();
|
||
return;
|
||
}
|
||
|
||
projectFamilyParamsHandler.ParameterList = ParamsList.ToList();
|
||
if (IsFamilyParam)
|
||
{
|
||
projectFamilyParamsHandler.IsFamilyParam = true;
|
||
projectFamilyParamsEvent.Raise();
|
||
}
|
||
|
||
if (IsSharedFamilyParam)
|
||
{
|
||
projectFamilyParamsHandler.IsFamilyParam = false;
|
||
projectFamilyParamsEvent.Raise();
|
||
}
|
||
}
|
||
|
||
|
||
private void GenerateArchiTree(IList<ParamModel> paramsData)
|
||
{
|
||
var majors = paramsData.GroupBy(param => param.Major);
|
||
|
||
//专业
|
||
foreach (var major in majors)
|
||
{
|
||
List<TreeModel> majorChildren = new List<TreeModel>();
|
||
TreeModel majorTreeModel = new TreeModel
|
||
{
|
||
Name = major.Key,
|
||
Icon = null,
|
||
IsChecked = false,
|
||
IsExpanded = false,
|
||
Children = majorChildren,
|
||
Tag = null
|
||
};
|
||
Models.Add(majorTreeModel);
|
||
//构件
|
||
var components = major.GroupBy(p => p.Component);
|
||
foreach (IGrouping<string, ParamModel> component in components)
|
||
{
|
||
TreeModel componentTreeModel = new TreeModel
|
||
{
|
||
Id = null,
|
||
Name = component.Key,
|
||
Icon = null,
|
||
IsExpanded = false,
|
||
Parent = null,
|
||
Children = null,
|
||
Tag = component.ToList()
|
||
};
|
||
majorChildren.Add(componentTreeModel);
|
||
}
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void ImportExcel()
|
||
{
|
||
string filter = "导入文件(*.xlsx)|*.xlsx";
|
||
OpenFileDialog openDialog = new OpenFileDialog
|
||
{
|
||
Filter = filter,
|
||
Title = "选择Excel文件"
|
||
};
|
||
openDialog.ShowDialog();
|
||
excelWorkbook = openDialog.FileName;
|
||
if (excelWorkbook != null)
|
||
{
|
||
ExcelPath = excelWorkbook;
|
||
|
||
ExcelPackage p = new ExcelPackage(new FileInfo(excelWorkbook));
|
||
WorkSheets = p.Workbook.Worksheets;
|
||
//CBSheetName.ItemsSource = p.Workbook.Worksheets.ToList();
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void ImportParams()
|
||
{
|
||
DataTable dt = new DataTable();
|
||
DirectoryInfo dir = new DirectoryInfo(FamilyPath);
|
||
//path为某个目录,如: “D:\Program Files”
|
||
FileInfo[] fiAr = dir.GetFiles();
|
||
foreach (FileInfo fi in fiAr)
|
||
{
|
||
if (fi.Extension.Equals(".rfa"))
|
||
{
|
||
Document doc = app.OpenDocumentFile(fi.FullName);
|
||
FamilyManager fm = doc.FamilyManager;
|
||
|
||
doc.Invoke(ts =>
|
||
{
|
||
for (int i = 0; i < dt.Rows.Count; i++)
|
||
{
|
||
string typestr = dt.Rows[i][3].ToString();
|
||
bool isInstance;
|
||
if (typestr == "实例")
|
||
{
|
||
isInstance = true;
|
||
}
|
||
else if (typestr == "类型")
|
||
{
|
||
isInstance = false;
|
||
}
|
||
else
|
||
{
|
||
continue;
|
||
}
|
||
|
||
Type tybpg = typeof(BuiltInParameterGroup);
|
||
var bpgAr = Enum.GetNames(typeof(BuiltInParameterGroup));
|
||
BuiltInParameterGroup bpg = BuiltInParameterGroup.INVALID;
|
||
foreach (string m in bpgAr)
|
||
{
|
||
bpg = (BuiltInParameterGroup)Enum.Parse(tybpg, m);
|
||
if (m == "Invalid" || m == "FamilyType")
|
||
{
|
||
continue;
|
||
}
|
||
|
||
string name = LabelUtils.GetLabelFor(bpg);
|
||
string tabname = dt.Rows[i][0].ToString();
|
||
if (name == tabname)
|
||
break;
|
||
}
|
||
|
||
Type type = typeof(ParameterType);
|
||
var names = Enum.GetNames(typeof(ParameterType));
|
||
ParameterType pt = ParameterType.Invalid;
|
||
foreach (string n in names)
|
||
{
|
||
pt = (ParameterType)Enum.Parse(type, n);
|
||
if (n == "Invalid" || n == "FamilyType")
|
||
{
|
||
continue;
|
||
}
|
||
|
||
string name = LabelUtils.GetLabelFor(pt);
|
||
string tabname = dt.Rows[i][2].ToString();
|
||
if (name == tabname)
|
||
break;
|
||
//System.Windows.MessageBox.ShowTop(string.Format("{0}{1}{2}", "第", i + 1, "行参数类型错误"));
|
||
}
|
||
|
||
fm.AddParameter(dt.Rows[i][1].ToString(), bpg, pt, isInstance);
|
||
//string na = LabelUtils.GetLabelFor(BuiltInParameterGroup.INVALID);
|
||
}
|
||
}, "添加族参数");
|
||
doc.Close(true);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ImportSharedParams(Autodesk.Revit.ApplicationServices.Application app)
|
||
{
|
||
DataTable dt = new DataTable();
|
||
DefinitionFile dfile = app.OpenSharedParameterFile();
|
||
DefinitionGroups gps = dfile.Groups;
|
||
for (int i = 0; i < dt.Rows.Count; i++)
|
||
{
|
||
var gp = gps.get_Item(dt.Rows[i][0].ToString()) ?? gps.Create(dt.Rows[i][0].ToString());
|
||
|
||
var strAr = Enum.GetNames(typeof(ParameterType));
|
||
foreach (string n in strAr)
|
||
{
|
||
ParameterType pt = (ParameterType)Enum.Parse(typeof(ParameterType), n);
|
||
if (n == "Invalid" || n == "FamilyType")
|
||
{
|
||
continue;
|
||
}
|
||
|
||
string name = LabelUtils.GetLabelFor(pt);
|
||
string tabname = dt.Rows[i][2].ToString();
|
||
if (name == tabname)
|
||
{
|
||
ExternalDefinitionCreationOptions op =
|
||
new ExternalDefinitionCreationOptions(dt.Rows[i][1].ToString(), pt);
|
||
op.UserModifiable = true;
|
||
Definition df = gp.Definitions.Create(op);
|
||
break;
|
||
}
|
||
//else
|
||
//{
|
||
// System.Windows.MessageBox.ShowTop("第" + i + 1 + "行参数类型错误");
|
||
//}
|
||
}
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void RemoveParamItem(object obj)
|
||
{
|
||
if (obj is ParamModel paramModel)
|
||
{
|
||
ParamsList.Remove(paramModel);
|
||
}
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void SelectFamilies()
|
||
{
|
||
VistaFolderBrowserDialog dialog = new VistaFolderBrowserDialog();
|
||
//dialog.Description = "请选择文件路径";
|
||
//dialog.SeletcedPath = @"C:\Users\ZGG\Documents";
|
||
if (dialog.ShowDialog())
|
||
{
|
||
FamilyPath = dialog.SelectedPath;
|
||
}
|
||
}
|
||
}
|
||
} |