Rhinoscript: spread rectangle pattern along irregular surface


As you see in the image, I am trying to spread rectangles along an irregular surface that was formed by more than 4 curves (lines). The problem is I can spread rectangles in a small area and not through all the surface.
My script is:

# construct rectangle pattern on a floor 2D plan
# Nabil Mohareb
import rhinoscriptsyntax as rs

def startPattern(surface, pattU, pattV):
    # dictionary of the points
    ptMTX = {}
    # get domain of the surface
    udomain = rs.SurfaceDomain(surface,0) 
    vdomain = rs.SurfaceDomain(surface,1)
    # calculate the steps ofr U (X) & V (Y), distance of the grid
    stepU = (udomain [1] - udomain [0])/pattU
    stepV = (vdomain [1] - vdomain [0])/pattV
    
    #plot points on surface
    for i in range (pattU+1):
        for j in range (pattV+1):
            #define u & v in terms of steps values, i, j
            u = udomain[0] + stepU*i
            v = vdomain[0] + stepV*j
            # diviede the surface and draw points on surfaces 
            point = rs.EvaluateSurface (surface, u, v)
            if rs.IsPointOnSurface(surface, point) != False:
                rs.AddPoint(point)
                # write the points in the dictionary 
                ptMTX [(i,j)] = point
    #loop to create geometry 
    for i in range (6,12):    #(6,12) # this is the problem, what range should I use?
        for j in range (6,12):# the numbers are just playing around, but I think this is the problem
            if i>0 and j>0:
                curverec= rs.AddPolyline((ptMTX[(i,j)], ptMTX[(i-1,j)],ptMTX[(i-1,j-1)], ptMTX[(i,j-1)], ptMTX[(i,j)]))
                #add surface to the small rectangles
                smlSurf= rs.AddPlanarSrf(curverec)
                # find the center of the small rectangle and add point to it
                center = rs.SurfaceAreaCentroid(smlSurf)
                rs.AddPoint( center[0] )

def main():
    #select the plan lines
    plane = rs.GetObjects('select curves please', rs.filter.curve)
    pattU = rs.GetInteger('write U (x) value', 12)
    pattV = rs.GetInteger('write V (y) value', 12)
    #join the plan lines
    planeJoined= rs.JoinCurves(plane)
    #convert plane to surface
    surface =rs.AddPlanarSrf(planeJoined)
    
    # call the function
    startPattern(surface, pattU, pattV)

main ()

Hello,

I think you need to use pattU and pattV for your range values.

If you put your curve and surface generating code inside a try...except KeyError block then it should only generate surfaces where the four corners are in your ptMTX dictionary.

Hope this helps - I have not tried it !

-Graham

Thank you Graham for your kind reply, I will try to use try…except keyError block and come back to you
best wishes