From a18cba1f37953a00f569910e49cf7804999afa08 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Fri, 14 Jun 2024 09:44:14 +0100 Subject: [PATCH] Use ProxyMap class that is source generated (#3507) * Initial commit from converter-tests * tests now run * adding more to proxy map * switch to FluentAssertions * fix IRevitLevel conversions * more cast handling * use proxymap for ofclass * update revit interfaces * fmt * add more proxies * better handling for wrapped types * fix the RevitContext * inject interfaces * convert to new way of casting * update and rejigger dependencies * handle null elements * proxy map is also proxy aware * fmt * change namespaces * updates to proxies * more cast changes * another cast issue fixed * update nugets * fmt * add System.IO to make CI happy? * Bumping proxy API and using source gen ProxyMap * fmt * putting root back * use IProxyMapper in root converter * bump host apis * revert registration change * remove sendselection --- .../DependencyInjection/ProxyMap.cs | 80 ------------------- .../RevitConnectorModule.cs | 2 - .../GlobalUsings.cs | 1 - .../Operations/Send/RevitRootObjectBuilder.cs | 2 +- .../RevitConverterModule.cs | 1 - ...rters.Revit2023.DependencyInjection.csproj | 2 +- .../Speckle.Converters.Revit2023.Tests.csproj | 2 +- .../packages.lock.json | 10 +-- .../Speckle.Converters.Revit2023.csproj | 2 +- .../Helpers/SlopeArrowExtractor.cs | 16 ---- .../Services/ReferencePointConverter.cs | 9 +-- .../RootToSpeckleConverter.cs | 8 +- .../Speckle.Converters.Common.csproj | 4 +- 13 files changed, 18 insertions(+), 121 deletions(-) delete mode 100644 DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/ProxyMap.cs delete mode 100644 DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/GlobalUsings.cs diff --git a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/ProxyMap.cs b/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/ProxyMap.cs deleted file mode 100644 index 2d7f7f8314..0000000000 --- a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/ProxyMap.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System.Collections.Concurrent; -using System.Diagnostics.CodeAnalysis; -using Speckle.Revit.Api; -using Speckle.Revit.Interfaces; - -namespace Speckle.Connectors.Revit2023.Converters; - -[SuppressMessage("Performance", "CA1810:Initialize reference type static fields inline")] -public class ProxyMap : IProxyMap -{ - private static readonly ConcurrentDictionary s_revitToInterfaceMap = new(); - private static readonly ConcurrentDictionary s_proxyToInterfaceMap = new(); - private static readonly ConcurrentDictionary s_interfaceToRevit = new(); - private static readonly ConcurrentDictionary> s_proxyFactory = new(); - - [SuppressMessage("Maintainability", "CA1506:Avoid excessive class coupling")] - static ProxyMap() - { - Add(x => new ElementProxy(x)); - Add(x => new FamilyInstanceProxy(x)); - Add(x => new CurveProxy(x)); - Add(x => new BoundarySegmentProxy(x)); - Add(x => new LevelProxy(x)); - Add(x => new LocationProxy(x)); - Add(x => new MaterialProxy(x)); - Add(x => new ModelCurveArrayProxy(x)); - Add(x => new ModelCurveArrArrayProxy(x)); - Add(x => new ParameterProxy(x)); - Add(x => new BasePointProxy(x)); - Add(x => new WallProxy(x)); - Add(x => new PanelProxy(x)); - Add(x => new FloorProxy(x)); - Add(x => new CeilingProxy(x)); - Add(x => new FootPrintRoofProxy(x)); - Add(x => new ModelLineProxy(x)); - Add(x => new RoofBaseProxy(x)); - } - - private static void Add(Func f) - where T : class - where TInterface : notnull - where TProxy : TInterface - { - s_revitToInterfaceMap.TryAdd(typeof(T), typeof(TInterface)); - s_proxyToInterfaceMap.TryAdd(typeof(TProxy), typeof(TInterface)); - s_proxyFactory.TryAdd(typeof(TInterface), w => f((T)w)); - s_interfaceToRevit.TryAdd(typeof(TInterface), typeof(T)); - } - - public Type? GetMappedTypeFromHostType(Type type) - { - if (s_revitToInterfaceMap.TryGetValue(type, out var t)) - { - return t; - } - return null; - } - - public Type? GetMappedTypeFromProxyType(Type type) - { - if (s_proxyToInterfaceMap.TryGetValue(type, out var t)) - { - return t; - } - - return null; - } - - public Type? GetHostTypeFromMappedType(Type type) - { - if (s_interfaceToRevit.TryGetValue(type, out var t)) - { - return t; - } - - return null; - } - - public object CreateProxy(Type type, object toWrap) => s_proxyFactory[type](toWrap); -} diff --git a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/RevitConnectorModule.cs b/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/RevitConnectorModule.cs index 83e0a6af57..879bf05041 100644 --- a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/RevitConnectorModule.cs +++ b/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/DependencyInjection/RevitConnectorModule.cs @@ -10,7 +10,6 @@ using Speckle.Connectors.Revit.HostApp; using Speckle.Connectors.Revit.Operations.Send; using Speckle.Connectors.Revit.Plugin; -using Speckle.Connectors.Revit2023.Converters; using Speckle.Connectors.RevitShared.Helpers; using Speckle.Connectors.Utils; using Speckle.Connectors.Utils.Builders; @@ -73,6 +72,5 @@ public void Load(SpeckleContainerBuilder builder) // register send conversion cache builder.AddSingleton(); - builder.AddSingleton(); } } diff --git a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/GlobalUsings.cs b/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/GlobalUsings.cs deleted file mode 100644 index dec8635815..0000000000 --- a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/GlobalUsings.cs +++ /dev/null @@ -1 +0,0 @@ -global using DB = Autodesk.Revit.DB; diff --git a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs b/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs index 63c2144e76..6652aad320 100644 --- a/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs +++ b/DUI3-DX/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs @@ -53,7 +53,7 @@ public RootObjectBuilderResult Build( throw new SpeckleException("Family Environment documents are not supported."); } - var revitElements = new List(); // = _contextStack.Current.Document.GetElements(sendSelection.SelectedItems).ToList(); + var revitElements = new List(); foreach (var id in objects) { diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/RevitConverterModule.cs b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/RevitConverterModule.cs index f5ecc652f0..533baf679e 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/RevitConverterModule.cs +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/RevitConverterModule.cs @@ -28,6 +28,5 @@ public void Load(SpeckleContainerBuilder builder) builder.AddScoped(); builder.AddScoped(); builder.AddScoped(); - builder.AddScoped(); } } diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/Speckle.Converters.Revit2023.DependencyInjection.csproj b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/Speckle.Converters.Revit2023.DependencyInjection.csproj index c390bba29b..810abe8b3f 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/Speckle.Converters.Revit2023.DependencyInjection.csproj +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.DependencyInjection/Speckle.Converters.Revit2023.DependencyInjection.csproj @@ -6,7 +6,7 @@ - + diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/Speckle.Converters.Revit2023.Tests.csproj b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/Speckle.Converters.Revit2023.Tests.csproj index 70941da6f7..eabdeb030a 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/Speckle.Converters.Revit2023.Tests.csproj +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/Speckle.Converters.Revit2023.Tests.csproj @@ -14,7 +14,7 @@ - + diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/packages.lock.json b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/packages.lock.json index b6ddfa328f..b5cef2200d 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/packages.lock.json +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023.Tests/packages.lock.json @@ -65,9 +65,9 @@ }, "Speckle.Revit2023.Interfaces": { "type": "Direct", - "requested": "[0.1.1-preview.0.20, )", - "resolved": "0.1.1-preview.0.20", - "contentHash": "L/8btGlDejt6OCe8Lee05N3ztKV+pz24WAU9fwXRHEeWMgSe+bqq477Jmk641QFcKmcWK25XJlprlHiCh4pAdQ==" + "requested": "[0.1.1-preview.0.22, )", + "resolved": "0.1.1-preview.0.22", + "contentHash": "Z6bfEIKFYJtXPjHQYlGBjiQLekVOU7L//H9gaQIw3ba9jqJeYHx5AV0z6S83g04eGBKkNJC6EwBr+fspsK0f+w==" }, "Castle.Core": { "type": "Transitive", @@ -499,7 +499,7 @@ "dependencies": { "Speckle.Autofac": "[2.0.999-local, )", "Speckle.Objects": "[2.0.999-local, )", - "Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.20, )" + "Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.22, )" } }, "speckle.converters.common.dependencyinjection": { @@ -513,7 +513,7 @@ "type": "Project", "dependencies": { "Speckle.Converters.Common": "[2.0.999-local, )", - "Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.20, )" + "Speckle.Revit2023.Interfaces": "[0.1.1-preview.0.22, )" } }, "Speckle.Core": { diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023/Speckle.Converters.Revit2023.csproj b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023/Speckle.Converters.Revit2023.csproj index 351d1fa2e3..7fe5bd12ca 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023/Speckle.Converters.Revit2023.csproj +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.Revit2023/Speckle.Converters.Revit2023.csproj @@ -9,7 +9,7 @@ - + diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Helpers/SlopeArrowExtractor.cs b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Helpers/SlopeArrowExtractor.cs index f1db970430..1ae02630be 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Helpers/SlopeArrowExtractor.cs +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Helpers/SlopeArrowExtractor.cs @@ -97,19 +97,3 @@ public double GetSlopeArrowHeadOffset(IRevitModelLine slopeArrow, double tailOff return headOffset; } } - -// POC: why do we need this send selection? -// why does conversion need to know about selection in this way? -public class SendSelection -{ - private readonly HashSet _selectedItemIds; - - public SendSelection(IEnumerable selectedItemIds) - { - _selectedItemIds = new HashSet(selectedItemIds); - } - - public bool Contains(string elementId) => _selectedItemIds.Contains(elementId); - - public IReadOnlyCollection SelectedItems => _selectedItemIds.ToList().AsReadOnly(); -} diff --git a/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Services/ReferencePointConverter.cs b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Services/ReferencePointConverter.cs index da8c985834..4eef722c25 100644 --- a/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Services/ReferencePointConverter.cs +++ b/DUI3-DX/Converters/Revit/Speckle.Converters.RevitShared/Services/ReferencePointConverter.cs @@ -1,4 +1,4 @@ -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using Speckle.Converters.Common; using Speckle.InterfaceGenerator; using Speckle.Revit.Interfaces; @@ -32,7 +32,6 @@ public class ReferencePointConverter : IReferencePointConverter private readonly IRevitTransformUtils _transformUtils; private readonly IRevitFilterFactory _revitFilterFactory; private readonly IRevitXYZUtils _revitXyzUtils; - private readonly IProxyMap _proxyMap; private readonly Dictionary _docTransforms = new(); @@ -41,8 +40,7 @@ public ReferencePointConverter( IRevitConversionSettings revitSettings, IRevitFilterFactory revitFilterFactory, IRevitTransformUtils transformUtils, - IRevitXYZUtils revitXyzUtils, - IProxyMap proxyMap + IRevitXYZUtils revitXyzUtils ) { _contextStack = contextStack; @@ -50,7 +48,6 @@ IProxyMap proxyMap _revitFilterFactory = revitFilterFactory; _transformUtils = transformUtils; _revitXyzUtils = revitXyzUtils; - _proxyMap = proxyMap; } // POC: the original allowed for the document to be passed in @@ -92,7 +89,7 @@ public IRevitTransform GetReferencePointTransform(string referencePointSetting) // POC: bogus disposal below var points = _revitFilterFactory .CreateFilteredElementCollector(_contextStack.Current.Document) - .OfClass(_proxyMap) + .OfClass() .ToList(); var projectPoint = NotNullExtensions.NotNull(points.FirstOrDefault(o => o.IsShared == false), "No projectPoint"); diff --git a/DUI3-DX/Sdk/Speckle.Converters.Common/RootToSpeckleConverter.cs b/DUI3-DX/Sdk/Speckle.Converters.Common/RootToSpeckleConverter.cs index 65ea7156a2..04d87e8349 100644 --- a/DUI3-DX/Sdk/Speckle.Converters.Common/RootToSpeckleConverter.cs +++ b/DUI3-DX/Sdk/Speckle.Converters.Common/RootToSpeckleConverter.cs @@ -10,18 +10,18 @@ namespace Speckle.Converters.Common; public class RootToSpeckleConverter : IRootToSpeckleConverter { private readonly IFactory _toSpeckle; - private readonly IProxyMap _proxyMap; + private readonly IProxyMapper _proxyMapper; - public RootToSpeckleConverter(IFactory toSpeckle, IProxyMap proxyMap) + public RootToSpeckleConverter(IFactory toSpeckle, IProxyMapper proxyMapper) { _toSpeckle = toSpeckle; - _proxyMap = proxyMap; + _proxyMapper = proxyMapper; } public Base Convert(object target) { Type revitType = target.GetType(); - var wrapper = _proxyMap.WrapIfExists(revitType, target); + var wrapper = _proxyMapper.WrapIfExists(revitType, target); if (wrapper == null) { throw new NotSupportedException($"No wrapper found for Revit type: {revitType.Name}"); diff --git a/DUI3-DX/Sdk/Speckle.Converters.Common/Speckle.Converters.Common.csproj b/DUI3-DX/Sdk/Speckle.Converters.Common/Speckle.Converters.Common.csproj index 7fb8e9d333..f9162e3030 100644 --- a/DUI3-DX/Sdk/Speckle.Converters.Common/Speckle.Converters.Common.csproj +++ b/DUI3-DX/Sdk/Speckle.Converters.Common/Speckle.Converters.Common.csproj @@ -1,10 +1,10 @@ - + netstandard2.0 - +