Files
Shrlalgo.RvKits/ShrlAlgoToolkit.RevitAddins/RvMEP/StandMepCurveViewModel.cs
2025-04-24 20:56:10 +08:00

178 lines
6.3 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI.Selection;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nice3point.Revit.Toolkit.External.Handlers;
namespace ShrlAlgo.RvKits.RvMEP;
public partial class StandMepCurveViewModel : ObservableObject
{
public StandMepCurveViewModel(Document doc)
{
PipeTypes = doc.OfClass<PipeType>().Cast<PipeType>();
DuctTypes = doc.OfClass<DuctType>().Cast<DuctType>();
ConduitTypes = doc.OfClass<ConduitType>().Cast<ConduitType>();
CableTrayTypes = doc.OfClass<CableTrayType>().Cast<CableTrayType>();
PipeSystemTypes = doc.OfClass<PipingSystemType>().Cast<PipingSystemType>();
DuctSystemTypes = doc.OfClass<MechanicalSystemType>().Cast<MechanicalSystemType>();
}
private readonly ActionEventHandler standMepCurveEventHandler = new();
[ObservableProperty]
private IEnumerable<CableTrayType> cableTrayTypes;
[ObservableProperty]
private IEnumerable<ConduitType> conduitTypes;
[ObservableProperty]
private IEnumerable<MechanicalSystemType> ductSystemTypes;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private IEnumerable<DuctType> ductTypes;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private bool? isCableTray;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private bool? isConduit;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private bool? isDuct;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private bool? isPipe = true;
[ObservableProperty]
private IEnumerable<PipingSystemType> pipeSystemTypes;
[ObservableProperty]
private IEnumerable<PipeType> pipeTypes;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private CableTrayType selectedCableTrayType;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private ConduitType selectedConduitType;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private MechanicalSystemType selectedDuctSystemType;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private DuctType selectedDuctType;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private PipingSystemType selectedPipeSystemType;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CreateMepCurveCommand))]
private PipeType selectedPipeType;
partial void OnSelectedPipeTypeChanged(PipeType value)
{
var rule = value.RoutingPreferenceManager.GetRule(RoutingPreferenceRuleGroupType.Segments, 0);
var segment = value.Document.GetElement(rule.MEPPartId) as PipeSegment;
PipeSizes = segment.GetSizes();
}
[ObservableProperty]
private ICollection<MEPSize> pipeSizes;
private bool isRunning;
private bool CanCreate()
{
return (IsPipe == true && SelectedPipeType != null && SelectedPipeSystemType != null)
|| (IsDuct == true && SelectedDuctType != null && SelectedDuctSystemType != null)
|| (IsConduit == true && SelectedConduitType != null)
|| (IsCableTray == true && SelectedCableTrayType != null && !isRunning);
}
[RelayCommand]
private void Cancel()
{
if (!isRunning)
{
return;
}
KeyIntPtrHelper.RaiseEscTwice();
}
[RelayCommand(CanExecute = nameof(CanCreate))]
private void CreateMepCurve()
{
isRunning = true;
standMepCurveEventHandler.Raise(app =>
{
try
{
while (true)
{
var uidoc = app.ActiveUIDocument;
var view = uidoc.ActiveView;
var doc = uidoc.Document;
const ObjectSnapTypes ss = ObjectSnapTypes.Points
| ObjectSnapTypes.Endpoints
| ObjectSnapTypes.Intersections
| ObjectSnapTypes.Centers
| ObjectSnapTypes.Midpoints
| ObjectSnapTypes.Perpendicular
| ObjectSnapTypes.Quadrants
| ObjectSnapTypes.Tangents;
var point = uidoc.Selection.PickPoint(ss, "选择创建立管的点");
var startPoint = new XYZ(point.X, point.Y, view.GenLevel.Elevation);
var endPoint = new XYZ(point.X, point.Y, view.GenLevel.Elevation + (2000 / 304.8));
doc.Invoke(
_ =>
{
if (IsPipe == true)
{
Pipe.Create(doc, SelectedPipeSystemType.Id, SelectedPipeType.Id, view.GenLevel.Id, startPoint, endPoint);
}
else if (IsDuct == true)
{
Duct.Create(doc, SelectedDuctSystemType.Id, SelectedDuctType.Id, view.GenLevel.Id, startPoint, endPoint);
}
else if (IsConduit == true)
{
Conduit.Create(doc, SelectedConduitType.Id, startPoint, endPoint, view.GenLevel.Id);
}
else if (IsCableTray == true)
{
CableTray.Create(doc, SelectedCableTrayType.Id, startPoint, endPoint, view.GenLevel.Id);
}
},
"创建立管"
);
}
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
}
});
isRunning = false;
}
}