GH C# - Filtering a set of brep faces using conditional statements

Hello Everyone!,

Long time reader, first time having anything I couldn’t quite grasp by the examples provided in the forum;

For context:
I’m at a c# beginners level; I’m trying to generate my own set of “batteries” by bundling several GH functions into c# tools (in order to get familiar with the language) to deal with tasks that I’m often presented with. This time around, I’m trying to achieve filtering a List of Breps, and returning only the Closest Brep face to the dot product of the normal and a given vector given a certain treshold, nothing otherworldly.

But for the life of me, I’m having trouble with relating the two lists that I extract from the breps (faces and dot products of the vectors) since the lists I need to filter, all happen inside the nested loops of one another and I can’t find the way to retrieve them.

Please excuse the noob request.

This is how I normally go about it in GH:

And this is my c# code with a few comments that I hope are not too dumb to add for experienced programmers:

private void RunScript(List breps, Point3d testUV, Vector3d matchAxis, double treshold, ref object A, ref object B, ref object C)
{

//Get Geometry List as Brep
List <BrepFace> brepFaces = new List <BrepFace>();

//For each Brep, get the individual face information into a new List using .AddRange
for(int i = 0; i < breps.Count; i++)
{
  brepFaces.AddRange(breps[i].Faces);
  
  List <Vector3d> bFaceNormals = new List <Vector3d>();
  
  //For each face, reparametrize domain and extract normal at 0.5,0.5
  foreach(BrepFace bFace in brepFaces)
  {
    
    bFace.SetDomain(0, new Interval(0, 1));
    bFace.SetDomain(1, new Interval(0, 1));

    Vector3d bFaceNormal = bFace.NormalAt(0.5, 0.5);

    bFaceNormals.Add(bFaceNormal);

    } B = bFaceNormals;// <----------------------------------

  List <Double> dotPts = new List <Double>();

  foreach (Vector3d bNorm in bFaceNormals)
  {
    Double dotPt = Vector3d.Multiply(bNorm, matchAxis);
    dotPts.Add(dotPt);

    } C = dotPts;// <---------------------------------------
 
  //This is the part I have no idea how to get to work
  /*if(dotPts[i] >= treshold){Print(dotPts[i].ToString()); A = brepFaces[i];}*/

} //A = brepFaces;// <-------------------------------------

Thank you in advance for the tips,
My best regards,

Jaime.

I was able to get it to work using the following:

private void RunScript(List breps, Point3d testUV, Vector3d matchAxis, double treshold, double areaRange1, double areaRange2, ref object A)
{

//Get Geometry List as Brep
List <BrepFace> brepFaces = new List <BrepFace>();

//For each Brep, get the individual face information into a new List using .AddRange
for(int i = 0; i < breps.Count; i++)
{
  brepFaces.AddRange(breps[i].Faces);

  List <BrepFace> bFaceFiltered = new List <BrepFace>();

  foreach(BrepFace bFace in brepFaces)
  {
    bFace.SetDomain(0, new Interval(0, 1));
    bFace.SetDomain(1, new Interval(0, 1));

    Double dotPt = Vector3d.Multiply((bFace.NormalAt(0.5, 0.5)), matchAxis);
 
    if(dotPt >= treshold){bFaceFiltered.Add(bFace);}

    A = bFaceFiltered;
  }
}

I would suggest the following “general” approach on that matter:

  1. Create a custom class that samples the “properties” of the objects that you want to query - say like this:

  1. Build up a List of the custom type:

  2. Query the List (LINQ and the likes):

Notify if you want an entry level demo on the above (not exactly 100% suitable for novices mind … but no pain no gain).

1 Like

You never fail to deliver Peter!, Would you mind sharing your demo?, my component’s not as fast as I would like it to be, but I don’t know where to put the blame… (besides me :joy:)

The C# component is half the time than the GH battery cluster, but I still feel like it should be faster–

Get the demo then (as usual nothing is interlalized: NEVER do that if Breps are around).

Brep_QueryViaClass_V1.3dm (1.3 MB) Brep_QueryViaClass_V1.gh (121.0 KB)

Note:I’m 1M miles away from base meaning that I don’t have access to my stuff in the practice. So this entry level demo is about the query strategy and not about performance matters (// this, // that, PLINQ stuff etc etc).

May the Force (the dark option) be with you.

1 Like