|
32 | 32 | #include "ignition/gazebo/components/CanonicalLink.hh"
|
33 | 33 | #include "ignition/gazebo/components/Component.hh"
|
34 | 34 | #include "ignition/gazebo/components/Factory.hh"
|
| 35 | +#include "ignition/gazebo/components/Light.hh" |
35 | 36 | #include "ignition/gazebo/components/Name.hh"
|
36 | 37 | #include "ignition/gazebo/components/ParentEntity.hh"
|
37 | 38 |
|
@@ -341,35 +342,45 @@ Entity EntityComponentManager::Clone(const Entity _entity, const Entity _parent,
|
341 | 342 | this->CreateComponent(clonedEntity, components::ParentEntity(_parent));
|
342 | 343 | }
|
343 | 344 |
|
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) |
346 | 348 | {
|
347 |
| - if (type == components::Name::typeId) |
| 349 | + if (clonedName.empty()) |
348 | 350 | {
|
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"; |
366 | 354 | }
|
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) |
368 | 376 | {
|
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); |
372 | 381 | }
|
| 382 | + |
| 383 | + this->CreateComponentImplementation(clonedEntity, type, clonedComp.get()); |
373 | 384 | }
|
374 | 385 |
|
375 | 386 | // keep track of canonical link information (for clones of models, the cloned
|
|
0 commit comments