Compare commits

...

3 Commits

Author SHA1 Message Date
Oğuzhan Koral 92435ce1a1 Merge pull request #376 from specklesystems/dev
.NET Build and Publish / build (push) Has been cancelled
.NET Build and Publish / test (push) Has been cancelled
.NET Build and Publish / deploy-installers (push) Has been cancelled
Update release/3.0.0 with changes from dev
2024-11-08 18:16:16 +00:00
Oğuzhan Koral 34457ead0a remove useless code (#377) 2024-11-08 18:06:07 +00:00
Dimitrie Stefanescu 5e92889825 Rhino stability fixes (#375)
* fix: (wip) ensures layer creation is happening where it should (InvokeAndWait)

* fix: ensures polylines with null domains still convert ok

* feat: comments and makes non-public function private

* feat: wraps up fixes

makes layer purging quiet, and removes stale comments
2024-11-08 17:54:41 +00:00
6 changed files with 83 additions and 46 deletions
@@ -133,18 +133,6 @@ internal sealed class RevitSendBinding : RevitBaseBinding, ISendBinding
);
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)
@@ -118,10 +118,7 @@ public class RhinoInstanceBaker : IInstanceBaker<List<string>>
)
{
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 };
@@ -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;
@@ -25,18 +26,48 @@ public class RhinoLayerBaker : TraversalContextUnpacker
/// 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))
@@ -44,6 +75,17 @@ public class RhinoLayerBaker : TraversalContextUnpacker
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);
@@ -21,5 +21,6 @@ public sealed class DisableRedrawScope : IDisposable
public void Dispose()
{
_viewTable.RedrawEnabled = _returnToStatus;
_viewTable.Redraw();
}
}
@@ -54,7 +54,9 @@ public class RhinoHostObjectBuilder : IHostObjectBuilder
_activityFactory = activityFactory;
}
#pragma warning disable CA1506
public async Task<HostObjectBuilderResult> Build(
#pragma warning restore CA1506
Base rootObject,
string projectName,
string modelName,
@@ -68,9 +70,9 @@ public class RhinoHostObjectBuilder : IHostObjectBuilder
// 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
@@ -107,11 +109,11 @@ public class RhinoHostObjectBuilder : IHostObjectBuilder
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
@@ -130,7 +132,7 @@ public class RhinoHostObjectBuilder : IHostObjectBuilder
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 ?? "";
@@ -217,7 +219,6 @@ public class RhinoHostObjectBuilder : IHostObjectBuilder
}
_converterSettings.Current.Document.Views.Redraw();
return new HostObjectBuilderResult(bakedObjectIds, conversionResults);
}
@@ -230,31 +231,35 @@ public class RhinoHostObjectBuilder : IHostObjectBuilder
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>
@@ -55,7 +55,11 @@ public class PolylineToHostConverter
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;
}
}