1- using System . Linq ;
1+ using System . Xml . Linq ;
22using System ;
33using System . IO ;
44using System . Collections . Generic ;
@@ -52,6 +52,12 @@ public JObject JSON {
5252 set => _json = value ;
5353 }
5454
55+ public XDocument XML {
56+ get ; set ;
57+ } = new XDocument ( ) ;
58+
59+
60+
5561 /// <summary>
5662 /// The name of this zosim document root. Note that it *should* be unique, so if you are
5763 /// spawing a bunch of these make sure to set the "document_name" in the ZeroSim JSON to
@@ -65,6 +71,9 @@ public string Name {
6571 set => gameObject . name = value ;
6672 }
6773
74+ public string URDFExportDirectory {
75+ get ; set ;
76+ } = "" ;
6877
6978 private static AssetBundle _assetBundle = null ;
7079
@@ -117,7 +126,7 @@ public UnityEngine.Object[] FindAssetsByName(string name) {
117126 result . Add ( asset ) ;
118127
119128 }
120- #else // RUNTIME
129+ #else // RUNTIME using asset bundle
121130 UnityEngine . Object asset = AssetBundle . LoadAsset ( name ) ;
122131
123132 if ( asset ) {
@@ -224,31 +233,6 @@ public JObject GetComponentJSON(string componentName) {
224233
225234 }
226235
227- public JObject GetOccurrenceJSON ( string occurrenceName , JArray occurrences = null ) {
228- if ( occurrences == null ) {
229- occurrences = ( JArray ) JSON [ "occurrences" ] ;
230- }
231-
232- foreach ( JObject oc in occurrences ) {
233- if ( oc [ "name" ] . Value < string > ( ) == occurrenceName ) {
234- return oc ;
235- }
236-
237- // search children
238- JArray children = ( JArray ) oc [ "children" ] ;
239- if ( children != null ) {
240- JObject childrenResult = GetOccurrenceJSON ( occurrenceName , children ) ;
241- if ( childrenResult != null ) {
242- return childrenResult ;
243- }
244-
245- }
246-
247- }
248-
249- return null ;
250-
251- }
252236
253237 /// <summary>
254238 /// Get zosim occurrence by name.
@@ -265,6 +249,66 @@ public ZOSimOccurrence GetOccurrence(string occurrenceName) {
265249 return null ;
266250 }
267251
252+ public void ExportURDF ( string exportDirectory ) {
253+ URDFExportDirectory = exportDirectory ;
254+
255+ // create root document and robot element
256+ XElement robot = new XElement ( "robot" ) ;
257+ robot . SetAttributeValue ( "name" , Name ) ;
258+ XML = new XDocument ( robot ) ;
259+
260+ // go through the ZOSimOccurrences and convert into URDF Links and Joints
261+ List < XElement > links = new List < XElement > ( ) ;
262+ foreach ( Transform child in transform ) {
263+ ZOSimOccurrence simOccurence = child . GetComponent < ZOSimOccurrence > ( ) ;
264+ if ( simOccurence ) {
265+ simOccurence . BuildURDFJoints ( this , robot ) ;
266+
267+ }
268+ }
269+
270+ robot . Add ( links ) ;
271+
272+ string urdfFilePath = Path . Combine ( URDFExportDirectory , $ "{ Name } .urdf") ;
273+ XML . Save ( urdfFilePath ) ;
274+
275+ }
276+
277+ public void ImportURDF ( string urdfFilePath ) {
278+ //TODO
279+ }
280+ /// <summary>
281+ /// Saves to ZOSim file.
282+ /// </summary>
283+ public void SaveToZOSimFile ( string filePath ) {
284+ JSON = Serialize ( ) ;
285+ // Save ZoSim file
286+ using ( StreamWriter streamWriter = File . CreateText ( filePath ) ) {
287+ streamWriter . Write ( JSON . ToString ( ) ) ;
288+ }
289+ }
290+
291+ /// <summary>
292+ /// Load Zero Sim JSON file.
293+ /// </summary>
294+ /// <param name="filePath">path to zosim file</param>
295+ public void LoadFromZOSimFile ( string filePath ) {
296+
297+ if ( File . Exists ( filePath ) == true ) { // load from file
298+ using ( StreamReader file = File . OpenText ( filePath ) ) {
299+ using ( JsonTextReader reader = new JsonTextReader ( file ) ) {
300+ JSON = ( JObject ) JToken . ReadFrom ( reader ) ;
301+ }
302+ }
303+
304+ // load json file
305+ Deserialize ( JSON ) ;
306+
307+ } else {
308+ Debug . LogError ( "ERROR: Could not load ZoSim Project. File does not exist: " + filePath ) ;
309+ }
310+ }
311+
268312
269313 /// <summary>
270314 /// Builds ZOSim document by traversing hierarchy.
@@ -335,43 +379,8 @@ public JObject Serialize() {
335379 return zoSimDocumentJSON ;
336380 }
337381
338- /// <summary>
339- /// Saves to ZOSim file.
340- /// </summary>
341- public void SaveToZOSimFile ( string filePath ) {
342- JSON = Serialize ( ) ;
343- // Save ZoSim file
344- using ( StreamWriter streamWriter = File . CreateText ( filePath ) ) {
345- streamWriter . Write ( JSON . ToString ( ) ) ;
346- }
347- }
348-
349- /// <summary>
350- /// Load Zero Sim JSON file.
351- /// </summary>
352- /// <param name="filePath">path to zosim file</param>
353- public void LoadFromZOSimFile ( string filePath ) {
354-
355- if ( File . Exists ( filePath ) == true ) { // load from file
356- using ( StreamReader file = File . OpenText ( filePath ) ) {
357- using ( JsonTextReader reader = new JsonTextReader ( file ) ) {
358- JSON = ( JObject ) JToken . ReadFrom ( reader ) ;
359- }
360- }
361-
362- // load json file
363- Deserialize ( JSON ) ;
364-
365- } else {
366- Debug . LogError ( "ERROR: Could not load ZoSim Project. File does not exist: " + filePath ) ;
367- }
368- }
369382
370383 public void Deserialize ( JObject json ) {
371-
372- // unload asset bundle because otherwise weird stuff can happen
373- // AssetBundle.Unload(true);
374-
375384 JSON = json ;
376385 Name = JSON [ "document_name" ] . Value < string > ( ) ;
377386
0 commit comments