145 lines
6.0 KiB
C#
145 lines
6.0 KiB
C#
using Autodesk.Revit.Attributes;
|
|
using Autodesk.Revit.DB;
|
|
|
|
using Nice3point.Revit.Toolkit.External;
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
using System.Windows;
|
|
|
|
namespace Szmedi.RvKits.ModelManager;
|
|
|
|
[Transaction(TransactionMode.Manual)]
|
|
|
|
public class SetModelDisplayCmd : ExternalCommand
|
|
{
|
|
public override void Execute()
|
|
{
|
|
StringBuilder sb = new();
|
|
using (TransactionGroup tg = new(Document, "清理辅助图元"))
|
|
{
|
|
tg.Start();
|
|
//建模过程中所使用到的“模型线”“参照线”“辅助线”“参照平面”等临时模型,及时删除。
|
|
using (Transaction trans = new(Document, "隐藏模型线"))
|
|
{
|
|
try
|
|
{
|
|
List<ElementId> elementToDelete = new();
|
|
ICollection<ElementId> mc = new FilteredElementCollector(Document)
|
|
.OfClass(typeof(CurveElement))
|
|
.OfCategory(BuiltInCategory.OST_Lines)
|
|
.ToElementIds();
|
|
IEnumerable<ElementId> referPlane = new FilteredElementCollector(Document)
|
|
.OfClass(typeof(ReferencePlane))
|
|
.OfCategory(BuiltInCategory.OST_CLines)
|
|
.Where(e => e.get_BoundingBox(Document.ActiveView) != null)
|
|
.Select(e => e.Id);
|
|
elementToDelete.AddRange(mc);
|
|
elementToDelete.AddRange(referPlane);
|
|
trans.Start();
|
|
IEnumerable<FamilyInstance> instances = new FilteredElementCollector(Document)
|
|
.OfClass(typeof(FamilyInstance))
|
|
.Cast<FamilyInstance>();
|
|
List<ElementId> eleIdsToHide = new();
|
|
|
|
foreach (FamilyInstance instance in instances)
|
|
{
|
|
if (instance.Host != null)
|
|
{
|
|
List<ElementId> existId = elementToDelete.FindAll(e => e == instance.Host.Id);
|
|
if (existId.Any())
|
|
{
|
|
eleIdsToHide.Add(existId.FirstOrDefault());
|
|
elementToDelete.RemoveAll(e => e == instance.Host.Id);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (eleIdsToHide.Any())
|
|
{
|
|
ActiveView.HideElements(eleIdsToHide);
|
|
}
|
|
if (elementToDelete.Any())
|
|
{
|
|
Document.Delete(elementToDelete);
|
|
}
|
|
trans.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
sb.AppendLine($"隐藏元素时,出现{ex.Message}的错误。");
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|
{
|
|
trans.Commit();
|
|
}
|
|
}
|
|
}
|
|
|
|
//建模过程中绘制结构构件等有时会无意中勾选产生“分析线”“分析节点”等,注意清理。
|
|
using (Transaction trans = new(Document, "处理分析模型"))
|
|
{
|
|
var views = new FilteredElementCollector(Document).OfClass(typeof(View)).OfType<View>();
|
|
List<Element> analyticalElements = new();
|
|
|
|
IList<Element> structuralColumns = new FilteredElementCollector(Document)
|
|
.OfClass(typeof(FamilyInstance))
|
|
.OfCategory(BuiltInCategory.OST_StructuralColumns)
|
|
.ToElements();
|
|
IList<Element> structuralFoundations = new FilteredElementCollector(Document)
|
|
.OfClass(typeof(FamilyInstance))
|
|
.OfCategory(BuiltInCategory.OST_StructuralFoundation)
|
|
.ToElements();
|
|
IList<Element> structuralWall = new FilteredElementCollector(Document).OfClass(typeof(Wall)).ToElements();
|
|
IList<Element> structuralFraming = new FilteredElementCollector(Document)
|
|
.OfClass(typeof(FamilyInstance))
|
|
.OfCategory(BuiltInCategory.OST_StructuralFraming)
|
|
.ToElements();
|
|
IList<Element> structuralFloor = new FilteredElementCollector(Document).OfClass(typeof(Floor)).ToElements();
|
|
|
|
analyticalElements.AddRange(structuralColumns);
|
|
analyticalElements.AddRange(structuralFoundations);
|
|
analyticalElements.AddRange(structuralWall);
|
|
analyticalElements.AddRange(structuralFraming);
|
|
analyticalElements.AddRange(structuralFloor);
|
|
try
|
|
{
|
|
trans.Start();
|
|
foreach (View v in views)
|
|
{
|
|
if (v.CanHaveAnalyticalModel())
|
|
{
|
|
ActiveView.AreAnalyticalModelCategoriesHidden = true;
|
|
}
|
|
}
|
|
|
|
foreach (Element element in analyticalElements)
|
|
{
|
|
Parameter param = element.get_Parameter(BuiltInParameter.STRUCTURAL_ANALYTICAL_MODEL);
|
|
if (param is { IsReadOnly: false })
|
|
{
|
|
param.Set(0);
|
|
}
|
|
}
|
|
trans.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
sb.AppendLine($"关闭分析模型显示时,出现{ex.Message}的错误。");
|
|
if (trans.GetStatus() == TransactionStatus.Started)
|
|
{
|
|
trans.Commit();
|
|
}
|
|
}
|
|
}
|
|
tg.Assimilate();
|
|
}
|
|
|
|
if (sb.Length > 0)
|
|
{
|
|
MessageBox.Show($"{sb}", "处理结果", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|