Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Nate Koenig <[email protected]>
  • Loading branch information
Nate Koenig committed Nov 11, 2021
1 parent 304d004 commit 14d78f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
10 changes: 9 additions & 1 deletion include/ignition/gazebo/components/Recreate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ namespace gazebo
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace components
{
/// \brief A component that identifies an entity needs to be recreated
/// \brief A component that identifies an entity needs to be recreated.
/// Currently, only Models will be processed for recreation by the
/// SimulationRunner in the ProcessRecreateEntitiesRemove and
/// ProcessRecreateEntitiesCreate functions.
///
/// The GUI ModelEditor contains example code on how to use this
/// component. For example, the ModelEditor allows a user to add a link to an
/// existing model. The existing model is tagged with this component so
/// that it can be recreated by the server.
using Recreate = Component<NoData, class RecreateTag>;
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.Recreate", Recreate)
}
Expand Down
48 changes: 34 additions & 14 deletions src/SimulationRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1312,27 +1312,47 @@ void SimulationRunner::ProcessRecreateEntitiesCreate()
IGN_PROFILE("SimulationRunner::ProcessRecreateEntitiesCreate");

// clone the original entities
std::set<Entity> entitiesToRemoveRecreateComp;
for (auto & ent : this->entitiesToRecreate)
{
auto nameComp = this->entityCompMgr.Component<components::Name>(ent);
auto parentComp =
this->entityCompMgr.Component<components::ParentEntity>(ent);
// set allowRenaming to false so the entities keep their original name
Entity clonedEntity = this->entityCompMgr.Clone(ent,
parentComp->Data(), nameComp->Data(), false);
entitiesToRemoveRecreateComp.insert(clonedEntity);
entitiesToRemoveRecreateComp.insert(ent);
}
if (nameComp && parentComp)
{
// set allowRenaming to false so the entities keep their original name
Entity clonedEntity = this->entityCompMgr.Clone(ent,
parentComp->Data(), nameComp->Data(), false);

// remove the Recreate component so they do not get recreated again in the
// next iteration
for (auto &ent : entitiesToRemoveRecreateComp)
{
if (!this->entityCompMgr.RemoveComponent<components::Recreate>(ent))
// remove the Recreate component so they do not get recreated again in the
// next iteration
if (!this->entityCompMgr.RemoveComponent<components::Recreate>(ent))
{
ignerr << "Failed to remove Recreate component from entity["
<< ent << "]" << std::endl;
}

if (!this->entityCompMgr.RemoveComponent<components::Recreate>(
clonedEntity))
{
ignerr << "Failed to remove Recreate component from entity["
<< clonedEntity << "]" << std::endl;
}
}
else
{
ignerr << "Failed to remove Recreate component from entity["
<< ent << "]" << std::endl;
if (!nameComp)
{
ignerr << "Missing name component for entity[" << ent << "]. "
<< "The entity will not be cloned during the recreation process."
<< std::endl;
}

if (!parentComp)
{
ignerr << "Missing parent component for entity[" << ent << "]. "
<< "The entity will not be cloned during the recreation process."
<< std::endl;
}
}
}

Expand Down

0 comments on commit 14d78f3

Please sign in to comment.