Python patch

But, there are intersections. I see the intersections. I cannot understand what I am doing wrong.

Those are point intersections. Curve intersections would be where the input curve overlaps the input brep.

:sweat_smile: sorry, I could find that in your former post and should not ask that

Do you might know how to make a domain guid? I am doing something wrong with rs.TrimCurve.

I already tried some ways, but I can get no result.

20190123 problem patch 04.gh (19.4 KB)

As I said above, I don’t use rhinoscriptsyntax (other than interfacing with the Rhino document), so afraid I can’t help further.

@nathancoatney @Dancergraham Do you might know how to make a domain guid?

@ForestOwl, you are about to traverse the rhinoscriptsyntax <-> rhinocommon threshold. It is a big step, and you will have to spend some time learning it. My take on it is:

The rs. functions are kind of wrappers around traversing down into the rhinocommon .net object world, and back out. They are convenient, but higher level, so you have less control/granularity exposure. They generally deal in guids, and push things to the document, even if not needed.

The rhinocommon (Rhino.) are objects that represent geometry (and all other things). You make them, use them, and either throw them away or add them to the document, which is when they are ‘assigned’ a guid.

It seems like your script would benefit from going down to the rhinocommon level. I agree with @AndersDeleuran in that it is probably better to use it inside of grasshopper components. Also as he said, probably the best way to learn is to look at the rs. function source. I would also recommend using the python editor first, because grasshopper adds some additional layer(s).

About domains though, I think you need an Interval. You can make an interval with:

Rhino.Geometry.Interval() or rs.CreateInterval()

1 Like

Arghh, I cannot get it right. When trying to make an interval, I am still doing something wrong.

20190123 problem patch 04.gh (18.8 KB)

Keep input as ghdoc object, ah-ha!

Hi @ForestOwl,

This is a GHPython solution to your last problem, written purely in rhinocommon (without rhinoscriptsyntax).

import Rhino.Geometry as rg


def list_to_tree(input, none_and_holes=True, source=[0]):
    """Transforms nestings of lists or tuples to a Grasshopper DataTree"""
    # written by Giulio Piacentino, giulio@mcneel.com
    from Grasshopper import DataTree as Tree
    from Grasshopper.Kernel.Data import GH_Path as Path
    from System import Array
    def proc(input,tree,track):
        path = Path(Array[int](track))
        if len(input) == 0 and none_and_holes: tree.EnsurePath(path); return
        for i,item in enumerate(input):
            if hasattr(item, '__iter__'): #if list or tuple
                track.append(i); proc(item,tree,track); track.pop()
            else:
                if none_and_holes: tree.Insert(item,path,i)
                elif item is not None: tree.Add(item,path)
    if input is not None: t=Tree[object]();proc(input,t,source[:]);return t



all_outside_fragments = []
all_inside_fragments = []

for crv in crvs:
    # Explode polycurves into segments
    crv_segments = []
    exploded_crv = crv.DuplicateSegments()
    if len(exploded_crv) > 0: # polyline
        crv_segments.extend(crv.DuplicateSegments())
    else: # line
        crv_segments.append(crv)
    
    crv_out_frags = []
    crv_in_frags = []
    
    # Evaluate each curve segments for intersections with the brep
    for seg in crv_segments:
        intersection_pts = rg.Intersect.Intersection.CurveBrep(seg, br, 0.01)[2]
        
        if len(intersection_pts) > 0: # brep intersects the curve segment
            out_frags = [] # curve segment fragments outside of the brep
            in_frags = [] # curve segment fragments inside of the brep
            for pt in intersection_pts:
                t = seg.ClosestPoint(pt)[1] # find the curve parameter of the intersection point
                seg_fragments = seg.Split(t) # split the curve segment into fragments
                #test.extend(seg_fragments)
                # Evaluate each curve segment fragment for inside or outside relations with the brep
                for frag in seg_fragments:
                    start_pt = frag.PointAtStart
                    end_pt = frag.PointAtEnd
                    int_pts = rg.Intersect.Intersection.CurveBrep(frag, br, 0.01)[2]
                    if len(int_pts) <= 1: # the fragment isn't partially inside the brep
                        # Check wether the fragment is fully inside or outside the brep
                        if not br.IsPointInside(start_pt, 0.01, True) and not br.IsPointInside(end_pt, 0.01, True):
                            out_frags.append(frag)
                        else:
                            in_frags.append(frag)
                    else: # the fragment is partially inside the brep
                        #test.extend(int_pts)
                        for i in range(len(int_pts)):
                            # Identify the curve segment fragment long enough to be split again (for the inside segment)
                            if int_pts[i].DistanceTo(start_pt) < 0.0001:
                                split_pt = int_pts[abs(i-1)] # intersection point to split the curve segment fragment at
                                tf = frag.ClosestPoint(split_pt)[1] # find the curve parameter of the intersection point
                                frag_fragments = frag.Split(tf) # split the curve segment fragment into smaller fragments
                                # Evaluate each curve segment fragment for inside or outside relations with the brep
                                for ffrag in frag_fragments:
                                    fstart_pt = ffrag.PointAtStart
                                    fend_pt = ffrag.PointAtEnd
                                    if br.IsPointInside(fstart_pt, 0.01, False) and br.IsPointInside(fend_pt, 0.01, False):
                                        in_frags.append(ffrag)
            
            crv_out_frags.extend(out_frags)
            crv_in_frags.extend(in_frags)        
        
        else: # brep does NOT intersect the curve segment
            start_pt = seg.PointAtStart
            end_pt = seg.PointAtEnd
            # Check if the curve segment is outside or inside of the brep
            if not br.IsPointInside(start_pt, 0.01, True) and not br.IsPointInside(end_pt, 0.01, True):
                crv_out_frags.append(seg)
            else:
                crv_in_frags.append(seg)
                
    all_outside_fragments.append(crv_out_frags)
    all_inside_fragments.append(crv_in_frags)
        

