2025-02-10 20:53:40 +08:00
|
|
|
|
using System.IO;
|
2024-12-22 10:26:12 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
|
|
using Autodesk.Revit.DB;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2024-12-22 10:26:12 +08:00
|
|
|
|
using Nice3point.Revit.Toolkit.External.Handlers;
|
2025-04-24 20:56:44 +08:00
|
|
|
|
using ShrlAlgoToolkit.Revit.Assists;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using Exception = System.Exception;
|
2024-12-22 10:26:12 +08:00
|
|
|
|
|
|
|
|
|
|
using UIFrameworkServices;
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
namespace ShrlAlgo.Addin.Test;
|
2024-12-22 10:26:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// SeparateModelWin.xaml 的交互逻辑
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class SeparateModelWin
|
|
|
|
|
|
{
|
|
|
|
|
|
ActionEventHandler separate = new ActionEventHandler();
|
|
|
|
|
|
public SeparateModelWin()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
TbFolder.Text = "F:\\燃气改造项目\\归档";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void MetroButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
separate.Raise(
|
|
|
|
|
|
uiapp =>
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var uidoc = uiapp.ActiveUIDocument;
|
|
|
|
|
|
var doc = uidoc.Document;
|
|
|
|
|
|
var list = uidoc.Selection.GetElementIds();
|
|
|
|
|
|
|
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("未选中元素", "提示");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var walls = list.Select(id => doc.GetElement(id)).Where(e => e is Wall).Select(e => e.Id).ToList();
|
|
|
|
|
|
var others = list.Select(id => doc.GetElement(id)).Where(e => e is not Wall).Select(e => e.Id).ToList();
|
|
|
|
|
|
SpearateByIds(doc, walls, true);
|
|
|
|
|
|
SpearateByIds(doc, others, false);
|
|
|
|
|
|
using (Transaction trans = new Transaction(doc, "删除选中"))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
trans.Start();
|
|
|
|
|
|
doc.Delete(list);
|
|
|
|
|
|
trans.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
private void SpearateByIds(Document doc, System.Collections.Generic.ICollection<ElementId> list, bool isA)
|
2024-12-22 10:26:12 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
var allOthers = doc.OfModelCollector()?
|
2024-12-22 10:26:12 +08:00
|
|
|
|
.Excluding(list)
|
|
|
|
|
|
.ToElementIds();
|
2025-02-10 20:53:40 +08:00
|
|
|
|
if (allOthers == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-12-22 10:26:12 +08:00
|
|
|
|
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();
|
|
|
|
|
|
using (Transaction trans = new Transaction(doc, "拆分模型"))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
trans.Start();
|
|
|
|
|
|
doc.Delete(allOthers);
|
|
|
|
|
|
doc.Delete(ids);
|
|
|
|
|
|
doc.Delete(modelLineIds);
|
|
|
|
|
|
doc.Delete(texts);
|
|
|
|
|
|
trans.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw ex;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!Directory.Exists(TbFolder.Text))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(TbFolder.Text);
|
|
|
|
|
|
}
|
|
|
|
|
|
var fileName = isA ? TbFileName.Text + "-A" : TbFileName.Text + "-GE";
|
|
|
|
|
|
var filePath = Path.Combine(TbFolder.Text, $"{fileName}.rvt");
|
|
|
|
|
|
SaveAsOptions options = new SaveAsOptions() { OverwriteExistingFile = true, PreviewViewId = doc.ActiveView.Id, Compact = true };
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
2024-12-22 10:26:12 +08:00
|
|
|
|
doc.SaveAs(filePath, options);
|
|
|
|
|
|
QuickAccessToolBarService.performMultipleUndoRedoOperations(true, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|