Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working in python 3.12.3 as of september 26 2024 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions URDF_Exporter_Ros2/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os.path, re
from xml.etree import ElementTree
from xml.dom import minidom
from distutils.dir_util import copy_tree
import shutil # Replaced distutils with shutil
Gautham-Ramkumar03 marked this conversation as resolved.
Show resolved Hide resolved
import fileinput
import sys

Expand All @@ -26,23 +26,23 @@ def copy_body(allOccs, occs):
transform = adsk.core.Matrix3D.create()

# Create new components from occs
# This support even when a component has some occses.
# This supports even when a component has some occurrences.

new_occs = allOccs.addNewComponent(transform) # this create new occs
new_occs = allOccs.addNewComponent(transform) # this creates new occs
if occs.component.name == 'base_link':
occs.component.name = 'old_component'
new_occs.component.name = 'base_link'
else:
new_occs.component.name = re.sub('[ :()]', '_', occs.name)
new_occs = allOccs.item((allOccs.count-1))
new_occs = allOccs.item((allOccs.count - 1))
for i in range(bodies.count):
body = bodies.item(i)
body.copyToComponent(new_occs)

allOccs = root.occurrences
oldOccs = []
coppy_list = [occs for occs in allOccs]
for occs in coppy_list:
copy_list = [occs for occs in allOccs]
for occs in copy_list:
if occs.bRepBodies.count > 0:
copy_body(allOccs, occs)
oldOccs.append(occs)
Expand All @@ -53,7 +53,7 @@ def copy_body(allOccs, occs):

def export_stl(design, save_dir, components):
"""
export stl files into "sace_dir/"
export stl files into "save_dir/"


Parameters
Expand All @@ -67,8 +67,10 @@ def export_stl(design, save_dir, components):
# create a single exportManager instance
exportMgr = design.exportManager
# get the script location
try: os.mkdir(save_dir + '/meshes')
except: pass
try:
os.mkdir(save_dir + '/meshes')
except:
pass
scriptDir = save_dir + '/meshes'
# export the occurrence one by one in the component to a specified file
for component in components:
Expand All @@ -82,11 +84,11 @@ def export_stl(design, save_dir, components):
stlExportOptions = exportMgr.createSTLExportOptions(occ, fileName)
stlExportOptions.sendToPrintUtility = False
stlExportOptions.isBinaryFormat = True
# options are .MeshRefinementLow .MeshRefinementMedium .MeshRefinementHigh
# options are .MeshRefinementLow, .MeshRefinementMedium, .MeshRefinementHigh
stlExportOptions.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementLow
exportMgr.execute(stlExportOptions)
except:
print('Component ' + occ.component.name + 'has something wrong.')
print('Component ' + occ.component.name + ' has something wrong.')


def file_dialog(ui):
Expand All @@ -106,7 +108,7 @@ def file_dialog(ui):

def origin2center_of_mass(inertia, center_of_mass, mass):
"""
convert the moment of the inertia about the world coordinate into
convert the moment of inertia about the world coordinate into
that about center of mass coordinate


Expand All @@ -123,9 +125,9 @@ def origin2center_of_mass(inertia, center_of_mass, mass):
x = center_of_mass[0]
y = center_of_mass[1]
z = center_of_mass[2]
translation_matrix = [y**2+z**2, x**2+z**2, x**2+y**2,
-x*y, -y*z, -x*z]
return [ round(i - mass*t, 6) for i, t in zip(inertia, translation_matrix)]
translation_matrix = [y**2 + z**2, x**2 + z**2, x**2 + y**2,
-x * y, -y * z, -x * z]
return [round(i - mass * t, 6) for i, t in zip(inertia, translation_matrix)]


def prettify(elem):
Expand All @@ -138,36 +140,51 @@ def prettify(elem):

Returns
----------
pretified xml : str
prettified xml : str
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")


def create_package(package_name, save_dir, package_dir):
try: os.mkdir(save_dir + '/launch')
except: pass
try:
os.mkdir(save_dir + '/launch')
except:
pass

try: os.mkdir(save_dir + '/urdf')
except: pass
try:
os.mkdir(save_dir + '/urdf')
except:
pass

try: os.mkdir(save_dir + '/config')
except: pass
try:
os.mkdir(save_dir + '/config')
except:
pass

try: os.mkdir(save_dir + '/' +package_name)
except: pass
try:
os.mkdir(save_dir + '/' + package_name)
except:
pass
with open(os.path.join(save_dir, package_name, '__init__.py'), 'w'):
pass

try: os.mkdir(save_dir + '/resource')
except: pass
try:
os.mkdir(save_dir + '/resource')
except:
pass
with open(os.path.join(save_dir, 'resource', package_name), 'w'):
pass

try: os.mkdir(save_dir + '/test')
except: pass
try:
os.mkdir(save_dir + '/test')
except:
pass

# Refactor: using shutil.copytree in place of distutils.copy_tree
shutil.copytree(package_dir, save_dir, dirs_exist_ok=True) # Allows overwriting existing directories

copy_tree(package_dir, save_dir)

def update_setup_py(save_dir, package_name):
file_name = save_dir + '/setup.py'
Expand All @@ -178,6 +195,7 @@ def update_setup_py(save_dir, package_name):
else:
sys.stdout.write(line)


def update_setup_cfg(save_dir, package_name):
file_name = save_dir + '/setup.cfg'

Expand All @@ -189,6 +207,7 @@ def update_setup_cfg(save_dir, package_name):
else:
sys.stdout.write(line)


def update_package_xml(save_dir, package_name):
file_name = save_dir + '/package.xml'

Expand Down