Skip to content

Commit

Permalink
Merge pull request #376 from specklesystems/dev
Browse files Browse the repository at this point in the history
Update release/3.0.0 with changes from dev
  • Loading branch information
oguzhankoral authored Nov 8, 2024
2 parents 71409e8 + 34457ea commit 92435ce
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,6 @@ public async Task Send(string modelCardId)
);

List<Element> elements = await RefreshElementsOnSender(modelCard.NotNull()).ConfigureAwait(false);

if (modelCard is SenderModelCard senderModel)
{
foreach (Element element in elements)
{
senderModel.SendFilter.NotNull().IdMap.NotNull()[element.Id.ToString()] = element.UniqueId;
}
await Commands
.SetFilterObjectIds(modelCardId, senderModel.SendFilter.NotNull().IdMap.NotNull())
.ConfigureAwait(false);
}

List<ElementId> elementIds = elements.Select(el => el.Id).ToList();

if (elementIds.Count == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ instanceOrDefinition is InstanceProxy instanceProxy
)
{
var transform = MatrixToTransform(instanceProxy.transform, instanceProxy.units);

// POC: having layer creation during instance bake means no render materials!!
int layerIndex = _layerBaker.GetAndCreateLayerFromPath(layerCollection, baseLayerName);

int layerIndex = _layerBaker.GetLayerIndex(layerCollection, baseLayerName);
string instanceProxyId = instanceProxy.applicationId ?? instanceProxy.id;

ObjectAttributes atts = new() { LayerIndex = layerIndex };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Rhino;
using Rhino.DocObjects;
using Speckle.Connectors.Common.Operations.Receive;
using Speckle.Sdk;
using Speckle.Sdk.Models.Collections;
using Layer = Rhino.DocObjects.Layer;

Expand All @@ -25,25 +26,66 @@ public RhinoLayerBaker(RhinoMaterialBaker materialBaker, RhinoColorBaker colorBa
/// Creates the base layer and adds it to the cache.
/// </summary>
/// <param name="baseLayerName"></param>
public void CreateBaseLayer(string baseLayerName)
private void CreateBaseLayer(string baseLayerName)
{
var index = RhinoDoc.ActiveDoc.Layers.Add(new Layer { Name = baseLayerName }); // POC: too much effort right now to wrap around the interfaced layers and doc
_hostLayerCache.Add(baseLayerName, index);
_hostLayerCache[baseLayerName] = index;
}

/// <summary>
/// <para>For receive: Use this method to construct layers in the host app when receiving. It progressively caches layers while creating them, so a second call to get the same layer will be fast.</para>
/// Creates all layers needed for receiving data.
/// </summary>
public int GetAndCreateLayerFromPath(Collection[] collectionPath, string baseLayerName)
/// <param name="paths">Collections of paths</param>
/// <param name="baseLayerName">Name of the base layer</param>
/// <remarks>Make sure this is executing on the main thread, using e.g RhinoApp.InvokeAndWait.</remarks>
public void CreateAllLayersForReceive(IEnumerable<Collection[]> paths, string baseLayerName)
{
var layerPath = collectionPath.Select(o => string.IsNullOrWhiteSpace(o.name) ? "unnamed" : o.name);
CreateBaseLayer(baseLayerName);
var uniquePaths = new Dictionary<string, Collection[]>();
foreach (var path in paths)
{
var names = path.Select(o => string.IsNullOrWhiteSpace(o.name) ? "unnamed" : o.name);
var key = string.Join(",", names!);
uniquePaths[key] = path;
}

foreach (var uniquePath in uniquePaths)
{
var layerIndex = CreateLayerFromPath(uniquePath.Value, baseLayerName);
}
}

/// <summary>
/// Retrieves the index of a layer based on the given collection path and base layer name.
/// </summary>
/// <param name="collectionPath">The array containing the collection path to the layer.</param>
/// <param name="baseLayerName">The name of the base layer.</param>
/// <returns>The index of the layer in the cache.</returns>
/// <exception cref="SpeckleException">Thrown when the layer is not found in the cache. This can happen if you didn't call previously <see cref="CreateAllLayersForReceive"/></exception>
public int GetLayerIndex(Collection[] collectionPath, string baseLayerName)
{
var layerPath = collectionPath
.Select(o => string.IsNullOrWhiteSpace(o.name) ? "unnamed" : o.name)
.Prepend(baseLayerName);

var layerFullName = string.Join(Layer.PathSeparator, layerPath);

if (_hostLayerCache.TryGetValue(layerFullName, out int existingLayerIndex))
{
return existingLayerIndex;
}

throw new SpeckleException("Did not find a layer in the cache.");
}

/// <summary>
/// Creates a layer based on the given collection path and adds it to the Rhino document.
/// </summary>
/// <param name="collectionPath">An array of Collection objects representing the path to create the layer.</param>
/// <param name="baseLayerName">The base layer name to start creating the new layer.</param>
/// <returns>The index of the last created layer.</returns>
private int CreateLayerFromPath(Collection[] collectionPath, string baseLayerName)
{
var currentLayerName = baseLayerName;
var currentDocument = RhinoDoc.ActiveDoc; // POC: too much effort right now to wrap around the interfaced layers
Layer? previousLayer = currentDocument.Layers.FindName(currentLayerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public DisableRedrawScope(ViewTable viewTable, bool returnToStatus = true)
public void Dispose()
{
_viewTable.RedrawEnabled = _returnToStatus;
_viewTable.Redraw();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ ISdkActivityFactory activityFactory
_activityFactory = activityFactory;
}

#pragma warning disable CA1506
public async Task<HostObjectBuilderResult> Build(
#pragma warning restore CA1506
Base rootObject,
string projectName,
string modelName,
Expand All @@ -68,9 +70,9 @@ CancellationToken cancellationToken

// 0 - Clean then Rock n Roll!
PreReceiveDeepClean(baseLayerName);
_layerBaker.CreateBaseLayer(baseLayerName);

// 1 - Unpack objects and proxies from root commit object

var unpackedRoot = _rootObjectUnpacker.Unpack(rootObject);

// 2 - Split atomic objects and instance components with their path
Expand Down Expand Up @@ -107,11 +109,11 @@ CancellationToken cancellationToken
onOperationProgressed.Report(new("Baking layers (redraw disabled)", null));
using (var _ = _activityFactory.Start("Pre baking layers"))
{
using var layerNoDraw = new DisableRedrawScope(_converterSettings.Current.Document.Views);
foreach (var (path, _) in atomicObjectsWithPath)
RhinoApp.InvokeAndWait(() =>
{
_layerBaker.GetAndCreateLayerFromPath(path, baseLayerName);
}
using var layerNoDraw = new DisableRedrawScope(_converterSettings.Current.Document.Views);
_layerBaker.CreateAllLayersForReceive(atomicObjectsWithPath.Select(t => t.path), baseLayerName);
});
}

// 5 - Convert atomic objects
Expand All @@ -130,7 +132,7 @@ CancellationToken cancellationToken
try
{
// 0: get pre-created layer from cache in layer baker
int layerIndex = _layerBaker.GetAndCreateLayerFromPath(path, baseLayerName);
int layerIndex = _layerBaker.GetLayerIndex(path, baseLayerName);

// 1: create object attributes for baking
string name = obj["name"] as string ?? "";
Expand Down Expand Up @@ -217,7 +219,6 @@ CancellationToken cancellationToken
}

_converterSettings.Current.Document.Views.Redraw();

return new HostObjectBuilderResult(bakedObjectIds, conversionResults);
}

Expand All @@ -230,31 +231,35 @@ private void PreReceiveDeepClean(string baseLayerName)
RhinoMath.UnsetIntIndex
);

_instanceBaker.PurgeInstances(baseLayerName);
_materialBaker.PurgeMaterials(baseLayerName);

var doc = _converterSettings.Current.Document;
// Cleans up any previously received objects
if (rootLayerIndex != RhinoMath.UnsetIntIndex)
RhinoApp.InvokeAndWait(() =>
{
var documentLayer = doc.Layers[rootLayerIndex];
var childLayers = documentLayer.GetChildren();
if (childLayers != null)
_instanceBaker.PurgeInstances(baseLayerName);
_materialBaker.PurgeMaterials(baseLayerName);

var doc = _converterSettings.Current.Document;
// Cleans up any previously received objects
if (rootLayerIndex != RhinoMath.UnsetIntIndex)
{
using var layerNoDraw = new DisableRedrawScope(doc.Views);
foreach (var layer in childLayers)
var documentLayer = doc.Layers[rootLayerIndex];
var childLayers = documentLayer.GetChildren();
if (childLayers != null)
{
var purgeSuccess = doc.Layers.Purge(layer.Index, true);
if (!purgeSuccess)
using var layerNoDraw = new DisableRedrawScope(doc.Views);
foreach (var layer in childLayers)
{
Console.WriteLine($"Failed to purge layer: {layer}");
var purgeSuccess = doc.Layers.Purge(layer.Index, true);
if (!purgeSuccess)
{
Console.WriteLine($"Failed to purge layer: {layer}");
}
}
}
doc.Layers.Purge(documentLayer.Index, true);
}
}

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

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public RG.Polyline Convert(SOG.Polyline target)
RG.PolylineCurve ITypedConverter<SOG.Polyline, RG.PolylineCurve>.Convert(SOG.Polyline target)
{
var poly = Convert(target).ToPolylineCurve();
poly.Domain = _intervalConverter.Convert(target.domain);
if (target.domain is not null) // note it can be null
{
poly.Domain = _intervalConverter.Convert(target.domain);
}

return poly;
}
}

0 comments on commit 92435ce

Please sign in to comment.