DataTree problem creating component solid difference

Hello Guys

I understand and study with the creation of nodes on
I’m trying to create a node that makes the Boolean difference of solids (I know that it is in the grasshopper). For educational purposes, I do it.

Can you please tell me how to fix it, such an error occurs

pManager.AddBrepParameter(“Solid”, “S”, “Solid”, GH_ParamAccess.tree);
pManager.AddBrepParameter(“Difference”, “D”, “Difference”, GH_ParamAccess.tree);

DataTree Solid = new DataTree();
DataTree Difference = new DataTree();

if (!DA.GetDataTree(0, Solid)) return;
if (!DA.GetDataTree(1, Difference)) return;

GetDataTree() expects DataTree< T>, no DataTree .

DataTree< Brep> Solid = new DataTree< Brep>();
DataTree< Brep> Difference = new DataTree< Brep>();
1 Like

Thank you very much for the answer :slightly_smiling_face:
can you please explain what to change, I did not understand :innocent: :innocent:, or maybe you have an example?

If you already has this in your code:

DataTree< Brep> Solid = new DataTree< Brep>();
DataTree< Brep> Difference = new DataTree< Brep>();

then the text editor on this page took it out. Try this:

if (!DA.GetDataTree< Brep>(0, Solid)) return;
if (!DA.GetDataTree< Brep>(1, Difference)) return;
1 Like

Thank you very much for the clarification
I tried as you wrote, but all the same gives an error

Ups, you are missing the out keyword.

if (!DA.GetDataTree(0, out Solid)) return;
if (!DA.GetDataTree(1, out Difference)) return;

If this is not enough, try this:

if (!DA.GetDataTree< GH_Brep>(0, out Solid)) return;
if (!DA.GetDataTree< GH_Brep>(1, out Difference)) return;
1 Like

GetDataTree requires an argument of type GH_Structure<IGH_Goo>. DataTree does not derive from IGH_Structure, in my opinion it won’t work. You need

GH_Structure<GH_Brep> solid = new GH_Structure<GH_Brep>();
GH_Structure<GH_Brep> difference = new GH_Structure<GH_Brep>();

if (!DA.GetDataTree(0, out solid)) return;
if (!DA.GetDataTree(1, out difference)) return;

The datatype can be inferred when the method is called. No idea though why you would want tree access in the first place.

1 Like

Shit, that was it. Sorry @AlexWer.

1 Like

Thank you very much for the clarification.
I want to do the following
there is a solid tree on each branch
and there is a tree of bodies (there are several on each branch) that need to be subtracted from each solid body

Or can this problem be solved easier?

I now get an error after the changes in the following

Just on a side note, if you look in the API:

Anyways, as for your screenshots: If you want to access a Branch, you need to use an indexer ( there is no method to call). tree.Branch[path] should return the list which is stored in that path.
As for the second one, the DataTree class and the GH_Structure class are unrelated (they have similar functionality but are not compatible). So assigning the one to the other won’t work. If you would really want to do that, you need to rebuild the tree. Don’t do it.

But yeah, generally speaking, I think the boolean difference component works with List/List. The RhinoCommon command in the API also works with two Lists of Breps. If you want to work with trees, you need make sure they are compatible (same branch count, matching path structure, etc.) and manage all that. I think it’s easier letting Grasshopper do that, but maybe I don’t see the full picture here.

Hi @AlexWer

You can try something like this, done in a C# comp, which you can easily copy paste in to your VS project.


 /// <summary>
  /// 
  /// </summary>
  /// <param name="subtractFrom"></param>
  /// <param name="subtractWith"></param>
  /// <param name="tolerance"></param>
  /// <param name="parallel"></param>
  /// <returns></returns>
  public DataTree<Brep> SolidDifference(DataTree<Brep> subtractFrom, DataTree<Brep> subtractWith, double tolerance, bool parallel)
  {
    DataTree<Brep> result = new DataTree<Brep>();
    if(parallel ==false)
    {
      result = BoolDifference(subtractFrom, subtractWith, tolerance);
    }
    else
    {
      result = BoolDifferenceParallel(subtractFrom, subtractWith, tolerance);
      
    }
  
    
    return result;
  }
  
  
  
  /// <summary>
  /// 
  /// </summary>
  /// <param name="subtractFrom"></param>
  /// <param name="subtractWith"></param>
  /// <param name="tolerance"></param>
  /// <returns></returns>
  private DataTree<Brep> BoolDifference(DataTree<Brep> subtractFrom, DataTree<Brep> subtractWith, double tolerance)
  {

    DataTree<Brep> result = new DataTree<Brep>();
    
    for (int i = 0; i < subtractWith.BranchCount; i++)
    {
      

      for (int j = 0; j < subtractWith.Branch(i).Count; j++)
      {

        for (int k = 0; k < subtractFrom.BranchCount; k++)
        {
          
          for (int m = 0; m < subtractFrom.Branch(k).Count; m++)
          {
            if(subtractFrom.Branch(k)[m].IsValid == false)
            {
              throw new ArgumentException("Input a valid Brep to substract from!");
            }
            
            if(subtractWith.Branch(i)[j].IsValid == false)
            {
              throw new ArgumentException("Input a valid Brep to substract with!");
            }

            Brep [] diff = Brep.CreateBooleanDifference(subtractFrom.Branch(k)[m], subtractWith.Branch(i)[j], tolerance);

            result.AddRange(diff, new GH_Path(k));

          }
        }
      }



    }

    return result;

  }
  
  
  
  /// <summary>
  /// 
  /// </summary>
  /// <param name="subtractFrom"></param>
  /// <param name="subtractWith"></param>
  /// <param name="tolerance"></param>
  /// <returns></returns>
  private DataTree<Brep> BoolDifferenceParallel(DataTree<Brep> subtractFrom, DataTree<Brep> subtractWith, double tolerance)
  {

    DataTree<Brep> result = new DataTree<Brep>();
    
    Parallel.For(0, subtractWith.BranchCount, i =>
      {
      

      for (int j = 0; j < subtractWith.Branch(i).Count; j++)
      {

        for (int k = 0; k < subtractFrom.BranchCount; k++)
        {
          
          for (int m = 0; m < subtractFrom.Branch(k).Count; m++)
          {
            if(subtractFrom.Branch(k)[m].IsValid == false)
            {
              throw new ArgumentException("Input a valid Brep to substract from!");
            }
            
            if(subtractWith.Branch(i)[j].IsValid == false)
            {
              throw new ArgumentException("Input a valid Brep to substract with!");
            }

            Brep [] diff = Brep.CreateBooleanDifference(subtractFrom.Branch(k)[m], subtractWith.Branch(i)[j], tolerance);

            result.AddRange(diff, new GH_Path(k));

          }
        }
      }



      });

    return result;


which is used as


private void RunScript(DataTree<Brep> substract, DataTree<Brep> solid, ref object A)
  {
  

    double docTol = RhinoDocument.ModelAbsoluteTolerance;


    A = SolidDifference(substract, solid, docTol,parallel); 

  }

  • make sure you have using System.Threading.Tasks; to be able to use the Parallel class
1 Like

Thank you very much now I’ll try to do