整理代码
This commit is contained in:
332
ShrlAlgoToolkit.RevitAddins/RvView/VisibilityViewModel.cs
Normal file
332
ShrlAlgoToolkit.RevitAddins/RvView/VisibilityViewModel.cs
Normal file
@@ -0,0 +1,332 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls.Primitives;
|
||||
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||||
|
||||
|
||||
namespace ShrlAlgo.RvKits.RvView;
|
||||
|
||||
public partial class VisibilityViewModel : ObservableObject
|
||||
{
|
||||
public ActionEventHandler VisibilityHandler { get; } = new();
|
||||
|
||||
[ObservableProperty]
|
||||
private bool wallChecked;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool floorChecked;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool beamChecked;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool mechanicalChecked;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool columnChecked;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool plumbingChecked;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool cableTrayChecked;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool conduitChecked;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user