insidecrvs = list_to_tree(all_inside_fragments)
outsidecrvs = list_to_tree(all_outside_fragments)

It works quite well. You are welcome to check it out!

20190123 problem patch 05.gh (19.8 KB)

1 Like

Thank you for this example :smiley:

1 Like

Thank you for your response. :slight_smile:
I have another question.

Now, I try to do it with more surfaces, but I am doing something wrong.
Do you know what I am doing wrong?

20190123 problem patch 04.gh (10.3 KB)

You forgot to internalise your geometry, however this should work:

import rhinoscriptsyntax as rs
import scriptcontext as sc

all_crvs = [] # flat list of all unioned itersection curves

for srf in srfs:
    srf_crvs = [] # list of intersection curves per surface
    # Perform the intersections
    for brp in breps:
        crvs = rs.IntersectBreps(brp, srf, sc.doc.ModelAbsoluteTolerance)
        for crv in crvs:
            # check to make sure intersection curves are closed and planar
            if rs.IsCurveClosed(crv) and rs.IsCurvePlanar(crv):
                srf_crvs.append(crv)
    # Perform the boolean operation on surface curves of the same level
    bool_crvs = rs.CurveBooleanUnion(srf_crvs)
    all_crvs.extend(bool_crvs) # extend instead of append to avoid nested lists
    
# check curve list
print all_crvs

paper = rs.AddPlanarSrf(all_crvs)

Still messing around with rhinoscriptsyntax? :wink:

1 Like

Thank you!

And no no, I am grooooving to rhinocommon. Sometimes I have to use some older parts for other scripts.
:smiley:

tenor

1 Like

I tried it in this context too, and tried to implement a if-function, but I am doing something wrong.

This is probably because of Rhinosyntax right?

20190123 problem patch 05.gh (13.7 KB)

One of the things you might want to consider, if you are focusing on developing in ghpython, is utilizing the built-in power of the native Ghpython component. Things like setting type hints, item/list/tree access can really make your life much easier. (granted, on more complex geomerty/scripts, setting type hints can result in some performance degradation…but you probably don’t have to worry about that now).

That, in conjunction with using rhinocommon, is really a very powerful approach. While you absolutely can use rhinoscriptsyntax, (which is also very powerful), it can lead to a bit more work when calling it from a ghpython component.
By way of example, (if I understand your intended goal from you definition above), this is a more direct approach:
1 - get the resulting curves of the intersections of a bunch of breps
2 - boolean union any overlapping intersection curves into one curve, (if possible).

import Rhino

intersectionCrvs = []

for i in x:
    intersectionCrvs.extend(Rhino.Geometry.Intersect.Intersection.BrepBrep(i, y, 0.01)[1])

a = intersectionCrvs
b = Rhino.Geometry.Curve.CreateBooleanUnion(intersectionCrvs)

type hint for x is Brep, List Access
type hing for y is Brep, Item Access

GHpy_RCIntersectionExample.gh (32.3 KB)

reference:
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Intersect_Intersection_BrepBrep.htm

IMHO, as mentioned by others, it would be well worth the investment of your time to try and use rhinocommon, exclusively, when working in a ghpython component.

1 Like

You have a typo in the first line! Should be import rhinoscriptsyntax as rs.

1 Like