collada_urdf: more exceptions
This commit is contained in:
parent
da358799d0
commit
51a5c72b31
|
@ -75,15 +75,14 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
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
|
* \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);
|
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 robot The URDF model to write
|
||||||
* \param source The source of the model, e.g. the URL the URDF was read from
|
* \param source The source of the model, e.g. the URL the URDF was read from
|
||||||
|
@ -92,8 +91,7 @@ public:
|
||||||
|
|
||||||
virtual ~ColladaWriter();
|
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
|
* \param documentName The filename of the document to write to
|
||||||
* \return True if the file was successfully written
|
* \return True if the file was successfully written
|
||||||
|
|
|
@ -276,20 +276,37 @@ void ColladaWriter::loadMesh(string const& filename, domGeometryRef geometry, st
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try assimp first, then STLLoader
|
// Try assimp first, then STLLoader
|
||||||
if (!loadMeshWithSTLLoader(resource, geometry, geometry_id))
|
try {
|
||||||
std::cerr << "Can't load mesh " << filename << std::endl;
|
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) {
|
bool ColladaWriter::loadMeshWithSTLLoader(resource_retriever::MemoryResource const& resource, domGeometryRef geometry, string const& geometry_id) {
|
||||||
// Write the resource to a temporary file
|
// Write the resource to a temporary file
|
||||||
char tmp_filename[] = "/tmp/collada_urdf_XXXXXX";
|
char tmp_filename[] = "/tmp/collada_urdf_XXXXXX";
|
||||||
int fd = mkstemp(tmp_filename);
|
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);
|
close(fd);
|
||||||
|
|
||||||
// Import the mesh using STLLoader
|
// Import the mesh using STLLoader
|
||||||
STLLoader loader;
|
STLLoader loader;
|
||||||
shared_ptr<Mesh> stl_mesh = loader.load(string(tmp_filename));
|
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);
|
buildMeshFromSTLLoader(stl_mesh, geometry, geometry_id);
|
||||||
|
|
||||||
// Delete the temporary file
|
// Delete the temporary file
|
||||||
|
|
|
@ -73,9 +73,11 @@ void Mesh::addIndex(unsigned int i) { indices.push_back(i); }
|
||||||
// STLLoader
|
// STLLoader
|
||||||
|
|
||||||
shared_ptr<Mesh> STLLoader::load(std::string const& filename) {
|
shared_ptr<Mesh> STLLoader::load(std::string const& filename) {
|
||||||
mesh_ = shared_ptr<Mesh>(new Mesh);
|
|
||||||
|
|
||||||
file_ = fopen(filename.c_str(), "r");
|
file_ = fopen(filename.c_str(), "r");
|
||||||
|
if (file_ == NULL)
|
||||||
|
return shared_ptr<Mesh>();
|
||||||
|
|
||||||
|
mesh_ = shared_ptr<Mesh>(new Mesh);
|
||||||
readBinary();
|
readBinary();
|
||||||
fclose(file_);
|
fclose(file_);
|
||||||
file_ = NULL;
|
file_ = NULL;
|
||||||
|
|
Loading…
Reference in New Issue