add-obj-to-hrpproject.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import os.path, sys, os, getopt
00004 import subprocess
00005 from xml.dom.minidom import parse, parseString
00006 import xml.dom
00007 import re
00008 import string
00009 
00010 #from xml.dom import minidom
00011 #import sys
00012 reload(sys)
00013 sys.setdefaultencoding('utf-8')
00014 
00015 #### >>> copied from xacro/src/xacro.py
00016 # Better pretty printing of xml
00017 # Taken from http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/
00018 def fixed_writexml(self, writer, indent="", addindent="", newl=""):
00019     # indent = current indentation
00020     # addindent = indentation to add to higher levels
00021     # newl = newline string
00022     writer.write(indent+"<" + self.tagName)
00023 
00024     attrs = self._get_attributes()
00025     a_names = attrs.keys()
00026     a_names.sort()
00027 
00028     for a_name in a_names:
00029         writer.write(" %s=\"" % a_name)
00030         xml.dom.minidom._write_data(writer, attrs[a_name].value)
00031         writer.write("\"")
00032     if self.childNodes:
00033         if len(self.childNodes) == 1 \
00034           and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE:
00035             writer.write(">")
00036             self.childNodes[0].writexml(writer, "", "", "")
00037             writer.write("</%s>%s" % (self.tagName, newl))
00038             return
00039         writer.write(">%s"%(newl))
00040         for node in self.childNodes:
00041             if node.nodeType is not xml.dom.minidom.Node.TEXT_NODE: # 3:
00042                 node.writexml(writer,indent+addindent,addindent,newl) 
00043                 #node.writexml(writer,indent+addindent,addindent,newl)
00044         writer.write("%s</%s>%s" % (indent,self.tagName,newl))
00045     else:
00046         writer.write("/>%s"%(newl))
00047 # replace minidom's function with ours
00048 xml.dom.minidom.Element.writexml = fixed_writexml
00049 #### <<< copied from xacro/src/xacro.py
00050 
00051 def add_attr(nd, name, value):
00052     attr = doc.createAttribute(name)
00053     attr.value = value
00054     nd.setAttributeNode(attr)
00055     return nd
00056 
00057 def append_prop_node(pelem, name, value):
00058     nd = doc.createElement("property")
00059     nd = add_attr(nd, "name", name)
00060     nd = add_attr(nd, "value", value)
00061     pelem.appendChild(nd)
00062     return pelem
00063 
00064 def append_item_node(pelem, objname, url, robot = False, translation = '0 0 0', rotation = '1 0 0 0'):
00065     nd = doc.createElement("item")
00066     nd = add_attr(nd, "class", "com.generalrobotix.ui.item.GrxModelItem")
00067     nd = add_attr(nd, "name", objname)
00068     nd = add_attr(nd, "select", "true")
00069     nd = add_attr(nd, "url", url)
00070     if robot:
00071         nd = append_prop_node(nd, "isRobot", "true")
00072     else:
00073         nd = append_prop_node(nd, "isRobot", "false")
00074     nd = append_prop_node(nd, "WAIST.rotation",    rotation)
00075     nd = append_prop_node(nd, "WAIST.translation", translation)
00076 
00077     pelem.appendChild(nd)
00078     return pelem
00079 
00080 def append_collision_node(pelem, obj1, obj2):
00081     nd = doc.createElement("item")
00082     nd = add_attr(nd, "class", "com.generalrobotix.ui.item.GrxCollisionPairItem")
00083     nd = add_attr(nd, "name", "CP#%s_#%s_"%(obj1, obj2))
00084     nd = add_attr(nd, "select", "true")
00085     nd = append_prop_node(nd, "springConstant"   , "0 0 0 0 0 0")
00086     nd = append_prop_node(nd, "slidingFriction"  , "0.5")
00087     nd = append_prop_node(nd, "jointName1"       , "") ##
00088     nd = append_prop_node(nd, "jointName2"       , "") ##
00089     nd = append_prop_node(nd, "damperConstant"   , "0 0 0 0 0 0")
00090     nd = append_prop_node(nd, "objectName2"      , obj1)
00091     nd = append_prop_node(nd, "objectName1"      , obj2)
00092     nd = append_prop_node(nd, "springDamperModel", "false")
00093     nd = append_prop_node(nd, "staticFriction "  , "0.5")
00094     pelem.appendChild(nd)
00095     return pelem
00096 
00097 def add_object_to_projectfile(objname, url, robot=False, add_collision=True, translation = '0 0 0', rotation = '1 0 0 0'):
00098     mode_elems = doc.getElementsByTagName('mode')
00099     # check mode size
00100     items = mode_elems[0].getElementsByTagName('item')
00101     item_list = []
00102     ## dolist items
00103     for item in items:
00104         if item.getAttribute('class') == "com.generalrobotix.ui.item.GrxModelItem":
00105             item_list.append (item.getAttribute('name'))
00106 
00107     #append_item_node(mode_elems[0], objname, url, robot)
00108     append_item_node(mode_elems[0], objname, url, robot, translation, rotation)
00109 
00110     if add_collision:
00111         for obj in item_list:
00112             append_collision_node(mode_elems[0], obj, objname)
00113 
00114 ## 
00115 if __name__ == '__main__':
00116     global doc
00117     argvs = sys.argv
00118     argc = len(argvs)
00119 
00120     if argc < 4:
00121         print '### usage   : <in>.xml <out>.xml objname,url,is_robot_q,add_collision_q,0,0,0,0,0,0,0'
00122         print '### example : hoge.xml fuga.xml  table,url_of_table,True,True,0.0,0.0,1.0,0.0,0.0,1.0,90.0'
00123         print '###                             string,string      ,bool,bool,trans<3>   ,rotation<4>'
00124         exit(0)
00125 
00126     infile = argvs[1]
00127     outfile = argvs[2]
00128     objlist = argvs[3:]
00129 
00130     doc = xml.dom.minidom.parse(infile)
00131 
00132     for obj in objlist:
00133         ### parse add objects
00134         params = obj.split(',')
00135         #print params
00136         objname = params[0]
00137         url = params[1]
00138         robot = False
00139         if len(params) > 2:
00140             robot = bool(params[2])
00141         add_collision = False
00142         if len(params) > 3:
00143             add_collision = bool(params[3])
00144         translation = '0 0 0'
00145         if len(params) > 6:
00146             translation = "%s %s %s"%(params[4],params[5],params[6])
00147         rotation = '1 0 0 0'
00148         if len(params) > 10:
00149             rotation = "%s %s %s %s"%(params[7],params[8],params[9],params[10])
00150 
00151         add_object_to_projectfile(objname, url, robot, add_collision, translation, rotation)
00152 
00153     f = open(outfile, 'w')
00154     f.write(doc.toprettyxml(indent = '  '))
00155 #    f.write(doc.toxml())
00156     f.close()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


hrpsys_ros_bridge_tutorials
Author(s): Kei Okada
autogenerated on Tue Jul 23 2013 11:51:58