diff --git a/collada_urdf/include/collada_urdf/ColladaWriter.h b/collada_urdf/include/collada_urdf/ColladaWriter.h index 63450e5..dac9890 100644 --- a/collada_urdf/include/collada_urdf/ColladaWriter.h +++ b/collada_urdf/include/collada_urdf/ColladaWriter.h @@ -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 diff --git a/collada_urdf/src/ColladaWriter.cpp b/collada_urdf/src/ColladaWriter.cpp index 78c3d54..0981b40 100644 --- a/collada_urdf/src/ColladaWriter.cpp +++ b/collada_urdf/src/ColladaWriter.cpp @@ -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 stl_mesh = loader.load(string(tmp_filename)); + if (stl_mesh == shared_ptr()) { + 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 diff --git a/collada_urdf/src/STLLoader.cpp b/collada_urdf/src/STLLoader.cpp index 88dfe6a..138f56c 100644 --- a/collada_urdf/src/STLLoader.cpp +++ b/collada_urdf/src/STLLoader.cpp @@ -73,9 +73,11 @@ void Mesh::addIndex(unsigned int i) { indices.push_back(i); } // STLLoader shared_ptr STLLoader::load(std::string const& filename) { - mesh_ = shared_ptr(new Mesh); - file_ = fopen(filename.c_str(), "r"); + if (file_ == NULL) + return shared_ptr(); + + mesh_ = shared_ptr(new Mesh); readBinary(); fclose(file_); file_ = NULL;