328 lines
11 KiB
C#
328 lines
11 KiB
C#
using System.Windows;
|
||
using Autodesk.Revit.DB;
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||
|
||
|
||
namespace ShrlAlgoToolkit.RevitAddins.RvView;
|
||
|
||
public partial class VisibilityViewModel : ObservableObject
|
||
{
|
||
public ActionEventHandler VisibilityHandler { get; } = new();
|
||
|
||
[ObservableProperty]
|
||
public partial bool WallChecked { get; set; }
|
||
|
||
[ObservableProperty]
|
||
public partial bool FloorChecked { get; set; }
|
||
|
||
[ObservableProperty]
|
||
public partial bool BeamChecked { get; set; }
|
||
|
||
[ObservableProperty]
|
||
public partial bool MechanicalChecked { get; set; }
|
||
|
||
[ObservableProperty]
|
||
public partial bool ColumnChecked { get; set; }
|
||
|
||
[ObservableProperty]
|
||
public partial bool PlumbingChecked { get; set; }
|
||
|
||
[ObservableProperty]
|
||
public partial bool CableTrayChecked { get; set; }
|
||
|
||
[ObservableProperty]
|
||
public partial bool ConduitChecked { get; set; }
|
||
|
||
partial void OnBeamCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in beamCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"梁显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
partial void OnCableTrayCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in cabletrayCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"桥架显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
partial void OnColumnCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in columnsCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"柱显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
partial void OnWallCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in wallCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"墙显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
partial void OnFloorCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in floorCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"楼板显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
partial void OnConduitCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in conduitCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"线管显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
partial void OnMechanicalCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in mechanicalCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"暖通显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
partial void OnPlumbingCheckedChanged(bool value)
|
||
{
|
||
VisibilityHandler.Raise(uiapp =>
|
||
{
|
||
UIDocument uidoc = uiapp.ActiveUIDocument;
|
||
var doc = uidoc.Document;
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
MessageBox.Show("当前视图已应用视图样板,无法修改其可见性", "提示");
|
||
return;
|
||
}
|
||
doc.Invoke(
|
||
_ =>
|
||
{
|
||
foreach (var builtInCategory in pipingCategory)
|
||
{
|
||
var categoryId = Category.GetCategory(doc, builtInCategory).Id;
|
||
|
||
uidoc.ActiveView.SetCategoryHidden(categoryId, value == false);
|
||
}
|
||
},
|
||
"给排水显隐"
|
||
);
|
||
});
|
||
}
|
||
|
||
public VisibilityViewModel(UIApplication uiapp)
|
||
{
|
||
UpdateCheckedState(uiapp.ActiveUIDocument.Document);
|
||
uiapp.ViewActivated -= Uiapp_ViewActivated;
|
||
uiapp.ViewActivated += Uiapp_ViewActivated;
|
||
}
|
||
|
||
private void Uiapp_ViewActivated(object sender, Autodesk.Revit.UI.Events.ViewActivatedEventArgs e)
|
||
{
|
||
if (!e.Document.IsFamilyDocument)
|
||
{
|
||
UpdateCheckedState(e.Document);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// true为可见,false为不可见
|
||
/// </summary>
|
||
/// <param name="doc"></param>
|
||
/// <param name="builtInCategories"></param>
|
||
/// <returns></returns>
|
||
private static bool GetCheckedStatue(Document doc, params BuiltInCategory[] builtInCategories)
|
||
{
|
||
foreach (var cate in builtInCategories)
|
||
{
|
||
var category = Category.GetCategory(doc, cate);
|
||
return !doc.ActiveView.GetCategoryHidden(category.Id);
|
||
}
|
||
return true;
|
||
}
|
||
readonly BuiltInCategory[] wallCategory = [BuiltInCategory.OST_Walls];
|
||
readonly BuiltInCategory[] beamCategory = [BuiltInCategory.OST_StructuralFraming];
|
||
readonly BuiltInCategory[] floorCategory = [BuiltInCategory.OST_Floors];
|
||
readonly BuiltInCategory[] columnsCategory = [
|
||
BuiltInCategory.OST_StructuralColumns,
|
||
BuiltInCategory.OST_Columns];
|
||
readonly BuiltInCategory[] mechanicalCategory = [
|
||
BuiltInCategory.OST_DuctCurves,
|
||
BuiltInCategory.OST_DuctAccessory,
|
||
BuiltInCategory.OST_DuctFitting,
|
||
BuiltInCategory.OST_DuctInsulations,
|
||
BuiltInCategory.OST_DuctTerminal,
|
||
BuiltInCategory.OST_FlexDuctCurves,
|
||
BuiltInCategory.OST_PlaceHolderDucts,BuiltInCategory.OST_MechanicalEquipment];
|
||
readonly BuiltInCategory[] pipingCategory = [
|
||
BuiltInCategory.OST_PipeCurves,
|
||
BuiltInCategory.OST_PipeFitting,
|
||
BuiltInCategory.OST_PipeAccessory,
|
||
BuiltInCategory.OST_PipeInsulations,
|
||
BuiltInCategory.OST_Sprinklers,
|
||
BuiltInCategory.OST_FlexPipeCurves,
|
||
BuiltInCategory.OST_PlaceHolderPipes,
|
||
BuiltInCategory.OST_PlumbingFixtures];
|
||
readonly BuiltInCategory[] cabletrayCategory = [
|
||
BuiltInCategory.OST_CableTray,
|
||
BuiltInCategory.OST_CableTrayFitting,];
|
||
readonly BuiltInCategory[] conduitCategory = [
|
||
BuiltInCategory.OST_Conduit,
|
||
BuiltInCategory.OST_ConduitFitting,];
|
||
|
||
private void UpdateCheckedState(Document doc)
|
||
{
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
return;
|
||
}
|
||
WallChecked = GetCheckedStatue(doc, wallCategory);
|
||
BeamChecked = GetCheckedStatue(doc, beamCategory);
|
||
FloorChecked = GetCheckedStatue(doc, floorCategory);
|
||
ColumnChecked = GetCheckedStatue(doc, columnsCategory);
|
||
MechanicalChecked = GetCheckedStatue(doc, mechanicalCategory);
|
||
PlumbingChecked = GetCheckedStatue(doc, pipingCategory);
|
||
CableTrayChecked = GetCheckedStatue(doc, cabletrayCategory);
|
||
ConduitChecked = GetCheckedStatue(doc, conduitCategory);
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void Closing()
|
||
{
|
||
VisibilityHandler.Raise(uiapp => uiapp.ViewActivated -= Uiapp_ViewActivated);
|
||
}
|
||
}
|