This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from speckleworks/matt/gh-projects
Matt/gh projects
- Loading branch information
Showing
11 changed files
with
381 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.