将DEM转化为STL格式三维模型用于Flow3D建模

参考:
linney=string.split(f.readline())
if len(linney) == 4:
if linney[0] ==
'translation':
transx = float(linney[1])
transy = float(linney[2])
transz = float(linney[3])
#print linney
#raw_input()
if linney == ['point']:
break
xyz = f.readline()
linney=string.split(xyz)
i = string.find(xyz,' ')
if linney==[']']:
# end of vertex coordinates
break
else:
verts.append(linney) #
building a list of xyz strings
linney=string.split(f.readline())
# linney was a list, now it's a string
if linney[0] == '[':
break
abc=string.split(row)
if len(abc)<3:
break
else:
#print abc[0]
#print int(abc[0])
xyz = verts[int(abc[0])]
# look up a vertex in the list
#print xyz
#raw_input()
x1 =
float(xyz[0])
y1 = float(xyz[1])
z1 = float(xyz[2])
xyz =
verts[int(abc[1])]
x2 = float(xyz[0])
y2 = float(xyz[1])
z2 = float(xyz[2])
xyz =
verts[int(abc[2])]
x3 = float(xyz[0])
y3 = float(xyz[1])
z3 = float(xyz[2])
dx1 = x1-x3
dy1 = y1-y3
dz1 = z1-z3
dx2 = x2-x3
dy2 = y2-y3
dz2 = z2-z3
vx = dy1*dz2 - dz1*dy2
# take the cross product of two edges
vy = dz1*dx2 -
dx1*dz2
vz = dx1*dy2 -
dy1*dx2
templength = (vx*vx +
vy*vy + vz*vz)**.5
xn = vx/templength
# normalize the normal
vector
yn = vy/templength
zn = vz/templength
g.write(' facet normal
%f %f %f\n outer loop\n' % (xn,yn,zn))
g.write('
vertex %d %d %.2f\n' % (x1,y1,z1))
g.write('
vertex %d %d %.2f\n' % (x2,y2,z2))
g.write('
vertex %d %d %.2f\n' % (x3,y3,z3))
g.write('
endloop\n endfacet\n')
https://geonet.esri.com/thread/185176-convert-raster-dem-to-stl-for-3d-printing
http://gis.ess.washington.edu/data/vrml/vrml2stl.py
修改后,支持arcgis10.2,代码如下:
#! /usr/bin/env python
# Translates simple vrml to stl
# There is no support for textures.
# This has been tested on a large vrml file (representing
topography) created
# by the tinvrml command in ARC/INFO 8.1, and running
with
# python 2.2.1 under solaris8 unix.
# The one argument is the root name of a .wrl file.
# The output is root.stl
# Version 1.1 does not stop at the first separator.
# Copyleft Harvey Greenberg, University of Washington,
hgreen@u.washington.edu
import string
import sys
root = sys.argv[1]
print 'Converting ',root+'.wrl to ',root+'.stl'
f=open(root+'.wrl')
while 1:
# skip through initial stuff
# print linney
# print
'xyz',transx,transy,transz # I don't use these values.
#print 'Reading vertex coordinates.'
skipline=f.readline()
verts = [] # The first triplet is index zero in the wrl
file.
while 1:
print 'We have',len(verts)-1,'vertices.'
while 1: # skip to
triplets of vertex numbers
g=open(root+'.stl','w') # open stl file for writing.
g.write('solid %s\n' % root)
print 'Reading triangles.'
linney=f.readline()
#print linney
linney=linney.split('-1')
for row in linney:
# print
'dx1=',dx1,'\ndy1=',dy1,'\ndz1=',dz1,'\ndx2=',dx2,'\ndy2=',dy2,'\ndz2=',dz2,'\n'
# print 'Normal %f,%f,%f\n' %
(xn,yn,zn)
g.write('endsolid %s\n' % root)
print 'Thank you.'
前一篇:SYP如何封装模型来构建业务系统
后一篇:新的开始