Skip to content

Commit

Permalink
Address sdformat11 deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoferigo committed Mar 31, 2021
1 parent f175b93 commit c9cea97
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
4 changes: 1 addition & 3 deletions cpp/scenario/gazebo/include/scenario/gazebo/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ namespace scenario::gazebo::utils {
sdf::World renameSDFWorld(const sdf::World& world,
const std::string& newWorldName);

bool renameSDFModel(sdf::Root& sdfRoot,
const std::string& newModelName,
const size_t modelIndex = 0);
bool renameSDFModel(sdf::Root& sdfRoot, const std::string& newModelName);

bool updateSDFPhysics(sdf::Root& sdfRoot,
const double maxStepSize,
Expand Down
13 changes: 7 additions & 6 deletions cpp/scenario/gazebo/include/scenario/gazebo/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ namespace scenario::gazebo::utils {
/**
* Get the name of a model from a SDF file.
*
* @note sdformat supports only one model per SDF file.
*
* @param fileName An SDF file. It could be either an absolute path
* to the file or the file name if the parent folder is part
* of the ``IGN_GAZEBO_RESOURCE_PATH`` environment variable.
* @param modelIndex The index of the model in the SDF file. By
* default it finds the first model.
* @return The name of the model.
* @return The name of the model if the file was found and is valid,
* an empty string otherwise.
*/
std::string getModelNameFromSdf(const std::string& fileName,
const size_t modelIndex = 0);
std::string getModelNameFromSdf(const std::string& fileName);

/**
* Get the name of a world from a SDF file.
Expand All @@ -130,7 +130,8 @@ namespace scenario::gazebo::utils {
* of the ``IGN_GAZEBO_RESOURCE_PATH`` environment variable.
* @param worldIndex The index of the world in the SDF file. By
* default it finds the first world.
* @return The name of the world.
* @return The name of the world if the file was found and is valid,
* an empty string otherwise.
*/
std::string getWorldNameFromSdf(const std::string& fileName,
const size_t worldIndex = 0);
Expand Down
3 changes: 1 addition & 2 deletions cpp/scenario/gazebo/src/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ bool World::insertModel(const std::string& modelFile,
// Update the name in the sdf model. This is necessary because model plugins
// are loaded right before the creation of the model entity and, instead of
// receiving the model entity name, they receive the model sdf name.
if (!utils::renameSDFModel(
*modelSdfRoot, finalModelEntityName, ModelIndex)) {
if (!utils::renameSDFModel(*modelSdfRoot, finalModelEntityName)) {
sError << "Failed to rename SDF model" << std::endl;
return false;
}
Expand Down
23 changes: 9 additions & 14 deletions cpp/scenario/gazebo/src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,20 @@ sdf::World utils::renameSDFWorld(const sdf::World& world,
return renamedWorld;
}

bool utils::renameSDFModel(sdf::Root& sdfRoot,
const std::string& newModelName,
const size_t modelIndex)
bool utils::renameSDFModel(sdf::Root& sdfRoot, const std::string& newModelName)
{
const size_t initialNrOfModels = sdfRoot.ModelCount();

// Create a new model with the scoped name
auto renamedModel = std::make_shared<sdf::Element>();
renamedModel->SetName("model");
renamedModel->AddAttribute("name", "string", newModelName, true);

if (!sdfRoot.Model()) {
sError << "The sdf Root does not contain any model" << std::endl;
return false;
}

// Get the first child of the original model element
sdf::ElementPtr child =
sdfRoot.ModelByIndex(modelIndex)->Element()->GetFirstElement();
sdf::ElementPtr child = sdfRoot.Model()->Element()->GetFirstElement();

// Add all the children to the renamed model element
while (child) {
Expand All @@ -343,19 +343,14 @@ bool utils::renameSDFModel(sdf::Root& sdfRoot,
}

// Remove the old model
auto originalModelElement = sdfRoot.ModelByIndex(modelIndex)->Element();
auto originalModelElement = sdfRoot.Model()->Element();
originalModelElement->RemoveFromParent();

// Insert the renamed model
renamedModel->SetParent(sdfRoot.Element());
sdfRoot.Element()->InsertElement(renamedModel);

if (sdfRoot.ModelCount() != initialNrOfModels) {
sError << "Failed to rename SDF model" << std::endl;
return false;
}

if (!sdfRoot.ModelNameExists(newModelName)) {
if (sdfRoot.Model()->Name() != newModelName) {
sError << "Failed to insert renamed model in SDF root" << std::endl;
return false;
}
Expand Down
20 changes: 6 additions & 14 deletions cpp/scenario/gazebo/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ std::string utils::getSdfString(const std::string& fileName)
return root->Element()->ToString("");
}

std::string utils::getModelNameFromSdf(const std::string& fileName,
const size_t modelIndex)
std::string utils::getModelNameFromSdf(const std::string& fileName)
{
std::string absFileName = findSdfFile(fileName);

Expand All @@ -116,25 +115,18 @@ std::string utils::getModelNameFromSdf(const std::string& fileName,
return {};
}

auto root = utils::getSdfRootFromFile(absFileName);
const auto root = utils::getSdfRootFromFile(absFileName);

if (!root) {
return {};
}

if (root->ModelCount() == 0) {
sError << "Didn't find any model in file " << fileName << std::endl;
return {};
}

if (modelIndex >= root->ModelCount()) {
sError << "Model with index " << modelIndex
<< " not found. The model has only " << root->ModelCount()
<< " model(s)" << std::endl;
return {};
if (const auto model = root->Model()) {
return model->Name();
}

return root->ModelByIndex(modelIndex)->Name();
sError << "No model found in file " << fileName << std::endl;
return {};
}

std::string utils::getWorldNameFromSdf(const std::string& fileName,
Expand Down

0 comments on commit c9cea97

Please sign in to comment.