Skip to content

Commit

Permalink
fix (tekla): greyscaled objects in the viewer (#412)
Browse files Browse the repository at this point in the history
* adds teklamaterialcahce

* Revert "adds teklamaterialcahce"

This reverts commit 3fbba01.

* detach applicationids from displayvalue

---------

Co-authored-by: Oğuzhan Koral <[email protected]>
  • Loading branch information
dogukankaratas and oguzhankoral authored Nov 27, 2024
1 parent 9d6c7de commit 3ad4d46
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 65 deletions.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public Base Convert(object target)
var displayValue = _displayValueExtractor.GetDisplayValue(modelObject).ToList();
if (displayValue.Count > 0)
{
foreach (var displayValueObject in displayValue)
{
// NOTE: since we put ModelObject applicationIds into proxies, we should also mutate the same application id for its displayValue objects.
// otherwise we will have anonymous objects on receive for other host apps
displayValueObject.applicationId = modelObject.GetSpeckleApplicationId();
}
result["displayValue"] = displayValue;
}

Expand Down

0 comments on commit 3ad4d46

Please sign in to comment.