Selecting individual surface from a brep or polysurface

Hi all,
Is it possible to select an individual surface from a polysurface or Brep? I am trying to select an individual surface from a polysurface to split using curves. But I cannot select individual surface using ObjectType.Surface, but can select the whole Brep if I use ObjectType.Brep

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{

        var go = new GetObject();

        go.DisablePreSelect();
        go.SetCommandPrompt("Select a surface");
        go.BottomObjectPreference = true;
        go.GeometryFilter = ObjectType.Surface;                     
        go.Get();
        if (go.CommandResult() != Result.Success)
            return go.CommandResult();

        var surface1_obj = go.Object(0);                       
        Rhino.Geometry.Surface surface1 = surface1_obj.Surface(); 
        
        var brep = surface1.ToBrep();
        
        if (surface1 == null) return Result.Failure;
                   

        ObjRef[] crvRefs;
        Result res = RhinoGet.GetMultipleObjects("Select splitting curves", false, ObjectType.Curve, out crvRefs);
        if (res != Result.Success)
            return res;
        Curve[] splitCurves = crvRefs.Select(r => r.Curve()).ToArray();


        foreach (BrepFace bf in brep.Faces)
        {
            Brep split = bf.Split(splitCurves, doc.ModelAbsoluteTolerance);
            doc.Objects.AddBrep(split);
        }
        
             
             return Result.Success;
         }

Hi @Darryl_Menezes
Yes, you can select individual face in a polysurface. Here is a sample:

  var go = new GetObject();
  go.DisablePreSelect();
  go.SetCommandPrompt("Select a surface");
  go.GeometryFilter = ObjectType.Surface;
  go.SubObjectSelect = true;
  go.Get();
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  Rhino.DocObjects.ObjRef objref = go.Object(0);
  Rhino.Geometry.BrepFace face = objref.Face();
  if (null == face)
    return Rhino.Commands.Result.Failure;

  Rhino.Geometry.Brep brep = face.DuplicateFace(true);
  if (null == brep)
    return Rhino.Commands.Result.Failure;
  
  ObjRef[] crvRefs;
  Result res = RhinoGet.GetMultipleObjects("Select splitting curves", false, ObjectType.Curve, out crvRefs);
  if (res != Result.Success)
    return res;
  Curve[] splitCurves = crvRefs.Select(r => r.Curve()).ToArray();

  foreach (BrepFace bf in brep.Faces)
  {
    Brep split = bf.Split(splitCurves, doc.ModelAbsoluteTolerance);
    doc.Objects.AddBrep(split);
  }
  return Result.Success;

Hi @rajaa
Thank you for sending the solution. But unfortunately it is still not possible to select a surface out of a polysurface. After the prompt “Select a surface”, when I click a surface, the command fails. Only if I give the filter as polysurface or Brep, I am able to select the whole polysurface.

Can you post the example you are using?

Hi @rajaa,
The code works when I put the ‘Selecting Curves’ part of the code in the beginning… I don’t know the reason for this. I have posted it below. The code is same, but Ive arranged it in a different way, and it works.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            ObjRef[] crvRefs;
            Result res = RhinoGet.GetMultipleObjects("Select splitting curves", false, ObjectType.Curve, out crvRefs);
            if (res != Result.Success)
                return res;
            Curve[] splitCurves = crvRefs.Select(r => r.Curve()).ToArray();
                                                       
            var go1 = new GetObject();
            go1.DisablePreSelect();
            go1.SetCommandPrompt("Select first surface");
            go1.SubObjectSelect = true;
            go1.GeometryFilter = ObjectType.Surface;                        
            go1.Get();
            if (go1.CommandResult() != Result.Success)
                return go1.CommandResult();

            Rhino.DocObjects.ObjRef objref = go1.Object(0);
            
            var face = objref.Face();
            if (null == face)
                return Rhino.Commands.Result.Failure;
            
            var brep = face.DuplicateFace(true);
            if (null == brep)
                return Rhino.Commands.Result.Failure; 
            foreach (BrepFace bf in brep.Faces)
            {
                Brep split = bf.Split(splitCurves, doc.ModelAbsoluteTolerance);
                doc.Objects.AddBrep(split);
                
            }
                                                                             
            return Result.Success;
        }