Building meshes and writing .obj files

## Surface

We start with a surface object and a GhPython component placed in the GH canvas.

Leave 'x' as 'ghdoc Object if geometry' and connect the surface with the 'x'

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

#### Isocurves

```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)
```

#### Closest points

Place two points underneath the surface

Then extend the GhComponent as

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

Now, extend the GhComponent as
```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)
```

#### 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))
```

#### Writing a .obj file

```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)
```

page revision: 12, last edited: 07 May 2013 13:23