diff --git a/DUI3-DX/Sdk/Speckle.Converters.Common/IRootToSpeckleConverter.cs b/DUI3-DX/Sdk/Speckle.Converters.Common/IRootToSpeckleConverter.cs index fed3c97d43..bf918fa591 100644 --- a/DUI3-DX/Sdk/Speckle.Converters.Common/IRootToSpeckleConverter.cs +++ b/DUI3-DX/Sdk/Speckle.Converters.Common/IRootToSpeckleConverter.cs @@ -1,6 +1,38 @@ +using Speckle.Autofac.DependencyInjection; +using Speckle.Converters.Common.Objects; +using Speckle.Core.Models; +using Speckle.InterfaceGenerator; + namespace Speckle.Converters.Common; -public interface IRootToSpeckleConverter +[GenerateAutoInterface] +public class RootToSpeckleConverter : IRootToSpeckleConverter { - Speckle.Core.Models.Base Convert(object target); + private readonly IFactory _toSpeckle; + + public RootToSpeckleConverter(IFactory toSpeckle) + { + _toSpeckle = toSpeckle; + } + + public Base Convert(Type type, object target) + { + try + { + var objectConverter = _toSpeckle.ResolveInstance(type.Name); //poc: would be nice to have supertypes resolve + + if (objectConverter == null) + { + throw new NotSupportedException($"No conversion found for {type.Name}"); + } + var convertedObject = objectConverter.Convert(target); + + return convertedObject; + } + catch (SpeckleConversionException e) + { + Console.WriteLine(e); + throw; // Just rethrowing for now, Logs may be needed here. + } + } }