-
Notifications
You must be signed in to change notification settings - Fork 6
/
conversion_urdf_ros_2_ros2.py
78 lines (61 loc) · 2.69 KB
/
conversion_urdf_ros_2_ros2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import shutil
import sys
# Configuration variable
# This variable is the path to the solidworks output folder of urdf files
source_dir = '/home/sxm/Project/ros/test_urdf/'
# This variable is the package path of ros2
target_dir = '/home/sxm/Project/ros/test_urdf_tool_ws/src/test_urdf_tool/'
# This variable is the package-name of ros2
package_name = "test_urdf_tool"
# This variable is the solidworks output folder name
output_folder_name = "test_urdf"
def run_command_dir(command_dir, command):
os.system("cd " + command_dir + " && " + command)
def replace_str(file, old_str, new_str):
file_data = ""
with open(file, "r", encoding="utf-8") as f:
for line in f:
if old_str in line:
line = line.replace(old_str, new_str)
file_data += line
with open(file, "w", encoding="utf-8") as f:
f.write(file_data)
if __name__ == '__main__':
# Create folders
run_command_dir(target_dir, "mkdir launch meshes meshes/collision meshes/visual urdf")
# Copy files
# Copy stl files
run_command_dir(source_dir, "cp -r ./meshes/* " + target_dir + "meshes/visual")
run_command_dir(source_dir, "cp -r ./meshes/* " + target_dir + "meshes/collision")
# Copy urdf files
run_command_dir(source_dir, "cp ./urdf/" + output_folder_name + ".urdf " + target_dir + "urdf/")
# replace files
os.system("cp -f ./replace_files/setup.py " + target_dir)
os.system("cp -f ./replace_files/package.xml " + target_dir)
os.system("cp -f ./replace_files/launch.py " + target_dir + "launch")
# Change file content
# launch.py
replace_str(target_dir + "launch/launch.py", "lesson_urdf", package_name)
replace_str(target_dir + "launch/launch.py", "planar_3dof.urdf", output_folder_name + ".urdf")
# setup.py
replace_str(target_dir + "setup.py", "lesson_urdf", package_name)
# package.xml
replace_str(target_dir + "package.xml", "lesson_urdf", package_name)
# urdf files
replace_str(target_dir + "urdf/" + output_folder_name + ".urdf", output_folder_name + "/meshes",
package_name + "/meshes/visual")
# Insert base_footprint
keyword = "name=\"" + output_folder_name + "\">"
str = ""
with open("./replace_files/insert_content.txt", "r", encoding="utf-8") as f:
str = f.read()
file = open(target_dir + "/urdf/" + output_folder_name + ".urdf", 'r')
content = file.read()
post = content.find(keyword)
if post != -1:
content = content[:post + len(keyword)] + "\n" + str + content[post + len(keyword):]
file = open(target_dir + "/urdf/" + output_folder_name + ".urdf", "w")
file.write(content)
file.close()
print("conversion success!")