Skip to content

Commit

Permalink
dogukan/cnx-744-model-card-highlight-in-tekla (#392)
Browse files Browse the repository at this point in the history
* adds highlighting methods

* clears selection before highlighting

* adds a functionality to zoom selected

---------

Co-authored-by: Oğuzhan Koral <[email protected]>
  • Loading branch information
dogukankaratas and oguzhankoral authored Nov 19, 2024
1 parent a7941bb commit a4f27b5
Showing 1 changed file with 101 additions and 3 deletions.
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; }
}

0 comments on commit a4f27b5

Please sign in to comment.