UV and Normal alignment

Hi,

Could you please add a function in R7 that will align surfaces orientation and UV directions based on a guide surface and tolerance degrees?

Below is my dodgy script that does that, I’m sure it can be done better.

#200327-
import rhinoscriptsyntax as rs
import math

guidesrf = rs.GetObject(message="Select guide surface", filter=8, preselect=True, select=False, custom_filter=None, subobjects=False)
srfs = rs.GetObjects(message="Select surfaces to align", filter=8, group=True, preselect=False, select=False, objects=None, minimum_count=1, maximum_count=0, custom_filter=None) 


domainU = rs.SurfaceDomain(guidesrf, 0)
domainV = rs.SurfaceDomain(guidesrf, 1)


srf_normal = rs.SurfaceNormal(guidesrf, [domainU[0], domainV[0]])
srfeval1 = rs.SurfaceEvaluate(guidesrf, [domainU[0], domainV[0]],1)


#flip normal first
for srf in srfs:
    domainU = rs.SurfaceDomain(srf, 0)
    domainV = rs.SurfaceDomain(srf, 1)

    srf_normal2 = rs.SurfaceNormal(srf, [domainU[0], domainV[0]])

    
    vec = rs.VectorAngle(srf_normal,srf_normal2)

    if vec>30: #tolerance in degrees -----------
        state = rs.FlipSurface(srf)
        if state == True:
            rs.FlipSurface(srf,False)
        if state == False:
            rs.FlipSurface(srf,True)
            print "Surface Flipped"




for srf in srfs:
    domainU = rs.SurfaceDomain(srf, 0)
    domainV = rs.SurfaceDomain(srf, 1)
    srfeval2 = rs.SurfaceEvaluate(srf, [domainU[0], domainV[0]],1)


    vecU = rs.VectorAngle(srfeval1[1],srfeval2[1])
    vecV = rs.VectorAngle(srfeval1[2],srfeval2[2])

    
    print ("vecU="+str(vecU))
    print ("vecV="+str(vecV))



    if vecU>35 and vecU<182 and vecV>35 and vecV<182: #tolerance in degrees
        rs.ReverseSurface(srf,4) 
        print "Transpose UV"
    

    if vecU>35 and vecU<182:
        rs.ReverseSurface(srf,1)
        print "reversed u"

    if vecV>35 and vecV<182:
        rs.ReverseSurface(srf,2)
        print "reversed v"

    
#+1 = both vectors are parallel.
#0 = vectors are not parallel or at least one of the vectors is zero.
#-1 = vectors are anti-parallel.

tolerance = 30 #degrees ---------

for srf in srfs:
    domainU = rs.SurfaceDomain(srf, 0)
    domainV = rs.SurfaceDomain(srf, 1)
    srfeval2 = rs.SurfaceFrame(srf, [domainU[0], domainV[0]])


    if srfeval1[1].IsParallelTo(srfeval2[1],tolerance) ==-1:
         rs.ReverseSurface(srf,1)


    if srfeval1[2].IsParallelTo(srfeval2[2],tolerance) ==-1:
         rs.ReverseSurface(srf,2)
2 Likes