Python patch

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 (16.6 KB)

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

insidecrvs.extend(cr)
1 Like

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

You’re welcome :slight_smile:
Well according to the docs they represent an ‘intersection object’ 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
    :confused: Are your inputs simple curves or polylines?
1 Like

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.

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:

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.

2 Likes

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 (18.9 KB)

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.

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

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

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