def exportMeshTopology():
"""GET MESH OBJECT IDENTIFIER"""
rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select MESH", False, Rhino.DocObjects.ObjectType.Mesh)
if rc!=Rhino.Commands.Result.Success: return
meshID = scriptcontext.doc.Objects.Find(objref.ObjectId)
meshObj = meshID.Geometry
"""SETUP EXPORT TEXT FILE"""
filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
filename = rs.SaveFileName("Gimme a destination FILENAME", filter)
if( filename==None ): return
output = open( filename, "w" )
"""PROCESS MESH"""
meshObj.Compact()
meshObj.Normals.ComputeNormals()
mVs = meshObj.Vertices
mNs = meshObj.Normals
mFs = meshObj.Faces
mEs = meshObj.TopologyEdges
mVTs = meshObj.TopologyVertices
mVTs.SortEdges()
"""EXPORT VERTICES & VERTEX DATA"""
for i in range( mVTs.Count ):
# vertex id
vIndex = mVTs.TopologyVertexIndex(i)
vConnections = mVTs.ConnectedTopologyVertices(i)
# text file header
strVtxHeader = "#VERTEX " + str( vIndex )
# vtx coordinates
strVtxCoord = "vCoord " + str(mVs[vIndex].X) +","+ str(mVs[vIndex].Y) +","+ str(mVs[vIndex].Z)
# vtx normal
strVtxNormal = "vNormal " + str(mNs[vIndex].X) +","+ str(mNs[vIndex].Y) +","+ str(mNs[vIndex].Z)
# solve connected faces
strVtxFaces = "vFaces "
conFaces = mVTs.ConnectedFaces(i)
for j in range(conFaces.Count):
if(j==0):
strVtxFaces = strVtxFaces + str(conFaces[j])
else:
strVtxFaces = strVtxFaces +"//"+ str(conFaces[j])
# solve vertex topology
strVtxTopo = "vTopo "
for k in range(vConnections.Count):
if(k==0):
strVtxTopo = strVtxTopo + str(vConnections[k])
else:
strVtxTopo = strVtxTopo +"//"+ str(vConnections[k])
# export data
output.write( strVtxHeader +" "+ strVtxCoord +" "+ strVtxNormal +" "+ strVtxFaces +" "+ strVtxTopo + "\n" )
rs.AddTextDot(strVtxHeader +"__"+ strVtxFaces +"__"+ strVtxTopo, mVs[vIndex] )
"""EXPORT FACES & FACEDATA"""
for i in range(mFs.Count):
strFaceHeader = "#FACE " + str(i)
strFaceVtcs = "Vtcs " + str(mFs[i].A) +"//"+ str(mFs[i].B) +"//"+ str(mFs[i].C) +"//"+ str(mFs[i].D)
eSet = mEs.GetEdgesForFace(i)
strEdgeSet = "Edges "
arrPtCentroid = averagePoints( [ mVs[mFs[i].A], mVs[mFs[i].B], mVs[mFs[i].C], mVs[mFs[i].D] ] )
for m in range( eSet.Count ):
if(m==0): strEdgeSet = strEdgeSet + str(eSet[m])
else: strEdgeSet = strEdgeSet +"//"+ str(eSet[m])
# notate
rs.AddTextDot( str(i) +"__"+ strFaceVtcs +"__"+ strEdgeSet, arrPtCentroid )
# export data
output.write(strFaceHeader +" "+ strFaceVtcs +" "+ strEdgeSet + "\n")
output.flush()
output.close()
def averagePoints(PTS):
count = 0
sum = Rhino.Geometry.Point3d(0,0,0)
for i in range(PTS.Count):
sum = Rhino.Geometry.Point3d.Add(sum, PTS[i])
count+=1
avg = Rhino.Geometry.Point3d.Divide(sum, count)
return avg
if( __name__ == "__main__" ):
exportMeshTopology()