using System.IO; using System.Windows; using Autodesk.Revit.DB; using Nice3point.Revit.Toolkit.External.Handlers; using ShrlAlgoToolkit.Revit.Assists; using Exception = System.Exception; using UIFrameworkServices; namespace ShrlAlgo.Addin.Test; /// /// SeparateModelWin.xaml 的交互逻辑 /// 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; } } }); } private void SpearateByIds(Document doc, System.Collections.Generic.ICollection list, bool isA) { var allOthers = doc.OfModelCollector()? .Excluding(list) .ToElementIds(); if (allOthers == null) { return; } var ids = doc.OfClass().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 }; doc.SaveAs(filePath, options); QuickAccessToolBarService.performMultipleUndoRedoOperations(true, 1); } }