StepFileImporter.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #ifndef ASSIMP_BUILD_NO_STEP_IMPORTER
  35. #include "StepFileImporter.h"
  36. #include "../../Importer/STEPParser/STEPFileReader.h"
  37. #include <assimp/importerdesc.h>
  38. #include <assimp/DefaultIOSystem.h>
  39. namespace Assimp {
  40. namespace StepFile {
  41. using namespace STEP;
  42. static const aiImporterDesc desc = { "StepFile Importer",
  43. "",
  44. "",
  45. "",
  46. 0,
  47. 0,
  48. 0,
  49. 0,
  50. 0,
  51. "stp" };
  52. StepFileImporter::StepFileImporter()
  53. : BaseImporter() {
  54. }
  55. StepFileImporter::~StepFileImporter() {
  56. }
  57. bool StepFileImporter::CanRead(const std::string& file, IOSystem* pIOHandler, bool checkSig) const {
  58. const std::string &extension = GetExtension(file);
  59. if ( extension == "stp" || extension == "step" ) {
  60. return true;
  61. } else if ((!extension.length() || checkSig) && pIOHandler) {
  62. const char* tokens[] = { "ISO-10303-21" };
  63. const bool found(SearchFileHeaderForToken(pIOHandler, file, tokens, 1));
  64. return found;
  65. }
  66. return false;
  67. }
  68. const aiImporterDesc *StepFileImporter::GetInfo() const {
  69. return &desc;
  70. }
  71. static const std::string mode = "rb";
  72. static const std::string StepFileSchema = "CONFIG_CONTROL_DESIGN";
  73. void StepFileImporter::InternReadFile(const std::string &file, aiScene*, IOSystem* pIOHandler) {
  74. // Read file into memory
  75. std::shared_ptr<IOStream> fileStream(pIOHandler->Open(file, mode));
  76. if (!fileStream.get()) {
  77. throw DeadlyImportError("Failed to open file " + file + ".");
  78. }
  79. std::unique_ptr<STEP::DB> db(STEP::ReadFileHeader(fileStream));
  80. const STEP::HeaderInfo& head = static_cast<const STEP::DB&>(*db).GetHeader();
  81. if (!head.fileSchema.size() || head.fileSchema != StepFileSchema) {
  82. DeadlyImportError("Unrecognized file schema: " + head.fileSchema);
  83. }
  84. }
  85. } // Namespace StepFile
  86. } // Namespace Assimp
  87. #endif // ASSIMP_BUILD_NO_STEP_IMPORTER