Trimming a mesh in Python-Rhinocommon

I am trying to create a script in python-Rhinocommon that trims an existing mesh. It seems to work for a small mesh, but when applying it to a large one it seems to trim more faces than necessary. I’ve nicked part of the code about topology vertices from another script, but I do not fully understand yet how it works.test_grids.3dm (112.9 KB)

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext
import rhinoscript.utility as rhutil

me=rs.GetObject("Select a mesh to trim ")
mesh = rhutil.coercemesh(me, True)

go=Rhino.Input.Custom.GetObject()
go.GeometryFilter=Rhino.DocObjects.ObjectType.MeshVertex
go.SetCommandPrompt("Get mesh vertex")
go.GetMultiple(0,1000)
objrefs = go.Objects()
topology_indices=[item.GeometryComponentIndex.Index for item in objrefs]
points = []
for index in topology_indices:
    vertex_indices = mesh.TopologyVertices.MeshVertexIndices(index)
    points.append(vertex_indices[0])

points = sorted(points)
for pt in reversed(points):
    print pt
    test = mesh.Vertices.Remove(pt,True)

scriptcontext.doc.Objects.Replace(me,mesh)
mesh.Dispose()

scriptcontext.doc.Views.Redraw()

@Goodriver,

a topology mesh vertex can include multiple regular mesh vertices. Both are not the same. You might speed up your script somewhat by collecting the faces a single topology vertex has using

MeshTopologyVertexList.ConnectedFaces(topologyVertexIndex)

and save the returned face indices by extending a set, at the end of your script delete all faces in one go using the method: mesh.Faces.DeleteFaces()

Also note that since you´ve working on a mesh which is still controlled by the document, create a private copy of it before doing any edits by using mesh.EnsurePrivateCopy()

Before replacing the old mesh with the changed one, use mesh.Compact() to remove unreferenced objects…

c.