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 642 detect selections in Tekla #312

Merged
merged 8 commits into from
Oct 21, 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,4 +1,4 @@
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.DUI.Models;
using Speckle.Connectors.DUI.Models.Card;
Expand Down Expand Up @@ -34,11 +34,11 @@ DocumentModelStore store

public DocumentModelStore GetDocumentState() => _store;

public void AddModel(ModelCard model) => throw new NotImplementedException();
public void AddModel(ModelCard model) => _store.Models.Add(model);

public void UpdateModel(ModelCard model) => throw new NotImplementedException();
public void UpdateModel(ModelCard model) => _store.UpdateModel(model);

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

public Task HighlightModel(string modelCardId) => throw new NotImplementedException();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bridge;
using Tekla.Structures.Model;

namespace Speckle.Connector.Tekla2024.Bindings;

public class TeklaSelectionBinding : ISelectionBinding
{
private readonly IAppIdleManager _idleManager;
private const string SELECTION_EVENT = "setSelection";
private readonly Tekla.Structures.Model.Events _events;
private readonly object _selectionEventHandlerLock = new object();
private readonly Tekla.Structures.Model.UI.ModelObjectSelector _selector;

public string Name => "selectionBinding";
public IBrowserBridge Parent { get; }

public TeklaSelectionBinding(
IAppIdleManager idleManager,
IBrowserBridge parent,
Events events,
Tekla.Structures.Model.UI.ModelObjectSelector selector
)
{
_idleManager = idleManager;
Parent = parent;
_events = events;
_selector = selector;

_events.SelectionChange += Events_SelectionChangeEvent;
_events.Register();

UpdateSelection();
}

private void Events_SelectionChangeEvent()
{
lock (_selectionEventHandlerLock)
{
_idleManager.SubscribeToIdle(nameof(TeklaSelectionBinding), UpdateSelection);
UpdateSelection();
}
}

private void UpdateSelection()
{
SelectionInfo selInfo = GetSelection();
Parent.Send(SELECTION_EVENT, selInfo);
}

public SelectionInfo GetSelection()
{
ModelObjectEnumerator selectedObjects = _selector.GetSelectedObjects();
List<string> objectIds = new List<string>();
List<string> objectTypes = new List<string>();

while (selectedObjects.MoveNext())
{
ModelObject modelObject = selectedObjects.Current;
string globalId = modelObject.Identifier.GUID.ToString();
objectIds.Add(globalId);
objectTypes.Add(modelObject.GetType().Name);
}

string typesString = string.Join(", ", objectTypes.Distinct());

return new SelectionInfo(
objectIds,
objectIds.Count == 0 ? "No objects selected." : $"{objectIds.Count} objects ({typesString})"
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.DUI.Models.Card.SendFilter;
using Speckle.Connectors.DUI.Settings;
Expand All @@ -9,17 +9,19 @@ public class TeklaSendBinding : ISendBinding
{
public string Name => "sendBinding";
public IBrowserBridge Parent { get; }
private readonly List<ISendFilter> _sendFilters;

public TeklaSendBinding(IBrowserBridge parent)
public TeklaSendBinding(IBrowserBridge parent, IEnumerable<ISendFilter> sendFilters)
{
Parent = parent;
_sendFilters = sendFilters.ToList();
}

public List<ISendFilter> GetSendFilters() => [];
public List<ISendFilter> GetSendFilters() => _sendFilters;

public List<ICardSetting> GetSendSettings() => [];

public Task Send(string modelCardId) => throw new NotImplementedException();
public Task Send(string modelCardId) => Task.CompletedTask;

public void CancelSend(string modelCardId) => throw new NotImplementedException();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Speckle.Connectors.DUI.Models.Card.SendFilter;

namespace Speckle.Connector.Tekla2024.Filters;

public class TeklaSelectionFilter : DirectSelectionSendFilter
{
public override List<string> GetObjectIds() => SelectedObjectIds;

public override bool CheckExpiry(string[] changedObjectIds) => SelectedObjectIds.Intersect(changedObjectIds).Any();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Speckle.Connectors.DUI.Bridge;
using Tekla.Structures.Model;

namespace Speckle.Connector.Tekla2024.HostApp;

public sealed class TeklaIdleManager : AppIdleManager
{
private readonly IIdleCallManager _idleCallManager;
private readonly Events _events;

public TeklaIdleManager(IIdleCallManager idleCallManager, Events events)
: base(idleCallManager)
{
_idleCallManager = idleCallManager;
_events = events;
}

protected override void AddEvent()
{
_events.ModelSave += TeklaEventsOnIdle;
_events.Register();
}

private void TeklaEventsOnIdle()
{
_idleCallManager.AppOnIdle(() =>
{
_events.ModelSave -= TeklaEventsOnIdle;
_events.UnRegister();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Tekla.Structures.Plugins;
using Tekla.Structures.Plugins;

namespace Speckle.Connector.Tekla2024.Plugin;

Expand All @@ -8,6 +8,9 @@ namespace Speckle.Connector.Tekla2024.Plugin;
public class TeklaPlugin : PluginBase
{
#pragma warning disable IDE1006

static TeklaPlugin() { }

public override bool Run(List<InputDefinition> Input)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Speckle.Connector.Tekla2024.Bindings;
using Speckle.Connector.Tekla2024.Filters;
using Speckle.Connector.Tekla2024.HostApp;
using Speckle.Connectors.Common;
using Speckle.Connectors.DUI;
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.DUI.Models;
using Speckle.Connectors.DUI.Models.Card.SendFilter;
using Speckle.Connectors.DUI.WebView;

namespace Speckle.Connector.Tekla2024;
Expand All @@ -29,5 +32,11 @@ public static void AddTekla(this IServiceCollection serviceCollection)
serviceCollection.AddSingleton<IBasicConnectorBinding, TeklaBasicConnectorBinding>();

serviceCollection.AddSingleton<IBinding, TeklaSendBinding>();
serviceCollection.AddSingleton<IBinding, TeklaSelectionBinding>();
serviceCollection.AddSingleton<IAppIdleManager, TeklaIdleManager>();

serviceCollection.AddScoped<ISendFilter, TeklaSelectionFilter>();
serviceCollection.AddSingleton(new Tekla.Structures.Model.Events());
serviceCollection.AddSingleton(new Tekla.Structures.Model.UI.ModelObjectSelector());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Windows.Forms;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using Microsoft.Extensions.DependencyInjection;
using Speckle.Connectors.Common;
Expand Down
Loading