Background: As part of an effort to automate mechanical tasks in our (architecture) office, I am developing a Grasshopper definition that relies on Rhinocommon to bake trimmed surfaces to Rhino, so that the user (my coworkers) can then manually select the ones they want to use for further processing with GH. The problem is that I cannot figure out how to bake the trimmed surfaces and not the underlying, untrimmed rectangles. It seems that if I manually bake the surfaces I get what I want, but I want to automate this part so that non-Rhino colleagues (i.e. most of them) can also use the script without tweaking things.
Upstream/Downstream: The pipeline starts with a BIM output that contains the elementary building volume as adjacent blocks, specifically one block per floor. These blocks are exploded and their outputs are split into the different types (surfaces, meshes, etc). I need to extract the roof surfaces and groundfloor surfaces for the purpose of casting shadows on a mesh or a plane, but since the exploded elements are kind of complex to handle programmatically I would like to bake all surfaces in Rhino and then let the users pick manually the ones that they want to use.
- Transforming trimmed surfaces in curves, exploding them and then connecting vertices as polylines. I have surfaces with holes and these don’t seem to work with “explode curve”.
- Using ShrinkTrimmedSurface before baking: surfaces with holes cannot be shrunk (says that brep couldn’t be converted to curve/surface) and the ones that do not throw errors are still baked as untrimmed surfaces.
Do you have any suggestions on how to move forward, or where I shoud look to do so? I am stuck at the moment.
Thank you very much in advance!
Left: what I get with GhPython/Rhinocommon. Right: what I would like to get with GhPython but can only get by manually baking.
00_question_trimmed_srf_forum.gh (238.3 KB)
trimmed surfaces are Breps with one surface and a trimming boundary.
Hi @vongree ,
Take a look at this, I believe it is what you are after?
FYI you have multiple overlapping surfaces in your aptly named “messed_up_surfaces” input.
I switched the type hint of expl_volume_srf input to Brep to simplify the code
import rhinoscriptsyntax as rs
import scriptcontext as sc
# Set the Rhino document to the active Rhino document
sc.doc = Rhino.RhinoDoc.ActiveDoc
layer_names = rs.LayerNames()
for layer in layer_names:
if layer == "Default":
rs.AddLayer("volume_srf", color=rs.CreateColor(9, 255, 0))
srf_ids = 
trimmed_srf = 
for srf in expl_volume_srf:
#Shrink The Surface
#Add Shrinked Surface To List
for srf in trimmed_srf:
# Add each Brep to the Rhino document and get the ID
obj_id = sc.doc.Objects.AddBrep(srf)
# Set the layer for the added object
# Set the Rhino document back to the Grasshopper document
sc.doc = ghdoc
Mostly the bit you are interested in here:
Result In Model Space:
Cheers, hope this helps!
20230918_00_question_trimmed_srf_forum_Response_01a.gh (232.4 KB)
Turns out the problem is the type hint. If you just change the type hint in @vongree’s original file everything works as expected. I reported the behavior here: IGH_Goo.CastTo<GeometryBase> bug. It’s a bug in the grasshopper API and happens when grasshopper casts geometry from a surface.
Oh how interesting, thank you @cullen.sarles !
I would question that this is a bug .
As I initially said, a “trimmed surface” is not a NurbsSurface, but a Brep with one Face.
What you do here is dangerous. You “cast” from an interface reference to an abstract base class. Consider the IGH_Goo is a GH_Surface and you try to cast it to a GeometryBase. Well, what is it supposed to be? A Nurbs or a Brep? The constructor of GH_Surface supports both. But if you don’t explicitly tell its meant to be Brep, it chooses a NurbsSurface as its probably the first option to pick from. The only bug I see here is that api allows you to do such nasty things. Better would be to throw an exception here.
I think that the other thread is a better place to talk about the technical details, so I’m going to reply there and @ you. IGH_Goo.CastTo<GeometryBase> bug - #5 by cullen.sarles
Amazing, I am still very fresh with Rhinocommon so the provided code is very helpful. Thank you!
Quick question: is there a reason why you separated the process into two different loops instead of just doing it all at once? Or why you create the srf_ids list if you never use it afterwards?
Because I’m a newbie with code
I didn’t really take any time to review or optimize the code. Normally I try to of course but was in a rush for this.
The srf_ids normally I would provide as an output option because I like have GUIDs available for items that were added to the document.
I have other scripts that select, delete, or do different operations by taking in GUID inputs.
Makes perfect sense. I like asking about the rationale behind code snippets because there are “unsaid” best practices that aren’t always easy to pick up. What you do with GUIDs, for example, is very helpful for future reference. Thank you!
I don’t know what the etiquette is here on the forums, but since @michaelvollrath made the extra effort with code/files/images, I will check his answer as the solution to my issue. Nonetheless, thank you everyone for your answers and the interesting discussion in the related post linked here. They were all very helpful.