|
| 1 | +using Objects; |
| 2 | +using Speckle.Converters.Common; |
| 3 | +using Speckle.Converters.Common.Objects; |
| 4 | +using Speckle.Converters.RevitShared.Helpers; |
| 5 | +using Speckle.Converters.RevitShared.ToSpeckle; |
| 6 | + |
| 7 | +namespace Speckle.Converters.RevitShared.ToHost.TopLevel; |
| 8 | + |
| 9 | +[NameAndRankValue(nameof(SOBE.GridLine), 0)] |
| 10 | +internal class GridlineToHostTopLevelConverter : BaseTopLevelConverterToHost<SOBE.GridLine, DB.Grid> |
| 11 | +{ |
| 12 | + private readonly ITypedConverter<ICurve, DB.CurveArray> _curveConverter; |
| 13 | + private readonly IRevitConversionContextStack _contextStack; |
| 14 | + |
| 15 | + public GridlineToHostTopLevelConverter( |
| 16 | + ITypedConverter<ICurve, DB.CurveArray> curveConverter, |
| 17 | + IRevitConversionContextStack contextStack |
| 18 | + ) |
| 19 | + { |
| 20 | + _curveConverter = curveConverter; |
| 21 | + _contextStack = contextStack; |
| 22 | + } |
| 23 | + |
| 24 | + public override DB.Grid Convert(SOBE.GridLine target) |
| 25 | + { |
| 26 | + DB.Curve curve = _curveConverter.Convert(target.baseLine).get_Item(0); |
| 27 | + |
| 28 | + using DB.Grid revitGrid = curve switch |
| 29 | + { |
| 30 | + DB.Arc arc => DB.Grid.Create(_contextStack.Current.Document, arc), |
| 31 | + DB.Line line => DB.Grid.Create(_contextStack.Current.Document, line), |
| 32 | + _ => throw new SpeckleConversionException($"Grid line curve is of type {curve.GetType()} which is not supported") |
| 33 | + }; |
| 34 | + |
| 35 | + if (!string.IsNullOrEmpty(target.label) && !GridNameIsTaken(target.label)) |
| 36 | + { |
| 37 | + revitGrid.Name = target.label; |
| 38 | + } |
| 39 | + |
| 40 | + return revitGrid; |
| 41 | + } |
| 42 | + |
| 43 | + private bool GridNameIsTaken(string gridName) |
| 44 | + { |
| 45 | + using var collector = new DB.FilteredElementCollector(_contextStack.Current.Document); |
| 46 | + |
| 47 | + IEnumerable<string> gridNames = collector |
| 48 | + .WhereElementIsNotElementType() |
| 49 | + .OfClass(typeof(DB.Grid)) |
| 50 | + .ToElements() |
| 51 | + .Cast<DB.Grid>() |
| 52 | + .Select(grid => grid.Name); |
| 53 | + |
| 54 | + return gridNames.Contains(gridName); |
| 55 | + } |
| 56 | +} |
0 commit comments