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

CNX-755: Create Collections by Category #387

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,58 @@
using Speckle.Converter.Tekla2024;
using Speckle.Converters.Common;
using Speckle.Sdk.Models.Collections;

namespace Speckle.Connector.Tekla2024.HostApp;
bjoernsteinhagen marked this conversation as resolved.
Show resolved Hide resolved

public class SendCollectionManager
{
private readonly IConverterSettingsStore<TeklaConversionSettings> _converterSettings;
private readonly Dictionary<string, Collection> _collectionCache = new();

public SendCollectionManager(IConverterSettingsStore<TeklaConversionSettings> converterSettings)
{
_converterSettings = converterSettings;
}

public Collection GetAndCreateObjectHostCollection(TSM.ModelObject teklaObject, Collection rootObject)
{
// Tekla Data Structure: rootObject > objectType > name
// Very high-level at this stage. Would be good to have sub-groups in future releases
// TODO: Refine further according to section types (for beams), constituent elements (for components) etc. at later stage
var path = new List<string>();
path.Add(teklaObject.GetType().ToString().Split('.').Last());

// NOTE: First pass at seeing if a collection key already exists
string fullPathName = string.Concat(path);
if (_collectionCache.TryGetValue(fullPathName, out Collection? value))
{
return value;
}

// NOTE: As this point, we need to create a suitable collection
// This would be using a recursive approach to see where to add collection
// However, since data structure is flat, this returns quick (shoutout to Revit at this stage ;) )
string flatPathName = "";
Collection previousCollection = rootObject;

foreach (var pathItem in path)
bjoernsteinhagen marked this conversation as resolved.
Show resolved Hide resolved
{
flatPathName += pathItem;
Collection childCollection;
if (_collectionCache.TryGetValue(flatPathName, out Collection? collection))
{
childCollection = collection;
}
else
{
childCollection = new Collection(pathItem);
previousCollection.elements.Add(childCollection);
_collectionCache[flatPathName] = childCollection;
}

previousCollection = childCollection;
}

return previousCollection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TeklaRootObjectBuilder : IRootObjectBuilder<TSM.ModelObject>
private readonly IRootToSpeckleConverter _rootToSpeckleConverter;
private readonly ISendConversionCache _sendConversionCache;
private readonly IConverterSettingsStore<TeklaConversionSettings> _converterSettings;
private readonly SendCollectionManager _sendCollectionManager;
private readonly ILogger<TeklaRootObjectBuilder> _logger;
private readonly ISdkActivityFactory _activityFactory;
private readonly TeklaMaterialUnpacker _materialUnpacker;
Expand All @@ -28,13 +29,15 @@ public TeklaRootObjectBuilder(
IRootToSpeckleConverter rootToSpeckleConverter,
ISendConversionCache sendConversionCache,
IConverterSettingsStore<TeklaConversionSettings> converterSettings,
SendCollectionManager sendCollectionManager,
ILogger<TeklaRootObjectBuilder> logger,
ISdkActivityFactory activityFactory,
TeklaMaterialUnpacker materialUnpacker
)
{
_sendConversionCache = sendConversionCache;
_converterSettings = converterSettings;
_sendCollectionManager = sendCollectionManager;
_rootToSpeckleConverter = rootToSpeckleConverter;
_logger = logger;
_activityFactory = activityFactory;
Expand Down Expand Up @@ -110,8 +113,10 @@ string projectId
converted = _rootToSpeckleConverter.Convert(teklaObject);
}

var collection = _sendCollectionManager.GetAndCreateObjectHostCollection(teklaObject, collectionHost);

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

return new(Status.SUCCESS, applicationId, sourceType, converted);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static IServiceCollection AddTekla(this IServiceCollection services)
services.AddScoped<ISendFilter, TeklaSelectionFilter>();
services.AddSingleton<ISendConversionCache, SendConversionCache>();
services.AddSingleton(DefaultTraversal.CreateTraversalFunc());
services.AddScoped<SendCollectionManager>();
services.AddScoped<IRootObjectBuilder<ModelObject>, TeklaRootObjectBuilder>();
services.AddScoped<SendOperation<ModelObject>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SpeckleApplicationIdExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Filters\TeklaSelectionFilter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GlobalUsing.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HostApp\SendCollectionManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HostApp\TeklaDocumentModelStore.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HostApp\TeklaIdleManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HostApp\TeklaMaterialUnpacker.cs" />
Expand Down