ClippingPlane based on a geometry

Hey Guys.

Im trying to create a simple script, that creates a clipping plane, based on a selected geometry. My script is nowhere near to be working.
Someone with some python skills here?

import rhinoscriptsyntax as rs

def create_clipping_plane():
    # Select a surface
    surface_id = rs.GetObject("Select a surface to create a clipping plane", rs.filter.surface)

    if surface_id:
        # Get the surface's bounding box
        bbox = rs.SurfaceBoundingBox(surface_id)

        if bbox:
            # Extract the dimensions of the bounding box
            xmin, ymin, zmin, xmax, ymax, zmax = bbox

            # Calculate the size of the bounding box in the X, Y, and Z directions
            width = xmax - xmin
            height = ymax - ymin
            depth = zmax - zmin

            # Calculate the center point of the bounding box
            center = rs.SurfaceAreaCentroid(surface_id)[0]

            # Create a plane aligned with the surface's axes and passing through the center
            plane = rs.PlaneFromNormal(center, (0, 0, 1), (1, 0, 0))

            # Create a rectangle representing the size of the surface
            rectangle = rs.AddRectangle(plane, width, height)

            # Create a clipping plane based on the rectangle
            clipping_plane = rs.AddClippingPlane(rectangle)

            if clipping_plane:
                # Enable the clipping plane for the surface
                rs.EnableClippingPlane(surface_id, clipping_plane)
                rs.DeleteObject(rectangle)
                rs.Redraw()

# Run the function
create_clipping_plane()

Ideally id like to select a whole box and in that way create 6 clippingplanes identical to the surface dimensions. In that way creating a “SectionBox” ala Revit

Hi @filip.heim
Your code was a bit messy so I decided to just go my own way


import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
def add_clipping_planes_to_brep(brep_id):
    brep = rs.coercebrep(brep_id)
    if brep is None: 
        print("No brep found.")
        return Rhino.Commands.Result.Failure
    bbox = brep.GetBoundingBox(True)
    cplane = Rhino.Geometry.Plane(bbox.Center, Rhino.Geometry.Vector3d.ZAxis)
    gap = rs.GetInteger("Gap :")
    for i in [gap, -gap]:
        vector = Rhino.Geometry.Vector3d.Multiply(i, cplane.Normal)
        origin_new = Rhino.Geometry.Point3d.Add(cplane.Origin, vector)
        plane = Rhino.Geometry.Plane(origin_new, vector)
        rs.AddClippingPlane(plane, 20, 20, ["Perspective", "Front", "Top", "Left"])
ciro=rs.GetObject("Select srf")
add_clipping_planes_to_brep(ciro)

Hope this helps
Farouk