Issue with selection of the mesh faces

Hi,
we are trying to create a command that selects only trianglular faces for a given mesh. I prototype that in python looking at a pre-existing forum entry and everything is working nice and dandy. Now I created the C# code for that and it does not behave the same way. Any idea why this is? Am I missing a command style or some settings?

public class TriangleSelectCmd : Command
{
    static TriangleSelectCmd _instance;
    public TriangleSelectCmd()
    {
      _instance = this;
    }

    public static TriangleSelectCmd Instance
    {
      get {
        return _instance;
      }
    }

    public override string EnglishName
    {
      get {
        return "SelectTriangles";
      }
    }

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      var objects = new List<MeshObject>();

      RhinoGet.GetOneObject("Get Mesh", false, ObjectType.Mesh, out var objref);
      SelectFaces(objref.Object() as MeshObject, true);

      doc.Views.Redraw();
      return Result.Success;
    }


    private void SelectFaces(MeshObject obj, bool selectTris)
    {
      var mesh = obj.MeshGeometry;
      
      for (int i = 0; i < mesh.Faces.Count; ++i) {
        var componentIndex = new ComponentIndex(ComponentIndexType.MeshFace, i);
        if (selectTris && mesh.Faces[i].IsTriangle) {
          obj.SelectSubObject(componentIndex, true, true, true);
        } else if (!selectTris && mesh.Faces[i].IsQuad) {
          obj.SelectSubObject(componentIndex, true, true, true);
        }
      }

    }

}

What’s the difference, and what is it that you don’t like?

// Rolf

It does not work. The faces are not selected at all at the end of the command. That is my issue.

Have you debug-stepped though the code to find out exactly what details does and what does not work as expected, for example checking if the function “SelectFaces” is actually run, or if the variable “selectTris” is actually set to true, and so on?

Since you didn’t say what you have tried it’s difficult to give good advice. At first you didn’t even say what was wrong with it… :wink:

// Rolf

Hi @Alberto, i’ve tried it using python and it did not select the triangles (pre-pick or post-pick) unless i unselect the object before selecting the triangle faces. Maybe below helps:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def SelectOnlyTriangles():
    
    obj_type = Rhino.DocObjects.ObjectType.Mesh
    rc, objref = Rhino.Input.RhinoGet.GetOneObject("Mesh", False, obj_type)
    if rc != Rhino.Commands.Result.Success: return
    
    # must unselect before...
    scriptcontext.doc.Objects.UnselectAll()
    
    obj = objref.Object()
    mesh = obj.MeshGeometry
    index_type = Rhino.Geometry.ComponentIndexType.MeshFace
    
    for i in xrange(mesh.Faces.Count):
        ci = Rhino.Geometry.ComponentIndex(index_type, i)
        if mesh.Faces[i].IsTriangle:
            obj.SelectSubObject(ci, True, True, True)
    
    scriptcontext.doc.Views.Redraw()
    
SelectOnlyTriangles()

_
c.

Hi clement,
my problem is that I used a script from a previous post of yours and in python is working just fine. The C# command is not working. If I use the script you posted it will work, I am sure. The code I posted above does not do anything, that’s what I do not understand.

Alberto

The reason is that you need to de-select the mesh object after selection.

The following code works:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
      RhinoGet.GetOneObject("Get Mesh", false, ObjectType.Mesh, out var objref);
      // de-select the mesh before continuing.
      doc.Objects.Select(objref.ObjectId, false);
      
      var obj = objref.Object();
      if (obj is MeshObject meshObj)
        SelectFaces(meshObj, true);

      return Result.Success;
    }


    private void SelectFaces(MeshObject obj, bool selectTris)
    {
      var mesh = obj.MeshGeometry;
      
      for (int i = 0; i < mesh.Faces.Count; ++i) {
        var componentIndex = new ComponentIndex(ComponentIndexType.MeshFace, i);
        if (selectTris && mesh.Faces[i].IsTriangle) {
          obj.SelectSubObject(componentIndex, true, true, true);
        } else if (!selectTris && mesh.Faces[i].IsQuad) {
          obj.SelectSubObject(componentIndex, true, true, true);
        }
      }
    }
1 Like

Thats what i tried to express with my script above. (see commented code line)
_
c.