加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2017-05-25 16:49:39)
参考:
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
  linney=string.split(f.readline())
# print linney
  if len(linney) == 4:
    if linney[0] == 'translation':
      transx = float(linney[1])
      transy = float(linney[2])
      transz = float(linney[3])
#     print 'xyz',transx,transy,transz # I don't use these values.
  #print linney
  #raw_input()
  if linney == ['point']:
    break
#print 'Reading vertex coordinates.'
skipline=f.readline()
verts = [] # The first triplet is index zero in the wrl file.
while 1:
  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
print 'We have',len(verts)-1,'vertices.'
while 1:     # skip to triplets of vertex numbers
  linney=string.split(f.readline())  # linney was a list, now it's a string
  if linney[0] == '[':
    break 
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:
  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
# print 'dx1=',dx1,'\ndy1=',dy1,'\ndz1=',dz1,'\ndx2=',dx2,'\ndy2=',dy2,'\ndz2=',dz2,'\n'
    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
# print 'Normal  %f,%f,%f\n' % (xn,yn,zn)

    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')
g.write('endsolid %s\n' % root)
print 'Thank you.'

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有