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 #280 from speckleworks/matt/gh-projects
Browse files Browse the repository at this point in the history
Matt/gh projects
  • Loading branch information
didimitrie authored Sep 23, 2019
2 parents 842ca4b + a5df505 commit f9bd358
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 21 deletions.
168 changes: 168 additions & 0 deletions SpeckleGrasshopper/Management/AddStreamsToProject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;
using SpeckleCore;
using System.Collections.Generic;
using System.Linq;
using Grasshopper.Kernel;
using Rhino.Geometry;
using SpeckleGrasshopper.Properties;

namespace SpeckleGrasshopper.Management
{
public class AddStreamsToProject : GH_Component
{
/// <summary>
/// Initializes a new instance of the AddStreamsToProject class.
/// </summary>

SpeckleApiClient Client = new SpeckleApiClient();
Action ExpireComponent;
List<string> AddedStreamIds = new List<string>();
public AddStreamsToProject()
: base("AddStreamsToProject", "Add Streams","Add a list of streams to a project","Speckle", "Management")
{

}

public override void AddedToDocument(GH_Document document)
{
base.AddedToDocument(document);
ExpireComponent = () => this.ExpireSolution(true);
}

/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddGenericParameter("Account", "A", "Account with project write access", GH_ParamAccess.item);
pManager.AddGenericParameter("Project", "P", "Project to add streams to", GH_ParamAccess.item);
pManager.AddGenericParameter("Streams", "S", "Streams to add", GH_ParamAccess.list);
}

/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
}

/// <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)
{
//var streams = new List<SpeckleStream>();
var streamObjects = new List<object>();
var streamIds = new List<string>();
Project project = null;
Account account = null;

DA.GetData("Project", ref project);
DA.GetDataList("Streams", streamObjects);
DA.GetData("Account", ref account);

if (null == project) {
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"You must connect an account to this component");
return;
}

if (null == streamObjects)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"You must connect at least one stream to this component");
return;
}

if (null == project)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
"You must connect a project to this component");
return;
}

foreach (object streamObject in streamObjects)
{
if (streamObject is Grasshopper.Kernel.Types.GH_String)
{
var streamId = (streamObject as Grasshopper.Kernel.Types.GH_String).Value;
streamIds.Add(streamId);
continue;
}
if (streamObject is Grasshopper.Kernel.Types.GH_ObjectWrapper)
{
var stream = (SpeckleStream)(streamObject as Grasshopper.Kernel.Types.GH_ObjectWrapper).Value;
if (null != stream)
{
streamIds.Add(stream.StreamId);
continue;
}
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "This component only works with streamIds or SpeckleStream objects");
}
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "This component only works with streamIds or SpeckleStream objects");
return;
}

if (null != AddedStreamIds && AddedStreamIds.Count > 0)
{
string addedList = "";
foreach (string streamId in AddedStreamIds)
{
addedList += streamId + ",";
}
addedList = addedList.Substring(0, addedList.Length - 1);
AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Added " +
addedList +
"to the project");
}

//Do nothing if the project already contains these streams
var streamsToAdd = streamIds.Where(streamId => !project.Streams.Contains(streamId)).ToList();
if (streamsToAdd.Count() < 1 )
{
AddedStreamIds.Clear();
return;
}

project.Streams.AddRange(streamsToAdd);

Client.BaseUrl = account.RestApi;
Client.AuthToken = account.Token;

Client.ProjectUpdateAsync(project._id, project).ContinueWith(task =>
{
if (task.Result.Success == false)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, task.Result.Message);
return;
}
AddedStreamIds.Clear();
AddedStreamIds.AddRange(streamsToAdd);
Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponent);
});

}

/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon
{
get
{
//You can add image files to your project resources and access them like this:
// return Resources.IconForThisComponent;
return Resources.AddStreamToProject;
}
}

/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("6712f7f4-3faa-4d3a-b060-7385966dc5c7"); }
}
}
}
9 changes: 6 additions & 3 deletions SpeckleGrasshopper/Management/ListMyAccounts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ public override void AppendAdditionalMenuItems( ToolStripDropDown menu )

