33using System . Collections ;
44using System . Collections . Generic ;
55using System . IO ;
6+ using System ;
67using System . Text ;
78using ZO . Document ;
89using ZO . Physics ;
@@ -16,28 +17,73 @@ namespace ZO.ImportExport {
1617
1718 public class ZOImportURDF {
1819
19- public ZOSimDocumentRoot Import ( string urdfFilePath ) {
20- using ( StreamReader streamReader = new StreamReader ( urdfFilePath ) ) {
20+ public static ZOSimDocumentRoot Import ( string urdfFilePath ) {
21+ using ( StreamReader streamReader = new StreamReader ( urdfFilePath ) ) {
2122 XmlDocument xmlDocument = new XmlDocument ( ) ;
2223 xmlDocument . Load ( streamReader ) ;
2324 return Import ( xmlDocument ) ;
2425 }
2526 }
2627
27- public ZOSimDocumentRoot Import ( XmlDocument xmlDocument ) {
28+ public static ZOSimDocumentRoot Import ( XmlDocument xmlDocument ) {
2829 XmlNode robot = xmlDocument . GetChildByName ( "robot" ) ;
2930
30- GameObject rootObject = new GameObject ( robot . Name ) ;
31+ GameObject rootObject = new GameObject ( robot . Name ) ;
3132
3233 ZOSimDocumentRoot simDocumentRoot = rootObject . AddComponent < ZOSimDocumentRoot > ( ) ;
3334
35+ // get the joints & links
36+ XmlNode [ ] xmlLinks = robot . GetChildrenByName ( "link" ) ;
37+ Dictionary < string , Tuple < XmlNode , GameObject > > goLinks = new Dictionary < string , Tuple < XmlNode , GameObject > > ( ) ;
3438
35- // create the URDF links in Unity
36- XmlNode [ ] links = robot . GetChildrenByName ( "link" ) ;
37- foreach ( XmlNode link in links ) {
38- // process the visuals
39- XmlNode [ ] visuals = link . GetChildrenByName ( "visual" ) ;
39+ // find root link. which is a link without an explicit parent
4040
41+ // create links
42+ foreach ( XmlNode xmlLink in xmlLinks ) {
43+
44+ string linkName = xmlLink . Attributes [ "name" ] . Value ;
45+ GameObject goLink = new GameObject ( linkName ) ;
46+ ZOSimOccurrence occurrence = goLink . AddComponent < ZOSimOccurrence > ( ) ;
47+
48+ GameObject goVisualsEmpty = new GameObject ( "visuals" ) ;
49+ goVisualsEmpty . transform . SetParent ( goLink . transform ) ;
50+
51+ GameObject goCollisionsEmpty = new GameObject ( "collisions" ) ;
52+ goCollisionsEmpty . transform . SetParent ( goLink . transform ) ;
53+
54+ goLinks [ linkName ] = new Tuple < XmlNode , GameObject > ( xmlLink , goLink ) ;
55+ // // process the visuals
56+ // XmlNode[] visuals = xmlLink.GetChildrenByName("visual");
57+
58+ // foreach (XmlNode visual in visuals) {
59+
60+ // }
61+
62+ // get the origin
63+ // XmlNode origin =
64+
65+ }
66+
67+ // create hierarchy from joints
68+ XmlNode [ ] xmlJoints = robot . GetChildrenByName ( "joint" ) ;
69+ foreach ( var goLink in goLinks ) {
70+
71+ // find link parent
72+ GameObject linkParent = rootObject ;
73+ foreach ( XmlNode joint in xmlJoints ) {
74+ XmlNode xmlChildLink = joint . GetChildByName ( "child" ) ;
75+ string childName = xmlChildLink . Attributes [ "link" ] . Value ;
76+
77+ if ( goLink . Value . Item2 . name == childName ) {
78+ XmlNode xmlParentLink = joint . GetChildByName ( "parent" ) ;
79+ string parentName = xmlParentLink . Attributes [ "link" ] . Value ;
80+ linkParent = goLinks [ parentName ] . Item2 ;
81+ break ;
82+ }
83+
84+ }
85+
86+ goLink . Value . Item2 . transform . SetParent ( linkParent . transform ) ;
4187 }
4288
4389 return simDocumentRoot ;
0 commit comments