Skip to content
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,15 +1,25 @@
using Rhino;
using Rhino.DocObjects;
using Speckle.Converters.Common;
using Speckle.Sdk.Logging;
using Speckle.Sdk.Models.Proxies;

namespace Speckle.Connectors.Rhino.HostApp;

/// <summary>
/// Unpacks the group lists for each object and sub-objects.
/// POC: Split me into group unpacker and group baker classes please!
/// It should be in scoped lifetime.
/// </summary>
public class RhinoGroupManager // POC: later make it more clean with RhinoGroupUnpacker Packer??? + see same POC comments in instance managers
{
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;

public RhinoGroupManager(IConversionContextStack<RhinoDoc, UnitSystem> contextStack)
{
_contextStack = contextStack;
}

public Dictionary<string, GroupProxy> GroupProxies { get; } = new();

public void UnpackGroups(IEnumerable<RhinoObject> rhinoObjects)
Expand Down Expand Up @@ -44,4 +54,31 @@ public void UnpackGroups(IEnumerable<RhinoObject> rhinoObjects)
}
}
}

public void BakeGroups(
List<GroupProxy> groupProxies,
Dictionary<string, List<string>> applicationIdMap,
string baseLayerName
)
{
using var _ = SpeckleActivityFactory.Start();
foreach (GroupProxy groupProxy in groupProxies.OrderBy(g => g.objects.Count))
{
var appIds = groupProxy.objects.SelectMany(oldObjId => applicationIdMap[oldObjId]).Select(id => new Guid(id));
var groupName = (groupProxy.name ?? "No Name Group") + $" ({baseLayerName})";
_contextStack.Current.Document.Groups.Add(groupName, appIds);
}
}

public void PurgeGroups(string baseLayerName)
{
for (int i = _contextStack.Current.Document.Groups.Count; i >= 0; i--)
{
var group = _contextStack.Current.Document.Groups.FindIndex(i);
if (group is { Name: not null } && group.Name.Contains(baseLayerName))
{
_contextStack.Current.Document.Groups.Delete(i);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void PurgeMaterials(string namePrefix)
var currentDoc = RhinoDoc.ActiveDoc; // POC: too much right now to interface around
foreach (Material material in currentDoc.Materials)
{
if (!material.IsDeleted && material.Name.Contains(namePrefix))
if (!material.IsDeleted && material.Name != null && material.Name.Contains(namePrefix))
{
currentDoc.Materials.Delete(material);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class RhinoHostObjectBuilder : IHostObjectBuilder
private readonly RhinoLayerManager _layerManager;
private readonly RhinoMaterialManager _materialManager;
private readonly RhinoColorManager _colorManager;
private readonly RhinoGroupManager _groupManager;
private readonly ISyncToThread _syncToThread;

public RhinoHostObjectBuilder(
Expand All @@ -39,6 +40,7 @@ public RhinoHostObjectBuilder(
RhinoInstanceObjectsManager instanceObjectsManager,
RhinoMaterialManager materialManager,
RhinoColorManager colorManager,
RhinoGroupManager groupManager,
ISyncToThread syncToThread
)
{
Expand All @@ -49,6 +51,7 @@ ISyncToThread syncToThread
_instanceObjectsManager = instanceObjectsManager;
_materialManager = materialManager;
_colorManager = colorManager;
_groupManager = groupManager;
_syncToThread = syncToThread;
}

Expand Down Expand Up @@ -217,28 +220,13 @@ private HostObjectBuilderResult BakeObjects(
// Stage 3: Groups
if (groupProxies is not null)
{
using (var _ = SpeckleActivityFactory.Start("Converting groups"))
{
BakeGroups(groupProxies, applicationIdMap);
}
_groupManager.BakeGroups(groupProxies, applicationIdMap, baseLayerName);
}

// Stage 4: Return
return new(bakedObjectIds, conversionResults);
}

private void BakeGroups(List<GroupProxy> groupProxies, Dictionary<string, List<string>> applicationIdMap)
{
using var _ = SpeckleActivityFactory.Start();
foreach (GroupProxy groupProxy in groupProxies.OrderBy(g => g.objects.Count))
{
var appIds = groupProxy.objects.SelectMany(oldObjId => applicationIdMap[oldObjId]).Select(id => new Guid(id));
var index = RhinoDoc.ActiveDoc.Groups.Add(appIds);
var addedGroup = RhinoDoc.ActiveDoc.Groups.FindIndex(index);
addedGroup.Name = groupProxy.name;
}
}

private void PreReceiveDeepClean(string baseLayerName)
{
// Remove all previously received layers and render materials from the document
Expand Down Expand Up @@ -266,6 +254,9 @@ private void PreReceiveDeepClean(string baseLayerName)
}
}
}

// Cleans up any previously received group
_groupManager.PurgeGroups(baseLayerName);
}

private IReadOnlyList<string> HandleConversionResult(
Expand Down
Loading