collada_urdf: more exceptions

This commit is contained in:
tfield 2010-04-23 22:25:33 +00:00
parent da358799d0
commit 51a5c72b31
3 changed files with 28 additions and 11 deletions

View File

@ -75,15 +75,14 @@ private:
};
public:
/**
* \brief Create a ColladaWriter using the specified URDF filename.
/** \brief Create a ColladaWriter using the specified URDF filename.
*
* \param filename The name of the URDF file to convert
* \throws ColladaWriterException if file could not be opened, or URDF could not be parsed
*/
ColladaWriter(std::string const& filename);
/**
* \brief Create a ColladaWriter using the specified URDF robot model and source.
/** \brief Create a ColladaWriter using the specified URDF robot model and source.
*
* \param robot The URDF model to write
* \param source The source of the model, e.g. the URL the URDF was read from
@ -92,8 +91,7 @@ public:
virtual ~ColladaWriter();
/**
* \brief Write the model to a COLLADA file.
/** \brief Write the model to a COLLADA file.
*
* \param documentName The filename of the document to write to
* \return True if the file was successfully written

View File

@ -276,20 +276,37 @@ void ColladaWriter::loadMesh(string const& filename, domGeometryRef geometry, st
}
// Try assimp first, then STLLoader
if (!loadMeshWithSTLLoader(resource, geometry, geometry_id))
std::cerr << "Can't load mesh " << filename << std::endl;
try {
loadMeshWithSTLLoader(resource, geometry, geometry_id);
}
catch (ColladaWriterException e) {
std::cerr << "Unable to load mesh file " << filename << ": " << e.what() << std::endl;
}
}
bool ColladaWriter::loadMeshWithSTLLoader(resource_retriever::MemoryResource const& resource, domGeometryRef geometry, string const& geometry_id) {
// Write the resource to a temporary file
char tmp_filename[] = "/tmp/collada_urdf_XXXXXX";
int fd = mkstemp(tmp_filename);
write(fd, resource.data.get(), resource.size);
if (fd == -1)
throw ColladaWriterException("Couldn't create temporary file");
if (write(fd, resource.data.get(), resource.size) != resource.size) {
close(fd);
unlink(tmp_filename);
throw ColladaWriterException("Couldn't write resource to file");
}
close(fd);
// Import the mesh using STLLoader
STLLoader loader;
shared_ptr<Mesh> stl_mesh = loader.load(string(tmp_filename));
if (stl_mesh == shared_ptr<Mesh>()) {
unlink(tmp_filename);
throw ColladaWriterException("Couldn't import mesh with STLLoader");
}
// Build the COLLADA mesh
buildMeshFromSTLLoader(stl_mesh, geometry, geometry_id);
// Delete the temporary file

View File

@ -73,9 +73,11 @@ void Mesh::addIndex(unsigned int i) { indices.push_back(i); }
// STLLoader
shared_ptr<Mesh> STLLoader::load(std::string const& filename) {
mesh_ = shared_ptr<Mesh>(new Mesh);
file_ = fopen(filename.c_str(), "r");
if (file_ == NULL)
return shared_ptr<Mesh>();
mesh_ = shared_ptr<Mesh>(new Mesh);
readBinary();
fclose(file_);
file_ = NULL;