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

dogukan/cnx-744-model-card-highlight-in-tekla #392

Merged
merged 4 commits into from
Nov 19, 2024
Merged
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,8 +1,12 @@
using System.Collections;
using Microsoft.Extensions.Logging;
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.DUI.Models;
using Speckle.Connectors.DUI.Models.Card;
using Speckle.Sdk;
using Tekla.Structures;
using Tekla.Structures.Geometry3d;

namespace Speckle.Connector.Tekla2024.Bindings;

Expand All @@ -12,16 +16,22 @@ public class TeklaBasicConnectorBinding : IBasicConnectorBinding
private readonly DocumentModelStore _store;
public string Name => "baseBinding";
public IBrowserBridge Parent { get; }
private readonly ILogger<TeklaBasicConnectorBinding> _logger;
private readonly TSM.Model _model;

public TeklaBasicConnectorBinding(
IBrowserBridge parent,
ISpeckleApplication speckleApplication,
DocumentModelStore store
DocumentModelStore store,
ILogger<TeklaBasicConnectorBinding> logger,
TSM.Model model
)
{
_speckleApplication = speckleApplication;
_store = store;
Parent = parent;
_logger = logger;
_model = model;
}

public string GetSourceApplicationName() => _speckleApplication.Slug;
Expand All @@ -40,9 +50,97 @@ DocumentModelStore store

public void RemoveModel(ModelCard model) => _store.RemoveModel(model);

public Task HighlightModel(string modelCardId) => throw new NotImplementedException();
public async Task HighlightModel(string modelCardId)
{
try
{
var model = _store.GetModelById(modelCardId);
if (model == null)
{
_logger.LogError("Model was null when highlighting received model");
return;
}

List<string> objectIds = new();
if (model is SenderModelCard senderModel)
{
objectIds = senderModel.SendFilter?.RefreshObjectIds() ?? new List<string>();
}
else if (model is ReceiverModelCard receiverModel)
{
objectIds = receiverModel.BakedObjectIds?.ToList() ?? new List<string>();
}

if (objectIds.Count == 0)
{
await Commands
.SetModelError(modelCardId, new OperationCanceledException("No objects found to highlight."))
.ConfigureAwait(false);
return;
}

await HighlightObjects(objectIds).ConfigureAwait(false);
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "Failed to highlight model");
await Commands.SetModelError(modelCardId, ex).ConfigureAwait(false);
}
}

public async Task HighlightObjects(IReadOnlyList<string> objectIds)
{
try
{
await Task.Run(() =>
{
// passing an empty list to create current selection
var selector = new TSMUI.ModelObjectSelector();
selector.Select(new ArrayList());

public Task HighlightObjects(IReadOnlyList<string> objectIds) => throw new NotImplementedException();
if (objectIds.Count > 0)
{
var modelObjects = objectIds
.Select(id => _model.SelectModelObject(new Identifier(new Guid(id))))
.Where(obj => obj != null)
.ToList();

selector.Select(new ArrayList(modelObjects));

// to find the min and max coordinates of the selected objects
// with that we can create a bounding box and zoom selected
var points = new List<Point>();
foreach (var obj in modelObjects)
{
points.Add(obj.GetCoordinateSystem().Origin);
foreach (TSM.ModelObject child in obj.GetChildren())
{
points.Add(child.GetCoordinateSystem().Origin);
}
}

var minX = points.Min(p => p.X);
var minY = points.Min(p => p.Y);
var minZ = points.Min(p => p.Z);
var maxX = points.Max(p => p.X);
var maxY = points.Max(p => p.Y);
var maxZ = points.Max(p => p.Z);

// create the bounding box
var bounds = new AABB { MinPoint = new Point(minX, minY, minZ), MaxPoint = new Point(maxX, maxY, maxZ) };

// zoom in to bounding box
TSMUI.ViewHandler.ZoomToBoundingBox(bounds);
}
_model.CommitChanges();
})
.ConfigureAwait(false);
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "Failed to highlight objects");
}
}

public BasicConnectorBindingCommands Commands { get; }
}
Loading