Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #18 from alvpickmans/Alvaro/dev_converter
Browse files Browse the repository at this point in the history
Dynamo Converter
  • Loading branch information
teocomi authored Jun 6, 2018
2 parents a8bc382 + 04de881 commit efed5b9
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 518 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
---
typora-copy-images-to: images
---

# SpeckleDynamo

![speckle](images/speckle.gif)
Expand Down Expand Up @@ -55,7 +51,7 @@ Start actions have been set to launch Dynamo Sandbox 1.3.

- dynamic inputs and outputs on sender/receiver



#### What's not working

Expand All @@ -66,7 +62,6 @@ Start actions have been set to launch Dynamo Sandbox 1.3.
-



## About Speckle

Speckle reimagines the design process from the Internet up: an open source (MIT) initiative for developing an extensible Design & AEC data communication protocol and platform. Contributions are welcome - we can't build this alone!
160 changes: 109 additions & 51 deletions SpeckleDynamo/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ namespace SpeckleDynamo
[InPortTypes("string")]
[InPortDescriptions("Stream ID")]


//TODO: dynamically generate out ports
[OutPortNames("A")]
[OutPortTypes("string")]
[OutPortDescriptions("No data received yet")]

[IsDesignScriptCompatible]
public class Receiver : NodeModel, INotifyPropertyChanged
{
private string _authToken;
private string _restApi;
private string _email;
private string _server;
private string _stream;
private string _oldStream;
private string _streamId;
private string _oldStreamId;
private string _message = "Initialising...";
private bool _paused = false;

private int elCount = 0;
private int subsetCount = 0;
private List<object> subset = new List<object>();

private bool _registeringPorts = false;

public string AuthToken { get => _authToken; set { _authToken = value; NotifyPropertyChanged("AuthToken"); } }
public string RestApi { get => _restApi; set { _restApi = value; NotifyPropertyChanged("RestApi"); } }
public string Email { get => _email; set { _email = value; NotifyPropertyChanged("Email"); } }
public string Server { get => _server; set { _server = value; NotifyPropertyChanged("Server"); } }
public string Stream { get => _stream; set { _stream = value; NotifyPropertyChanged("Stream"); } }
public string StreamId { get => _streamId; set
{ _streamId = value; NotifyPropertyChanged("StreamId");
} }

public string Message { get => _message; set { _message = value; NotifyPropertyChanged("Message"); } }
public bool Paused { get => _paused; set { _paused = value; NotifyPropertyChanged("Paused"); NotifyPropertyChanged("Receiving"); } }
Expand Down Expand Up @@ -87,52 +87,101 @@ public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode
foreach (Layer layer in Layers)
{

var subset = ConvertedObjects.GetRange((int)layer.StartIndex, (int)layer.ObjectCount);
//if (layer.Topology == "")
//{
Functions.SpeckleTempData.AddLayerObjects(Stream+layer.Guid, subset);

var functionCall = AstFactory.BuildFunctionCall(
new Func<string, object>(Functions.Functions.SpeckleOutput),
new List<AssociativeNode>
{
AstFactory.BuildStringNode(Stream+layer.Guid)
});

associativeNodes.Add(AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex((int)layer.OrderIndex), functionCall));
//}

//else
//{
// //HIC SVNT DRACONES
// var tree = new DataTree<object>();
// var treeTopo = layer.Topology.Split(' ');
// int subsetCount = 0;
// foreach (var branch in treeTopo)
// {
// if (branch != "")
// {
// var branchTopo = branch.Split('-')[0].Split(';');
// var branchIndexes = new List<int>();
// foreach (var t in branchTopo) branchIndexes.Add(Convert.ToInt32(t));

// var elCount = Convert.ToInt32(branch.Split('-')[1]);
// GH_Path myPath = new GH_Path(branchIndexes.ToArray());

// for (int i = 0; i < elCount; i++)
// tree.EnsurePath(myPath).Add(new Grasshopper.Kernel.Types.GH_ObjectWrapper(subset[subsetCount + i]));
// subsetCount += elCount;
// }
// }

//}
subset = ConvertedObjects.GetRange((int)layer.StartIndex, (int)layer.ObjectCount);
if (layer.Topology == "")
{
Functions.SpeckleTempData.AddLayerObjects(StreamId + layer.Guid, subset);
}

else
{
//HIC SVNT DRACONES
var tree = new List<object>();
var treeTopo = layer.Topology.Split(' ').Where(x => x != "");
subsetCount = 0;
foreach (var branch in treeTopo)
{

var branchTopo = branch.Split('-')[0].Split(';');
var branchIndexes = new List<int>();
foreach (var t in branchTopo)
branchIndexes.Add(Convert.ToInt32(t));

elCount = Convert.ToInt32(branch.Split('-')[1]);

RecursivelyCreateNestedLists(tree, branchIndexes, 0);

}

object output;

//if only one branch simplify output structure
if (treeTopo.Count() == 1)
{
//if only one item simplify output structure
if (tree[0] is List<object> && (tree[0] as List<object>).Count == 1)
output = (tree[0] as List<object>)[0];
else
output = tree[0];
}

else
output = tree;

Functions.SpeckleTempData.AddLayerObjects(StreamId + layer.Guid, output);

}



var functionCall = AstFactory.BuildFunctionCall(
new Func<string, object>(Functions.Functions.SpeckleOutput),
new List<AssociativeNode>
{
AstFactory.BuildStringNode(StreamId+layer.Guid)
});

associativeNodes.Add(AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex((int)layer.OrderIndex), functionCall));

}

