Files

176 lines
7.0 KiB
C#
Raw Permalink Normal View History

2025-04-24 20:56:44 +08:00
using Autodesk.Revit.DB;
2024-09-22 11:05:41 +08:00
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;
2026-02-22 20:03:42 +08:00
using ShrlAlgoToolkit.RevitAddins.RvMEP;
using ShrlAlgoToolkit;
using ShrlAlgoToolkit.RevitAddins;
2024-09-22 11:05:41 +08:00
2026-02-22 20:03:42 +08:00
namespace ShrlAlgoToolkit.RevitAddins.Mep;
2024-09-22 11:05:41 +08:00
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]
public partial IEnumerable<CableTrayType> CableTrayTypes { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
public partial IEnumerable<ConduitType> ConduitTypes { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
public partial IEnumerable<MechanicalSystemType> DuctSystemTypes { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial IEnumerable<DuctType> DuctTypes { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial bool? IsCableTray { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial bool? IsConduit { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial bool? IsDuct { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial bool? IsPipe { get; set; } = true;
2024-09-22 11:05:41 +08:00
[ObservableProperty]
public partial IEnumerable<PipingSystemType> PipeSystemTypes { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
public partial IEnumerable<PipeType> PipeTypes { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial CableTrayType SelectedCableTrayType { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial ConduitType SelectedConduitType { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial MechanicalSystemType SelectedDuctSystemType { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial DuctType SelectedDuctType { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial PipingSystemType SelectedPipeSystemType { get; set; }
2024-09-22 11:05:41 +08:00
[ObservableProperty]
2026-02-22 20:03:42 +08:00
[NotifyCanExecuteChangedFor(nameof(Mep.StandMepCurveViewModel.CreateMepCurveCommand))]
public partial PipeType SelectedPipeType { get; set; }
2024-09-22 11:05:41 +08:00
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<MEPSize> PipeSizes { get; set; }
2024-09-22 11:05:41 +08:00
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;
}
2024-12-22 10:26:12 +08:00
KeyIntPtrHelper.RaiseEscTwice();
2024-09-22 11:05:41 +08:00
}
[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;
}
}