Skip to content

Commit

Permalink
feat(rhino): adds names to all objects on bake (#327)
Browse files Browse the repository at this point in the history
* adds names to all objects on bake

* Update RhinoHostObjectBuilder.cs

* refactors to pass in attributes for bake from parent

* adds name property to all rhino objects on send
  • Loading branch information
clairekuang authored Oct 25, 2024
1 parent 900427a commit a14b0a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,21 @@ CancellationToken cancellationToken
onOperationProgressed.Report(new("Converting objects", (double)++count / atomicObjects.Count));
try
{
// 1: get pre-created layer from cache in layer baker
// 0: get pre-created layer from cache in layer baker
int layerIndex = _layerBaker.GetAndCreateLayerFromPath(path, baseLayerName);

// 1: create object attributes for baking
string name = obj["name"] as string ?? "";
using ObjectAttributes atts = new() { LayerIndex = layerIndex, Name = name };

// 2: convert
var result = _converter.Convert(obj);

// 3: bake
var conversionIds = new List<string>();
if (result is GeometryBase geometryBase)
{
var guid = BakeObject(geometryBase, obj, layerIndex);
var guid = BakeObject(geometryBase, obj, atts);
conversionIds.Add(guid.ToString());
}
else if (result is List<GeometryBase> geometryBases) // one to many raw encoding case
Expand All @@ -150,13 +154,13 @@ CancellationToken cancellationToken
// EXTRA EXTRA NOTE: TY Ogu, i am no longer than unhappy about it. It's legit "mess".
foreach (var gb in geometryBases)
{
var guid = BakeObject(gb, obj, layerIndex);
var guid = BakeObject(gb, obj, atts);
conversionIds.Add(guid.ToString());
}
}
else if (result is IEnumerable<(object, Base)> fallbackConversionResult) // one to many fallback conversion
{
var guids = BakeObjectsAsGroup(fallbackConversionResult, obj, layerIndex, baseLayerName);
var guids = BakeObjectsAsFallbackGroup(fallbackConversionResult, obj, atts, baseLayerName);
conversionIds.AddRange(guids.Select(id => id.ToString()));
}

Expand Down Expand Up @@ -253,9 +257,19 @@ private void PreReceiveDeepClean(string baseLayerName)
_groupBaker.PurgeGroups(baseLayerName);
}

private Guid BakeObject(GeometryBase obj, Base originalObject, int layerIndex)
/// <summary>
/// Bakes an object to the document.
/// </summary>
/// <param name="obj"></param>
/// <param name="originalObject"></param>
/// <param name="atts"></param>
/// <returns></returns>
/// <remarks>
/// Material and Color attributes are processed here due to those properties existing sometimes on fallback geometry (instead of parent).
/// and this method is called by <see cref="BakeObjectsAsFallbackGroup(IEnumerable{ValueTuple{object, Base}}, Base, ObjectAttributes, string)"/>
/// </remarks>
private Guid BakeObject(GeometryBase obj, Base originalObject, ObjectAttributes atts)
{
ObjectAttributes atts = new() { LayerIndex = layerIndex };
var objectId = originalObject.applicationId ?? originalObject.id;

if (_materialBaker.ObjectIdAndMaterialIndexMap.TryGetValue(objectId, out int mIndex))
Expand All @@ -273,10 +287,10 @@ private Guid BakeObject(GeometryBase obj, Base originalObject, int layerIndex)
return _converterSettings.Current.Document.Objects.Add(obj, atts);
}

private List<Guid> BakeObjectsAsGroup(
private List<Guid> BakeObjectsAsFallbackGroup(
IEnumerable<(object, Base)> fallbackConversionResult,
Base originatingObject,
int layerIndex,
ObjectAttributes atts,
string baseLayerName
)
{
Expand All @@ -289,15 +303,17 @@ string baseLayerName
continue;
}

var id = BakeObject(geometryBase, originalBaseObject, layerIndex);
var id = BakeObject(geometryBase, originalBaseObject, atts);
objectIds.Add(id);
}

var groupIndex = _converterSettings.Current.Document.Groups.Add(
$@"{originatingObject.speckle_type.Split('.').Last()} - {originatingObject.applicationId ?? originatingObject.id} ({baseLayerName})",
objectIds
);

var group = _converterSettings.Current.Document.Groups.FindIndex(groupIndex);

objectIds.Insert(0, group.Id);
return objectIds;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Speckle.Converters.Common.Objects;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk.Models;
using RhinoObject = Rhino.DocObjects.RhinoObject;

Expand Down Expand Up @@ -28,6 +28,10 @@ public virtual Base Convert(object target)

// POC: Any common operations for all RhinoObjects should be done here, not on the specific implementer
// Things like user-dictionaries and other user-defined metadata.
if (!string.IsNullOrEmpty(typedTarget.Attributes.Name))
{
result["name"] = typedTarget.Attributes.Name;
}

return result;
}
Expand Down

0 comments on commit a14b0a6

Please sign in to comment.