Building meshes and writing .obj files

Surface

We start with a surface object and a GhPython component placed in the GH canvas.
01.png
Leave 'x' as 'ghdoc Object if geometry' and connect the surface with the 'x'
02.png
Our first python code with surfaces is

import rhinoscriptsyntax as rs
print rs.SurfaceDomain(x,0)
print rs.SurfaceDomain(x,1)
print rs.SurfaceParameter(x,(1,1))

|(0.0, 21.609621390438143)
|
|(0.0, 22.801021072379147)
|
|(21.609621390438143, 22.801021072379147)

Therefore, in this case,
rs.EvaluateSurface(x,(21.6096,22.8010))
is equivalent to
rs.EvaluateSurface(x,rs.SurfaceParameter(1,1))

Let's try

import rhinoscriptsyntax as rs
print rs.SurfaceDomain(x,0)
print rs.SurfaceDomain(x,1)
print rs.SurfaceParameter(x,(1,1))
(u,v)=rs.SurfaceParameter(x,(0.5,0.5))
a=rs.EvaluateSurface(x,u,v)

Then, you may get
03.png

Isocurves

Extend your GhPython component as
08.png

import rhinoscriptsyntax as rs
(u,v)=rs.SurfaceParameter(x,(0.5,0.5))
a=rs.EvaluateSurface(x,u,v)
uCurve=rs.ExtractIsoCurve(x,(u,v),0)
vCurve=rs.ExtractIsoCurve(x,(u,v),1)

09.png

Closest points

Place two points underneath the surface
04.png
Then extend the GhComponent as
05.png
Note that every input parameter is a 'ghdoc Object when geometry'.
Then run

import rhinoscriptsyntax as rs
(U1,V1)=rs.SurfaceClosestPoint(x,P1)
(U2,V2)=rs.SurfaceClosestPoint(x,P2)
cP1=rs.EvaluateSurface(x,U1,V1)
cP2=rs.EvaluateSurface(x,U2,V2)

You may get
06.png
Now, extend the GhComponent as
07.png
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
(U1,V1)=rs.SurfaceClosestPoint(x,P1)
(U2,V2)=rs.SurfaceClosestPoint(x,P2)
uCurve1=rs.ExtractIsoCurve(x,(U1,V1),0)
vCurve1=rs.ExtractIsoCurve(x,(U1,V1),1)
uCurve2=rs.ExtractIsoCurve(x,(U2,V2),0)
vCurve2=rs.ExtractIsoCurve(x,(U2,V2),1)

10.png

Building a mesh

import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
(U1,V1)=rs.SurfaceClosestPoint(x,P1)
(U2,V2)=rs.SurfaceClosestPoint(x,P2)
uCurve1=rs.ExtractIsoCurve(x,(U1,V1),0)
vCurve1=rs.ExtractIsoCurve(x,(U1,V1),1)
uCurve2=rs.ExtractIsoCurve(x,(U2,V2),0)
vCurve2=rs.ExtractIsoCurve(x,(U2,V2),1)
vertices=list()
for i in range(11):
    for j in range(11):
        u=U1+(U2-U1)*(i/10)
        v=V1+(V2-V1)*(j/10)
        vertices.append(rs.EvaluateSurface(x,u,v))
faces=list()
for i in range(10):
    for j in range(10):
        faces.append((i*11+j,i*11+j+1,(i+1)*11+j+1,(i+1)*11+j))
a=rs.AddMesh(vertices,faces)

11.png

Writing a .obj file

12.png
import rhinoscriptsyntax as rs
print '#'
print '# Wavefront OBJ'
print '#'
print ''
nNodes = rs.MeshVertexCount(x)
nFaces = rs.MeshFaceCount(x)
print "# %d vertices, %d quadliraterals" % (nNodes,nFaces)
for i in range(nNodes):
    P=rs.MeshVertices(x)[i]
    print "v %10.6f %10.6f %10.6f" % (P.X,P.Y,P.Z)
for i in range(nFaces):
    F=rs.MeshFaceVertices(x)[i]
    print "f %d %d %d %d" % (F[0]+1,F[1]+1,F[2]+1,F[3]+1)

13.png