How do I remove mid-edge EPoly Vertices?

A user asked

I want to filter the edge selection and create arrays containing connected edges, something like "edge selection elements". Edges from two arrays will never be connected, in other words will not share vertices.

Answer

The following code implements a possible solution:

SCRIPT

   macroScript KillMidEdgeVerts category: "MXS Help"
   (
       -- make sure a single EPoly object is selected
       on isEnabled return selection.count == 1 and classof selection[1].baseobject == Editable_Poly
       on execute do
       (
           thePoly = selection[1] --get the selected object
           select thePoly --select it
           max modify mode --switch to modify panel
           -- set the base object as current level:
           modPanel.setCurrentObject selection[1].baseobject
           subObjectLevel = 1 --set sub-*object level to vertex level
           numVerts = (polyop.getNumVerts thePoly) --get the total vertex count
           undo on "KillMidEdgeVerts" --enable undo context
           (
               -- loop backwards from the last to the first vertex
               for v = numVerts to 1 by -1 do
               (
                   -- get the edges using the vertex to check the count
                   nVerts = (polyop.getEdgesUsingVert thePoly v) as array
                   if nVerts.count == 2 do --if only two edges found, then
                   (
                       -- get the verts of the first edge
                       edge1verts = (polyop.getVertsUsingEdge thePoly nVerts[1]) as array
                       -- calculate the vector defined by the two vertices in the first edge
                       vector1 = (polyop.getVert thePoly edge1verts[1]) - (polyop.getVert thePoly edge1verts[2])
                       -- get the verts of the second edge
                       edge2verts = (polyop.getVertsUsingEdge thePoly nVerts[2] as array)
                       -- calculate the vector defined by the two vertices in the second edge
                       vector2 = (polyop.getVert thePoly edge2verts[1]) - (polyop.getVert thePoly edge2verts[2])
                       -- calculate the angle between the two normalized vectors
                       angle = acos (dot (normalize vector1) (normalize vector2))
                       -- if the angle is less than the threshold (change 0.01 to whatever threshold you want!)
                       if angle < 0.01 do
                       (
                           select thePoly.verts[v] --then select the current vertex
                           thePoly.EditablePoly.buttonOp #Remove --and hit the remove button
                       )
                   ) --end if
               ) --end v loop
           ) --end undo
       ) --end on
   ) --end macro