#!/usr/bin/python
# -*- coding:utf-8 -*-
#This programm is used to transfer all xml to stl in a folder
def xml2stl():
#get all the file names in the folder
import os
#os.getcwd() get the current working directory
# the place os.getcwd() can put any directory you want to do
filenames = os.listdir(os.getcwd()) #all the filenames in the current folder
for filename in filenames:
if '.xml' in filename: #only get .stl file
file_output_stl = filename.strip('.xml') + '.stl' #output file name
k = 0
lines = open(filename).readlines() #read all lines
for line in lines: #analysis each line
if '<' not in line:
k += 1 #get only numbers line
header_stl = 'solid ' + filename + '\n' +''' facet normal 1 0 0
outer loop
vertex '''
body_stl = '\n'+ ''' endloop
endfacet
facet normal 1 0 0
outer loop''' + '\n' +' vertex '
end_stl = '\n' + ''' endloop
endfacet
endsolid ''' + filename
def get_body_stl():
large_numbers = ''
i = 0
lines = open(filename).readlines() #read all lines
for line in lines: #analysis each line
if '<' not in line:
line_numbers = line.split() #get list for each line
for number in line_numbers:
large_number = float(number) * 1000
large_number_3 = ("%.3f" %large_number) #large_number_3 is the number we need
large_numbers +=str(large_number_3) + ' ' # add all the numbers to the string
i +=1
if (i%3 ==0 and i%9 != 0 and i <k * 3 ):
large_numbers += '\n' + ' vertex '
if (i%9 == 0 and i < k * 3):
large_numbers +=body_stl
return large_numbers
#input first line
open(file_output_stl,'w').write(header_stl)
open(file_output_stl,'a').write(get_body_stl()) #input the line data to file
#input last line
open(file_output_stl,'a').write(end_stl)
xml2stl()
xml2stl.py代码实现xml文件转换为stl文件格式
最新推荐文章于 2023-08-23 08:15:33 发布