SpeckleGrasshopper/Management/AddStreamsToProject.cs
/// <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>
This is the method that actually does the work.
This is the method that actually does the work .
protected override void SolveInstance(IGH_DataAccess DA)
    {
      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");
      }
      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);
      });
    }
protected override void SolveInstance ( IGH_DataAccess DA ) { var streamObjects = new List < object > ( ) ; var streamIds = new List < string > ( ) ; Project project = null ; Account account = null ; DA . GetData ( " " , ref project ) ; DA . GetDataList ( " " , streamObjects ) ; DA . GetData ( " " , ref account ) ; if ( null == project ) { AddRuntimeMessage ( GH_RuntimeMessageLevel . Warning , " " ) ; return ; } if ( null == streamObjects ) { AddRuntimeMessage ( GH_RuntimeMessageLevel . Warning , " " ) ; return ; } if ( null == project ) { AddRuntimeMessage ( GH_RuntimeMessageLevel . Warning , " " ) ; 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 , " " ) ; } AddRuntimeMessage ( GH_RuntimeMessageLevel . Error , " " ) ; 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 , " " + addedList + " " ) ; } 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 ) ; } ) ; }
This is the method that actually does the work.
This is the method that actually does the work .
//var streams = new List<SpeckleStream>(); //Do nothing if the project already contains these streams
DA
IGH_DataAccess
The DA object is used to retrieve from inputs and store in outputs.
The DA object is used to retrieve from inputs and store in outputs .