From fa31283d9405c618eb774aaa9f6b50dc9f659214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Koral?= <45078678+oguzhankoral@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:27:15 +0300 Subject: [PATCH] Chore(revit): Refactor material cache to ToHost ToSpeckle singletons (#262) * A bit better * Post conflict errors * Remove old notes --- .../Receive/RevitHostObjectBuilder.cs | 10 +-- .../Operations/Send/RevitRootObjectBuilder.cs | 8 +- .../RevitConverterModule.cs | 3 +- .../Helpers/RevitToHostCacheSingleton.cs | 12 +++ ...ton.cs => RevitToSpeckleCacheSingleton.cs} | 31 +++---- .../ServiceRegistration.cs | 3 +- .../Speckle.Converters.RevitShared.projitems | 6 +- .../Raw/Geometry/MeshConverterToHost.cs | 11 +-- .../MeshByMaterialDictionaryToSpeckle.cs | 17 ++-- ...erialAsRevitMaterialConversionToSpeckle.cs | 55 ++++++++++++ ...ialAsSpeckleMaterialConversionToSpeckle.cs | 50 +++++++++++ .../Raw/MaterialConversionToSpeckle.cs | 86 ------------------- .../Raw/MaterialQuantitiesToSpeckle.cs | 10 +-- 13 files changed, 165 insertions(+), 137 deletions(-) create mode 100644 Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToHostCacheSingleton.cs rename Converters/Revit/Speckle.Converters.RevitShared/Helpers/{RevitMaterialCacheSingleton.cs => RevitToSpeckleCacheSingleton.cs} (54%) create mode 100644 Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsRevitMaterialConversionToSpeckle.cs create mode 100644 Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsSpeckleMaterialConversionToSpeckle.cs delete mode 100644 Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialConversionToSpeckle.cs diff --git a/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Receive/RevitHostObjectBuilder.cs b/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Receive/RevitHostObjectBuilder.cs index dafc18681..b270b084e 100644 --- a/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Receive/RevitHostObjectBuilder.cs +++ b/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Receive/RevitHostObjectBuilder.cs @@ -19,7 +19,7 @@ internal sealed class RevitHostObjectBuilder : IHostObjectBuilder, IDisposable { private readonly IRootToHostConverter _converter; private readonly IConverterSettingsStore _converterSettings; - private readonly RevitMaterialCacheSingleton _revitMaterialCacheSingleton; + private readonly RevitToHostCacheSingleton _revitToHostCacheSingleton; private readonly ITransactionManager _transactionManager; private readonly ILocalToGlobalUnpacker _localToGlobalUnpacker; private readonly LocalToGlobalConverterUtils _localToGlobalConverterUtils; @@ -41,7 +41,7 @@ public RevitHostObjectBuilder( RevitMaterialBaker materialBaker, RootObjectUnpacker rootObjectUnpacker, ILogger logger, - RevitMaterialCacheSingleton revitMaterialCacheSingleton + RevitToHostCacheSingleton revitToHostCacheSingleton ) { _converter = converter; @@ -53,7 +53,7 @@ RevitMaterialCacheSingleton revitMaterialCacheSingleton _materialBaker = materialBaker; _rootObjectUnpacker = rootObjectUnpacker; _logger = logger; - _revitMaterialCacheSingleton = revitMaterialCacheSingleton; + _revitToHostCacheSingleton = revitToHostCacheSingleton; _activityFactory = activityFactory; } @@ -118,7 +118,7 @@ CancellationToken cancellationToken var map = _materialBaker.BakeMaterials(unpackedRoot.RenderMaterialProxies, baseGroupName); foreach (var kvp in map) { - _revitMaterialCacheSingleton.ObjectIdAndMaterialIndexMap.Add(kvp.Key, kvp.Value); + _revitToHostCacheSingleton.MaterialsByObjectId.Add(kvp.Key, kvp.Value); } } @@ -149,7 +149,7 @@ CancellationToken cancellationToken createGroupTransaction.Assimilate(); } - _revitMaterialCacheSingleton.ObjectIdAndMaterialIndexMap.Clear(); // Massive hack! + _revitToHostCacheSingleton.MaterialsByObjectId.Clear(); // Massive hack! return conversionResults; } diff --git a/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs b/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs index b832de98f..36001723b 100644 --- a/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs +++ b/Connectors/Revit/Speckle.Connectors.RevitShared/Operations/Send/RevitRootObjectBuilder.cs @@ -27,7 +27,7 @@ public class RevitRootObjectBuilder : IRootObjectBuilder private readonly ISendConversionCache _sendConversionCache; private readonly ElementUnpacker _elementUnpacker; private readonly SendCollectionManager _sendCollectionManager; - private readonly RevitMaterialCacheSingleton _revitMaterialCacheSingleton; + private readonly RevitToSpeckleCacheSingleton _revitToSpeckleCacheSingleton; private readonly ILogger _logger; private readonly ParameterDefinitionHandler _parameterDefinitionHandler; @@ -39,7 +39,7 @@ public RevitRootObjectBuilder( SendCollectionManager sendCollectionManager, ILogger logger, ParameterDefinitionHandler parameterDefinitionHandler, - RevitMaterialCacheSingleton revitMaterialCacheSingleton + RevitToSpeckleCacheSingleton revitToSpeckleCacheSingleton ) { _converter = converter; @@ -47,7 +47,7 @@ RevitMaterialCacheSingleton revitMaterialCacheSingleton _sendConversionCache = sendConversionCache; _elementUnpacker = elementUnpacker; _sendCollectionManager = sendCollectionManager; - _revitMaterialCacheSingleton = revitMaterialCacheSingleton; + _revitToSpeckleCacheSingleton = revitToSpeckleCacheSingleton; _logger = logger; _parameterDefinitionHandler = parameterDefinitionHandler; @@ -135,7 +135,7 @@ public async Task Build( } var idsAndSubElementIds = _elementUnpacker.GetElementsAndSubelementIdsFromAtomicObjects(atomicObjects); - var materialProxies = _revitMaterialCacheSingleton.GetRenderMaterialProxyListForObjects(idsAndSubElementIds); + var materialProxies = _revitToSpeckleCacheSingleton.GetRenderMaterialProxyListForObjects(idsAndSubElementIds); _rootObject[ProxyKeys.RENDER_MATERIAL] = materialProxies; // NOTE: these are currently not used anywhere, so we could even skip them (?). _rootObject[ProxyKeys.PARAMETER_DEFINITIONS] = _parameterDefinitionHandler.Definitions; diff --git a/Converters/Revit/Speckle.Converters.RevitShared.DependencyInjection/RevitConverterModule.cs b/Converters/Revit/Speckle.Converters.RevitShared.DependencyInjection/RevitConverterModule.cs index d2daf71af..1d0a98d23 100644 --- a/Converters/Revit/Speckle.Converters.RevitShared.DependencyInjection/RevitConverterModule.cs +++ b/Converters/Revit/Speckle.Converters.RevitShared.DependencyInjection/RevitConverterModule.cs @@ -24,7 +24,8 @@ public void Load(SpeckleContainerBuilder builder) builder.AddScoped(); builder.AddSingleton(new RevitContext()); - builder.AddSingleton(new RevitMaterialCacheSingleton()); + builder.AddSingleton(new RevitToHostCacheSingleton()); + builder.AddSingleton(new RevitToSpeckleCacheSingleton()); // POC: do we need ToSpeckleScalingService as is, do we need to interface it out? builder.AddScoped(); diff --git a/Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToHostCacheSingleton.cs b/Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToHostCacheSingleton.cs new file mode 100644 index 000000000..ae32f20e9 --- /dev/null +++ b/Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToHostCacheSingleton.cs @@ -0,0 +1,12 @@ +namespace Speckle.Converters.RevitShared.Helpers; + +public class RevitToHostCacheSingleton +{ + /// + /// POC: Not sure is there a way to create it on "RevitHostObjectBuilder" with a scope instead singleton. For now we fill this dictionary and clear it on "RevitHostObjectBuilder". + /// Map extracted by revit material baker to be able to use it in converter. + /// This is needed because we cannot set materials for meshes in connector. + /// They needed to be set while creating "TessellatedFace". + /// + public Dictionary MaterialsByObjectId { get; } = new(); +} diff --git a/Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitMaterialCacheSingleton.cs b/Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToSpeckleCacheSingleton.cs similarity index 54% rename from Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitMaterialCacheSingleton.cs rename to Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToSpeckleCacheSingleton.cs index 2a441f241..9cd5ce502 100644 --- a/Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitMaterialCacheSingleton.cs +++ b/Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToSpeckleCacheSingleton.cs @@ -1,37 +1,34 @@ -using Speckle.Objects.Other; +using Speckle.Objects.Other; using Speckle.Objects.Other.Revit; namespace Speckle.Converters.RevitShared.Helpers; /// -/// Persistent cache (across conversions) for all generated render material proxies. This cache stores a list of render material proxies per element id, and provides a method to get out the merged render material proxy list from a set of object ids for setting on the root commit object post conversion phase. /// -/// Why is this needed? Because two reasons: send caching bypasses converter and revit conversions typically generate multiple display values per element. Ask dim for more and he might start crying. +/// Lifetime of this singleton should be per document. +/// +/// +/// Why is this needed? Because two reasons: send caching bypasses converter and revit conversions typically generate multiple display values per element. +/// Ask dim for more and he might start crying. /// -/// TODO: this dude needs to be split into single responsability (render materials and material quantities), and removed from the context - as it's not needed for it to be there. It can be DI'ed as appropriate (see ParameterDefinitionHandler) /// -public class RevitMaterialCacheSingleton +public class RevitToSpeckleCacheSingleton { /// - /// map(object id, ( map (materialId, proxy) ) ) - /// a per object map of material proxies. not the best way??? + /// (DB.Material id, RevitMaterial). This can be generated from converting render materials or material quantities. /// - public Dictionary> ObjectRenderMaterialProxiesMap { get; } = new(); + public Dictionary RevitMaterialCache { get; } = new(); /// - /// POC: The map we mutate PER RECEIVE operation, this smells a LOT! Once we have better conversion context stack that we can manage our data between connector - converter, this property must go away! + /// (DB.Material id, RenderMaterial). This can be generated from converting render materials or material quantities. /// - public Dictionary ObjectIdAndMaterialIndexMap { get; } = new(); + public Dictionary SpeckleRenderMaterialCache { get; } = new(); /// - /// map (DB.Material id, RevitMaterial). This can be generated from converting render materials or material quantities. - /// - public Dictionary ConvertedRevitMaterialMap { get; } = new(); - - /// - /// map (DB.Material id, RenderMaterial). This can be generated from converting render materials or material quantities. + /// map(object id, ( map (materialId, proxy) ) ) + /// a per object map of material proxies. not the best way??? /// - public Dictionary ConvertedRenderMaterialMap { get; } = new(); + public Dictionary> ObjectRenderMaterialProxiesMap { get; } = new(); /// /// Returns the merged material proxy list for the given object ids. Use this to get post conversion a correct list of material proxies for setting on the root commit object. diff --git a/Converters/Revit/Speckle.Converters.RevitShared/ServiceRegistration.cs b/Converters/Revit/Speckle.Converters.RevitShared/ServiceRegistration.cs index 787d41326..ed644f76c 100644 --- a/Converters/Revit/Speckle.Converters.RevitShared/ServiceRegistration.cs +++ b/Converters/Revit/Speckle.Converters.RevitShared/ServiceRegistration.cs @@ -26,7 +26,8 @@ public static IServiceCollection AddRevitConverters(this IServiceCollection serv serviceCollection.AddScoped(); serviceCollection.AddSingleton(new RevitContext()); - serviceCollection.AddSingleton(new RevitMaterialCacheSingleton()); + serviceCollection.AddSingleton(new RevitToHostCacheSingleton()); + serviceCollection.AddSingleton(new RevitToSpeckleCacheSingleton()); // POC: do we need ToSpeckleScalingService as is, do we need to interface it out? serviceCollection.AddScoped(); diff --git a/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems b/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems index c33cdb8a9..c7214cd00 100644 --- a/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems +++ b/Converters/Revit/Speckle.Converters.RevitShared/Speckle.Converters.RevitShared.projitems @@ -12,7 +12,8 @@ - + + @@ -69,7 +70,8 @@ - + + diff --git a/Converters/Revit/Speckle.Converters.RevitShared/ToHost/Raw/Geometry/MeshConverterToHost.cs b/Converters/Revit/Speckle.Converters.RevitShared/ToHost/Raw/Geometry/MeshConverterToHost.cs index a5652fb60..2cc0974d4 100644 --- a/Converters/Revit/Speckle.Converters.RevitShared/ToHost/Raw/Geometry/MeshConverterToHost.cs +++ b/Converters/Revit/Speckle.Converters.RevitShared/ToHost/Raw/Geometry/MeshConverterToHost.cs @@ -9,15 +9,15 @@ namespace Speckle.Converters.RevitShared.ToHost.TopLevel; public class MeshConverterToHost : ITypedConverter> { private readonly ITypedConverter _pointConverter; - private readonly RevitMaterialCacheSingleton _revitMaterialCacheSingleton; + private readonly RevitToHostCacheSingleton _revitToHostCacheSingleton; public MeshConverterToHost( ITypedConverter pointConverter, - RevitMaterialCacheSingleton revitMaterialCacheSingleton + RevitToHostCacheSingleton revitToHostCacheSingleton ) { _pointConverter = pointConverter; - _revitMaterialCacheSingleton = revitMaterialCacheSingleton; + _revitToHostCacheSingleton = revitToHostCacheSingleton; } public List Convert(SOG.Mesh mesh) @@ -39,10 +39,7 @@ RevitMaterialCacheSingleton revitMaterialCacheSingleton ElementId materialId = ElementId.InvalidElementId; if ( - _revitMaterialCacheSingleton.ObjectIdAndMaterialIndexMap.TryGetValue( - mesh.applicationId ?? mesh.id, - out var mappedElementId - ) + _revitToHostCacheSingleton.MaterialsByObjectId.TryGetValue(mesh.applicationId ?? mesh.id, out var mappedElementId) ) { materialId = mappedElementId; diff --git a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/Geometry/MeshByMaterialDictionaryToSpeckle.cs b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/Geometry/MeshByMaterialDictionaryToSpeckle.cs index 40c60762d..93c346434 100644 --- a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/Geometry/MeshByMaterialDictionaryToSpeckle.cs +++ b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/Geometry/MeshByMaterialDictionaryToSpeckle.cs @@ -3,7 +3,6 @@ using Speckle.Converters.RevitShared.Helpers; using Speckle.Converters.RevitShared.Settings; using Speckle.Objects.Other; -using Speckle.Objects.Other.Revit; using Speckle.Sdk.Common; namespace Speckle.Converters.RevitShared.ToSpeckle; @@ -12,21 +11,21 @@ public class MeshByMaterialDictionaryToSpeckle : ITypedConverter<(Dictionary> target, DB.ElementId parentElementId), List> { private readonly IConverterSettingsStore _converterSettings; - private readonly ITypedConverter _materialConverter; + private readonly ITypedConverter _speckleRenderMaterialConverter; private readonly ITypedConverter, SOG.Mesh> _meshListConverter; - private readonly RevitMaterialCacheSingleton _revitMaterialCacheSingleton; + private readonly RevitToSpeckleCacheSingleton _revitToSpeckleCacheSingleton; public MeshByMaterialDictionaryToSpeckle( - ITypedConverter materialConverter, ITypedConverter, SOG.Mesh> meshListConverter, IConverterSettingsStore converterSettings, - RevitMaterialCacheSingleton revitMaterialCacheSingleton + RevitToSpeckleCacheSingleton revitToSpeckleCacheSingleton, + ITypedConverter speckleRenderMaterialConverter ) { - _materialConverter = materialConverter; _meshListConverter = meshListConverter; _converterSettings = converterSettings; - _revitMaterialCacheSingleton = revitMaterialCacheSingleton; + _revitToSpeckleCacheSingleton = revitToSpeckleCacheSingleton; + _speckleRenderMaterialConverter = speckleRenderMaterialConverter; } /// @@ -46,7 +45,7 @@ RevitMaterialCacheSingleton revitMaterialCacheSingleton public List Convert((Dictionary> target, DB.ElementId parentElementId) args) { var result = new List(args.target.Keys.Count); - var objectRenderMaterialProxiesMap = _revitMaterialCacheSingleton.ObjectRenderMaterialProxiesMap; + var objectRenderMaterialProxiesMap = _revitToSpeckleCacheSingleton.ObjectRenderMaterialProxiesMap; var materialProxyMap = new Dictionary(); objectRenderMaterialProxiesMap[args.parentElementId.ToString().NotNull()] = materialProxyMap; @@ -69,7 +68,7 @@ RevitMaterialCacheSingleton revitMaterialCacheSingleton // get the render material if any if (_converterSettings.Current.Document.GetElement(materialId) is DB.Material material) { - (RevitMaterial _, RenderMaterial convertedRenderMaterial) = _materialConverter.Convert(material); + RenderMaterial convertedRenderMaterial = _speckleRenderMaterialConverter.Convert(material); if (!materialProxyMap.TryGetValue(materialIdString, out RenderMaterialProxy? renderMaterialProxy)) { diff --git a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsRevitMaterialConversionToSpeckle.cs b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsRevitMaterialConversionToSpeckle.cs new file mode 100644 index 000000000..3d125e652 --- /dev/null +++ b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsRevitMaterialConversionToSpeckle.cs @@ -0,0 +1,55 @@ +using Speckle.Converters.Common.Objects; +using Speckle.Converters.RevitShared.Helpers; +using Speckle.Objects.Other.Revit; + +namespace Speckle.Converters.RevitShared.ToSpeckle; + +public class MaterialAsRevitMaterialConversionToSpeckle : ITypedConverter +{ + private readonly RevitToSpeckleCacheSingleton _revitToSpeckleCacheSingleton; + + public MaterialAsRevitMaterialConversionToSpeckle(RevitToSpeckleCacheSingleton revitToSpeckleCacheSingleton) + { + _revitToSpeckleCacheSingleton = revitToSpeckleCacheSingleton; + } + + public RevitMaterial Convert(DB.Material target) + { + RevitMaterial material; + if ( + _revitToSpeckleCacheSingleton.RevitMaterialCache.TryGetValue(target.UniqueId, out RevitMaterial? cachedMaterial) + ) + { + material = cachedMaterial; + } + else + { + material = ConvertToRevitMaterial(target); + _revitToSpeckleCacheSingleton.RevitMaterialCache.Add(target.UniqueId, material); + } + + return material; + } + + private RevitMaterial ConvertToRevitMaterial(DB.Material target) + { + // POC: we need to validate these properties on the RevitMaterial class, ie are they used? + RevitMaterial material = + new( + target.Name, + target.MaterialCategory, + target.MaterialClass, + target.Shininess, + target.Smoothness, + target.Transparency + ) + { + applicationId = target.UniqueId + }; + + // POC: I'm removing material parameter assigning here as we're exploding a bit the whole space with too many parameters. + // We can bring this back if needed/requested by our end users. + // _parameterObjectAssigner.AssignParametersToBase(target, material); + return material; + } +} diff --git a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsSpeckleMaterialConversionToSpeckle.cs b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsSpeckleMaterialConversionToSpeckle.cs new file mode 100644 index 000000000..1a1b8144b --- /dev/null +++ b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsSpeckleMaterialConversionToSpeckle.cs @@ -0,0 +1,50 @@ +using Speckle.Converters.Common.Objects; +using Speckle.Converters.RevitShared.Helpers; +using Speckle.Objects.Other; + +namespace Speckle.Converters.RevitShared.ToSpeckle; + +public class MaterialAsSpeckleMaterialConversionToSpeckle : ITypedConverter +{ + private readonly RevitToSpeckleCacheSingleton _revitToSpeckleCacheSingleton; + + public MaterialAsSpeckleMaterialConversionToSpeckle(RevitToSpeckleCacheSingleton revitToSpeckleCacheSingleton) + { + _revitToSpeckleCacheSingleton = revitToSpeckleCacheSingleton; + } + + public RenderMaterial Convert(DB.Material target) + { + RenderMaterial renderMaterial; + if ( + _revitToSpeckleCacheSingleton.SpeckleRenderMaterialCache.TryGetValue( + target.UniqueId, + out RenderMaterial? cachedRenderMaterial + ) + ) + { + renderMaterial = cachedRenderMaterial; + } + else + { + renderMaterial = ConvertToRenderMaterial(target); + _revitToSpeckleCacheSingleton.SpeckleRenderMaterialCache.Add(target.UniqueId, renderMaterial); + } + + return renderMaterial; + } + + private RenderMaterial ConvertToRenderMaterial(DB.Material target) + { + RenderMaterial renderMaterial = + new() + { + name = target.Name, + opacity = 1 - target.Transparency / 100d, + diffuse = System.Drawing.Color.FromArgb(target.Color.Red, target.Color.Green, target.Color.Blue).ToArgb(), + applicationId = target.UniqueId + }; + + return renderMaterial; + } +} diff --git a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialConversionToSpeckle.cs b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialConversionToSpeckle.cs deleted file mode 100644 index 247e997b0..000000000 --- a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialConversionToSpeckle.cs +++ /dev/null @@ -1,86 +0,0 @@ -using Speckle.Converters.Common.Objects; -using Speckle.Converters.RevitShared.Helpers; -using Speckle.Objects.Other; -using Speckle.Objects.Other.Revit; - -namespace Speckle.Converters.RevitShared.ToSpeckle; - -public class MaterialConversionToSpeckle : ITypedConverter -{ - private readonly RevitMaterialCacheSingleton _materialCacheSingleton; - - public MaterialConversionToSpeckle(RevitMaterialCacheSingleton materialCacheSingleton) - { - _materialCacheSingleton = materialCacheSingleton; - } - - public (RevitMaterial, RenderMaterial) Convert(DB.Material target) - { - RevitMaterial material; - if ( - _materialCacheSingleton.ConvertedRevitMaterialMap.TryGetValue(target.UniqueId, out RevitMaterial? cachedMaterial) - ) - { - material = cachedMaterial; - } - else - { - material = ConvertToRevitMaterial(target); - _materialCacheSingleton.ConvertedRevitMaterialMap.Add(target.UniqueId, material); - } - - RenderMaterial renderMaterial; - if ( - _materialCacheSingleton.ConvertedRenderMaterialMap.TryGetValue( - target.UniqueId, - out RenderMaterial? cachedRenderMaterial - ) - ) - { - renderMaterial = cachedRenderMaterial; - } - else - { - renderMaterial = ConvertToRenderMaterial(target); - _materialCacheSingleton.ConvertedRenderMaterialMap.Add(target.UniqueId, renderMaterial); - } - - return (material, renderMaterial); - } - - private RevitMaterial ConvertToRevitMaterial(DB.Material target) - { - // POC: we need to validate these properties on the RevitMaterial class, ie are they used? - RevitMaterial material = - new( - target.Name, - target.MaterialCategory, - target.MaterialClass, - target.Shininess, - target.Smoothness, - target.Transparency - ) - { - applicationId = target.UniqueId - }; - - // POC: I'm removing material parameter assigning here as we're exploding a bit the whole space with too many parameters. - // We can bring this back if needed/requested by our end users. - // _parameterObjectAssigner.AssignParametersToBase(target, material); - return material; - } - - private RenderMaterial ConvertToRenderMaterial(DB.Material target) - { - RenderMaterial renderMaterial = - new() - { - name = target.Name, - opacity = 1 - target.Transparency / 100d, - diffuse = System.Drawing.Color.FromArgb(target.Color.Red, target.Color.Green, target.Color.Blue).ToArgb(), - applicationId = target.UniqueId - }; - - return renderMaterial; - } -} diff --git a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialQuantitiesToSpeckle.cs b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialQuantitiesToSpeckle.cs index 37c32a36b..0953be5a0 100644 --- a/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialQuantitiesToSpeckle.cs +++ b/Converters/Revit/Speckle.Converters.RevitShared/ToSpeckle/Raw/MaterialQuantitiesToSpeckle.cs @@ -70,19 +70,19 @@ public List> Convert(DB.Element target) [Obsolete("Creates a rather bloated data structure - 2.0 style. More in the comment above.")] public class MaterialQuantitiesToSpeckle : ITypedConverter> { - private readonly ITypedConverter _materialConverter; + private readonly ITypedConverter _revitMaterialConverter; private readonly ScalingServiceToSpeckle _scalingService; private readonly IConverterSettingsStore _converterSettings; public MaterialQuantitiesToSpeckle( - ITypedConverter materialConverter, ScalingServiceToSpeckle scalingService, - IConverterSettingsStore converterSettings + IConverterSettingsStore converterSettings, + ITypedConverter revitMaterialConverter ) { - _materialConverter = materialConverter; _scalingService = scalingService; _converterSettings = converterSettings; + _revitMaterialConverter = revitMaterialConverter; } /// @@ -117,7 +117,7 @@ public List Convert(DB.Element target) if (_converterSettings.Current.Document.GetElement(matId) is DB.Material material) { - (RevitMaterial convertedMaterial, RenderMaterial _) = _materialConverter.Convert(material); + RevitMaterial convertedMaterial = _revitMaterialConverter.Convert(material); // NOTE: the RevitMaterial class is semi useless, and it used to extract parameters out too for each material. Overkill. quantities.Add(new(convertedMaterial, volume, area, _converterSettings.Current.SpeckleUnits)); }