# Python patch

**URL:** <https://discourse.mcneel.com/t/python-patch/77765>\
**Category:** Grasshopper\
**Tags:** python\
**Created:** [January 23, 2019, 2:41pm UTC](https://discourse.mcneel.com/t/python-patch/77765 "2019-01-23T14:41:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 23, 2019, 2:41pm UTC](https://discourse.mcneel.com/t/python-patch/77765/1 "2019-01-23T14:41:37Z")

</div>

I am trying to make a patch, but I do not know what they meant with a ‘Guid’ for the ‘rs.AddPatch()’ part .😓😒

What am I doing wrong?

[20190123 problem patch 00.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/1/f/1f7f100b112af77d4303dedd0a5129e273c7fc3b.gh) (13.6 KB)

 ![20190123%20problem%20patch%2000](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/d/a/da7959003d50127e157747dae5411611338756d1.png)

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [January 23, 2019, 4:30pm UTC](https://discourse.mcneel.com/t/python-patch/77765/2 "2019-01-23T16:30:58Z")

</div>

It has been a while since last time I coded for Rhino, but isn’t there an attribute `.id` of the patch?

try `cr[0].id`

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [January 23, 2019, 4:39pm UTC](https://discourse.mcneel.com/t/python-patch/77765/3 "2019-01-23T16:39:20Z")

</div>

Try printing `print cr[0]` to see what you have got.  
If the Brep intersection failed then you may have `None`

---

<div class="post-metadata">

**Author:** ![nathancoatney](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathancoatney/32/767339_2.png) [@nathancoatney](https://discourse.mcneel.com/u/nathancoatney)\
**Post date:** [January 23, 2019, 5:08pm UTC](https://discourse.mcneel.com/t/python-patch/77765/4 "2019-01-23T17:08:04Z")

</div>

I think:

rs.AddPatch() needs a list of at least 2 items. Your are passing in the 0th item of the intersection curve list (cr[0]). Making some assumptions about the needed output, this might help:

```
import rhinoscriptsyntax as rs
import scriptcontext as sc

all_curves = []

print(breps)

for i, b in enumerate(breps):
    curves = rs.IntersectBreps(b, srf, sc.doc.ModelAbsoluteTolerance)
    print(i, curves)
    for c in curves:
        all_curves.append(c)
        
brep_out = rs.AddPatch(all_curves, (span, span))

```

 ![image](https://global.discourse-cdn.com/mcneel/original/3X/2/b/2b07793663a6fa22da6c428619b775e8cf85c2d2.png)

![image](https://global.discourse-cdn.com/mcneel/original/3X/3/f/3f8d21156841808e210212deb8c0283c1c98f30a.png)

It intersects multiple breps with a surface, collects the intersection curves, and makes a patch from them. Set the brps input to list access. Not sure if that is what you are after though.

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 23, 2019, 7:26pm UTC](https://discourse.mcneel.com/t/python-patch/77765/5 "2019-01-23T19:26:41Z")

</div>

Thank you for your response.  
A question. What I try to achieve creating from a closed curve a surface. A closed curve which also could be one with more than four corners, so, a polysurface from curves.  
Those curves picked from the intersections of the breps with the other surface. This with the idea in mind I can merge the surfaces.

However, do you know, is patch the best option for that because it seems to work differently than the component as in grasshopper?

I am sure I am doing something wrong, but looking at the span option which does not make the patch. I should put myself deeper into understanding ‘rs.Patch.’ Did you make those arcs on purpose not flat?

I want to merge the patches afterwards creating the patches.

[20190123 problem patch 01.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/4/c/4cfe468ad3e1e4dd169c804e56298a4dc02adc60.gh) (16.4 KB)

 ![20190123%20problem%20patch%2001](https://global.discourse-cdn.com/mcneel/original/3X/7/a/7aeb9ea88a2fa527df038194e963d5e8db1773cc.png)

---

<div class="post-metadata">

**Author:** ![nathancoatney](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathancoatney/32/767339_2.png) [@nathancoatney](https://discourse.mcneel.com/u/nathancoatney)\
**Post date:** [January 23, 2019, 10:01pm UTC](https://discourse.mcneel.com/t/python-patch/77765/6 "2019-01-23T22:01:50Z")

</div>

Hmm, you might try Intersect-\>Shape-\>Region Union. I haven’t used it previously but it seems like if you can guarantee each intersection set of lines can be closed and planar, then it might be able to curve-boolean to get the blue lines.

Another approach maybe is to solidify/boolean the shapes before intersecting.

Yes they were curved on purpose.

rs.Patch is just like the Rhino command Patch, I think, so if you can get that to work manually then that would be the first step. Spans parameter is to set the control points for the span. I think Patch is mainly for filling in surfaces to make them closed.

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 24, 2019, 1:38pm UTC](https://discourse.mcneel.com/t/python-patch/77765/7 "2019-01-24T13:38:00Z")

</div>

Hmmm… I think I am a step further, but I cannot find a way to merge the patches like with the component ‘region union.’

I am seriously trying to do it python because I need to implement it in  
a larger script.

Do you might know a way to merge the patches? Even trying to let them all intersect won’t result in a _merged_ result.

[20190123 problem patch 02.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/e/ae7e15fdc016dd055ba0c2501311329bb2c1e1d4.gh) (14.5 KB)

 ![20190123%20problem%20patch%2002](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/6/5/653306121d35f6441dc2bed3d153a5f07deba086.png)

---

<div class="post-metadata">

**Author:** ![nathancoatney](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathancoatney/32/767339_2.png) [@nathancoatney](https://discourse.mcneel.com/u/nathancoatney)\
**Post date:** [January 24, 2019, 4:46pm UTC](https://discourse.mcneel.com/t/python-patch/77765/8 "2019-01-24T16:46:01Z")

</div>

Take a look at the output variable.

I don’t think I know the full scope, but you probably don’t need patches.

Here are some hints:

[https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-CurveBooleanUnion](https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-CurveBooleanUnion)  
[https://developer.rhino3d.com/api/RhinoScriptSyntax/#curve-IsCurveClosed](https://developer.rhino3d.com/api/RhinoScriptSyntax/#curve-IsCurveClosed)  
[https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-IsCurveClosed](https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-IsCurveClosed)  
[https://developer.rhino3d.com/api/RhinoScriptSyntax/#surface-AddPlanarSrf](https://developer.rhino3d.com/api/RhinoScriptSyntax/#surface-AddPlanarSrf)

If you want to skip straight to a possible solution:

```
import rhinoscriptsyntax as rs
import scriptcontext as sc

all_curves = []

for i, b in enumerate(breps):
    crvs = rs.IntersectBreps(b, srf, sc.doc.ModelAbsoluteTolerance)
    for c in crvs:
        # check to make sure intersection curves are closed and planar
        if rs.IsCurveClosed(c) and rs.IsCurvePlanar(c):
            all_curves.append(c)
# check curve list
print(all_curves)

curve_boolean = rs.CurveBooleanUnion(all_curves)

paper = rs.AddPlanarSrf(curve_boolean)

```

![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/3/a39cd8300373deef53fe14347c21e859ebc30083.jpeg)

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 24, 2019, 6:08pm UTC](https://discourse.mcneel.com/t/python-patch/77765/9 "2019-01-24T18:08:10Z")

</div>

🐦 take off 🎉🎉🎉🎉

---

<div class="post-metadata">

**Author:** ![nathancoatney](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathancoatney/32/767339_2.png) [@nathancoatney](https://discourse.mcneel.com/u/nathancoatney)\
**Post date:** [January 24, 2019, 6:52pm UTC](https://discourse.mcneel.com/t/python-patch/77765/10 "2019-01-24T18:52:37Z")

</div>

👍

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 25, 2019, 9:07am UTC](https://discourse.mcneel.com/t/python-patch/77765/11 "2019-01-25T09:07:18Z")

</div>

Do you might know? Are those Guids points or curves or parameters?

I am trying to get the curves inside the brep.

My thought on that was, I get the parameters, cut the curves and look which curves are inside by the curves midpoint.

[20190123 problem patch 03.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/c/6/c6ec5a39d5b80fc6373ce5558aabeaac49a11037.gh) (16.6 KB)

 ![20190123%20problem%20patch%2003](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/c/5/c5794dcc29b2367fa489da237ea984a0779946e2.png)

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [January 25, 2019, 9:14am UTC](https://discourse.mcneel.com/t/python-patch/77765/12 "2019-01-25T09:14:26Z")

</div>

Hi,  
I think you are appending empty lists for non-intersecting curves.  
Maybe try:

```python
insidecrvs.extend(cr)
```

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 25, 2019, 9:22am UTC](https://discourse.mcneel.com/t/python-patch/77765/13 "2019-01-25T09:22:52Z")

</div>

Thank you. 🙂 Also, do you know what kind of Guid objects these are?

 ![20190123%20problem%20patch%2003a](https://global.discourse-cdn.com/mcneel/original/3X/f/8/f8da4e6492f8d90eb4c0e1bc944f445ea0b0dffb.png)

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [January 25, 2019, 9:36am UTC](https://discourse.mcneel.com/t/python-patch/77765/14 "2019-01-25T09:36:26Z")

</div>

You’re welcome 🙂  
Well according to the docs they represent an ‘intersection object’ [https://developer.rhino3d.com/api/RhinoScriptSyntax/#curve-CurveBrepIntersect](https://developer.rhino3d.com/api/RhinoScriptSyntax/#curve-CurveBrepIntersect)  
Searching Google for rhino + ‘intersection object’ I find nothing so I presume they represent curves? I’m confused by the nesting structure though - I would expect this code to generate a flat list of guids but you seem to have a list containing:

- An empty list
- a list of 2 guids,
- an empty list
- a list of 2 guids  
😕 Are your inputs simple curves or polylines?

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 25, 2019, 10:02am UTC](https://discourse.mcneel.com/t/python-patch/77765/15 "2019-01-25T10:02:59Z")

</div>

I know exploded the curves, but I get the same results. I cannot understand what it could be those Guid objects. I try to get the ‘inside the brep’ curves.

 ![20190123%20problem%20patch%2003b](https://global.discourse-cdn.com/mcneel/original/3X/5/b/5b26071ac1b445aa6e91b2ef6000b37b89669be9.png)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [January 25, 2019, 10:04am UTC](https://discourse.mcneel.com/t/python-patch/77765/16 "2019-01-25T10:04:59Z")

</div>

If you can’t figure out what a `rhinoscriptsyntax` function is doing, I’d recommend looking up its source code. `rhinoscriptsyntax` is a Python-style module wrapping RhinoCommon located (on my system) here:

`C:\Users\AHD\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript`

As you can see the function you’re calling is really just a bunch of Rhino document related boiler plate code around [this RhinoCommon method](https://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_Geometry_Intersect_Intersection_CurveBrep.htm):

```auto
def CurveBrepIntersect(curve_id, brep_id, tolerance=None):
    """Intersects a curve object with a brep object. Note, unlike the
    CurveSurfaceIntersection function, this function works on trimmed surfaces.
    Parameters:
      curve_id = identifier of a curve object
      brep_id = identifier of a brep object
      tolerance [opt] = distance tolerance at segment midpoints.
                        If omitted, the current absolute tolerance is used.
    Returns:
      List of identifiers for the newly created intersection curve and
      point objects if successful. None on error.            
    """
    curve = rhutil.coercecurve(curve_id, -1, True)
    brep = rhutil.coercebrep(brep_id, True)
    if tolerance is None or tolerance<0:
        tolerance = scriptcontext.doc.ModelAbsoluteTolerance
    rc, out_curves, out_points = Rhino.Geometry.Intersect.Intersection.CurveBrep(curve, brep, tolerance)
    if not rc: return scriptcontext.errorhandler()
    
    curves = []
    points = []
    for curve in out_curves:
        if curve and curve.IsValid:
            rc = scriptcontext.doc.Objects.AddCurve(curve)
            curve.Dispose()
            if rc==System.Guid.Empty: raise Exception("unable to add curve to document")
            curves.append(rc)
    for point in out_points:
        if point and point.IsValid:
            rc = scriptcontext.doc.Objects.AddPoint(point)
            points.append(rc)
    if not curves and not points: return None
    scriptcontext.doc.Views.Redraw()
    return curves, points

```

As discussed quite a lot around this board and the old GH forum, I’d personally recommend going with straight up RhinoCommon when coding up geometry stuff in GHPython. Can save yourself a lot of (guid-related) headaches, and perhaps also speed up your code a bit.

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 25, 2019, 10:14am UTC](https://discourse.mcneel.com/t/python-patch/77765/18 "2019-01-25T10:14:07Z")

</div>

I do not understand why I do not get the curves out of it. Do you might know what I am doing wrong with _rs.CurveBrepIntersect?_

Oh, because they do not intersect one curve at once. But, when having a polyline, it still is the same problem. Does it interpret a polyline as many lines, do you ,might know that?

I am trying to get the curves inside the brep.  
[20190123 problem patch 03.gh](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/f/0/f09724252d92970dcfd9310c9c3de1007c229a4d.gh) (18.9 KB)

 ![20190123%20problem%20patch%2003c](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/0/e03ee781fb521db585f05fd4869eb7ac21bfea35.png)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [January 25, 2019, 10:32am UTC](https://discourse.mcneel.com/t/python-patch/77765/19 "2019-01-25T10:32:01Z")

</div>

Did you even try to understand the function that you’re calling? More specifically what it returns and under which circumstances, at least try to make a `print(cr)` statement after line 7.

---

<div class="post-metadata">

**Author:** ![ForestOwl](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/forestowl/32/149352_2.png) [@ForestOwl](https://discourse.mcneel.com/u/ForestOwl)\
**Post date:** [January 25, 2019, 10:36am UTC](https://discourse.mcneel.com/t/python-patch/77765/20 "2019-01-25T10:36:49Z")

</div>

Yes, but I do not understand the empty ones, the `[]` when doing print(cr).  
I am not able the retrieve the inside curves.

 ![20190123%20problem%20patch%2003d](https://global.discourse-cdn.com/mcneel/original/3X/9/1/9148e99757bd0f561afdacccbdd9c7eb901bc15e.png)

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [January 25, 2019, 10:40am UTC](https://discourse.mcneel.com/t/python-patch/77765/21 "2019-01-25T10:40:40Z")

</div>

That means that no intersection curves were found. Looks like you got some points though.

[Next page](https://discourse.mcneel.com/t/python-patch/77765.md?page=2)
