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); + } +}