Gazebo Gazebo

API Reference

6.16.0
gz/sim/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_GAZEBO_COMPONENTS_MODEL_HH_
18 #define GZ_GAZEBO_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 ignition
30 {
31 namespace gazebo
32 {
33 // Inline bracket to help doxygen filtering.
34 inline namespace IGNITION_GAZEBO_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  ignwarn << "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/ignitionrobotics/ign-gazebo/issues/1071
62  // Once https://github.com/ignitionrobotics/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  ignwarn << "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  ignwarn << "Unable to deserialize sdf::Model: " << sdf
112  << std::endl;
113  return _in;
114  }
115 
116  _model = *root.Model();
117  return _in;
118  }
119  };
120 }
121 
122 namespace components
123 {
126  IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.Model", Model)
127 
128 
129  using ModelSdf = Component<sdf::Model,
130  class ModelTag,
131  serializers::SdfModelSerializer>;
132  IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.ModelSdf", ModelSdf)
133 }
134 }
135 }
136 }
137 
138 #endif
This library is part of the Gazebo project.
STL class.
#define IGN_GAZEBO_REGISTER_COMPONENT(_compType, _classname)
Static component registration macro.
Definition: gz/sim/components/Factory.hh:509
static std::ostream & Serialize(std::ostream &_out, const sdf::Model &_model)
Serialization for sdf::Model.
Definition: gz/sim/components/Model.hh:43
STL class.
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.Actor", Actor) using AnimationTime
Time in seconds within animation being currently played.
#define ignwarn
Component< NoData, class ModelTag > Model
A component that identifies an entity as being a model.
Definition: gz/sim/components/Model.hh:125
A component type that wraps any data type. The intention is for this class to be used to create simpl...
Definition: gz/sim/components/Component.hh:324
T endl(T... args)
static std::istream & Deserialize(std::istream &_in, sdf::Model &_model)
Deserialization for sdf::Model.
Definition: gz/sim/components/Model.hh:97
STL class.
Definition: gz/sim/components/Model.hh:37