Files
ShrlAlgoToolkit/ShrlAlgoToolkit.RevitAddins/General/SeparateModelViewModel.cs
2026-02-24 11:34:18 +08:00

77 lines
2.7 KiB
C#

using System.IO;
using System.Windows;
using Autodesk.Revit.DB;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nice3point.Revit.Toolkit.External.Handlers;
using UIFrameworkServices;
namespace ShrlAlgoToolkit.RevitAddins.General
{
public partial class SeparateModelViewModel : ObservableObject
{
private readonly ActionEventHandler separate = new();
[ObservableProperty]
public partial string FileName { get; set; } = "示例";
[ObservableProperty]
public partial string FolderPath { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
[RelayCommand]
private void Separate()
{
separate.Raise(
uiapp =>
{
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
var list = uidoc.Selection.GetElementIds();
if (list.Count == 0)
{
MessageBox.Show("未选中元素", "提示");
return;
}
SpearateByIds(doc, list);
doc.Invoke(
_ =>
{
doc.Delete(list);
},
"删除元素");
});
}
private void SpearateByIds(Document doc, ICollection<ElementId> list)
{
var allOthers = doc.OfModelCollector()?
.Excluding(list)
.ToElementIds();
if (allOthers == null)
{
return;
}
var ids = doc.OfClass<ImportInstance>().ToElementIds();
var modelLineIds = doc.OfCollector().OfCategory(BuiltInCategory.OST_Lines).Select(e => e.Id).ToList();
var texts = doc.OfCollector().OfCategory(BuiltInCategory.OST_TextNotes).Select(e => e.Id).ToList();
doc.Invoke(
_ =>
{
doc.Delete(allOthers);
doc.Delete(ids);
doc.Delete(modelLineIds);
doc.Delete(texts);
},
"删除其他");
if (!Directory.Exists(FolderPath))
{
Directory.CreateDirectory(FolderPath);
}
var filePath = Path.Combine(FolderPath, $"{FileName}.rvt");
SaveAsOptions options = new SaveAsOptions() { OverwriteExistingFile = true, PreviewViewId = doc.ActiveView.Id, Compact = true };
doc.SaveAs(filePath, options);
QuickAccessToolBarService.performMultipleUndoRedoOperations(true, 1);
}
}
}