-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore(revit): Refactor material cache to ToHost ToSpeckle singletons (#…
…262) * A bit better * Post conflict errors * Remove old notes
- Loading branch information
1 parent
d8f3fa7
commit fa31283
Showing
13 changed files
with
165 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Converters/Revit/Speckle.Converters.RevitShared/Helpers/RevitToHostCacheSingleton.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Speckle.Converters.RevitShared.Helpers; | ||
|
||
public class RevitToHostCacheSingleton | ||
{ | ||
/// <summary> | ||
/// 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". | ||
/// </summary> | ||
public Dictionary<string, DB.ElementId> MaterialsByObjectId { get; } = new(); | ||
} |
31 changes: 14 additions & 17 deletions
31
...ed/Helpers/RevitMaterialCacheSingleton.cs → ...d/Helpers/RevitToSpeckleCacheSingleton.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...peckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsRevitMaterialConversionToSpeckle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DB.Material, RevitMaterial> | ||
{ | ||
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; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ckle.Converters.RevitShared/ToSpeckle/Raw/MaterialAsSpeckleMaterialConversionToSpeckle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DB.Material, RenderMaterial> | ||
{ | ||
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; | ||
} | ||
} |
Oops, something went wrong.