From 7451d8e4ee35e6d2b52c15680267d5adb6d8fba4 Mon Sep 17 00:00:00 2001 From: connorivy <43247197+connorivy@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:37:10 -0500 Subject: [PATCH] Dui3 178 gridlines (#3554) add gridline conversion Co-authored-by: Connor Ivy --- .../Speckle.Converters.RevitShared.projitems | 1 + .../GridlineToHostTopLevelConverter.cs | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/ToHost/TopLevel/GridlineToHostTopLevelConverter.cs diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems index dce4c4898e..2db6172222 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems @@ -36,6 +36,7 @@ + diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/ToHost/TopLevel/GridlineToHostTopLevelConverter.cs b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/ToHost/TopLevel/GridlineToHostTopLevelConverter.cs new file mode 100644 index 0000000000..24080b9516 --- /dev/null +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/ToHost/TopLevel/GridlineToHostTopLevelConverter.cs @@ -0,0 +1,56 @@ +using Objects; +using Speckle.Converters.Common; +using Speckle.Converters.Common.Objects; +using Speckle.Converters.RevitShared.Helpers; +using Speckle.Converters.RevitShared.ToSpeckle; + +namespace Speckle.Converters.RevitShared.ToHost.TopLevel; + +[NameAndRankValue(nameof(SOBE.GridLine), 0)] +internal class GridlineToHostTopLevelConverter : BaseTopLevelConverterToHost +{ + private readonly ITypedConverter _curveConverter; + private readonly IRevitConversionContextStack _contextStack; + + public GridlineToHostTopLevelConverter( + ITypedConverter curveConverter, + IRevitConversionContextStack contextStack + ) + { + _curveConverter = curveConverter; + _contextStack = contextStack; + } + + public override DB.Grid Convert(SOBE.GridLine target) + { + DB.Curve curve = _curveConverter.Convert(target.baseLine).get_Item(0); + + using DB.Grid revitGrid = curve switch + { + DB.Arc arc => DB.Grid.Create(_contextStack.Current.Document, arc), + DB.Line line => DB.Grid.Create(_contextStack.Current.Document, line), + _ => throw new SpeckleConversionException($"Grid line curve is of type {curve.GetType()} which is not supported") + }; + + if (!string.IsNullOrEmpty(target.label) && !GridNameIsTaken(target.label)) + { + revitGrid.Name = target.label; + } + + return revitGrid; + } + + private bool GridNameIsTaken(string gridName) + { + using var collector = new DB.FilteredElementCollector(_contextStack.Current.Document); + + IEnumerable gridNames = collector + .WhereElementIsNotElementType() + .OfClass(typeof(DB.Grid)) + .ToElements() + .Cast() + .Select(grid => grid.Name); + + return gridNames.Contains(gridName); + } +}