From 4099052abb3b02cd44a1e60e906a7895548cee7c Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Fri, 14 Jun 2024 08:32:09 +0100 Subject: [PATCH] use IProxyMapper in root converter --- ...Converter.cs => RootToSpeckleConverter.cs} | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) rename DUI3-DX/Sdk/Speckle.Converters.Common/{IRootToSpeckleConverter.cs => RootToSpeckleConverter.cs} (52%) diff --git a/DUI3-DX/Sdk/Speckle.Converters.Common/IRootToSpeckleConverter.cs b/DUI3-DX/Sdk/Speckle.Converters.Common/RootToSpeckleConverter.cs similarity index 52% rename from DUI3-DX/Sdk/Speckle.Converters.Common/IRootToSpeckleConverter.cs rename to DUI3-DX/Sdk/Speckle.Converters.Common/RootToSpeckleConverter.cs index bf918fa591..04d87e8349 100644 --- a/DUI3-DX/Sdk/Speckle.Converters.Common/IRootToSpeckleConverter.cs +++ b/DUI3-DX/Sdk/Speckle.Converters.Common/RootToSpeckleConverter.cs @@ -2,6 +2,7 @@ using Speckle.Converters.Common.Objects; using Speckle.Core.Models; using Speckle.InterfaceGenerator; +using Speckle.Revit.Interfaces; namespace Speckle.Converters.Common; @@ -9,23 +10,32 @@ namespace Speckle.Converters.Common; public class RootToSpeckleConverter : IRootToSpeckleConverter { private readonly IFactory _toSpeckle; + private readonly IProxyMapper _proxyMapper; - public RootToSpeckleConverter(IFactory toSpeckle) + public RootToSpeckleConverter(IFactory toSpeckle, IProxyMapper proxyMapper) { _toSpeckle = toSpeckle; + _proxyMapper = proxyMapper; } - public Base Convert(Type type, object target) + public Base Convert(object target) { + Type revitType = target.GetType(); + var wrapper = _proxyMapper.WrapIfExists(revitType, target); + if (wrapper == null) + { + throw new NotSupportedException($"No wrapper found for Revit type: {revitType.Name}"); + } + var (wrappedType, wrappedObject) = wrapper.Value; try { - var objectConverter = _toSpeckle.ResolveInstance(type.Name); //poc: would be nice to have supertypes resolve + var objectConverter = _toSpeckle.ResolveInstance(wrappedType.Name); //poc: would be nice to have supertypes resolve if (objectConverter == null) { - throw new NotSupportedException($"No conversion found for {type.Name}"); + throw new NotSupportedException($"No conversion found for {wrappedType.Name}"); } - var convertedObject = objectConverter.Convert(target); + var convertedObject = objectConverter.Convert(wrappedObject); return convertedObject; }