270 lines
9.4 KiB
C#
270 lines
9.4 KiB
C#
using System.Windows;
|
||
|
||
using Autodesk.Revit.DB;
|
||
using Autodesk.Revit.UI;
|
||
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
|
||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||
|
||
namespace Sai.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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_StructuralFraming), 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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_CableTray), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_CableTrayFitting), 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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_StructuralColumns), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_Columns), 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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_Walls), 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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_Floors), 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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_Conduit), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_ConduitFitting), 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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_DuctCurves), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_DuctAccessory), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_DuctFitting), 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(
|
||
_ =>
|
||
{
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_PipeCurves), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_PipeFitting), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_PipeAccessory), value == false);
|
||
uidoc.ActiveView.SetCategoryHidden(new ElementId(BuiltInCategory.OST_Sprinklers), 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;
|
||
}
|
||
|
||
private void UpdateCheckedState(Document doc)
|
||
{
|
||
if (doc.ActiveView.ViewTemplateId != ElementId.InvalidElementId)
|
||
{
|
||
return;
|
||
}
|
||
WallChecked = GetCheckedStatue(doc, BuiltInCategory.OST_Walls);
|
||
BeamChecked = GetCheckedStatue(doc, BuiltInCategory.OST_StructuralFraming);
|
||
FloorChecked = GetCheckedStatue(doc, BuiltInCategory.OST_Floors);
|
||
ColumnChecked = GetCheckedStatue(doc, BuiltInCategory.OST_StructuralColumns, BuiltInCategory.OST_Columns);
|
||
MechanicalChecked = GetCheckedStatue(doc, BuiltInCategory.OST_DuctCurves, BuiltInCategory.OST_DuctAccessory, BuiltInCategory.OST_DuctFitting);
|
||
PlumbingChecked = GetCheckedStatue(doc, BuiltInCategory.OST_PipeCurves, BuiltInCategory.OST_PipeFitting, BuiltInCategory.OST_PipeAccessory, BuiltInCategory.OST_Sprinklers);
|
||
CableTrayChecked = GetCheckedStatue(doc, BuiltInCategory.OST_CableTray, BuiltInCategory.OST_CableTrayFitting);
|
||
ConduitChecked = GetCheckedStatue(doc, BuiltInCategory.OST_Conduit, BuiltInCategory.OST_ConduitFitting);
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void Closing()
|
||
{
|
||
VisibilityHandler.Raise(uiapp => uiapp.ViewActivated -= Uiapp_ViewActivated);
|
||
}
|
||
}
|