Skip to content

Commit 1091cd1

Browse files
committed
update name of cloned sdf::light component
Signed-off-by: Ashton Larkin <[email protected]>
1 parent fe95598 commit 1091cd1

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/EntityComponentManager.cc

+35-24
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "ignition/gazebo/components/CanonicalLink.hh"
3333
#include "ignition/gazebo/components/Component.hh"
3434
#include "ignition/gazebo/components/Factory.hh"
35+
#include "ignition/gazebo/components/Light.hh"
3536
#include "ignition/gazebo/components/Name.hh"
3637
#include "ignition/gazebo/components/ParentEntity.hh"
3738

@@ -341,35 +342,45 @@ Entity EntityComponentManager::Clone(const Entity _entity, const Entity _parent,
341342
this->CreateComponent(clonedEntity, components::ParentEntity(_parent));
342343
}
343344

344-
// copy all components from _entity to clonedEntity
345-
for (const auto &type : this->ComponentTypes(_entity))
345+
// make sure that the cloned entity has a unique name
346+
auto clonedName = _name;
347+
if (!uniqueNameGenerated)
346348
{
347-
if (type == components::Name::typeId)
349+
if (clonedName.empty())
348350
{
349-
// make sure that the cloned entity has a unique name
350-
auto clonedName = _name;
351-
if (!uniqueNameGenerated)
352-
{
353-
if (clonedName.empty())
354-
{
355-
auto originalNameComp = this->Component<components::Name>(_entity);
356-
clonedName =
357-
originalNameComp ? originalNameComp->Data() : "cloned_entity";
358-
}
359-
uint64_t suffix = 1;
360-
while (kNullEntity != this->EntityByComponents(
361-
components::Name(clonedName + "_" + std::to_string(suffix))))
362-
suffix++;
363-
clonedName += "_" + std::to_string(suffix);
364-
}
365-
this->CreateComponent(clonedEntity, components::Name(clonedName));
351+
auto originalNameComp = this->Component<components::Name>(_entity);
352+
clonedName =
353+
originalNameComp ? originalNameComp->Data() : "cloned_entity";
366354
}
367-
else if (type != components::ParentEntity::typeId)
355+
uint64_t suffix = 1;
356+
while (kNullEntity != this->EntityByComponents(
357+
components::Name(clonedName + "_" + std::to_string(suffix))))
358+
suffix++;
359+
clonedName += "_" + std::to_string(suffix);
360+
}
361+
this->CreateComponent(clonedEntity, components::Name(clonedName));
362+
363+
// copy all components from _entity to clonedEntity
364+
for (const auto &type : this->ComponentTypes(_entity))
365+
{
366+
// skip the Name and ParentEntity components since those were already
367+
// handled above
368+
if ((type == components::Name::typeId) ||
369+
(type == components::ParentEntity::typeId))
370+
continue;
371+
372+
auto originalComp = this->ComponentImplementation(_entity, type);
373+
auto clonedComp = originalComp->Clone();
374+
375+
if (type == components::Light::typeId)
368376
{
369-
auto originalComp = this->ComponentImplementation(_entity, type);
370-
auto clonedComp = originalComp->Clone();
371-
this->CreateComponentImplementation(clonedEntity, type, clonedComp.get());
377+
// set the sdf::Light's name to clonedName
378+
auto derivedComp =
379+
static_cast<components::Light *>(clonedComp.get());
380+
derivedComp->Data().SetName(clonedName);
372381
}
382+
383+
this->CreateComponentImplementation(clonedEntity, type, clonedComp.get());
373384
}
374385

375386
// keep track of canonical link information (for clones of models, the cloned

0 commit comments

Comments
 (0)