STEPFileReader.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file STEPFileReader.cpp
  34. * @brief Implementation of the STEP file parser, which fills a
  35. * STEP::DB with data read from a file.
  36. */
  37. #include "STEPFileReader.h"
  38. #include "STEPFileEncoding.h"
  39. #include <assimp/TinyFormatter.h>
  40. #include <assimp/fast_atof.h>
  41. #include <memory>
  42. #include <functional>
  43. using namespace Assimp;
  44. namespace EXPRESS = STEP::EXPRESS;
  45. // ------------------------------------------------------------------------------------------------
  46. std::string AddLineNumber(const std::string& s,uint64_t line /*= LINE_NOT_SPECIFIED*/, const std::string& prefix = "")
  47. {
  48. return line == STEP::SyntaxError::LINE_NOT_SPECIFIED ? prefix+s : static_cast<std::string>( (Formatter::format(),prefix,"(line ",line,") ",s) );
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. std::string AddEntityID(const std::string& s,uint64_t entity /*= ENTITY_NOT_SPECIFIED*/, const std::string& prefix = "")
  52. {
  53. return entity == STEP::TypeError::ENTITY_NOT_SPECIFIED ? prefix+s : static_cast<std::string>( (Formatter::format(),prefix,"(entity #",entity,") ",s));
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. STEP::SyntaxError::SyntaxError (const std::string& s,uint64_t line /* = LINE_NOT_SPECIFIED */)
  57. : DeadlyImportError(AddLineNumber(s,line))
  58. {
  59. }
  60. // ------------------------------------------------------------------------------------------------
  61. STEP::TypeError::TypeError (const std::string& s,uint64_t entity /* = ENTITY_NOT_SPECIFIED */,uint64_t line /*= LINE_NOT_SPECIFIED*/)
  62. : DeadlyImportError(AddLineNumber(AddEntityID(s,entity),line))
  63. {
  64. }
  65. static const char *ISO_Token = "ISO-10303-21;";
  66. static const char *FILE_SCHEMA_Token = "FILE_SCHEMA";
  67. // ------------------------------------------------------------------------------------------------
  68. STEP::DB* STEP::ReadFileHeader(std::shared_ptr<IOStream> stream) {
  69. std::shared_ptr<StreamReaderLE> reader = std::shared_ptr<StreamReaderLE>(new StreamReaderLE(stream));
  70. std::unique_ptr<STEP::DB> db = std::unique_ptr<STEP::DB>(new STEP::DB(reader));
  71. LineSplitter &splitter = db->GetSplitter();
  72. if (!splitter || *splitter != ISO_Token ) {
  73. throw STEP::SyntaxError("expected magic token: " + std::string( ISO_Token ), 1);
  74. }
  75. HeaderInfo& head = db->GetHeader();
  76. for(++splitter; splitter; ++splitter) {
  77. const std::string& s = *splitter;
  78. if (s == "DATA;") {
  79. // here we go, header done, start of data section
  80. ++splitter;
  81. break;
  82. }
  83. // want one-based line numbers for human readers, so +1
  84. const uint64_t line = splitter.get_index()+1;
  85. if (s.substr(0,11) == FILE_SCHEMA_Token) {
  86. const char* sz = s.c_str()+11;
  87. SkipSpaces(sz,&sz);
  88. std::shared_ptr< const EXPRESS::DataType > schema = EXPRESS::DataType::Parse(sz);
  89. // the file schema should be a regular list entity, although it usually contains exactly one entry
  90. // since the list itself is contained in a regular parameter list, we actually have
  91. // two nested lists.
  92. const EXPRESS::LIST* list = dynamic_cast<const EXPRESS::LIST*>(schema.get());
  93. if (list && list->GetSize()) {
  94. list = dynamic_cast<const EXPRESS::LIST*>( (*list)[0].get() );
  95. if (!list) {
  96. throw STEP::SyntaxError("expected FILE_SCHEMA to be a list",line);
  97. }
  98. // XXX need support for multiple schemas?
  99. if (list->GetSize() > 1) {
  100. ASSIMP_LOG_WARN(AddLineNumber("multiple schemas currently not supported",line));
  101. }
  102. const EXPRESS::STRING *string = dynamic_cast<const EXPRESS::STRING *>((*list)[0].get());
  103. if (!list->GetSize() || nullptr == string ) {
  104. throw STEP::SyntaxError("expected FILE_SCHEMA to contain a single string literal",line);
  105. }
  106. head.fileSchema = *string;
  107. }
  108. }
  109. // XXX handle more header fields
  110. }
  111. return db.release();
  112. }
  113. namespace {
  114. // ------------------------------------------------------------------------------------------------
  115. // check whether the given line contains an entity definition (i.e. starts with "#<number>=")
  116. bool IsEntityDef(const std::string& snext)
  117. {
  118. if (snext[0] == '#') {
  119. // it is only a new entity if it has a '=' after the
  120. // entity ID.
  121. for(std::string::const_iterator it = snext.begin()+1; it != snext.end(); ++it) {
  122. if (*it == '=') {
  123. return true;
  124. }
  125. if ((*it < '0' || *it > '9') && *it != ' ') {
  126. break;
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. void STEP::ReadFile(DB& db,const EXPRESS::ConversionSchema& scheme,
  135. const char* const* types_to_track, size_t len,
  136. const char* const* inverse_indices_to_track, size_t len2)
  137. {
  138. db.SetSchema(scheme);
  139. db.SetTypesToTrack(types_to_track,len);
  140. db.SetInverseIndicesToTrack(inverse_indices_to_track,len2);
  141. const DB::ObjectMap& map = db.GetObjects();
  142. LineSplitter& splitter = db.GetSplitter();
  143. while (splitter) {
  144. bool has_next = false;
  145. std::string s = *splitter;
  146. if (s == "ENDSEC;") {
  147. break;
  148. }
  149. s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
  150. // want one-based line numbers for human readers, so +1
  151. const uint64_t line = splitter.get_index()+1;
  152. // LineSplitter already ignores empty lines
  153. ai_assert(s.length());
  154. if (s[0] != '#') {
  155. ASSIMP_LOG_WARN(AddLineNumber("expected token \'#\'",line));
  156. ++splitter;
  157. continue;
  158. }
  159. // ---
  160. // extract id, entity class name and argument string,
  161. // but don't create the actual object yet.
  162. // ---
  163. const std::string::size_type n0 = s.find_first_of('=');
  164. if (n0 == std::string::npos) {
  165. ASSIMP_LOG_WARN(AddLineNumber("expected token \'=\'",line));
  166. ++splitter;
  167. continue;
  168. }
  169. const uint64_t id = strtoul10_64(s.substr(1,n0-1).c_str());
  170. if (!id) {
  171. ASSIMP_LOG_WARN(AddLineNumber("expected positive, numeric entity id",line));
  172. ++splitter;
  173. continue;
  174. }
  175. std::string::size_type n1 = s.find_first_of('(',n0);
  176. if (n1 == std::string::npos) {
  177. has_next = true;
  178. bool ok = false;
  179. for( ++splitter; splitter; ++splitter) {
  180. const std::string& snext = *splitter;
  181. if (snext.empty()) {
  182. continue;
  183. }
  184. // the next line doesn't start an entity, so maybe it is
  185. // just a continuation for this line, keep going
  186. if (!IsEntityDef(snext)) {
  187. s.append(snext);
  188. n1 = s.find_first_of('(',n0);
  189. ok = (n1 != std::string::npos);
  190. }
  191. else {
  192. break;
  193. }
  194. }
  195. if(!ok) {
  196. ASSIMP_LOG_WARN(AddLineNumber("expected token \'(\'",line));
  197. continue;
  198. }
  199. }
  200. std::string::size_type n2 = s.find_last_of(')');
  201. if (n2 == std::string::npos || n2 < n1 || n2 == s.length() - 1 || s[n2 + 1] != ';') {
  202. has_next = true;
  203. bool ok = false;
  204. for( ++splitter; splitter; ++splitter) {
  205. const std::string& snext = *splitter;
  206. if (snext.empty()) {
  207. continue;
  208. }
  209. // the next line doesn't start an entity, so maybe it is
  210. // just a continuation for this line, keep going
  211. if (!IsEntityDef(snext)) {
  212. s.append(snext);
  213. n2 = s.find_last_of(')');
  214. ok = !(n2 == std::string::npos || n2 < n1 || n2 == s.length() - 1 || s[n2 + 1] != ';');
  215. } else {
  216. break;
  217. }
  218. }
  219. if(!ok) {
  220. ASSIMP_LOG_WARN(AddLineNumber("expected token \')\'",line));
  221. continue;
  222. }
  223. }
  224. if (map.find(id) != map.end()) {
  225. ASSIMP_LOG_WARN(AddLineNumber((Formatter::format(),"an object with the id #",id," already exists"),line));
  226. }
  227. std::string::size_type ns = n0;
  228. do ++ns; while( IsSpace(s.at(ns)));
  229. std::string::size_type ne = n1;
  230. do --ne; while( IsSpace(s.at(ne)));
  231. std::string type = s.substr(ns,ne-ns+1);
  232. std::transform( type.begin(), type.end(), type.begin(), &Assimp::ToLower<char> );
  233. const char* sz = scheme.GetStaticStringForToken(type);
  234. if(sz) {
  235. const std::string::size_type szLen = n2-n1+1;
  236. char* const copysz = new char[szLen+1];
  237. std::copy(s.c_str()+n1,s.c_str()+n2+1,copysz);
  238. copysz[szLen] = '\0';
  239. db.InternInsert(new LazyObject(db,id,line,sz,copysz));
  240. }
  241. if(!has_next) {
  242. ++splitter;
  243. }
  244. }
  245. if (!splitter) {
  246. ASSIMP_LOG_WARN("STEP: ignoring unexpected EOF");
  247. }
  248. if ( !DefaultLogger::isNullLogger()){
  249. ASSIMP_LOG_DEBUG((Formatter::format(),"STEP: got ",map.size()," object records with ",
  250. db.GetRefs().size()," inverse index entries"));
  251. }
  252. }
  253. // ------------------------------------------------------------------------------------------------
  254. std::shared_ptr<const EXPRESS::DataType> EXPRESS::DataType::Parse(const char*& inout,uint64_t line, const EXPRESS::ConversionSchema* schema /*= nullptr*/)
  255. {
  256. const char* cur = inout;
  257. SkipSpaces(&cur);
  258. if (*cur == ',' || IsSpaceOrNewLine(*cur)) {
  259. throw STEP::SyntaxError("unexpected token, expected parameter",line);
  260. }
  261. // just skip over constructions such as IFCPLANEANGLEMEASURE(0.01) and read only the value
  262. if (schema) {
  263. bool ok = false;
  264. for(const char* t = cur; *t && *t != ')' && *t != ','; ++t) {
  265. if (*t=='(') {
  266. if (!ok) {
  267. break;
  268. }
  269. for(--t;IsSpace(*t);--t);
  270. std::string s(cur,static_cast<size_t>(t-cur+1));
  271. std::transform(s.begin(),s.end(),s.begin(),&ToLower<char> );
  272. if (schema->IsKnownToken(s)) {
  273. for(cur = t+1;*cur++ != '(';);
  274. const std::shared_ptr<const EXPRESS::DataType> dt = Parse(cur);
  275. inout = *cur ? cur+1 : cur;
  276. return dt;
  277. }
  278. break;
  279. }
  280. else if (!IsSpace(*t)) {
  281. ok = true;
  282. }
  283. }
  284. }
  285. if (*cur == '*' ) {
  286. inout = cur+1;
  287. return std::make_shared<EXPRESS::ISDERIVED>();
  288. }
  289. else if (*cur == '$' ) {
  290. inout = cur+1;
  291. return std::make_shared<EXPRESS::UNSET>();
  292. }
  293. else if (*cur == '(' ) {
  294. // start of an aggregate, further parsing is done by the LIST factory constructor
  295. inout = cur;
  296. return EXPRESS::LIST::Parse(inout,line,schema);
  297. }
  298. else if (*cur == '.' ) {
  299. // enum (includes boolean)
  300. const char* start = ++cur;
  301. for(;*cur != '.';++cur) {
  302. if (*cur == '\0') {
  303. throw STEP::SyntaxError("enum not closed",line);
  304. }
  305. }
  306. inout = cur+1;
  307. return std::make_shared<EXPRESS::ENUMERATION>(std::string(start, static_cast<size_t>(cur-start) ));
  308. }
  309. else if (*cur == '#' ) {
  310. // object reference
  311. return std::make_shared<EXPRESS::ENTITY>(strtoul10_64(++cur,&inout));
  312. }
  313. else if (*cur == '\'' ) {
  314. // string literal
  315. const char* start = ++cur;
  316. for(;*cur != '\'';++cur) {
  317. if (*cur == '\0') {
  318. throw STEP::SyntaxError("string literal not closed",line);
  319. }
  320. }
  321. if (cur[1] == '\'') {
  322. // Vesanen: more than 2 escaped ' in one literal!
  323. do {
  324. for(cur += 2;*cur != '\'';++cur) {
  325. if (*cur == '\0') {
  326. throw STEP::SyntaxError("string literal not closed",line);
  327. }
  328. }
  329. }
  330. while(cur[1] == '\'');
  331. }
  332. inout = cur + 1;
  333. // assimp is supposed to output UTF8 strings, so we have to deal
  334. // with foreign encodings.
  335. std::string stemp = std::string(start, static_cast<size_t>(cur - start));
  336. if(!StringToUTF8(stemp)) {
  337. // TODO: route this to a correct logger with line numbers etc., better error messages
  338. ASSIMP_LOG_ERROR("an error occurred reading escape sequences in ASCII text");
  339. }
  340. return std::make_shared<EXPRESS::STRING>(stemp);
  341. }
  342. else if (*cur == '\"' ) {
  343. throw STEP::SyntaxError("binary data not supported yet",line);
  344. }
  345. // else -- must be a number. if there is a decimal dot in it,
  346. // parse it as real value, otherwise as integer.
  347. const char* start = cur;
  348. for(;*cur && *cur != ',' && *cur != ')' && !IsSpace(*cur);++cur) {
  349. if (*cur == '.') {
  350. double f;
  351. inout = fast_atoreal_move<double>(start,f);
  352. return std::make_shared<EXPRESS::REAL>(f);
  353. }
  354. }
  355. bool neg = false;
  356. if (*start == '-') {
  357. neg = true;
  358. ++start;
  359. }
  360. else if (*start == '+') {
  361. ++start;
  362. }
  363. int64_t num = static_cast<int64_t>( strtoul10_64(start,&inout) );
  364. return std::make_shared<EXPRESS::INTEGER>(neg?-num:num);
  365. }
  366. // ------------------------------------------------------------------------------------------------
  367. std::shared_ptr<const EXPRESS::LIST> EXPRESS::LIST::Parse(const char*& inout,uint64_t line, const EXPRESS::ConversionSchema* schema /*= nullptr*/) {
  368. const std::shared_ptr<EXPRESS::LIST> list = std::make_shared<EXPRESS::LIST>();
  369. EXPRESS::LIST::MemberList& members = list->members;
  370. const char* cur = inout;
  371. if (*cur++ != '(') {
  372. throw STEP::SyntaxError("unexpected token, expected \'(\' token at beginning of list",line);
  373. }
  374. // estimate the number of items upfront - lists can grow large
  375. size_t count = 1;
  376. for(const char* c=cur; *c && *c != ')'; ++c) {
  377. count += (*c == ',' ? 1 : 0);
  378. }
  379. members.reserve(count);
  380. for(;;++cur) {
  381. if (!*cur) {
  382. throw STEP::SyntaxError("unexpected end of line while reading list");
  383. }
  384. SkipSpaces(cur,&cur);
  385. if (*cur == ')') {
  386. break;
  387. }
  388. members.push_back( EXPRESS::DataType::Parse(cur,line,schema));
  389. SkipSpaces(cur,&cur);
  390. if (*cur != ',') {
  391. if (*cur == ')') {
  392. break;
  393. }
  394. throw STEP::SyntaxError("unexpected token, expected \',\' or \')\' token after list element",line);
  395. }
  396. }
  397. inout = cur+1;
  398. return list;
  399. }
  400. // ------------------------------------------------------------------------------------------------
  401. static void handleSkippedDepthFromToken(const char *a, int64_t &skip_depth ) {
  402. if (*a == '(') {
  403. ++skip_depth;
  404. } else if (*a == ')') {
  405. --skip_depth;
  406. }
  407. }
  408. // ------------------------------------------------------------------------------------------------
  409. static int64_t getIdFromToken(const char *a) {
  410. const char *tmp;
  411. const int64_t num = static_cast<int64_t>(strtoul10_64(a + 1, &tmp));
  412. return num;
  413. }
  414. // ------------------------------------------------------------------------------------------------
  415. STEP::LazyObject::LazyObject(DB& db, uint64_t id,uint64_t /*line*/, const char* const type,const char* args)
  416. : id(id)
  417. , type(type)
  418. , db(db)
  419. , args(args)
  420. , obj() {
  421. // find any external references and store them in the database.
  422. // this helps us emulate STEPs INVERSE fields.
  423. if (!db.KeepInverseIndicesForType(type)) {
  424. return;
  425. }
  426. // do a quick scan through the argument tuple and watch out for entity references
  427. const char *a( args );
  428. int64_t skip_depth( 0 );
  429. while ( *a ) {
  430. handleSkippedDepthFromToken(a, skip_depth);
  431. /*if (*a == '(') {
  432. ++skip_depth;
  433. } else if (*a == ')') {
  434. --skip_depth;
  435. }*/
  436. if (skip_depth >= 1 && *a=='#') {
  437. if (*(a + 1) != '#') {
  438. /*const char *tmp;
  439. const int64_t num = static_cast<int64_t>(strtoul10_64(a + 1, &tmp));
  440. db.MarkRef(num, id);*/
  441. db.MarkRef(getIdFromToken(a), id);
  442. } else {
  443. ++a;
  444. }
  445. }
  446. ++a;
  447. }
  448. }
  449. // ------------------------------------------------------------------------------------------------
  450. STEP::LazyObject::~LazyObject() {
  451. // make sure the right dtor/operator delete get called
  452. if (obj) {
  453. delete obj;
  454. } else {
  455. delete[] args;
  456. }
  457. }
  458. // ------------------------------------------------------------------------------------------------
  459. void STEP::LazyObject::LazyInit() const {
  460. const EXPRESS::ConversionSchema& schema = db.GetSchema();
  461. STEP::ConvertObjectProc proc = schema.GetConverterProc(type);
  462. if (!proc) {
  463. throw STEP::TypeError("unknown object type: " + std::string(type),id);
  464. }
  465. const char* acopy = args;
  466. std::shared_ptr<const EXPRESS::LIST> conv_args = EXPRESS::LIST::Parse(acopy,(uint64_t)STEP::SyntaxError::LINE_NOT_SPECIFIED,&db.GetSchema());
  467. delete[] args;
  468. args = nullptr;
  469. // if the converter fails, it should throw an exception, but it should never return nullptr
  470. try {
  471. obj = proc(db,*conv_args);
  472. }
  473. catch(const TypeError& t) {
  474. // augment line and entity information
  475. throw TypeError(t.what(),id);
  476. }
  477. ++db.evaluated_count;
  478. ai_assert(obj);
  479. // store the original id in the object instance
  480. obj->SetID(id);
  481. }