Need help with getobjects custom_filter

I need to create a script where getobject can only select the closed curve to define the plane, and surface to define the plane. I have created a filter using custom_filter, but if it works independently, that is, selecting the object as curve or surface, my script works fine. but if included as below, the filter of surface does not work. Specifically, it still allows you to select distorted surfaces

import rhinoscriptsyntax as rs
def crv_filt(rhino_object, geometry, component_index):
if rs.IsCurvePlanar(geometry):
if rs.IsCurveClosed(geometry):
return True
elif rs.IsSurface(geometry):
if rs.IsSurfacePlanar(geometry):
return True
return False
msg = “Select objects”
crvID = rs.GetObjects(msg, (4+8), preselect=True, custom_filter=crv_filt)

First off, you can post correctly formatted code if you do the following:

Start with 3 “backticks” and the language name
Paste your code in
End with 3 more backticks

Like this:

image

Now as to your code, you are running into two things:

import rhinoscriptsyntax as rs

def crv_filt(rhino_object, geometry, component_index):
    if rs.IsCurve(geometry):  #you forgot this line
        if rs.IsCurvePlanar(geometry):
            if rs.IsCurveClosed(geometry):
                return True
    elif rs.IsSurface(geometry):
        if rs.IsSurfacePlanar(geometry):
            return True
    return False

But, it still doesn’t work to exclude non-planar surfaces. I went and looked at my function for this and it is the following:

def cpcs_filt(rhino_object, geometry, component_index):
    if rs.IsCurve(rhino_object.Id):
        if rs.IsCurveClosed(rhino_object.Id):
            if rs.IsCurvePlanar(rhino_object.Id):
                return True
    elif rs.IsSurface(rhino_object.Id):
        if rs.IsSurfacePlanar(rhino_object.Id):
            return True
    return False

Which is actually identical in structure, but uses rhino_object.Id to identify the object instead of geometry. This one works to limit the selection to only closed planar curves and planar surfaces. It’s actually odd that my code is written like that, I seem to recall that I had the same question a long time back and someone suggested this as a workaround. It seems like the code using geometry should also work…

Also, you can get a more compact code if you use some RhinoCommon notation instead:

def cpcs_filt(rhino_object, geometry, component_index):
    if geometry.IsPlanar():
        if isinstance(geometry, Rhino.Geometry.Curve):
            if not geometry.IsClosed: return False
        return True
    return False

What works here is that both curve and surface objects have the RhinoCommon method IsPlanar(), so afterwards you just have to check if the curve is closed.

Thanks so much Mitch. this is exactly what i need.