return associativeNodes;
}
}

private void RecursivelyCreateNestedLists(List<object> tree, List<int> branchIndexes, int currentIndexPosition)
{
try
{
var index = branchIndexes[currentIndexPosition];
//add missing branches
while (tree.Count <= index || !(tree.ElementAt(index) is List<object>))
{
if (tree.Count > index)
tree.Insert(index, new List<object>());
else
tree.Add(new List<object>());
}
//when reaching the end of the path just add the elements
if (branchIndexes.Count == currentIndexPosition + 1)
{
for (int i = 0; i < elCount; i++)
(tree[index] as List<object>).Add(subset[subsetCount + i]);
subsetCount += elCount;


return;
}
RecursivelyCreateNestedLists((tree[index] as List<object>), branchIndexes, currentIndexPosition + 1);

}
catch (Exception e)
{
Console.WriteLine(e);
}

}

public virtual void UpdateGlobal()
{

Expand Down Expand Up @@ -289,16 +338,18 @@ internal void InitReceiverEventsAndGlobals()

myReceiver.OnError += (sender, e) =>
{
if (e.EventName == "websocket-disconnected")
return;
throw new WarningException(e.EventName + ": " + e.EventData);
};

}

internal void Stream_LostFocus(object sender, RoutedEventArgs e)
{
if (Stream == _oldStream)
if (StreamId == _oldStreamId)
return;
_oldStream = Stream;
_oldStreamId = StreamId;
//TODO: check integrity of stream id? Maybe length comparison?
Console.WriteLine("Changing streams...");

Expand All @@ -310,7 +361,7 @@ internal void Stream_LostFocus(object sender, RoutedEventArgs e)
InitReceiverEventsAndGlobals();

//TODO: get documentname and guid, not sure how... Maybe with an extension?
myReceiver.IntializeReceiver(Stream, "none", "Dynamo", "none", AuthToken);
myReceiver.IntializeReceiver(StreamId, "none", "Dynamo", "none", AuthToken);


}
Expand Down Expand Up @@ -385,6 +436,13 @@ public virtual void OnWsMessage(object source, SpeckleEventArgs e)
}
}

public override void Dispose()
{
if (myReceiver != null)
myReceiver.Dispose();
base.Dispose();
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
Expand Down
Loading

0 comments on commit efed5b9

Please sign in to comment.