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; using ShrlAlgoToolkit.RevitAddins.RvMEP; using ShrlAlgoToolkit; using ShrlAlgoToolkit.RevitAddins; namespace ShrlAlgoToolkit.RevitAddins.Mep; public partial class StandMepCurveViewModel : ObservableObject { public StandMepCurveViewModel(Document doc) { PipeTypes = doc.OfClass().Cast(); DuctTypes = doc.OfClass().Cast(); ConduitTypes = doc.OfClass().Cast(); CableTrayTypes = doc.OfClass().Cast(); PipeSystemTypes = doc.OfClass().Cast(); DuctSystemTypes = doc.OfClass().Cast(); } private readonly ActionEventHandler standMepCurveEventHandler = new(); [ObservableProperty] public partial IEnumerable CableTrayTypes { get; set; } [ObservableProperty] public partial IEnumerable ConduitTypes { get; set; } [ObservableProperty] public partial IEnumerable DuctSystemTypes { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial IEnumerable DuctTypes { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial bool? IsCableTray { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial bool? IsConduit { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial bool? IsDuct { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial bool? IsPipe { get; set; } = true; [ObservableProperty] public partial IEnumerable PipeSystemTypes { get; set; } [ObservableProperty] public partial IEnumerable PipeTypes { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial CableTrayType SelectedCableTrayType { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial ConduitType SelectedConduitType { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial MechanicalSystemType SelectedDuctSystemType { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial DuctType SelectedDuctType { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial PipingSystemType SelectedPipeSystemType { get; set; } [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))] public partial PipeType SelectedPipeType { get; set; } 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] public partial ICollection PipeSizes { get; set; } 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; } }