-
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.
fix (tekla): greyscaled objects in the viewer (#412)
* adds teklamaterialcahce * Revert "adds teklamaterialcahce" This reverts commit 3fbba01. * detach applicationids from displayvalue --------- Co-authored-by: Oğuzhan Koral <[email protected]>
- Loading branch information
1 parent
9d6c7de
commit 3ad4d46
Showing
2 changed files
with
34 additions
and
65 deletions.
There are no files selected for viewing
93 changes: 34 additions & 59 deletions
93
Connectors/Tekla/Speckle.Connector.TeklaShared/HostApp/TeklaMaterialUnpacker.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 |
---|---|---|
@@ -1,80 +1,55 @@ | ||
using System.Drawing; | ||
using Microsoft.Extensions.Logging; | ||
using Speckle.Connector.Tekla2024.Extensions; | ||
using Speckle.Objects.Other; | ||
|
||
namespace Speckle.Connector.Tekla2024.HostApp; | ||
|
||
public class TeklaMaterialUnpacker | ||
{ | ||
private readonly ILogger<TeklaMaterialUnpacker> _logger; | ||
|
||
public TeklaMaterialUnpacker(ILogger<TeklaMaterialUnpacker> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public List<RenderMaterialProxy> UnpackRenderMaterial(List<TSM.ModelObject> atomicObjects) | ||
{ | ||
var renderMaterialProxies = new Dictionary<string, RenderMaterialProxy>(); | ||
var processedObjects = new HashSet<string>(); | ||
Dictionary<string, RenderMaterialProxy> renderMaterialProxies = new(); | ||
|
||
foreach (var atomicObject in atomicObjects) | ||
{ | ||
ProcessModelObject(atomicObject, renderMaterialProxies, processedObjects); | ||
} | ||
var flattenedAtomicObjects = new List<TSM.ModelObject>(); | ||
|
||
return renderMaterialProxies.Values.ToList(); | ||
} | ||
|
||
private void ProcessModelObject( | ||
TSM.ModelObject modelObject, | ||
Dictionary<string, RenderMaterialProxy> renderMaterialProxies, | ||
HashSet<string> processedObjects | ||
) | ||
{ | ||
var objectId = modelObject.GetSpeckleApplicationId(); | ||
|
||
// NOTE: Related to CNX 798, processing of BooleanPart led to renderMaterial overwrites. Hence, it was excluded | ||
// If duplicate objectIds are still appearing, there is another type causing issues. | ||
if (processedObjects.Contains(objectId)) | ||
foreach (var atomicObject in atomicObjects) | ||
{ | ||
_logger.LogError( | ||
$"The objectId {objectId} had already been processed. Check ModelObjectExtension.cs for nested object circular references." | ||
); | ||
flattenedAtomicObjects.Add(atomicObject); | ||
flattenedAtomicObjects.AddRange(atomicObject.GetSupportedChildren().ToList()); | ||
} | ||
|
||
processedObjects.Add(objectId); | ||
|
||
var color = new TSMUI.Color(); | ||
TSMUI.ModelObjectVisualization.GetRepresentation(modelObject, ref color); | ||
int r = (int)(color.Red * 255); | ||
int g = (int)(color.Green * 255); | ||
int b = (int)(color.Blue * 255); | ||
int a = (int)(color.Transparency * 255); | ||
int argb = (a << 24) | (r << 16) | (g << 8) | b; | ||
|
||
Color systemColor = Color.FromArgb(argb); | ||
var colorId = color.GetSpeckleApplicationId(); | ||
|
||
// Ensure unique RenderMaterialProxy for each color | ||
if (!renderMaterialProxies.TryGetValue(colorId, out RenderMaterialProxy? renderMaterialProxy)) | ||
foreach (TSM.ModelObject flattenedAtomicObject in flattenedAtomicObjects) | ||
{ | ||
renderMaterialProxy = new RenderMaterialProxy | ||
var color = new TSMUI.Color(); | ||
TSMUI.ModelObjectVisualization.GetRepresentation(flattenedAtomicObject, ref color); | ||
int r = (int)(color.Red * 255); | ||
int g = (int)(color.Green * 255); | ||
int b = (int)(color.Blue * 255); | ||
int a = (int)(color.Transparency * 255); | ||
int argb = (a << 24) | (r << 16) | (g << 8) | b; | ||
|
||
Color systemColor = Color.FromArgb(argb); | ||
|
||
var colorId = color.GetSpeckleApplicationId(); | ||
var objectId = flattenedAtomicObject.GetSpeckleApplicationId(); | ||
if (renderMaterialProxies.TryGetValue(colorId, out RenderMaterialProxy? value)) | ||
{ | ||
value = new RenderMaterial { name = colorId, diffuse = systemColor.ToArgb() }, | ||
objects = new List<string>(), | ||
applicationId = colorId | ||
}; | ||
renderMaterialProxies[colorId] = renderMaterialProxy; | ||
value.objects.Add(objectId); | ||
} | ||
else | ||
{ | ||
var renderMaterial = new RenderMaterial() { name = colorId, diffuse = systemColor.ToArgb() }; | ||
RenderMaterialProxy proxyRenderMaterial = | ||
new() | ||
{ | ||
value = renderMaterial, | ||
objects = [objectId], | ||
applicationId = colorId | ||
}; | ||
renderMaterialProxies[colorId] = proxyRenderMaterial; | ||
} | ||
} | ||
|
||
renderMaterialProxy.objects.Add(objectId); | ||
|
||
// Recursively process children (not included in s_excludedTypes) | ||
foreach (var child in modelObject.GetSupportedChildren()) | ||
{ | ||
ProcessModelObject(child, renderMaterialProxies, processedObjects); | ||
} | ||
return renderMaterialProxies.Values.ToList(); | ||
} | ||
} |
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