Delete object

Hi,
I’am using this tool to select then delete with rhino command button _Delete.
I was wondering if I can directly modify the C# to delete instead of only select.

using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

using System.IO;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.InteropServices;

using Rhino.DocObjects;
using Rhino.Collections;
using GH_IO;
using GH_IO.Serialization;

/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
  /// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
  /// <param name="text">String to print.</param>
  private void Print(string text) { /* Implementation hidden. */ }
  /// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
  /// <param name="format">String format.</param>
  /// <param name="args">Formatting parameters.</param>
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  /// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj) { /* Implementation hidden. */ }
  /// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(bool run, Guid ID, ref object A, ref object B)
  {
if(run){
  doc.Objects.Select(ID);
  A = ID;
}
  }

  // <Custom additional code> 

  // </Custom additional code> 
}`

I also have 2 additionnal question :
1- Where can I find this command line ?
Because I have been looking for a “library” on internet, and I just can’t find the original command
doc.Objects.Select(ID)
so I didn’t find any way to replace it by something such as :
doc.Delete.Objects(ID)
2- Any easy way to add something as a “print true” only when it’s done ?

Usually “doc” is from a more complete statement:
RhinoDoc doc = this.RhinoDocument;

Then you can delete rhino objects with:
bool success = doc.Objects.Delete(ID, false);

the bool “success” will be true or false depending if the deleting operation was successful or not.

See here: https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_Tables_ObjectTable_Delete_5.htm

Anyway, deleting during the script seems to trigger an error… experts needed here

Need to use callback to solve this problem,like this.

1 Like

Hi Naruto, mays I ask you the complete code in a file, cause when I just re-wrote what it’s wrote on your jpeg, my code doesn’t work…

Thansk !

I couldn’t reproduce this error. As far as know, there shouldn’t be any problem with deleting RhinoObjects during Grasshopper Solution.

There is an overload of ObjectTable.Delete Method which accept a list of guids (IEnumerable<Guid>) so it seems that there is no need to iterate over all guids.
DeleteObjects.gh (14.0 KB)

Yes, interesting.

By using a “Guid (ID)” like you did, it don’t gives an error.
It seems Guid parameter automatically internalize the Guid string inside the parameter itself, and so it’s not a referenced object from rhino.

If you use a Geometry parameter it give the error I did mean. (Geo casted to Guid in C# input)
(Personally i think it make more sense to work with referenced geometries… when you delete it from grasshopper, it stops to “exist” directly from the parameter itself)

1 Like

Hi Mahdiyar
Yes, it will not report an error if you just delete the guid, but we usually use guid for other operations, which will cause an error, like this.


Moving the tree before deleting the GUID will give an error.
111111

1 Like