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 646 - Tekla Converter Project #328

Merged
merged 36 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
54e9297
creates tekla converter
dogukankaratas Oct 22, 2024
0a9d4e8
adds send operation
dogukankaratas Oct 23, 2024
168d8df
services registrated
dogukankaratas Oct 23, 2024
ba38108
updates the panel host
dogukankaratas Oct 24, 2024
cdf4863
adds beam converter
dogukankaratas Oct 24, 2024
e49aebd
updates beam converter
dogukankaratas Oct 24, 2024
bd67a03
basic info attached
dogukankaratas Oct 24, 2024
b135f73
creates the mesh from solid
dogukankaratas Oct 25, 2024
a204c6e
updates the mesh converter
dogukankaratas Oct 25, 2024
78178e9
attachs colors to mesh
dogukankaratas Oct 25, 2024
0e62bd8
updates formatting
dogukankaratas Oct 25, 2024
6028e43
updates formatting again
dogukankaratas Oct 25, 2024
f042e54
updates assemblyinfo
dogukankaratas Oct 25, 2024
15ac134
Merge branch 'dev' into dogukan/cnx-646-set-up-tekla-converter-project
clairekuang Oct 25, 2024
ac0820d
clean locks
dogukankaratas Oct 25, 2024
1503e35
Merge branch 'dogukan/cnx-646-set-up-tekla-converter-project' of http…
dogukankaratas Oct 25, 2024
0519b59
Merge branch 'dev' into dogukan/cnx-646-set-up-tekla-converter-project
clairekuang Oct 25, 2024
81398d4
Merge branch 'dogukan/cnx-646-set-up-tekla-converter-project' of http…
dogukankaratas Oct 25, 2024
b5f3c45
Merge branch 'dogukan/cnx-646-set-up-tekla-converter-project' of http…
dogukankaratas Oct 25, 2024
03f07c2
updates package.lock
dogukankaratas Oct 25, 2024
3b8a378
adds global using
dogukankaratas Oct 28, 2024
f22e979
formats and cleans latest dev
dogukankaratas Oct 28, 2024
e17c63b
updates teklasendbinding
dogukankaratas Oct 28, 2024
e5495b9
update csproj
dogukankaratas Oct 28, 2024
94fa0a2
adds converter project to Local.sln
dogukankaratas Oct 28, 2024
59505ce
update packages
dogukankaratas Oct 28, 2024
3fcdb30
Merge branch 'dev' into dogukan/cnx-646-set-up-tekla-converter-project
dogukankaratas Oct 28, 2024
54806f4
updates csproj
dogukankaratas Oct 28, 2024
38d0696
objects package removed
dogukankaratas Oct 28, 2024
43d1daa
Revert "adds converter project to Local.sln"
dogukankaratas Oct 28, 2024
ba104ff
updates local.sln
dogukankaratas Oct 28, 2024
bf6d133
Merge branch 'dogukan/cnx-646-set-up-tekla-converter-project' of http…
dogukankaratas Oct 28, 2024
af24d82
Merge branch 'dev' into dogukan/cnx-646-set-up-tekla-converter-project
dogukankaratas Oct 28, 2024
a2e59c1
removed unnecessary container initialization
dogukankaratas Oct 28, 2024
c4fcd94
deactivated the argb attach to mesh
dogukankaratas Oct 28, 2024
4ac3e98
formatted one last time (hopefully)
dogukankaratas Oct 28, 2024
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,29 +1,205 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Speckle.Connectors.Common.Caching;
using Speckle.Connectors.Common.Cancellation;
using Speckle.Connectors.Common.Operations;
using Speckle.Connectors.DUI.Bindings;
using Speckle.Connectors.DUI.Bridge;
using Speckle.Connectors.DUI.Exceptions;
using Speckle.Connectors.DUI.Logging;
using Speckle.Connectors.DUI.Models;
using Speckle.Connectors.DUI.Models.Card;
using Speckle.Connectors.DUI.Models.Card.SendFilter;
using Speckle.Connectors.DUI.Settings;
using Speckle.Converter.Tekla2024;
using Speckle.Converters.Common;
using Speckle.Sdk;
using Speckle.Sdk.Common;
using Speckle.Sdk.Logging;
using Tekla.Structures;
using Tekla.Structures.Model;
dogukankaratas marked this conversation as resolved.
Show resolved Hide resolved
using Task = System.Threading.Tasks.Task;

