Home | Tutorials | Wiki | Issues
Ask Your Question
0

How to access an element attribute value in C++ plugin?

asked 2018-09-27 08:39:34 -0500

kumpakri gravatar image

I have this kind of structure inside my world plugin:

<sdf version='1.6'>
  <world name='default>

    <plugin name="MyPlugin" filename="libMyPlugin.so">
      <my_box name="box1">
        <pose>0 0 2 0 0 0</pose>
        <geometry>
            <box>
                <size>5 5 4</size>
            </box>
        </geometry>
      </my_box>

      <my_box name="box2">
        <pose>10 5 2 0 0 0</pose>
        <geometry>
            <box>
                <size>5 5 4</size>
            </box>
        </geometry>
      </my_box>
    </plugin>

  </world>
</sdf>

In my world plugin I want to access both <my_box> elements and extract the positions and sizes.

I can get the first <my_box> element by

auto my_box_sdf = _sdf->GetElement("my_box");

I can check if it has a attribute 'name' by

my_box_sdf.GetAttributeSet("name");

But I don't know how to get the value of this attribute. I tried all the functions from here that seemed as if they could return the value, but nothing worked. How do I access the attribute value?

How do I get the other <my_box> element?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-10-02 10:09:01 -0500

kumpakri gravatar image

updated 2018-10-02 10:14:03 -0500

I ended up using folowing lines, that do what I need

auto box_name = my_box_sdf->GetAttribute("name");
(*box_name).GetAsString();

End for access to other <my_box> elements I use:

std::vector<std::string>  boxes_name_vec;
while( _sdf->HasElement("my_box") )
{
   auto my_box_sdf = _sdf->GetElement("my_box");
   auto box_name = my_box_sdf->GetAttribute("name");
   boxes_name_vec.push_back((*box_name).GetAsString());
   _sdf->RemoveChild(my_box_sdf);
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-09-27 08:39:34 -0500

Seen: 63 times

Last updated: Oct 02 '18