import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DeleteNgonTest():
    go = Rhino.Input.Custom.GetObject()
    go.GeometryFilter = Rhino.DocObjects.ObjectType.MeshFace
    go.SubObjectSelect = True
    go.EnablePreSelect(True, True)
    
    get_rc = go.Get()
    if go.CommandResult() != Rhino.Commands.Result.Success: return
    
    if get_rc == Rhino.Input.GetResult.Object: 
        print "ObjectId:", go.Object(0).ObjectId
        print "Mesh:", go.Object(0).Mesh()
        print "Index:", go.Object(0).GeometryComponentIndex.Index
        print "IndexType:", go.Object(0).GeometryComponentIndex.ComponentIndexType

        mesh = go.Object(0).Mesh()
        if mesh.IsDocumentControlled: mesh.EnsurePrivateCopy()

        print "Ngons:", mesh.Ngons.Count
        print "Faces:", mesh.Faces.Count

        # get ngon by index
        index = go.Object(0).GeometryComponentIndex.Index
        ngon = mesh.Ngons[index]
        if not ngon: return

        # get faces of ngon
        faces = ngon.FaceIndexList()
        if not faces: 
            print "Picked mesh face is no ngon"
            return

        mesh.Faces.DeleteFaces(faces)
        mesh.Compact()

        print "Valid:", mesh.IsValid

        scriptcontext.doc.Objects.Replace(go.Object(0).ObjectId, mesh)
        scriptcontext.doc.Views.Redraw()

DeleteNgonTest()