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

Feat(tekla): cnx 703 add color proxies #341

Merged
merged 12 commits into from
Nov 1, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Speckle.Connector.Tekla2024.Extensions;

public static class ModelObjectExtensions
{
public static IEnumerable<TSM.ModelObject> GetSupportedChildren(this TSM.ModelObject modelObject)
{
foreach (TSM.ModelObject childObject in modelObject.GetChildren())
{
if (childObject is not TSM.ControlPoint or TSM.Weld or TSM.Fitting)
dogukankaratas marked this conversation as resolved.
Show resolved Hide resolved
{
yield return childObject;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Speckle.Connector.Tekla2024.Extensions;

public static class SpeckleApplicationIdExtensions
{
public static string GetSpeckleApplicationId(this TSM.ModelObject modelObject) =>
modelObject.Identifier.GUID.ToString();

public static string GetSpeckleApplicationId(this TSMUI.Color color) =>
$"color_{color.Red}_{color.Green}_{color.Blue}_{color.Transparency}";
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
global using TSM = Tekla.Structures.Model;
global using TSMUI = Tekla.Structures.Model.UI;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Drawing;
using Speckle.Connector.Tekla2024.Extensions;
using Speckle.Objects.Other;

namespace Speckle.Connector.Tekla2024.HostApp;

public class TeklaMaterialUnpacker
{
public List<RenderMaterialProxy> UnpackRenderMaterial(List<TSM.ModelObject> atomicObjects)
{
Dictionary<string, RenderMaterialProxy> renderMaterialProxies = new();

var flattenedAtomicObjects = new List<TSM.ModelObject>();

foreach (var atomicObject in atomicObjects)
{
flattenedAtomicObjects.Add(atomicObject);
flattenedAtomicObjects.AddRange(atomicObject.GetSupportedChildren().ToList());
}

foreach (TSM.ModelObject flattenedAtomicObject in flattenedAtomicObjects)
{
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.objects.Add(objectId);
}
else
{
var renderMaterial = new RenderMaterial() { name = colorId, diffuseColor = systemColor };
RenderMaterialProxy proxyRenderMaterial =
new()
{
value = renderMaterial,
objects = [objectId],
applicationId = colorId
};
renderMaterialProxies[colorId] = proxyRenderMaterial;
}
}

return renderMaterialProxies.Values.ToList();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Speckle.Connector.Tekla2024.Extensions;
using Speckle.Connector.Tekla2024.HostApp;
using Speckle.Connectors.Common.Builders;
using Speckle.Connectors.Common.Caching;
Expand All @@ -10,62 +11,57 @@
using Speckle.Sdk.Logging;
using Speckle.Sdk.Models;
using Speckle.Sdk.Models.Collections;
using Tekla.Structures.Model;
using Task = System.Threading.Tasks.Task;

namespace Speckle.Connector.Tekla2024.Operations.Send;

public class TeklaRootObjectBuilder : IRootObjectBuilder<ModelObject>
public class TeklaRootObjectBuilder : IRootObjectBuilder<TSM.ModelObject>
{
private readonly IRootToSpeckleConverter _rootToSpeckleConverter;
private readonly ISendConversionCache _sendConversionCache;
private readonly IConverterSettingsStore<TeklaConversionSettings> _converterSettings;
private readonly ILogger<TeklaRootObjectBuilder> _logger;
private readonly ISdkActivityFactory _activityFactory;
private readonly ComponentUnpacker _componentUnpacker;
private readonly TeklaMaterialUnpacker _materialUnpacker;

public TeklaRootObjectBuilder(
IRootToSpeckleConverter rootToSpeckleConverter,
ISendConversionCache sendConversionCache,
IConverterSettingsStore<TeklaConversionSettings> converterSettings,
ILogger<TeklaRootObjectBuilder> logger,
ISdkActivityFactory activityFactory,
ComponentUnpacker componentUnpacker
TeklaMaterialUnpacker materialUnpacker
)
{
_sendConversionCache = sendConversionCache;
_converterSettings = converterSettings;
_rootToSpeckleConverter = rootToSpeckleConverter;
_logger = logger;
_activityFactory = activityFactory;
_componentUnpacker = componentUnpacker;
_materialUnpacker = materialUnpacker;
}

public async Task<RootObjectBuilderResult> Build(
IReadOnlyList<ModelObject> teklaObjects,
IReadOnlyList<TSM.ModelObject> teklaObjects,
SendInfo sendInfo,
IProgress<CardProgress> onOperationProgressed,
CancellationToken cancellationToken = default
)
{
using var activity = _activityFactory.Start("Build");

var model = new Model();
var model = new TSM.Model();
string modelName = model.GetInfo().ModelName ?? "Unnamed model";

Collection rootObjectCollection = new() { name = modelName };
rootObjectCollection["units"] = _converterSettings.Current.SpeckleUnits;

// Step 0: unpack all component model objects
List<TSM.ModelObject> unpackedTeklaObjects = _componentUnpacker.UnpackComponents(teklaObjects).ToList();
rootObjectCollection["componentProxies"] = _componentUnpacker.ComponentProxiesCache.Values;

List<SendConversionResult> results = new(teklaObjects.Count);
int count = 0;

using (var _ = _activityFactory.Start("Convert all"))
{
foreach (ModelObject teklaObject in unpackedTeklaObjects)
foreach (TSM.ModelObject teklaObject in teklaObjects)
{
using var _2 = _activityFactory.Start("Convert");
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -83,13 +79,23 @@ public async Task<RootObjectBuilderResult> Build(
throw new SpeckleException("Failed to convert all objects.");
}

var renderMaterialProxies = _materialUnpacker.UnpackRenderMaterial(teklaObjects.ToList());
if (renderMaterialProxies.Count > 0)
{
rootObjectCollection[ProxyKeys.RENDER_MATERIAL] = renderMaterialProxies;
}

await Task.Yield();
return new RootObjectBuilderResult(rootObjectCollection, results);
}

private SendConversionResult ConvertTeklaObject(ModelObject teklaObject, Collection collectionHost, string projectId)
private SendConversionResult ConvertTeklaObject(
TSM.ModelObject teklaObject,
Collection collectionHost,
string projectId
)
{
string applicationId = teklaObject.Identifier.ToString();
string applicationId = teklaObject.GetSpeckleApplicationId();
string sourceType = teklaObject.GetType().Name;

try
Expand All @@ -102,7 +108,6 @@ private SendConversionResult ConvertTeklaObject(ModelObject teklaObject, Collect
else
{
converted = _rootToSpeckleConverter.Convert(teklaObject);
converted.applicationId = applicationId;
}

// Add to host collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Speckle.Connector.Tekla2024.Plugin;

[Plugin("Speckle.Connectors.Tekla")]
[Plugin("Speckle")]
[PluginUserInterface("Speckle.Connector.Tekla2024.SpeckleTeklaPanelHost")]
[InputObjectDependency(InputObjectDependency.NOT_DEPENDENT)] // See DevDocs/InputObjectDependency.NOT_DEPENDENT.png
public class TeklaPlugin : PluginBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public static IServiceCollection AddTekla(this IServiceCollection services)
IConverterSettingsStore<TeklaConversionSettings>,
ConverterSettingsStore<TeklaConversionSettings>
>();
services.AddScoped<ComponentUnpacker>();

// Register unpackers and bakers
services.AddScoped<TeklaMaterialUnpacker>();

services.AddMatchingInterfacesAsTransient(converterAssembly);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class SpeckleTeklaPanelHost : PluginFormBase

public SpeckleTeklaPanelHost()
{
this.Text = "Speckle";
this.Name = "Speckle";
dogukankaratas marked this conversation as resolved.
Show resolved Hide resolved
//TODO: Add Speckle icon
// TODO: Add thumbnail to connector
var services = new ServiceCollection();
services.Initialize(HostApplications.TeklaStructures, GetVersion());
services.AddTekla();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// this extension method is copy of the same method from connector
// we are using it for to make sure we are traversing children in the same way

namespace Speckle.Converter.Tekla2024.Extensions;

public static class ModelObjectExtensions
{
public static IEnumerable<TSM.ModelObject> GetSupportedChildren(this TSM.ModelObject modelObject)
{
foreach (TSM.ModelObject childObject in modelObject.GetChildren())
{
if (childObject is not TSM.ControlPoint or TSM.Weld or TSM.Fitting)
{
yield return childObject;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ public static class SpeckleApplicationIdExtensions
{
public static string GetSpeckleApplicationId(this TSM.ModelObject modelObject) =>
modelObject.Identifier.GUID.ToString();

public static string GetSpeckleApplicationId(this TSMUI.Color color) =>
$"color_{color.Red}_{color.Green}_{color.Blue}_{color.Transparency}";
}
2 changes: 2 additions & 0 deletions Converters/Tekla/Speckle.Converter.Tekla2024/GlobalUsing.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
global using SOG = Speckle.Objects.Geometry;
global using TG = Tekla.Structures.Geometry3d;
global using TSM = Tekla.Structures.Model;
global using TSMUI = Tekla.Structures.Model.UI;
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public Base Convert(object target)

Base result = objectConverter.Convert(target);

// add tekla specific identifiers
result.applicationId = modelObject.GetSpeckleApplicationId();
//TODO: attach properties

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk.Models;
using SOG = Speckle.Objects.Geometry;

namespace Speckle.Converter.Tekla2024.ToSpeckle.Helpers;

Expand Down Expand Up @@ -39,6 +38,13 @@ public IEnumerable<Base> GetDisplayValue(TSM.ModelObject modelObject)
}
break;

case TSM.Reinforcement reinforcement:
if (reinforcement.GetSolid() is TSM.Solid reinforcementSolid)
{
yield return _meshConverter.Convert(reinforcementSolid);
}
break;

default:
yield break;
}
Expand Down
Loading
Loading