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

New GH components for delta endpoints #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SpeckleGrasshopper/SpeckleGrasshopper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
<Compile Include="BaseComponents\GhSenderCoreClient.cs" />
<Compile Include="Parameters\Param_SpeckleStreams.cs" />
<Compile Include="UserDataUtils\QuerySpeckleObjectComponent.cs" />
<Compile Include="UserDataUtils\StreamApplyDelta.cs" />
<Compile Include="UserDataUtils\StreamGetDelta.cs" />
<Compile Include="Utilities\CollectionExtentions.cs" />
<Compile Include="Utilities\SpecklStreamExtentions.cs" />
<Compile Include="Loader.cs" />
Expand Down
116 changes: 116 additions & 0 deletions SpeckleGrasshopper/UserDataUtils/StreamApplyDelta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//extern alias SpeckleNewtonsoft;
using System;
using System.Collections.Generic;

using Grasshopper.Kernel;
using Rhino.Geometry;
//using Newtonsoft.Json;
using System.Windows.Forms;
using SpeckleCore;
using System.IO;
using Grasshopper.Kernel.Parameters;
using System.Reflection;

namespace SpeckleGrasshopper
{
public class StreamApplyDelta : GH_Component
{

/// <summary>
/// Initializes a new instance of the MyComponent1 class.
/// </summary>
public StreamApplyDelta()
: base("Applies a delta to a stream", "DELTADIFF",
"Applies a delta to a stream and returns a response. Make sure that the delta's revisionA matches with the input stream!",
"Speckle", "User Data Utils")
{
}

public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalMenuItems(menu);
}

/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
//pManager.AddGenericParameter("Client", "C", "Client.", GH_ParamAccess.item);
pManager.AddTextParameter("StreamID", "StreamID", "StreamID.", GH_ParamAccess.item);
pManager.AddGenericParameter("Delta", "Delta", "Delta to apply.", GH_ParamAccess.item);
}

/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Success", "Success", "True if the delta was applied onto the input stream.", GH_ParamAccess.item);
}

/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
SpeckleCore.SpeckleApiClient myClient = null;

Account account = null;
try
{
account = LocalContext.GetDefaultAccount();
var RestApi = account.RestApi;
var myToken = account.Token;
myClient = new SpeckleApiClient(account.RestApi);
myClient.AuthToken = myToken;
}
catch (Exception err)
{
}

string StreamID = null;
SpeckleCore.SpeckleDelta Delta = null;

DA.GetData(0, ref StreamID);
DA.GetData(1, ref Delta);

if (StreamID != null && Delta != null)
{
// TODO add exception for streams != Delta.RevisionA.id
var testApplyDelta = myClient.StreamApplyDeltaAsync(StreamID, Delta).Result;

var settings = new Newtonsoft.Json.JsonSerializerSettings()
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
Formatting = Newtonsoft.Json.Formatting.Indented
};

DA.SetData(0, testApplyDelta.Success);
Message = testApplyDelta.Message;

}

}

/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon
{
get
{
return Properties.Resources.Convert;
}
}

/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("{a314b496-4426-4f92-94b0-8176fe6577bc}"); }
}
}
}
145 changes: 145 additions & 0 deletions SpeckleGrasshopper/UserDataUtils/StreamGetDelta.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//extern alias SpeckleNewtonsoft;
using System;
using System.Collections.Generic;

using Grasshopper.Kernel;
using Rhino.Geometry;
using Newtonsoft.Json;
using Rhino.Collections;
using Grasshopper.Kernel.Types;
using System.Windows.Forms;
using SpeckleCore;
using System.IO;
using Grasshopper.Kernel.Parameters;

namespace SpeckleGrasshopper
{
public class DiffingStreamsDelta : GH_Component
{
string serialisedUDs;
/// <summary>
/// Initializes a new instance of the MyComponent1 class.
/// </summary>
public DiffingStreamsDelta()
: base("Diffing between two streams (delta)", "DELTADIFF",
"Operates a diffing between two different streams and returns the delta as response",
"Speckle", "User Data Utils")
{
}

public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalMenuItems(menu);
}

/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
//pManager.AddGenericParameter("Client", "C", "Client.", GH_ParamAccess.item);
pManager.AddTextParameter("StreamID", "StreamID_A", "StreamID.", GH_ParamAccess.item);
pManager.AddTextParameter("OtherStreamID", "StreamID_B", "OtherStreamID.", GH_ParamAccess.item);
}

/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddGeometryParameter("inA", "Deleted", "Objects only in A.", GH_ParamAccess.list);
pManager.AddGeometryParameter("inB", "Created", "Objects only in B.", GH_ParamAccess.list);
pManager.AddGeometryParameter("Common", "Common", "Objects common to A and B.", GH_ParamAccess.list);
pManager.AddGenericParameter("Delta", "Delta", "Computed Delta", GH_ParamAccess.item);
pManager.AddGenericParameter("DeltaJSON", "DeltaJSON", "Computed Delta", GH_ParamAccess.item);
}

/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{
SpeckleCore.SpeckleApiClient myClient = null;

Account account = null;
try
{
account = LocalContext.GetDefaultAccount();
var RestApi = account.RestApi;
var myToken = account.Token;
myClient = new SpeckleApiClient(account.RestApi);
}
catch (Exception err)
{
}

string StreamID = null;
string OtherStreamID = null;

DA.GetData(0, ref StreamID);
DA.GetData(1, ref OtherStreamID);

if (StreamID != null && OtherStreamID != null)
{
//var test = myClient.StreamDeltaDiffAsync(StreamID, OtherStreamID).Result;
//var testDiff = myClient.StreamDiffAsync(StreamID, OtherStreamID).Result.Objects;
var testDelta = myClient.StreamDeltaDiffAsync(StreamID, OtherStreamID).Result.Delta;

List<String> deletedIds = new List<String>();
foreach (var obj in testDelta.Deleted)
deletedIds.Add(obj._id);
string[] deleted = deletedIds.ToArray();
List<object> deletedObjs = Converter.Deserialise(myClient.ObjectGetBulkAsync(deleted, "").Result.Resources);
DA.SetDataList(0, deletedObjs);

List<String> createdIds = new List<String>();
foreach (var obj in testDelta.Created)
createdIds.Add(obj._id);
string[] created = createdIds.ToArray();
List<object> createdObjs = Converter.Deserialise(myClient.ObjectGetBulkAsync(created, "").Result.Resources);
DA.SetDataList(1, createdObjs);

List<String> commonIds = new List<String>();
foreach (var obj in testDelta.Common)
commonIds.Add(obj._id);
string[] common = commonIds.ToArray();
List<object> objsInCommon = Converter.Deserialise(myClient.ObjectGetBulkAsync(common, "").Result.Resources);
DA.SetDataList(2, objsInCommon);

var settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented
};

//dynamic parsedJson = Newtonsoft.Json.JsonConvert.DeserializeObject(testDelta.ToJson());
DA.SetData(3, testDelta);
DA.SetData(4, JsonConvert.SerializeObject(testDelta, settings));

//DA.SetData(3, testDelta);

}

}

/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon
{
get
{
return Properties.Resources.Convert;
}
}

/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("{b5bbc229-9e55-4540-984d-ddb8ab8357cb}"); }
}
}
}