Skip to content

Commit

Permalink
feat(rhino): receive parameters as user strings (#2061)
Browse files Browse the repository at this point in the history
* Experimentation with builder pattern

* Graph tests

* fix(rhino): Getting layer names to match existing behavour

* Traversal fixes

* cleanup and comments

* fix(Rhino): Fixed issue with traveral on dynamic displayValues

* fixed test regression

* feat(revit): Implemented traversal refactor mvp in revit connector bindings

* attaches parameters as user strings

* Performance optimisations and Revit fixes

* updates CanConvertToNative rhino method to be reflective of document bake objects only

* Removal of previous traversal logic (rhino and revit)

* Update Model.cs

---------

Co-authored-by: Jedd Morgan <[email protected]>
Co-authored-by: Matteo Cominetti <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2023
1 parent 6bcba4f commit f2ea0fd
Showing 1 changed file with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public partial class ConnectorBindingsRhino : ConnectorBindings
private static string ApplicationIdKey = "applicationId";

public Dictionary<string, Base> StoredObjects = new Dictionary<string, Base>();
public Dictionary<string, Base> StoredObjectParams = new Dictionary<string, Base>(); // these are to store any parameters found on parent objects to add to fallback objects
public List<ApplicationObject> Preview { get; set; } = new List<ApplicationObject>();
public PreviewConduit PreviewConduit { get; set; }
private string SelectedReceiveCommit { get; set; }
Expand Down Expand Up @@ -267,9 +268,7 @@ public override async Task<StreamState> PreviewReceive(StreamState state, Progre
}

SelectedReceiveCommit = commit.id;
Preview.Clear();
StoredObjects.Clear();

ClearStorage();

var commitLayerName = DesktopUI2.Formatting.CommitInfo(state.CachedStream.name, state.BranchName, commit.id); // get commit layer name
Preview = FlattenCommitObject(commitObject, converter);
Expand Down Expand Up @@ -367,8 +366,7 @@ public override async Task<StreamState> ReceiveStream(StreamState state, Progres

if (SelectedReceiveCommit != commit.id)
{
Preview.Clear();
StoredObjects.Clear();
ClearStorage();
SelectedReceiveCommit = commit.id;
}

Expand Down Expand Up @@ -587,14 +585,17 @@ private async Task<Base> GetCommit(Commit commit, StreamState state, ProgressVie
private List<ApplicationObject> FlattenCommitObject(Base obj, ISpeckleConverter converter)
{

void StoreObject(Base @base, ApplicationObject appObj)
void StoreObject(Base @base, ApplicationObject appObj, Base parameters = null)
{
if (StoredObjects.ContainsKey(@base.id))
appObj.Update(
logItem:
"Found another object in this commit with the same id. Skipped other object"); //TODO check if we are actually ignoring duplicates, since we are returning the app object anyway...
else
StoredObjects.Add(@base.id, @base);

if (parameters != null && !StoredObjectParams.ContainsKey(@base.id))
StoredObjectParams.Add(@base.id, parameters);
}

ApplicationObject CreateApplicationObject(Base current, string containerId)
Expand All @@ -617,14 +618,15 @@ ApplicationObject NewAppObj()

//Handle objects convertable using displayValues
var fallbackMember = current["displayValue"] ?? current["@displayValue"];
var parameters = current["parameters"] as Base;
if (fallbackMember != null)
{
var appObj = NewAppObj();
var fallbackObjects = GraphTraversal.TraverseMember(fallbackMember)
.Select(o => CreateApplicationObject(o, containerId));
appObj.Fallback.AddRange(fallbackObjects);

StoreObject(current, appObj);
StoreObject(current, appObj, parameters);
return appObj;
}

Expand Down Expand Up @@ -792,6 +794,7 @@ private void BakeObject(ApplicationObject appObj, ISpeckleConverter converter, s

private void SetUserInfo(Base obj, ObjectAttributes attributes, ApplicationObject parent = null)
{
// set user strings
if (obj[UserStrings] is Base userStrings)
foreach (var member in userStrings.GetMembers(DynamicBaseMemberType.Dynamic))
attributes.SetUserString(member.Key, member.Value as string);
Expand All @@ -804,13 +807,41 @@ private void SetUserInfo(Base obj, ObjectAttributes attributes, ApplicationObjec
}
catch { }

// set parameters
if (parent != null)
{
if (StoredObjectParams.ContainsKey(parent.OriginalId))
{
var parameters = StoredObjectParams[parent.OriginalId];
foreach (var member in parameters.GetMembers(DynamicBaseMemberType.Dynamic))
{
if (member.Value is Base parameter)
{
try
{
attributes.SetUserString(member.Key, GetStringFromBaseProp(parameter, "value"));
}
catch { }
}
}
}
}

// set user dictionaries
if (obj[UserDictionary] is Base userDictionary)
ParseDictionaryToArchivable(attributes.UserDictionary, userDictionary);

var name = obj["name"] as string;
if (name != null) attributes.Name = name;
}

// Clears the stored objects, params, and preview objects
private void ClearStorage()
{
Preview.Clear();
StoredObjects.Clear();
StoredObjectParams.Clear();
}
#endregion

#region sending
Expand Down Expand Up @@ -1099,6 +1130,23 @@ private List<string> GetObjectsFromFilter(ISelectionFilter filter)
return objs;
}

private string GetStringFromBaseProp(Base @base, string propName)
{
var val = @base[propName];
if (val == null) return null;
switch (val)
{
case double o:
return o.ToString();
case bool o:
return o.ToString();
case int o:
return o.ToString();
case string o:
return o;
}
return null;
}
/// <summary>
/// Copies a Base to an ArchivableDictionary
/// </summary>
Expand Down

0 comments on commit f2ea0fd

Please sign in to comment.