Get mesh vertex


(Karol Wierzbicki) #1

Hi,

How do I choose currently selected vertex number in C# GetObject ?
I have something like this:
var vert = go.Object(0).Mesh().Vertices[0];
And the 0 in the Vertices[0] is because I don’t know how to get currently selected vertex.


(Menno Deij - van Rijswijk) #2

You need to access the GeometryComponentIndex on the ObjRef retrieved from go.Object(0)

ObjRef obj = go.Object(0);
Mesh m = obj.Mesh();

ComponentIndex ci = obj.GeometryComponentIndex;
if(ci.ComponentIndexType == ComponentIndexType.MeshVertex)
{   
    int vi = ci.Index;
   var vertex = m.Vertices[vi];
}
else if (ci.ComponentIndexType == ComponentIndexType.MeshTopologyVertex)
{   
    int ti = ci.Index;
   var vertex = m.TopologyVertices[ti];
}
else
   // not a mesh or topology vertex selected - fail


(Karol Wierzbicki) #3

Thanks for the answer.

One more question, I was able to get topologyVertices from my mesh, bu mesh.Vertices.SetVertex(vi, translationPoint); takes Vertices, so how do I find relation between topologyVertices and Vertices?

To make clear what I’m trying to do I will put my code:

    public Result MoveVertices(RhinoDoc doc)
    {
        GetObject go = new GetObject();
        go.GeometryFilter = ObjectType.MeshVertex;
        go.Get();
        
        if (go.CommandResult() != Result.Success) 
            return go.CommandResult();

        ObjRef obj = go.Object(0);
        if (obj == null)
            return Result.Failure;

        Mesh mesh = obj.Mesh();
        if (mesh == null)
            return Result.Failure;

        ComponentIndex ci = obj.GeometryComponentIndex;

        int vi = ci.Index;

        var vertex = mesh.TopologyVertices[vi];
        var v = mesh.Vertices[vi];
        if (vertex == null)
            return Result.Failure;
            
        GetPoint gp = new GetPoint();
        gp.SetBasePoint(vertex, true);
        gp.DrawLineFromPoint(vertex, true);
        gp.Get();

        if (gp.CommandResult() == Result.Failure) 
            return gp.CommandResult();

        var translationPoint = gp.Point();
        
        mesh.Vertices.SetVertex(vi, translationPoint);
        
        Mesh duplicate_mesh = new Mesh();
        duplicate_mesh.CopyFrom(mesh);
        duplicate_mesh.Compact();

        if (duplicate_mesh.IsValid == true)
        {
            doc.Objects.Replace(obj.ObjectId, duplicate_mesh);
            doc.Views.Redraw();
            return Result.Success;
        }
        return Result.Failure;
    }

(Karol Wierzbicki) #4

OK, I see the problem, looks like it will be a bit more difficult than I thought, as I will actually need to move not one, but multiple vertices at once, as from what I think, one topology vertex may contain multiple vertices.


(Menno Deij - van Rijswijk) #5

Use the Item property to set all vertices associated with the topology index.

int topologyIndex = ci.Index;
int[] vertexIndices = mesh.TopologyVertices.Item[topologyIndex] = translationPoint;

(Karol Wierzbicki) #6

Yup menno, many thanks for help, finally it will go like this:

int[] vertexIndices = mesh.TopologyVertices.MeshVertexIndices(topologyIndex);


(Karol Wierzbicki) #7

Maybe anyone will find it useful:

        public Result MoveVertices(RhinoDoc doc)
        {
            GetObject go = new GetObject();
            go.GeometryFilter = ObjectType.MeshVertex;
            go.Get();
            if (go.CommandResult() != Result.Success) 
                return go.CommandResult();

            ObjRef obj = go.Object(0);
            if (obj == null)
                return Result.Failure;

            Mesh mesh = obj.Mesh();
            if (mesh == null)
                return Result.Failure;

            ComponentIndex ci = obj.GeometryComponentIndex;
            
            int topologyIndex = ci.Index;

            int[] vertexIndices = mesh.TopologyVertices.MeshVertexIndices(topologyIndex);
            var vertex = mesh.TopologyVertices[topologyIndex];

            if (vertex == null | vertexIndices == null)
                return Result.Failure;
                
            GetPoint gp = new GetPoint();
            gp.SetBasePoint(vertex, true);
            gp.DrawLineFromPoint(vertex, true);
            gp.Get();

            if (gp.CommandResult() == Result.Failure) 
                return gp.CommandResult();

            var endPoint = gp.Point();
            var translationVector = endPoint - vertex;

            foreach (var singleVertex in vertexIndices)
            {
                mesh.Vertices.SetVertex(singleVertex, endPoint);
            }

            Mesh duplicate_mesh = new Mesh();
            duplicate_mesh.CopyFrom(mesh);
            duplicate_mesh.Compact();

            if (duplicate_mesh.IsValid == true)
            {
                doc.Objects.Replace(obj.ObjectId, duplicate_mesh);
                doc.Views.Redraw();
                return Result.Success;
            }
            return Result.Failure;
        }