Check if points are inside a Brep (Python)

Hi everyone,
I’m having a real trouble checking if a point is inside a brep.

I have an entire python code designed to create a wavy, random, tube-like shape, which is then closed with caps to form a brep (when i connect a sticky panel to the output containing the shape, there is only one line, “0 | Closed brep”)

I then have a function to draw a dense 3D array of points, which overlaps my shape. I want to test for each point in the list if it is inside the Brep or not.

I’ve tried two methods: one, coded directly as a function in python (which is commented at the end of my code) using the brep.IsPointInside argument. I have found no ressources online about this except working pieces of code (I’ve tried them, they work but I just can’t use it in my code i don’t know why) I get various errors depending on how I arrange my code. I’ll add the GH file with said code for you guys to try if you want.

The second method is by using the BrepInc (Point in Brep) component directly in the graphical interface. In this method I create a list of booleans corresponding to the points of my points array, and create another python component which is supposed to draw a cube everytime a point is supposed to be “True” in the boolean list.

I’m currently helpless because those two methods won’t work no matter what I try. i feel like there’s a problem in the data type from my outputs but I just don’t find any documentation online about this or maybe I’m just a big noob. I really don’t know.

Thank you in advance for your help.

Projet Grasshopper.gh (12.0 KB)

I didn’t have time to look into your Python script, but you could use Cull Pattern to only get the inclusion points.

Keep the RhinoCommon API handy, this is for Rhino 5:

https://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_Geometry_Brep_IsPointInside.htm

And for Rhino 6:

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Brep_IsPointInside.htm

Which errors are you getting, more explicitly?

Thanks ! I will try this setup and let you know what happen!

If I launch my python node on the left straight ahead, uncommenting the problematic inclusioncheck function, I get “GUID object has no attribute ‘IsPointinside’” on line 108

If I launch a simple code after using a “PointInBrep” Node just to keep all the included points, I get an orange link on my output and nothing to preview or to read on a sticky node.


(i’ll link my file updated after that) Not even an error message ! weird. Everything runs, but no output? Am I being a gigantic coding noob?

About the documentation you gave me

The Rhino API is for C# coding right? any idea how to transpose the code in python? Or is there a python help somewhere? Sorry but I’m kinda using this project to learn coding with geometries, and I’m already lost enough. If I get it right, this method should be described here, right?
https://developer.rhino3d.com/api/RhinoScriptSyntax/
I have currently no idea on how to call for a C# function in python, without just refering to the rhinoscriptsyntax API.

Is this something I should code myself? If so do you have an idea where to start?
I’d like, for optimisation purpose (I did this project using nodes but Grasshopper seems to struggle to create iteration so I have to code it myself) to keep a maximum of my code in python, even possibly all of it !

Thanks a lot for all your kind answers,

Projet Grasshopper.gh (10.9 KB)

Edit for the file

Update: I managed to make it work using the cull node combined with the brep inclusion as advised above, without hurting my performances too much. I feel like this is sad that I couldn’t managed to do it by code only, so If you guys have the answers for the problem in python, I’m still glad to read it !

Thanks

as mentioned by the honorable judge @AndersDeleuran, the RhinoCommon method seems to work just fine?

import Rhino
import scriptcontext as sc

sc.doc = Rhino.RhinoDoc.ActiveDoc
tol = sc.doc.ModelAbsoluteTolerance
sc.doc = ghdoc

a = x.IsPointInside(y, tol, True) 

note, I probably err on the side of “overuse”, but anytime I need to do something with the active rhino document, (like get the tolerance), my rule of thumb is to use scriptcontext to switch the focus, do what I need to do, then switch the focus back to GH. Lots of discussion on that topic here in the forum.

To get that code to work, set your type hints to Brep and Point3d respectively.

Lastly, while the RhinoCommon documentation does have mostly C#/VB examples, (almost) all the methods can be used with python. (it’s not a common case, but the “almost” statement applies to methods that use something called “out parameters” in C#. You can still use those as well, but python requires a few more steps). Edge case example here:

2 Likes

Thanks for your reply !
It works ! I just also had to figure out that I needto coerce my breps into breps (they were GUID) and same for the points !
Thanks for the help I learned a lot through that !