`ToString` doesn't skip default elements and attributes on `sdf::ElementPtr` returned from `ToElement`
#867 opened on Mar 5, 2022
Repository metrics
- Stars
- (216 stars)
- PR merge metrics
- (PR metrics pending)
Description
Environment
- OS Version: any
- Source or binary build?
sdf12
Description
When using sdf::Element::ToString, libsdformat currently checks to see if an element or attribute is set by the user and skips printing it if it's not. This helps avoid verbose SDFormat files with elements and attributes having default values. However, this behavior only works if the sdf::ElementPtr was obtained from loading a file/string via sdf::readFile/sdf::readString or calling the Element function on DOM objects (e.g., sdf::Model::Element). It does not work if the sdf::ElementPtr is obtained form calling the ToElement function on DOM objects (e.g., sdf::Model::ToElement).
Example:
<?xml version="1.0" ?>
<sdf version="1.6">
<model name="test_model">
<link name="link"/>
</model>
</sdf>
Using model->Element()->ToString("")
<model name='test_model'>
<link name='link'/>
</model>
Using model->ToElement()->ToString("")
<model name='test_model'>
<static>false</static>
<self_collide>false</self_collide>
<allow_auto_disable>true</allow_auto_disable>
<enable_wind>false</enable_wind>
<pose>0 0 0 0 -0 0</pose>
<link name='link'>
<pose>0 0 0 0 -0 0</pose>
<inertial>
<pose>0 0 0 0 -0 0</pose>
<mass>1</mass>
<inertia>
<ixx>1</ixx>
<ixy>0</ixy>
<ixz>0</ixz>
<iyy>1</iyy>
<iyz>0</iyz>
<izz>1</izz>
</inertia>
</inertial>
<enable_wind>false</enable_wind>
</link>
</model>
Relates to : https://github.com/ignitionrobotics/sdformat/pull/575, https://github.com/ignitionrobotics/sdformat/pull/842