Gazebo Sim

API Reference

8.7.0
components/Model.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_SIM_COMPONENTS_MODEL_HH_
18 #define GZ_SIM_COMPONENTS_MODEL_HH_
19 
20 #include <string>
21 
22 #include <sdf/Model.hh>
23 #include <sdf/Root.hh>
24 
27 #include <gz/sim/config.hh>
28 
29 namespace gz
30 {
31 namespace sim
32 {
33 // Inline bracket to help doxygen filtering.
34 inline namespace GZ_SIM_VERSION_NAMESPACE {
35 namespace serializers
36 {
38  {
43  public: static std::ostream &Serialize(std::ostream &_out,
44  const sdf::Model &_model)
45  {
46  sdf::ElementPtr modelElem = _model.Element();
47  if (!modelElem)
48  {
49  gzwarn << "Unable to serialize sdf::Model" << std::endl;
50  return _out;
51  }
52 
53  bool skip = false;
54  if (modelElem->HasElement("pose"))
55  {
56  sdf::ElementPtr poseElem = modelElem->GetElement("pose");
57  if (poseElem->HasAttribute("relative_to"))
58  {
59  // Skip serializing models with //pose/@relative_to attribute
60  // since deserialization will fail. This could be a nested model.
61  // see https://github.com/gazebosim/gz-sim/issues/1071
62  // Once https://github.com/gazebosim/sdformat/issues/820 is
63  // resolved, there should be an API that returns sdf::Errors objects
64  // instead of printing console msgs so it would be easier to ignore
65  // specific errors in Deserialize.
66  static bool warned = false;
67  if (!warned)
68  {
69  gzwarn << "Skipping serialization / deserialization for models "
70  << "with //pose/@relative_to attribute."
71  << std::endl;
72  warned = true;
73  }
74  skip = true;
75  }
76  }
77 
78  if (!skip)
79  {
80  _out << "<?xml version=\"1.0\" ?>"
81  << "<sdf version='" << SDF_PROTOCOL_VERSION << "'>"
82  << modelElem->ToString("")
83  << "</sdf>";
84 
85  }
86  else
87  {
88  _out << "";
89  }
90  return _out;
91  }
92 
97  public: static std::istream &Deserialize(std::istream &_in,
98  sdf::Model &_model)
99  {
101  if (sdf.empty())
102  {
103  return _in;
104  }
105 
106  // Its super expensive to create an SDFElement for some reason
107  sdf::Root root;
108  sdf::Errors errors = root.LoadSdfString(sdf);
109  if (!root.Model())
110  {
111  gzwarn << "Unable to deserialize sdf::Model " << sdf<< std::endl;
112  return _in;
113  }
114 
115  _model = *root.Model();
116  return _in;
117  }
118  };
119 }
120 
121 namespace components
122 {
125  GZ_SIM_REGISTER_COMPONENT("gz_sim_components.Model", Model)
126 
127 
128  using ModelSdf = Component<sdf::Model,
129  class ModelTag,
130  serializers::SdfModelSerializer>;
131  GZ_SIM_REGISTER_COMPONENT("gz_sim_components.ModelSdf", ModelSdf)
132 }
133 }
134 }
135 }
136 
137 #endif