namespace Speckle.Connector.Tekla2024.Bindings;

public class TeklaSendBinding : ISendBinding
public sealed class TeklaSendBinding : ISendBinding, IDisposable
{
public string Name => "sendBinding";
public SendBindingUICommands Commands { get; }
public IBrowserBridge Parent { get; }

private readonly DocumentModelStore _store;
private readonly IAppIdleManager _idleManager;
private readonly IServiceProvider _serviceProvider;
private readonly List<ISendFilter> _sendFilters;
private readonly CancellationManager _cancellationManager;
private readonly ISendConversionCache _sendConversionCache;
private readonly IOperationProgressManager _operationProgressManager;
private readonly ILogger<TeklaSendBinding> _logger;
private readonly ITeklaConversionSettingsFactory _teklaConversionSettingsFactory;
private readonly ISpeckleApplication _speckleApplication;
private readonly ISdkActivityFactory _activityFactory;
private readonly Model _model;
private readonly Events _events;

public TeklaSendBinding(IBrowserBridge parent, IEnumerable<ISendFilter> sendFilters)
private ConcurrentDictionary<string, byte> ChangedObjectIds { get; set; } = new();

public TeklaSendBinding(
DocumentModelStore store,
IAppIdleManager idleManager,
IBrowserBridge parent,
IEnumerable<ISendFilter> sendFilters,
IServiceProvider serviceProvider,
CancellationManager cancellationManager,
ISendConversionCache sendConversionCache,
IOperationProgressManager operationProgressManager,
ILogger<TeklaSendBinding> logger,
ITeklaConversionSettingsFactory teklaConversionSettingsFactory,
ISpeckleApplication speckleApplication,
ISdkActivityFactory activityFactory
)
{
Parent = parent;
_store = store;
_idleManager = idleManager;
_serviceProvider = serviceProvider;
_sendFilters = sendFilters.ToList();
_cancellationManager = cancellationManager;
_sendConversionCache = sendConversionCache;
_operationProgressManager = operationProgressManager;
_logger = logger;
_teklaConversionSettingsFactory = teklaConversionSettingsFactory;
_speckleApplication = speckleApplication;
Parent = parent;
Commands = new SendBindingUICommands(parent);
_activityFactory = activityFactory;

_model = new Model();
_events = new Events();
SubscribeToTeklaEvents();
}

private void SubscribeToTeklaEvents()
{
_events.ModelObjectChanged += ModelHandler_OnChange;
_events.Register();
}

private void ModelHandler_OnChange(List<ChangeData> changes)
{
foreach (var change in changes)
{
if (change.Object is ModelObject modelObj)
{
ChangedObjectIds[modelObj.Identifier.ID.ToString()] = 1;
}
}

if (changes.Count > 0)
{
_idleManager.SubscribeToIdle(nameof(TeklaSendBinding), () => RunExpirationChecks());
}
}

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

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

public Task Send(string modelCardId) => Task.CompletedTask;
public async Task Send(string modelCardId)
{
using var activity = _activityFactory.Start();
using var scope = _serviceProvider.CreateScope();
scope
.ServiceProvider.GetRequiredService<IConverterSettingsStore<TeklaConversionSettings>>()
.Initialize(_teklaConversionSettingsFactory.Create(_model));

try
{
if (_store.GetModelById(modelCardId) is not SenderModelCard modelCard)
{
throw new InvalidOperationException("No publish model card was found.");
}

public void CancelSend(string modelCardId) => throw new NotImplementedException();
CancellationToken cancellationToken = _cancellationManager.InitCancellationTokenSource(modelCardId);

public SendBindingUICommands Commands { get; }
List<ModelObject> teklaObjects = modelCard
.SendFilter.NotNull()
.GetObjectIds()
.Select(id => _model.SelectModelObject(new Identifier(new Guid(id))))
.Where(obj => obj != null)
.ToList();

if (teklaObjects.Count == 0)
{
throw new SpeckleSendFilterException("No objects were found to convert. Please update your publish filter!");
}

var sendResult = await scope
.ServiceProvider.GetRequiredService<SendOperation<ModelObject>>()
.Execute(
teklaObjects,
modelCard.GetSendInfo(_speckleApplication.Slug),
_operationProgressManager.CreateOperationProgressEventHandler(Parent, modelCardId, cancellationToken),
cancellationToken
)
.ConfigureAwait(false);

await Commands
.SetModelSendResult(modelCardId, sendResult.RootObjId, sendResult.ConversionResults)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
return;
}
catch (Exception ex) when (!ex.IsFatal())
{
_logger.LogModelCardHandledError(ex);
await Commands.SetModelError(modelCardId, ex).ConfigureAwait(false);
}
}

public void CancelSend(string modelCardId) => _cancellationManager.CancelOperation(modelCardId);

private async Task RunExpirationChecks()
{
if (!_model.GetConnectionStatus())
{
_logger.LogError("Tekla expiration checks were running without an active model.");
return;
}

var senders = _store.GetSenders();
string[] objectIdsList = ChangedObjectIds.Keys.ToArray();
List<string> expiredSenderIds = new();

_sendConversionCache.EvictObjects(objectIdsList);

foreach (SenderModelCard modelCard in senders)
{
var intersection = modelCard.SendFilter.NotNull().GetObjectIds().Intersect(objectIdsList).ToList();
var isExpired = intersection.Count != 0;
if (isExpired)
{
expiredSenderIds.Add(modelCard.ModelCardId.NotNull());
}
}

await Commands.SetModelsExpired(expiredSenderIds).ConfigureAwait(false);
ChangedObjectIds = new();
}

private bool _disposed;

public void Dispose()
{
if (!_disposed)
{
if (_events != null)
{
_events.UnRegister();
}
_disposed = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Microsoft.Extensions.Logging;
using Speckle.Connectors.Common.Builders;
using Speckle.Connectors.Common.Caching;
using Speckle.Connectors.Common.Conversion;
using Speckle.Connectors.Common.Operations;
using Speckle.Converter.Tekla2024;
using Speckle.Converters.Common;
using Speckle.Sdk;
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>
{
private readonly IRootToSpeckleConverter _rootToSpeckleConverter;
private readonly ISendConversionCache _sendConversionCache;
private readonly IConverterSettingsStore<TeklaConversionSettings> _converterSettings;
private readonly ILogger<TeklaRootObjectBuilder> _logger;
private readonly ISdkActivityFactory _activityFactory;

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

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

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

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

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

using (var _ = _activityFactory.Start("Convert all"))
{
foreach (ModelObject teklaObject in teklaObjects)
{
using var _2 = _activityFactory.Start("Convert");
cancellationToken.ThrowIfCancellationRequested();

var result = ConvertTeklaObject(teklaObject, rootObjectCollection, sendInfo.ProjectId);
results.Add(result);

++count;
onOperationProgressed.Report(new("Converting", (double)count / teklaObjects.Count));
}
}

if (results.All(x => x.Status == Status.ERROR))
{
throw new SpeckleException("Failed to convert all objects.");
}

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

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

try
{
Base converted;
if (_sendConversionCache.TryGetValue(projectId, applicationId, out ObjectReference? value))
{
converted = value;
}
else
{
converted = _rootToSpeckleConverter.Convert(teklaObject);
converted.applicationId = applicationId;
}

// Add to host collection
collectionHost.elements.Add(converted);

return new(Status.SUCCESS, applicationId, sourceType, converted);
}
catch (Exception ex) when (!ex.IsFatal())
{
_logger.LogError(ex, sourceType);
return new(Status.ERROR, applicationId, sourceType, null, ex);
}
}
}
Loading
Loading