xml2stl.py代码实现xml文件转换为stl文件格式

这是一个Python脚本,用于将指定文件夹中的所有XML文件转换为STL文件。脚本首先获取当前工作目录下的所有文件名,然后遍历文件,找到.xml文件并读取其内容。通过分析每行内容,提取出数值并乘以1000,以保留三位小数的形式,生成STL文件所需的格式,并写入新文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/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()

我的系统是ubuntu22.04+ros2 humble,现在的结构目录是:xyg@xyg-T6AD:~/ros2_ws/src$ tree . ├── pca9685_hardware │   ├── CMakeLists.txt │   ├── config │   │   └── pca9685_params.yaml │   ├── include │   │   └── pca9685_hardware │   │   └── pca9685_system_hardware.hpp │   ├── package.xml │   ├── resource │   │   └── plugin_description.xml │   └── src │   └── pca9685_system_hardware.cpp └── xarm_moveit_config ├── CMakeLists.txt ├── config │   ├── initial_positions.yaml │   ├── initial_positions.yaml.bak │   ├── joint_limits.yaml │   ├── joint_limits.yaml.bak │   ├── kinematics.yaml │   ├── kinematics.yaml.bak │   ├── moveit_controllers.yaml │   ├── moveit_controllers.yaml.bak │   ├── moveit.rviz │   ├── pilz_cartesian_limits.yaml │   ├── pilz_cartesian_limits.yaml.bak │   ├── ros2_controllers.yaml │   ├── ros2_controllers.yaml.bak │   ├── temp.urdf.xacro │   ├── xarm_robot.ros2_control.xacro │   ├── xarm_robot.srdf │   ├── xarm_robot.urdf │   └── xarm_robot.urdf.xacro ├── include │   └── xarm_moveit_config ├── launch │   ├── all_in_one.launch.py │   ├── controller_manager.launch.py │   ├── demo.launch.py │   ├── main_launch.py │   ├── move_group.launch.py │   ├── moveit_rviz.launch.py │   ├── ros2_control.launch.py │   ├── rsp.launch.py │   ├── rsp.launch.py.old │   ├── setup_assistant.launch.py │   ├── spawn_controllers.launch.py │   ├── static_virtual_joint_tfs.launch.py │   └── warehouse_db.launch.py ├── meshes │   ├── base_link.STL │   ├── link_1.STL │   ├── link_2.STL │   ├── link_3.STL │   ├── link_4.STL │   ├── link_5.STL │   └── link_6.STL ├── package.xml ├── scripts │   └── install_data.py └── src
06-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值