foreach ( var account in Accounts )
{
Menu_AppendItem( menu, count++ + ". " + account.ServerName, ( sender, e ) =>
string displayName = account.ServerName;
displayName = displayName.Substring(0, 10);
displayName += "... - " + account.Email.Substring(0,10) + "...";
Menu_AppendItem( menu, count++ + ". " + displayName, ( sender, e ) =>
{
Selected = account;
this.NickName = account.ServerName;
NickName = displayName;
Rhino.RhinoApp.MainApplicationWindow.Invoke( ExpireComponent );
}, true );
}
Expand All @@ -99,7 +102,7 @@ protected override System.Drawing.Bitmap Icon
{
get
{
return Resources.GenericIconXS;
return Resources.Accounts;
}
}

Expand Down
134 changes: 134 additions & 0 deletions SpeckleGrasshopper/Management/ListMyProjects.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
using SpeckleCore;
using System.Linq;

using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino.Geometry;
using SpeckleGrasshopper.Properties;
using System.Windows.Forms;

namespace SpeckleGrasshopper.Management
{
//based on dimitrie's ListStreams component
public class ListMyProjects : GH_Component
{
List<Project> UserProjects = new List<Project>();
SpeckleApiClient Client = new SpeckleApiClient();
Project SelectedProject = null;
Action ExpireComponent;

public ListMyProjects()
: base("Projects", "Projects", "Lists projects you own or have access to", "Speckle", "Management")
{
SpeckleInitializer.Initialize();
LocalContext.Init();
}

public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalMenuItems(menu);
foreach (Project project in UserProjects)
{
var nameString = "";
if (project.number != null) { nameString += project.number + " - "; }
nameString += project.Name;
Menu_AppendItem(menu, nameString, (sender, e) =>
{
if (SelectedProject == project)
{
SelectedProject = null;
}
else { SelectedProject = project; }
Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponent);
},true, project == SelectedProject );
}
}

/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddGenericParameter("Account", "A", "Speckle account you want to retrieve your streams from.", GH_ParamAccess.item);
}

/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Projects", "Ps", "Projects you own or have write access to", GH_ParamAccess.list);
pManager.AddGenericParameter("Selected Project", "P", "Double click this component to select one project", GH_ParamAccess.item);
}

public override void AddedToDocument(GH_Document document)
{
base.AddedToDocument(document);
ExpireComponent = () => this.ExpireSolution(true);
}
/// <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)
{
Account Account = null;
DA.GetData("Account", ref Account);


if (Account == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Could not set the account");
return;
}

if (SelectedProject == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Right click this component to select a project");
}


DA.SetData("Selected Project", SelectedProject);
DA.SetDataList("Projects", UserProjects);

Client.BaseUrl = Account.RestApi; Client.AuthToken = Account.Token;
Client.ProjectGetAllAsync().ContinueWith(tsk =>
{
if (tsk.Result.Success == false)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, tsk.Result.Message);
return;
}
var newProjects = tsk.Result.Resources.ToList();
var notUpdated = UserProjects.Select(x => x._id).SequenceEqual(newProjects.Select(x => x._id));

if (!notUpdated){
UserProjects = tsk.Result.Resources.ToList();
Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponent);
}
});
}

/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon
{
get
{

return Resources.Projects;
}
}

/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("d82e4c2c-dd81-4ff7-bdfd-d15163dc64f7"); }
}
}
}
36 changes: 20 additions & 16 deletions SpeckleGrasshopper/Management/ListMyStreams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class ListStreams : GH_Component
List<SpeckleStream> SharedStreams = new List<SpeckleStream>();
SpeckleApiClient Client = new SpeckleApiClient();

Account OldAccount;

Action ExpireComponent;

public ListStreams( ) : base( "Streams", "Streams", "Lists your existing Speckle streams for a specified account.", "Speckle", "Management" )
Expand Down Expand Up @@ -46,35 +44,41 @@ public override void AddedToDocument( GH_Document document )

protected override void SolveInstance( IGH_DataAccess DA )
{
object preAccount = null;
Account Account = null;
DA.GetData( 0, ref preAccount );

Account = ( ( Account ) preAccount.GetType().GetProperty( "Value" ).GetValue( preAccount, null ) );
DA.GetData( 0, ref Account );

if ( Account == null )
if ( Account == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Couldn't set the account");
return;
}

DA.SetDataList( 0, UserStreams );

if ( Account == OldAccount )
return;

OldAccount = Account;


Client.BaseUrl = Account.RestApi; Client.AuthToken = Account.Token;
Client.StreamsGetAllAsync( "fields=streamId,name,description&isComputedResult=false&deleted=false" ).ContinueWith( tsk =>
{
UserStreams = tsk.Result.Resources.ToList();
Rhino.RhinoApp.MainApplicationWindow.Invoke( ExpireComponent );
if (tsk.Result.Success == false)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, tsk.Result.Message);
return;
}
var newStreams = tsk.Result.Resources.ToList();
var notUpdated = UserStreams.Select(x => x._id).SequenceEqual(newStreams.Select(x => x._id));

if (!notUpdated)
{
UserStreams = tsk.Result.Resources.ToList();
Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponent);
}
} );
}

protected override System.Drawing.Bitmap Icon
{
get
{
return Resources.GenericIconXS;
return Resources.Streams;
}
}

Expand Down
Loading

0 comments on commit f9bd358

Please sign in to comment.