Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (tekla): greyscaled objects in the viewer #412

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading