RhinoCommon API for Intersection between two surfaces (ver7)

Hi, I’ve got a problem when I tried to intersect two surfaces.
I attached some pictures as below.
#1: intersection surfaces
#2, #3: unexpected results
#4: expected result

#1. Two surfaces to get an intersection curve


#2. Intersection result by using Solid → Intersection or Rhno.Geometry.Intersect.SurfaceSurfce of RhinoCommon (Selection of two surfaces and an intersection curve)

#3. Intersection result by using Solid → Intersection (Selection of an intersection curve)

#4. Intersection result by using “IntersectTwoSets” Command or “Curve->Curve from objects->Intersection of two sets”

I’ve tried to get an intersection curve of two surfaces by using Solid->Intersection in Rhino App and Rhno.Geometry.Intersect.SurfaceSurfce of RhinoCommon, but results were not what I expected. (See #2 and #3)
By the way, I can get the good result as depicted #4 if I use “IntersectTwoSets” command in Rhino App.
Eventually, I am finding out the RhinoCommon function that works the same way with “IntersectTwoSets”.
I will very appreciate if somebody help me.

Hi @user1620,

Having the geometry, along with code that isn’t working for you. will be more helpful to us than the screen shots.

When posting geometry, please provide only what is relevant.

Thanks,

– Dale

intersection_test.3dm (186.4 KB)

Hi,
I attached 3dm file that contains two surfaces.
Thanks.

Hi @user1723, @user1620,

This Python script seems to work on your two surfaces:

import Rhino
import scriptcontext as sc

def test_intersect_breps():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt('Select two surfaces to intersect')
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface
    go.SubObjectSelect = False
    go.GetMultiple(2, 2)
    if go.CommandResult() != Rhino.Commands.Result.Success:
        return
    
    brep0 = go.Object(0).Brep()
    brep1 = go.Object(1).Brep()
    if not brep0 or not brep1:
        return
        
    rc, curves, points = Rhino.Geometry.Intersect.Intersection.BrepBrep(brep0, brep1, sc.doc.ModelAbsoluteTolerance)
    if rc:
        for c in curves:
            sc.doc.Objects.AddCurve(c)
        for p in points:
            sc.doc.Objects.AddPoint(p)
    sc.doc.Views.Redraw()

if __name__ == "__main__":
    test_intersect_breps()

Let me know if this helps.

– Dale

1 Like

Hi Dale,

Thank you for your kind reply.
It seems to be my mistake when I get Brep from RhinoObject.
The function of Rhino.Geometry.Intersect.Intersection.BrepBrep can’t generate intersectionCurves when I use Brep instance returned from ObjRef.Brep().
It works If I use go.Object().Brep().
I remerber that I didn’t have any problems when I used ObjRef.Brep() or ObjRef.Surface()/Curve().
Please let me know the difference betwwen RhinoObject.Brep() and ObjRef.Brep().

Best regards,
SG

My test code is as below.

public class IntersectionTest : Command
{
    public IntersectionTest()
    {
        Instance = this;
    }

    ///<summary>The only instance of the MyCommand command.</summary>
    public static IntersectionTest Instance { get; private set; }

    public override string EnglishName => "IntersectionTest";

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
        // TODO: complete command.
        Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject();
        go.SetCommandPrompt("select surfaces to intersect");
        go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface;
        go.SubObjectSelect = false;
        go.EnablePressEnterWhenDonePrompt(true);
        go.GetMultiple(2,2);
        if (go.CommandResult() != Rhino.Commands.Result.Success)
            return go.CommandResult();
        Rhino.DocObjects.ObjRef objRef1 = go.Object(0);
        Rhino.DocObjects.ObjRef objRef2 = go.Object(1);
        Brep brep1 = go.Object(0).Brep(); // objRef2.Brep();
        Brep brep2 = go.Object(1).Brep(); // objRef2.Brep();
        double tolerance = doc.ModelAbsoluteTolerance;
        Curve[] intersectionCurves;
        Point3d[] intersectionPoints;
        if(!Rhino.Geometry.Intersect.Intersection.BrepBrep(
            brep1, 
            brep2, 
            tolerance, 
            out intersectionCurves, 
            out intersectionPoints))
        {
            return Result.Failure;
        }
        foreach(Curve curve in intersectionCurves)
        {
            doc.Objects.Add(curve);
        }
        doc.Views.Redraw();

        return Result.Success;
    }
}

A RhinoObject represents a geometric object in the document. A RhinoObject consists of two pieces: something that inherits from GeometryBase, and ObjectAttributes.

An ObjRef represents a reference to a RhinoObject. These are usually created by an object picking operation, such as RhinoGet.GetOneObject, RhinoGet.GetMultipleObjects, and GetObject. There are other ways to construct these too. In addition to containing a reference to a RhinoObject, an ObjRef also provide details on how or where the object was picked. It can also create proxy objects when needed (a Brep when you pick a ExtrusionObject, for example).

Hope this helps.

– Dale