import rhinoscriptsyntax as rs
import Rhino
from scriptcontext import doc 

def GetBrepsWithOption():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select Parts")
    go.EnableClearObjectsOnEntry(False);
    go.EnableUnselectObjectsOnExit(False);
    go.DeselectAllBeforePostSelect = False;
    strLength=rs.GetDocumentUserText("HolesLength")
    if strLength:
        Length=float(strLength)
    else: Length=300
    dblOption = Rhino.Input.Custom.OptionDouble(Length, 0, 12000)
    go.AddOptionDouble("Hole_Length", dblOption)
    go.GeometryFilter=Rhino.DocObjects.ObjectType.Brep
    while True:
    	getResult = go.GetMultiple(1,0)
    	if getResult == Rhino.Input.GetResult.Cancel:
    	    return Rhino.Commands.Result.Cancel
    	elif getResult == Rhino.Input.GetResult.Option:
    	    go.EnablePreSelect(False, True)
	elif getResult == Rhino.Input.GetResult.Object:
	    ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]		
            break 
    rs.SetDocumentUserText("HolesLength",str(dblOption.CurrentValue))
    return ids, dblOption.CurrentValue

def SmallHoles():
    tol=doc.ModelAbsoluteTolerance
    Objects=GetBrepsWithOption()
    if str(Objects)=="Cancel":
        print "No Objects selected"
        print "Exit programm"
        return
    arrObjects=Objects[0]
    Holelength=Objects[1]
    for obj in arrObjects:
        brep=rs.coercebrep(obj)
        copybrep=brep.Duplicate()
        for i in range(copybrep.Faces.Count):
            copybrep.Faces[i].RemoveHoles(tol)
            copybrep.Compact()
        doc.Objects.Replace(obj, copybrep)
        #doc.Objects.AddBrep(copybrep)
        doc.Views.Redraw()
    return
    
if __name__ == "__main__":
    SmallHoles()