0

I am trying to write a program that generates a new Gazebo xml world file by inserting model data from one file to the other. The binary should take a default world file as an argument.

My method of approach is as follows:

  1. Have a default empty world file
  2. Have a model.sdf file which contains the model I want to insert into the world
  3. Read default world file into boost ptree.
  4. Find <world> tag and insert model data as a child.

I have no problem finding the <world> tag, however I am not sure how I can "copy and paste" the data from my model.sdf file into my world file.

World XML

Shortened so as not to flood screen here:

<sdf version='1.7'>
  <world name='default'>
    <light name='sun' type='directional'>
      <cast_shadows>1</cast_shadows>
      <pose>0 0 10 0 -0 0</pose>
      <diffuse>0.8 0.8 0.8 1</diffuse>
      <specular>0.2 0.2 0.2 1</specular>
      <attenuation>
        <range>1000</range>
        <constant>0.9</constant>
        <linear>0.01</linear>
        <quadratic>0.001</quadratic>
      </attenuation>
      <direction>-0.5 0.1 -0.9</direction>
      <spot>
        <inner_angle>0</inner_angle>
        <outer_angle>0</outer_angle>
        <falloff>0</falloff>
      </spot>
    </light>
    <!-- MODEL DATA TO GO HERE -->
</world>

model.sdf

Shortened:

<?xml version='1.0'?>
<sdf version='1.7'>
  <model name='box'>
    <link name='link'>
      <inertial>
        <mass>3.14999</mass>
        <inertia>
          <ixx>2.86712</ixx>
          <ixy>0</ixy>
          <ixz>0</ixz>
          <iyy>2.86712</iyy>
          <iyz>0</iyz>
          <izz>0.524998</izz>
        </inertia>
        <pose>0 0 0 0 -0 0</pose>
      </inertial>
    </link>
  </model>
</sdf>

Expected Output

<sdf version='1.7'>
  <world name='default'>
    <light name='sun' type='directional'>
      <cast_shadows>1</cast_shadows>
      <pose>0 0 10 0 -0 0</pose>
      <diffuse>0.8 0.8 0.8 1</diffuse>
      <specular>0.2 0.2 0.2 1</specular>
      <attenuation>
        <range>1000</range>
        <constant>0.9</constant>
        <linear>0.01</linear>
        <quadratic>0.001</quadratic>
      </attenuation>
      <direction>-0.5 0.1 -0.9</direction>
      <spot>
        <inner_angle>0</inner_angle>
        <outer_angle>0</outer_angle>
        <falloff>0</falloff>
      </spot>
    </light>
    <model name='box'>
      <link name='link'>
        <inertial>
          <mass>3.14999</mass>
          <inertia>
            <ixx>2.86712</ixx>
            <ixy>0</ixy>
            <ixz>0</ixz>
            <iyy>2.86712</iyy>
            <iyz>0</iyz>
            <izz>0.524998</izz>
          </inertia>
          <pose>0 0 0 0 -0 0</pose>
        </inertial>
      </link>
    </model>
  </world>
</sdf>

Code:

#include <iostream>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <utility>

using namespace std;

boost::property_tree::xml_writer_settings<string> xml_settings(' ', 4);

int main(int argc, char **argv){
    using boost::property_tree::ptree;
    cout << BOOST_VERSION << endl;
    cout << BOOST_LIB_VERSION << endl;

    const string filename = argv[1];

    ptree pt;

    std::ifstream is(filename.c_str(),std::ifstream::binary);
    std::ofstream newWorldFile("NEW_WORLD.xml");

    read_xml(is, pt);

    for(auto &it: pt){
        if(it.first == "sdf" && pt.get_child_optional(it.first)){
            
            ptree pt1 = pt.get_child(it.first);

            for(auto &it1 : pt1){
                cout << "it1.first = " << it1.first << endl;

                if(it1.first == "world"){
                    // insert model data here. use pt1.put("model.<xmlattr>.name","IamABox"); ? or something similar?
                }
                boost::property_tree::write_xml(newWorldFile, pt1, xml_settings);
            }
        }

    }

Edit: One way I have thought about doing this is by reading both xml files into seperate stringstream's, then using std::find to locate the tag in the main world file, and insert the .str() result from my model to the world. I will add an answer if I do solve this and no one else has added one.

0 Answers0