2012-08-11 13:24:08 +00:00
/********************************************************************************
2023-09-18 16:13:44 +02:00
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
2011-05-08 11:04:32 +00:00
2023-09-17 12:30:17 +02:00
# include "IfcParse.h"
# include "IfcBaseClass.h"
# include "IfcCharacterDecoder.h"
# include "IfcException.h"
# include "IfcFile.h"
2023-10-24 11:47:44 +02:00
# include "IfcLogger.h"
2023-09-17 12:30:17 +02:00
# include "IfcSchema.h"
# include "IfcSIPrefix.h"
# include "IfcSpfStream.h"
# include "utils.h"
2011-05-08 11:04:32 +00:00
2023-09-17 12:30:17 +02:00
# include <algorithm>
# include <boost/algorithm/string.hpp>
# include <boost/circular_buffer.hpp>
# include <boost/math/special_functions/fpclassify.hpp>
2019-07-03 12:10:27 +02:00
# include <ctime>
# include <mutex>
2023-09-17 12:30:17 +02:00
# include <set>
2019-07-03 12:10:27 +02:00
# include <stdio.h>
# include <stdlib.h>
2023-09-17 12:30:17 +02:00
# include <string>
2019-07-03 12:10:27 +02:00
2023-09-18 16:13:44 +02:00
# ifdef USE_MMAP
# include <boost/filesystem/path.hpp>
# endif
2016-06-12 11:22:08 +02:00
# define PERMISSIVE_FLOAT
2011-05-08 11:04:32 +00:00
using namespace IfcParse ;
2023-09-17 12:30:17 +02:00
// A static locale for the real number parser. strtod() is locale-dependent, causing issues
// in locales that have ',' as a decimal separator. Therefore the non standard _strtod_l() /
// strtod_l() is used and a reference to the "C" locale is obtained here. The alternative is
// to use std::istringstream::imbue(std::locale::classic()), but there are subtleties in
// parsing in MSVC2010 and it appears to be much slower.
2016-06-05 14:03:03 +02:00
# if defined(_MSC_VER)
2023-09-17 12:30:17 +02:00
static _locale_t locale = ( _locale_t ) 0 ;
2015-02-03 13:53:36 +00:00
void init_locale ( ) {
2023-09-17 12:30:17 +02:00
if ( locale = = ( _locale_t ) 0 ) {
locale = _create_locale ( LC_NUMERIC , " C " ) ;
}
2015-02-03 13:53:36 +00:00
}
2016-05-29 17:58:03 +01:00
2016-06-05 14:03:03 +02:00
# else
2015-02-03 13:53:36 +00:00
2016-06-05 14:03:03 +02:00
# if defined(__MINGW64__) || defined(__MINGW32__)
2016-05-29 17:58:03 +01:00
# include <locale>
# include <sstream>
typedef void * locale_t ;
2016-06-05 14:03:03 +02:00
static locale_t locale = ( locale_t ) 0 ;
2016-05-29 17:58:03 +01:00
void init_locale ( ) { }
double strtod_l ( const char * start , char * * end , locale_t loc ) {
2023-09-17 12:30:17 +02:00
double d ;
std : : stringstream ss ;
ss . imbue ( std : : locale : : classic ( ) ) ;
ss < < start ;
ss > > d ;
size_t nread = ss . tellg ( ) ;
* end = const_cast < char * > ( start ) + nread ;
return d ;
2016-06-05 14:03:03 +02:00
}
# else
# ifdef __APPLE__
# include <xlocale.h>
# endif
# include <locale.h>
static locale_t locale = ( locale_t ) 0 ;
void init_locale ( ) {
2023-09-17 12:30:17 +02:00
if ( locale = = ( locale_t ) 0 ) {
locale = newlocale ( LC_NUMERIC_MASK , " C " , ( locale_t ) 0 ) ;
}
2016-05-29 17:58:03 +01:00
}
2016-06-05 14:03:03 +02:00
2016-05-29 17:58:03 +01:00
# endif
2016-06-05 14:03:03 +02:00
# endif
2016-05-29 17:58:03 +01:00
2023-09-17 12:30:17 +02:00
//
2017-07-23 14:52:04 +02:00
// Opens the file and gets the filesize
2011-07-11 09:33:18 +00:00
//
2017-07-23 14:52:04 +02:00
# ifdef USE_MMAP
IfcSpfStream : : IfcSpfStream ( const std : : string & fn , bool mmap )
# else
2016-03-05 00:40:08 +02:00
IfcSpfStream : : IfcSpfStream ( const std : : string & fn )
2017-07-23 14:52:04 +02:00
# endif
2023-09-17 12:30:17 +02:00
: stream ( 0 ) ,
buffer ( 0 ) ,
valid ( false ) ,
eof ( false ) {
2012-12-31 11:47:46 +00:00
# ifdef _MSC_VER
2023-09-17 12:30:17 +02:00
std : : wstring fn_ws = IfcUtil : : path : : from_utf8 ( fn ) ;
const wchar_t * fn_wide = fn_ws . c_str ( ) ;
2017-07-23 14:52:04 +02:00
# ifdef USE_MMAP
2023-09-17 12:30:17 +02:00
if ( mmap ) {
mfs = boost : : iostreams : : mapped_file_source ( boost : : filesystem : : wpath ( fn_wide ) ) ;
} else {
2017-07-23 14:52:04 +02:00
# endif
2023-09-17 12:30:17 +02:00
stream = _wfopen ( fn_wide , L " rb " ) ;
# ifdef USE_MMAP
}
2017-07-23 14:52:04 +02:00
# endif
2012-12-31 11:47:46 +00:00
# else
2017-07-23 14:52:04 +02:00
# ifdef USE_MMAP
2023-09-17 12:30:17 +02:00
if ( mmap ) {
mfs = boost : : iostreams : : mapped_file_source ( fn ) ;
} else {
2012-12-31 11:47:46 +00:00
# endif
2023-09-17 12:30:17 +02:00
stream = fopen ( fn . c_str ( ) , " rb " ) ;
# ifdef USE_MMAP
}
2017-07-23 14:52:04 +02:00
# endif
# endif
# ifdef USE_MMAP
2023-09-17 12:30:17 +02:00
if ( mmap ) {
if ( ! mfs . is_open ( ) ) {
return ;
}
valid = true ;
buffer = mfs . data ( ) ;
ptr = 0 ;
len = mfs . size ( ) ;
} else {
2017-07-23 14:52:04 +02:00
# endif
2023-09-17 12:30:17 +02:00
if ( stream = = NULL ) {
return ;
}
valid = true ;
fseek ( stream , 0 , SEEK_END ) ;
size = ( unsigned int ) ftell ( stream ) ;
rewind ( stream ) ;
char * buffer_rw = new char [ size ] ;
len = ( unsigned int ) fread ( buffer_rw , 1 , size , stream ) ;
buffer = buffer_rw ;
eof = len = = 0 ;
ptr = 0 ;
fclose ( stream ) ;
stream = nullptr ;
# ifdef USE_MMAP
}
2012-05-26 09:00:30 +00:00
# endif
2011-07-11 09:33:18 +00:00
}
2016-03-05 00:40:08 +02:00
IfcSpfStream : : IfcSpfStream ( std : : istream & f , int l )
2023-09-17 12:30:17 +02:00
: stream ( 0 ) ,
buffer ( 0 ) {
eof = false ;
size = l ;
char * buffer_rw = new char [ size ] ;
f . read ( buffer_rw , size ) ;
buffer = buffer_rw ;
valid = f . gcount ( ) = = size ;
ptr = 0 ;
len = l ;
2011-08-20 09:08:56 +00:00
}
2016-03-05 00:40:08 +02:00
IfcSpfStream : : IfcSpfStream ( void * data , int l )
2023-09-17 12:30:17 +02:00
: stream ( 0 ) ,
buffer ( 0 ) {
eof = false ;
size = l ;
buffer = ( char * ) data ;
valid = true ;
ptr = 0 ;
len = l ;
2011-10-27 16:13:29 +00:00
}
2023-09-17 12:30:17 +02:00
IfcSpfStream : : ~ IfcSpfStream ( ) {
Close ( ) ;
2016-02-07 22:45:42 +02:00
}
2012-08-11 13:24:08 +00:00
void IfcSpfStream : : Close ( ) {
2017-07-23 14:52:04 +02:00
# ifdef USE_MMAP
2023-09-17 12:30:17 +02:00
if ( mfs . is_open ( ) ) {
mfs . close ( ) ;
return ;
}
2012-08-11 13:24:08 +00:00
# endif
2023-09-17 12:30:17 +02:00
delete [ ] buffer ;
2023-10-24 14:16:26 +02:00
if ( stream ! = nullptr ) {
2023-09-17 12:30:17 +02:00
fclose ( stream ) ;
}
2011-07-11 09:33:18 +00:00
}
//
// Seeks an arbitrary position in the file
//
2012-08-11 13:24:08 +00:00
void IfcSpfStream : : Seek ( unsigned int o ) {
2023-09-17 12:30:17 +02:00
ptr = o ;
if ( ptr > = len ) {
throw IfcException ( " Reading outside of file limits " ) ;
}
eof = false ;
2011-07-11 09:33:18 +00:00
}
//
// Returns the character at the cursor
//
2012-08-11 13:24:08 +00:00
char IfcSpfStream : : Peek ( ) {
2023-09-17 12:30:17 +02:00
return buffer [ ptr ] ;
2011-07-11 09:33:18 +00:00
}
//
// Returns the character at specified offset
//
2012-08-11 13:24:08 +00:00
char IfcSpfStream : : Read ( unsigned int o ) {
2023-09-17 12:30:17 +02:00
return buffer [ o ] ;
2011-07-11 09:33:18 +00:00
}
//
// Returns the cursor position
//
2012-08-11 13:24:08 +00:00
unsigned int IfcSpfStream : : Tell ( ) {
2023-09-17 12:30:17 +02:00
return ptr ;
2011-07-11 09:33:18 +00:00
}
//
// Increments cursor and reads new chunk if necessary
//
2012-08-11 13:24:08 +00:00
void IfcSpfStream : : Inc ( ) {
2023-09-17 12:30:17 +02:00
if ( + + ptr = = len ) {
eof = true ;
return ;
}
const char current = IfcSpfStream : : Peek ( ) ;
if ( current = = ' \n ' | | current = = ' \r ' ) {
// NB this is recursive. It might as well be a loop.
IfcSpfStream : : Inc ( ) ;
}
2011-07-11 09:33:18 +00:00
}
2023-09-17 12:30:17 +02:00
IfcSpfLexer : : IfcSpfLexer ( IfcParse : : IfcSpfStream * s , IfcParse : : IfcFile * f ) {
file = f ;
stream = s ;
decoder = new IfcCharacterDecoder ( s ) ;
2011-09-25 09:53:22 +00:00
}
2015-01-05 17:46:23 +00:00
IfcSpfLexer : : ~ IfcSpfLexer ( ) {
2023-09-17 12:30:17 +02:00
delete decoder ;
2011-07-11 09:33:18 +00:00
}
2015-01-05 17:46:23 +00:00
unsigned int IfcSpfLexer : : skipWhitespace ( ) {
2023-09-17 12:30:17 +02:00
unsigned int n = 0 ;
while ( ! stream - > eof ) {
char c = stream - > Peek ( ) ;
if ( ( c = = ' ' | | c = = ' \r ' | | c = = ' \n ' | | c = = ' \t ' ) ) {
stream - > Inc ( ) ;
+ + n ;
} else {
break ;
}
}
return n ;
2014-04-02 15:04:09 +00:00
}
2015-01-05 17:46:23 +00:00
unsigned int IfcSpfLexer : : skipComment ( ) {
2023-09-17 12:30:17 +02:00
char c = stream - > Peek ( ) ;
if ( c ! = ' / ' ) {
return 0 ;
}
stream - > Inc ( ) ;
c = stream - > Peek ( ) ;
if ( c ! = ' * ' ) {
stream - > Seek ( stream - > Tell ( ) - 1 ) ;
return 0 ;
}
unsigned int n = 2 ;
char p = 0 ;
while ( ! stream - > eof ) {
c = stream - > Peek ( ) ;
stream - > Inc ( ) ;
+ + n ;
if ( c = = ' / ' & & p = = ' * ' ) {
break ;
}
p = c ;
}
return n ;
2014-04-02 15:04:09 +00:00
}
2011-07-11 09:33:18 +00:00
//
// Returns the offset of the current Token and moves cursor to next
//
2015-01-05 17:46:23 +00:00
Token IfcSpfLexer : : Next ( ) {
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
if ( stream - > eof ) {
return NoneTokenPtr ( ) ;
}
2011-07-11 09:33:18 +00:00
2023-10-24 14:16:26 +02:00
while ( ( skipWhitespace ( ) ! = 0U ) | | ( skipComment ( ) ! = 0U ) ) {
2023-09-17 12:30:17 +02:00
}
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
if ( stream - > eof ) {
return NoneTokenPtr ( ) ;
}
unsigned int pos = stream - > Tell ( ) ;
char c = stream - > Peek ( ) ;
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
// If the cursor is at [()=,;$*] we know token consists of single char
if ( c = = ' ( ' | | c = = ' ) ' | | c = = ' = ' | | c = = ' , ' | | c = = ' ; ' | | c = = ' $ ' | | c = = ' * ' ) {
stream - > Inc ( ) ;
return OperatorTokenPtr ( this , pos , pos + 1 ) ;
}
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
int len = 0 ;
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
while ( ! stream - > eof ) {
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
// Read character and increment pointer if not starting a new token
c = stream - > Peek ( ) ;
2023-10-24 14:16:26 +02:00
if ( ( len ! = 0 ) & & ( c = = ' ( ' | | c = = ' ) ' | | c = = ' = ' | | c = = ' , ' | | c = = ' ; ' | | c = = ' / ' ) ) {
2023-09-17 12:30:17 +02:00
break ;
}
stream - > Inc ( ) ;
len + + ;
// If a string is encountered defer processing to the IfcCharacterDecoder
if ( c = = ' \' ' ) {
decoder - > skip ( ) ;
}
}
2023-10-24 14:16:26 +02:00
if ( len ! = 0 ) {
2023-09-17 12:30:17 +02:00
return GeneralTokenPtr ( this , pos , stream - > Tell ( ) ) ;
}
2023-10-24 12:21:32 +02:00
return NoneTokenPtr ( ) ;
2011-07-11 09:33:18 +00:00
}
2019-07-03 12:10:27 +02:00
bool IfcSpfStream : : is_eof_at ( unsigned int local_ptr ) {
2023-09-17 12:30:17 +02:00
return local_ptr > = len ;
2019-07-03 12:10:27 +02:00
}
void IfcSpfStream : : increment_at ( unsigned int & local_ptr ) {
2023-09-17 12:30:17 +02:00
if ( + + local_ptr = = len ) {
return ;
}
const char current = IfcSpfStream : : peek_at ( local_ptr ) ;
if ( current = = ' \n ' | | current = = ' \r ' ) {
IfcSpfStream : : increment_at ( local_ptr ) ;
}
2019-07-03 12:10:27 +02:00
}
char IfcSpfStream : : peek_at ( unsigned int local_ptr ) {
2023-09-17 12:30:17 +02:00
return buffer [ local_ptr ] ;
2019-07-03 12:10:27 +02:00
}
2011-07-11 09:33:18 +00:00
//
// Reads a std::string from the file at specified offset
// Omits whitespace and comments
//
2023-09-17 12:30:17 +02:00
void IfcSpfLexer : : TokenString ( unsigned int offset , std : : string & buffer ) {
buffer . clear ( ) ;
while ( ! stream - > is_eof_at ( offset ) ) {
char c = stream - > peek_at ( offset ) ;
2023-10-24 14:16:26 +02:00
if ( ! buffer . empty ( ) & & ( c = = ' ( ' | | c = = ' ) ' | | c = = ' = ' | | c = = ' , ' | | c = = ' ; ' | | c = = ' / ' ) ) {
2023-09-17 12:30:17 +02:00
break ;
}
stream - > increment_at ( offset ) ;
if ( c = = ' ' | | c = = ' \r ' | | c = = ' \n ' | | c = = ' \t ' ) {
continue ;
2023-10-24 12:21:32 +02:00
}
if ( c = = ' \' ' ) {
2023-09-17 12:30:17 +02:00
// todo, make decoder use local offset ptr
buffer = decoder - > get ( offset ) ;
break ;
}
2023-10-24 12:21:32 +02:00
buffer . push_back ( c ) ;
2023-09-17 12:30:17 +02:00
}
2011-07-11 09:33:18 +00:00
}
2016-06-09 22:25:50 +06:00
//Note: according to STEP standard, there may be newlines in tokens
2023-09-17 12:30:17 +02:00
inline void RemoveTokenSeparators ( IfcSpfStream * stream , unsigned start , unsigned end , std : : string & oDestination ) {
oDestination . clear ( ) ;
for ( unsigned i = start ; i < end ; i + + ) {
char c = stream - > Read ( i ) ;
if ( c = = ' ' | | c = = ' \r ' | | c = = ' \n ' | | c = = ' \t ' ) {
continue ;
}
oDestination + = c ;
}
2016-06-09 22:25:50 +06:00
}
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
bool ParseInt ( const char * pStart , int & val ) {
char * pEnd ;
long result = strtol ( pStart , & pEnd , 10 ) ;
if ( * pEnd ! = 0 ) {
return false ;
}
val = ( int ) result ;
return true ;
2016-06-09 22:25:50 +06:00
}
2023-09-17 12:30:17 +02:00
bool ParseFloat ( const char * pStart , double & val ) {
char * pEnd ;
2016-06-09 22:25:50 +06:00
# ifdef _MSC_VER
2023-09-17 12:30:17 +02:00
double result = _strtod_l ( pStart , & pEnd , locale ) ;
2016-06-09 22:25:50 +06:00
# else
2023-09-17 12:30:17 +02:00
double result = strtod_l ( pStart , & pEnd , locale ) ;
2016-06-09 22:25:50 +06:00
# endif
2023-09-17 12:30:17 +02:00
if ( * pEnd ! = 0 ) {
return false ;
}
val = result ;
return true ;
}
2021-07-29 14:54:51 +02:00
2023-09-17 12:30:17 +02:00
bool ParseBool ( const char * pStart , int & val ) {
if ( strlen ( pStart ) ! = 3 | | pStart [ 0 ] ! = ' . ' | | pStart [ 2 ] ! = ' . ' ) {
return false ;
}
char mid = pStart [ 1 ] ;
if ( mid = = ' T ' ) {
val = 1 ;
} else if ( mid = = ' F ' ) {
val = 0 ;
} else if ( mid = = ' U ' ) {
val = 2 ;
} else {
return false ;
}
return true ;
2016-06-09 22:25:50 +06:00
}
Token IfcParse : : OperatorTokenPtr ( IfcSpfLexer * lexer , unsigned start , unsigned end ) {
2023-09-17 12:30:17 +02:00
char first = lexer - > stream - > Read ( start ) ;
Token token ( lexer , start , end , Token_OPERATOR ) ;
token . value_char = first ;
return token ;
2016-06-09 22:25:50 +06:00
}
Token IfcParse : : GeneralTokenPtr ( IfcSpfLexer * lexer , unsigned start , unsigned end ) {
2023-09-17 12:30:17 +02:00
Token token ( lexer , start , end , Token_NONE ) ;
//extract token into temp buffer (remove eol-s, no encoding changes)
std : : string & tokenStr = lexer - > GetTempString ( ) ;
RemoveTokenSeparators ( lexer - > stream , start , end , tokenStr ) ;
//determine type of the token
char first = lexer - > stream - > Read ( start ) ;
if ( first = = ' # ' ) {
token . type = Token_IDENTIFIER ;
if ( ! ParseInt ( tokenStr . c_str ( ) + 1 , token . value_int ) ) {
throw IfcException ( " Identifier token as not integer " ) ;
}
} else if ( first = = ' \' ' ) {
token . type = Token_STRING ;
} else if ( first = = ' . ' ) {
token . type = Token_ENUMERATION ;
if ( ParseBool ( tokenStr . c_str ( ) , token . value_int ) ) { //bool is also enumeration
token . type = Token_BOOL ;
}
} else if ( first = = ' " ' ) {
token . type = Token_BINARY ;
} else if ( ParseInt ( tokenStr . c_str ( ) , token . value_int ) ) {
token . type = Token_INT ;
} else if ( ParseFloat ( tokenStr . c_str ( ) , token . value_double ) ) {
token . type = Token_FLOAT ;
} else {
token . type = Token_KEYWORD ;
}
2016-06-09 22:25:50 +06:00
2023-09-17 12:30:17 +02:00
return token ;
2016-06-09 22:25:50 +06:00
}
Token IfcParse : : NoneTokenPtr ( ) { return Token ( ) ; }
bool TokenFunc : : isOperator ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_OPERATOR ;
2011-05-08 11:04:32 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
bool TokenFunc : : isOperator ( const Token & t , char op ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_OPERATOR & & t . value_char = = op ;
2011-06-13 07:23:09 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
bool TokenFunc : : isIdentifier ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_IDENTIFIER ;
2011-05-08 11:04:32 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
bool TokenFunc : : isString ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_STRING ;
2011-05-08 11:04:32 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
bool TokenFunc : : isEnumeration ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_ENUMERATION | | t . type = = Token_BOOL ;
2011-05-08 11:04:32 +00:00
}
2015-02-03 13:53:36 +00:00
2015-04-07 15:06:56 +00:00
bool TokenFunc : : isBinary ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_BINARY ;
2015-04-07 15:06:56 +00:00
}
2015-01-05 17:46:23 +00:00
bool TokenFunc : : isKeyword ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_KEYWORD ;
2011-05-08 11:04:32 +00:00
}
2015-02-03 13:53:36 +00:00
2014-11-02 18:36:51 +00:00
bool TokenFunc : : isInt ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_INT ;
2014-11-02 18:36:51 +00:00
}
2015-02-03 13:53:36 +00:00
2014-11-02 18:36:51 +00:00
bool TokenFunc : : isBool ( const Token & t ) {
2023-09-17 12:30:17 +02:00
// Bool and logical share the same storage type, just logical unknown is stored as 2.
return t . type = = Token_BOOL & & t . value_int ! = 2 ;
2021-07-29 14:54:51 +02:00
}
bool TokenFunc : : isLogical ( const Token & t ) {
2023-09-17 12:30:17 +02:00
return t . type = = Token_BOOL ;
2014-11-02 18:36:51 +00:00
}
2015-02-03 13:53:36 +00:00
2014-11-02 18:36:51 +00:00
bool TokenFunc : : isFloat ( const Token & t ) {
2016-06-12 11:22:08 +02:00
# ifdef PERMISSIVE_FLOAT
2023-09-17 12:30:17 +02:00
/// NB: We are being more permissive here then allowed by the standard
return t . type = = Token_FLOAT | | t . type = = Token_INT ;
2016-06-12 11:22:08 +02:00
# else
2023-09-17 12:30:17 +02:00
return t . type = = Token_FLOAT ;
2016-06-12 11:22:08 +02:00
# endif
2014-11-02 18:36:51 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
int TokenFunc : : asInt ( const Token & t ) {
2023-09-17 12:30:17 +02:00
if ( t . type ! = Token_INT ) {
throw IfcInvalidTokenException ( t . startPos , toString ( t ) , " integer " ) ;
}
return t . value_int ;
2016-06-09 22:25:50 +06:00
}
int TokenFunc : : asIdentifier ( const Token & t ) {
2023-09-17 12:30:17 +02:00
if ( t . type ! = Token_IDENTIFIER ) {
throw IfcInvalidTokenException ( t . startPos , toString ( t ) , " instance name " ) ;
}
return t . value_int ;
2011-06-13 07:23:09 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
bool TokenFunc : : asBool ( const Token & t ) {
2023-09-17 12:30:17 +02:00
if ( t . type ! = Token_BOOL ) {
throw IfcInvalidTokenException ( t . startPos , toString ( t ) , " boolean " ) ;
}
return t . value_int = = 1 ;
2021-07-29 14:54:51 +02:00
}
boost : : logic : : tribool TokenFunc : : asLogical ( const Token & t ) {
2023-09-17 12:30:17 +02:00
if ( t . type ! = Token_BOOL ) {
throw IfcInvalidTokenException ( t . startPos , toString ( t ) , " boolean " ) ;
}
if ( t . value_int = = 0 ) {
return false ;
2023-10-24 12:21:32 +02:00
}
if ( t . value_int = = 1 ) {
2023-09-17 12:30:17 +02:00
return true ;
}
2023-10-24 12:21:32 +02:00
return boost : : logic : : indeterminate ;
2011-05-08 11:04:32 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
double TokenFunc : : asFloat ( const Token & t ) {
2016-06-12 11:22:08 +02:00
# ifdef PERMISSIVE_FLOAT
2023-09-17 12:30:17 +02:00
if ( t . type = = Token_INT ) {
/// NB: We are being more permissive here then allowed by the standard
return t . value_int ;
2023-10-24 12:21:32 +02:00
} // ----> continues beyond preprocessor directive
2016-06-12 11:22:08 +02:00
# endif
2023-10-24 12:21:32 +02:00
if ( t . type = = Token_FLOAT ) {
2023-09-17 12:30:17 +02:00
return t . value_double ;
}
2023-10-24 12:21:32 +02:00
throw IfcInvalidTokenException ( t . startPos , toString ( t ) , " real " ) ;
2016-06-09 22:25:50 +06:00
}
2023-09-17 12:30:17 +02:00
const std : : string & TokenFunc : : asStringRef ( const Token & t ) {
2017-03-02 16:12:20 +02:00
if ( t . type = = Token_NONE ) {
throw IfcParse : : IfcException ( " Null token encountered, premature end of file? " ) ;
}
2023-09-17 12:30:17 +02:00
std : : string & str = t . lexer - > GetTempString ( ) ;
t . lexer - > TokenString ( t . startPos , str ) ;
if ( ( isString ( t ) | | isEnumeration ( t ) | | isBinary ( t ) ) & & ! str . empty ( ) ) {
//remove start+end characters in-place
str . erase ( str . end ( ) - 1 ) ;
str . erase ( str . begin ( ) ) ;
}
return str ;
2011-07-11 09:33:18 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
std : : string TokenFunc : : asString ( const Token & t ) {
2023-09-17 12:30:17 +02:00
if ( isString ( t ) | | isEnumeration ( t ) | | isBinary ( t ) ) {
return asStringRef ( t ) ;
}
2023-10-24 12:21:32 +02:00
throw IfcInvalidTokenException ( t . startPos , toString ( t ) , " string " ) ;
2015-04-07 15:06:56 +00:00
}
boost : : dynamic_bitset < > TokenFunc : : asBinary ( const Token & t ) {
2023-09-17 12:30:17 +02:00
const std : : string & str = asStringRef ( t ) ;
if ( str . size ( ) < 1 ) {
throw IfcException ( " Token is not a valid binary sequence " ) ;
}
2015-04-07 15:06:56 +00:00
2023-09-17 12:30:17 +02:00
std : : string : : const_iterator it = str . begin ( ) ;
int n = * it - ' 0 ' ;
if ( ( n < 0 | | n > 3 ) | | ( str . size ( ) = = 1 & & n ! = 0 ) ) {
throw IfcException ( " Token is not a valid binary sequence " ) ;
}
+ + it ;
unsigned i = ( ( unsigned ) str . size ( ) - 1 ) * 4 - n ;
boost : : dynamic_bitset < > bitset ( i ) ;
for ( ; it ! = str . end ( ) ; + + it ) {
const std : : string : : value_type & c = * it ;
int value = ( c < ' A ' ) ? ( c - ' 0 ' ) : ( c - ' A ' + 10 ) ;
for ( unsigned j = 0 ; j < 4 ; + + j ) {
if ( i - - = = 0 ) {
break ;
}
2023-10-24 14:16:26 +02:00
if ( ( value & ( 1 < < ( 3 - j ) ) ) ! = 0 ) {
2023-09-17 12:30:17 +02:00
bitset . set ( i ) ;
}
}
}
2015-04-07 15:06:56 +00:00
2023-09-17 12:30:17 +02:00
return bitset ;
2011-05-08 11:04:32 +00:00
}
2015-02-03 13:53:36 +00:00
2012-08-11 13:24:08 +00:00
std : : string TokenFunc : : toString ( const Token & t ) {
2023-09-17 12:30:17 +02:00
std : : string result ;
t . lexer - > TokenString ( t . startPos , result ) ;
return result ;
2011-07-11 09:33:18 +00:00
}
2012-08-11 13:24:08 +00:00
TokenArgument : : TokenArgument ( const Token & t ) {
2023-09-17 12:30:17 +02:00
token = t ;
2011-05-08 11:04:32 +00:00
}
2011-07-11 09:33:18 +00:00
2015-01-05 14:11:42 +00:00
EntityArgument : : EntityArgument ( const Token & t ) {
2023-09-17 12:30:17 +02:00
IfcParse : : IfcFile * file = t . lexer - > file ;
IfcEntityInstanceData * data = read ( 0 , file , t . startPos ) ;
// Data needs to be loaded, for the tokens
// to be consumed and parsing to continue.
data - > load ( ) ;
entity = file - > schema ( ) - > instantiate ( data ) ;
2011-05-08 11:04:32 +00:00
}
2011-07-11 09:33:18 +00:00
2018-01-29 12:11:50 +01:00
namespace {
2023-09-17 12:30:17 +02:00
template < typename T >
class vector_or_array {
std : : vector < T > * vector_ ;
T * array_ ;
size_t size_ , index_ ;
public :
vector_or_array ( std : : vector < T > * vector )
: vector_ ( vector ) ,
array_ ( 0 ) ,
size_ ( 0 ) ,
index_ ( 0 ) { }
vector_or_array ( Argument * * arr , size_t size )
: vector_ ( 0 ) ,
array_ ( arr ) ,
size_ ( size ) ,
index_ ( 0 ) { }
void push_back ( const T & t ) {
// @todo this should log a warning when the size is exceeded
if ( array_ & & index_ < size_ ) {
array_ [ index_ + + ] = t ;
} else if ( vector_ ) {
vector_ - > push_back ( t ) ;
}
}
size_t index ( ) const {
if ( vector_ ) {
return vector_ - > size ( ) ;
}
2023-10-24 12:21:32 +02:00
return index_ ;
2023-09-17 12:30:17 +02:00
}
} ;
} // namespace
//
2011-07-11 09:33:18 +00:00
// Reads the arguments from a list of token
2017-06-05 17:26:58 +02:00
// Aditionally, registers the ids (i.e. #[\d]+) in the inverse map
2011-07-11 09:33:18 +00:00
//
2021-09-10 09:27:07 +02:00
size_t IfcParse : : IfcFile : : load ( unsigned entity_instance_name , const IfcParse : : entity * entity , Argument * * & attributes , size_t num_attributes , int attribute_index ) {
2023-09-17 12:30:17 +02:00
Token next = tokens - > Next ( ) ;
std : : vector < Argument * > * vector = 0 ;
vector_or_array < Argument * > filler ( attributes , num_attributes ) ;
if ( attributes = = 0 ) {
if ( num_attributes ! = 0 ) {
// If num_attributes is zero we know this is a top-level entity instance (or header entity) being parsed.
// There can only be parsed one of these at a time, so we can reuse the vector we have defined at the file
// scope.
2023-10-24 14:16:26 +02:00
if ( entity ! = nullptr ) {
2023-09-17 12:30:17 +02:00
vector = & internal_attribute_vector_ ;
} else {
vector = & internal_attribute_vector_simple_type_ ;
}
vector - > clear ( ) ;
} else {
vector = new std : : vector < Argument * > ;
}
filler = vector_or_array < Argument * > ( vector ) ;
}
2018-01-29 12:11:50 +01:00
2023-09-17 12:30:17 +02:00
size_t return_value = 0 ;
2023-10-24 14:16:26 +02:00
while ( ( next . startPos ! = 0U ) | | ( next . lexer ! = nullptr ) ) {
2023-09-17 12:30:17 +02:00
if ( TokenFunc : : isOperator ( next , ' , ' ) ) {
// do nothing
} else if ( TokenFunc : : isOperator ( next , ' ) ' ) ) {
break ;
} else if ( TokenFunc : : isOperator ( next , ' ( ' ) ) {
return_value + + ;
ArgumentList * alist = new ArgumentList ( ) ;
// entity is passed along here, after all the it is the type of the instance
// that owns the list that is significant for inverse attributes
alist - > size ( ) = load ( entity_instance_name , entity , alist - > arguments ( ) , 0 , attribute_index = = - 1 ? ( int ) filler . index ( ) : attribute_index ) ;
filler . push_back ( alist ) ;
} else {
return_value + + ;
if ( TokenFunc : : isIdentifier ( next ) ) {
if ( ! parsing_complete_ ) {
register_inverse ( entity_instance_name , entity , next , attribute_index = = - 1 ? ( int ) filler . index ( ) : attribute_index ) ;
}
}
if ( TokenFunc : : isKeyword ( next ) ) {
try {
auto ea = new EntityArgument ( next ) ;
addEntity ( ( ( IfcUtil : : IfcBaseClass * ) * ea ) ) ;
filler . push_back ( ea ) ;
} catch ( IfcException & e ) {
Logger : : Message ( Logger : : LOG_ERROR , e . what ( ) ) ;
}
} else {
filler . push_back ( new TokenArgument ( next ) ) ;
}
}
next = tokens - > Next ( ) ;
}
2018-01-29 12:11:50 +01:00
2023-10-24 14:16:26 +02:00
if ( vector ! = nullptr ) {
2023-09-17 12:30:17 +02:00
// Obviously don't try and create a 0-length array.
2023-10-24 14:16:26 +02:00
if ( ( num_attributes ! = 0U ) | | ! vector - > empty ( ) ) {
2023-09-17 12:30:17 +02:00
// @todo figure out whether all this logic is still necessary, since we know the
// expected amount of attributes and shouldn't be able to access more than allowed
// by the schema.
attributes = new Argument * [ ( std : : max ) ( num_attributes , vector - > size ( ) ) ] { nullptr } ;
// @todo this appears unnecessary, we increment this in the loop already,
// which is more accurate as the filler can't go above it's size in case
// it uses the pre-allocated c-array.
// -> return_value = vector->size();
for ( size_t i = 0 ; i < vector - > size ( ) ; + + i ) {
attributes [ i ] = vector - > at ( i ) ;
}
}
2018-01-29 12:11:50 +01:00
2023-09-17 12:30:17 +02:00
if ( ( vector ! = & internal_attribute_vector_ ) & & ( vector ! = & internal_attribute_vector_simple_type_ ) ) {
delete vector ;
}
}
return return_value ;
2011-05-08 11:04:32 +00:00
}
2011-07-11 09:33:18 +00:00
2014-11-02 18:36:51 +00:00
IfcUtil : : ArgumentType ArgumentList : : type ( ) const {
2023-09-17 12:30:17 +02:00
if ( size_ = = 0 ) {
return IfcUtil : : Argument_EMPTY_AGGREGATE ;
}
2016-11-08 17:24:08 +01:00
2023-09-17 12:30:17 +02:00
const IfcUtil : : ArgumentType elem_type = list_ [ 0 ] - > type ( ) ;
return IfcUtil : : make_aggregate ( elem_type ) ;
2014-11-02 18:36:51 +00:00
}
2015-04-07 15:06:56 +00:00
// templated helper function for reading arguments into a list
2023-09-17 12:30:17 +02:00
template < typename T >
2018-01-29 12:11:50 +01:00
std : : vector < T > read_aggregate_as_vector ( Argument * * list , size_t size ) {
2023-09-17 12:30:17 +02:00
std : : vector < T > return_value ;
return_value . reserve ( size ) ;
for ( size_t i = 0 ; i < size ; + + i ) {
return_value . push_back ( * list [ i ] ) ;
}
return return_value ;
}
template < typename T >
std : : vector < std : : vector < T > > read_aggregate_of_aggregate_as_vector2 ( Argument * * list , size_t size ) {
std : : vector < std : : vector < T > > return_value ;
return_value . reserve ( size ) ;
for ( size_t i = 0 ; i < size ; + + i ) {
return_value . push_back ( * list [ i ] ) ;
}
return return_value ;
2015-06-16 12:58:07 +00:00
}
2015-04-07 15:06:56 +00:00
2011-07-11 09:33:18 +00:00
//
// Functions for casting the ArgumentList to other types
//
2012-03-13 11:56:52 +00:00
ArgumentList : : operator std : : vector < double > ( ) const {
2023-09-17 12:30:17 +02:00
return read_aggregate_as_vector < double > ( list_ , size_ ) ;
2011-07-25 14:56:40 +00:00
}
2015-06-16 12:58:07 +00:00
2011-07-25 14:56:40 +00:00
ArgumentList : : operator std : : vector < int > ( ) const {
2023-09-17 12:30:17 +02:00
return read_aggregate_as_vector < int > ( list_ , size_ ) ;
2011-07-25 14:56:40 +00:00
}
2015-06-16 12:58:07 +00:00
2011-07-25 14:56:40 +00:00
ArgumentList : : operator std : : vector < std : : string > ( ) const {
2023-09-17 12:30:17 +02:00
return read_aggregate_as_vector < std : : string > ( list_ , size_ ) ;
2015-04-07 15:06:56 +00:00
}
2015-06-16 12:58:07 +00:00
2023-09-17 12:30:17 +02:00
ArgumentList : : operator std : : vector < boost : : dynamic_bitset < > > ( ) const {
return read_aggregate_as_vector < boost : : dynamic_bitset < > > ( list_ , size_ ) ;
2011-07-25 14:56:40 +00:00
}
2015-06-16 12:58:07 +00:00
2021-07-29 14:54:51 +02:00
ArgumentList : : operator aggregate_of_instance : : ptr ( ) const {
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr l ( new aggregate_of_instance ( ) ) ;
for ( size_t i = 0 ; i < size_ ; + + i ) {
// FIXME: account for $
2022-06-07 14:37:12 +03:00
try {
2023-09-17 12:30:17 +02:00
IfcUtil : : IfcBaseClass * entity = * list_ [ i ] ;
2022-06-07 14:37:12 +03:00
l - > push ( entity ) ;
} catch ( IfcException e ) {
Logger : : Error ( e ) ;
}
2023-09-17 12:30:17 +02:00
}
return l ;
2011-07-11 09:33:18 +00:00
}
2015-06-16 12:58:07 +00:00
2023-09-17 12:30:17 +02:00
ArgumentList : : operator std : : vector < std : : vector < int > > ( ) const {
return read_aggregate_of_aggregate_as_vector2 < int > ( list_ , size_ ) ;
2015-06-16 12:58:07 +00:00
}
2023-09-17 12:30:17 +02:00
ArgumentList : : operator std : : vector < std : : vector < double > > ( ) const {
return read_aggregate_of_aggregate_as_vector2 < double > ( list_ , size_ ) ;
2015-06-16 12:58:07 +00:00
}
2021-07-29 14:54:51 +02:00
ArgumentList : : operator aggregate_of_aggregate_of_instance : : ptr ( ) const {
2023-09-17 12:30:17 +02:00
aggregate_of_aggregate_of_instance : : ptr l ( new aggregate_of_aggregate_of_instance ( ) ) ;
for ( size_t i = 0 ; i < size_ ; + + i ) {
const Argument * arg = list_ [ i ] ;
const ArgumentList * arg_list ;
if ( ( arg_list = dynamic_cast < const ArgumentList * > ( arg ) ) ! = 0 ) {
aggregate_of_instance : : ptr e = * arg_list ;
l - > push ( e ) ;
} else {
auto token = dynamic_cast < const TokenArgument * > ( arg ) ;
2023-10-24 14:16:26 +02:00
int startpos = token ! = nullptr ? token - > token . startPos : 0 ;
2023-09-17 12:30:17 +02:00
std : : string string_rep = this - > toString ( ) ;
throw IfcInvalidTokenException ( startpos , string_rep , " nested aggregate " ) ;
}
}
return l ;
2014-05-04 14:42:55 +00:00
}
2015-06-16 12:58:07 +00:00
2018-01-29 12:11:50 +01:00
unsigned int ArgumentList : : size ( ) const { return ( unsigned int ) size_ ; }
2015-06-16 12:58:07 +00:00
2023-09-17 12:30:17 +02:00
Argument * ArgumentList : : operator [ ] ( unsigned int i ) const {
if ( i > = size_ ) {
throw IfcAttributeOutOfRangeException ( " Argument index out of range " ) ;
}
return list_ [ i ] ;
2011-07-11 09:33:18 +00:00
}
2015-06-16 12:58:07 +00:00
2018-01-29 12:11:50 +01:00
/*
2015-01-16 16:26:40 +00:00
void ArgumentList::set(unsigned int i, Argument* argument) {
while (size() < i) {
2016-06-09 22:25:50 +06:00
push(new NullArgument());
2015-01-16 16:26:40 +00:00
}
if (i < size()) {
delete list[i];
list[i] = argument;
} else {
list.push_back(argument);
}
}
2018-01-29 12:11:50 +01:00
*/
2015-06-16 12:58:07 +00:00
2012-03-13 11:56:52 +00:00
std : : string ArgumentList : : toString ( bool upper ) const {
2023-09-17 12:30:17 +02:00
std : : stringstream ss ;
ss < < " ( " ;
for ( size_t i = 0 ; i < size_ ; + + i ) {
if ( i ! = 0 ) {
ss < < " , " ;
}
ss < < list_ [ i ] - > toString ( upper ) ;
}
ss < < " ) " ;
return ss . str ( ) ;
2011-05-08 11:04:32 +00:00
}
2015-06-16 12:58:07 +00:00
2011-07-25 14:56:40 +00:00
bool ArgumentList : : isNull ( ) const { return false ; }
2015-06-16 12:58:07 +00:00
2011-09-03 13:58:59 +00:00
ArgumentList : : ~ ArgumentList ( ) {
2023-09-17 12:30:17 +02:00
for ( size_t i = 0 ; i < size_ ; + + i ) {
delete list_ [ i ] ;
}
delete [ ] list_ ;
2011-09-03 13:58:59 +00:00
}
2011-05-08 11:04:32 +00:00
2014-11-02 18:36:51 +00:00
IfcUtil : : ArgumentType TokenArgument : : type ( ) const {
2023-09-17 12:30:17 +02:00
if ( TokenFunc : : isInt ( token ) ) {
return IfcUtil : : Argument_INT ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isBool ( token ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_BOOL ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isLogical ( token ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_LOGICAL ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isFloat ( token ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_DOUBLE ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isString ( token ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_STRING ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isEnumeration ( token ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_ENUMERATION ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isIdentifier ( token ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_ENTITY_INSTANCE ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isBinary ( token ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_BINARY ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isOperator ( token , ' $ ' ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_NULL ;
2023-10-24 12:21:32 +02:00
}
if ( TokenFunc : : isOperator ( token , ' * ' ) ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_DERIVED ;
}
2023-10-24 12:21:32 +02:00
return IfcUtil : : Argument_UNKNOWN ;
2014-11-02 18:36:51 +00:00
}
2011-07-11 09:33:18 +00:00
//
// Functions for casting the TokenArgument to other types
//
TokenArgument : : operator int ( ) const { return TokenFunc : : asInt ( token ) ; }
TokenArgument : : operator bool ( ) const { return TokenFunc : : asBool ( token ) ; }
2021-07-29 14:54:51 +02:00
TokenArgument : : operator boost : : logic : : tribool ( ) const { return TokenFunc : : asLogical ( token ) ; }
2012-03-13 11:56:52 +00:00
TokenArgument : : operator double ( ) const { return TokenFunc : : asFloat ( token ) ; }
2011-07-11 09:33:18 +00:00
TokenArgument : : operator std : : string ( ) const { return TokenFunc : : asString ( token ) ; }
2015-04-07 15:06:56 +00:00
TokenArgument : : operator boost : : dynamic_bitset < > ( ) const { return TokenFunc : : asBinary ( token ) ; }
2017-12-13 13:16:49 +01:00
TokenArgument : : operator IfcUtil : : IfcBaseClass * ( ) const { return token . lexer - > file - > instance_by_id ( TokenFunc : : asIdentifier ( token ) ) ; }
2015-01-10 22:13:52 +00:00
unsigned int TokenArgument : : size ( ) const { return 1 ; }
2023-09-17 12:30:17 +02:00
Argument * TokenArgument : : operator [ ] ( unsigned int /*i*/ ) const { throw IfcException ( " Argument is not a list of attributes " ) ; }
std : : string TokenArgument : : toString ( bool upper ) const {
if ( upper & & TokenFunc : : isString ( token ) ) {
return IfcWrite : : IfcCharacterEncoder ( TokenFunc : : asString ( token ) ) ;
}
2023-10-24 12:21:32 +02:00
return TokenFunc : : toString ( token ) ;
2012-10-09 19:54:35 +00:00
}
2023-09-17 12:30:17 +02:00
bool TokenArgument : : isNull ( ) const { return TokenFunc : : isOperator ( token , ' $ ' ) ; }
2014-11-02 18:36:51 +00:00
IfcUtil : : ArgumentType EntityArgument : : type ( ) const {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_ENTITY_INSTANCE ;
2014-11-02 18:36:51 +00:00
}
2011-07-11 09:33:18 +00:00
//
// Functions for casting the EntityArgument to other types
//
2022-10-20 20:30:48 +02:00
EntityArgument : : operator IfcUtil : : IfcBaseClass * ( ) const {
2023-09-17 12:30:17 +02:00
return entity ;
2022-10-20 20:30:48 +02:00
}
unsigned int EntityArgument : : size ( ) const {
2023-09-17 12:30:17 +02:00
return 1 ;
2022-10-20 20:30:48 +02:00
}
2023-09-17 12:30:17 +02:00
Argument * EntityArgument : : operator [ ] ( unsigned int /*i*/ ) const {
throw IfcException ( " Argument is not a list of arguments " ) ;
2022-10-20 20:30:48 +02:00
}
std : : string EntityArgument : : toString ( bool upper ) const {
2023-09-17 12:30:17 +02:00
return entity - > data ( ) . toString ( upper ) ;
2012-03-13 11:56:52 +00:00
}
2022-10-20 20:30:48 +02:00
2011-07-25 14:56:40 +00:00
bool EntityArgument : : isNull ( ) const { return false ; }
2022-10-20 20:30:48 +02:00
EntityArgument : : ~ EntityArgument ( ) {
2023-09-17 12:30:17 +02:00
// We don't delete it here, rather it will be freed as part of the entity_file_map.
// For that purpose when parsed, the simple type instance is explicitly added to the
// file. The reason is we want parsed simply types to behave the same as constructed
// simple types.
2023-01-28 14:19:03 +01:00
2023-09-17 12:30:17 +02:00
// delete entity;
2022-10-20 20:30:48 +02:00
}
2011-07-11 09:33:18 +00:00
//
// Reads an Entity from the list of Tokens at the specified offset in the file
//
2017-06-05 17:26:58 +02:00
IfcEntityInstanceData * IfcParse : : read ( unsigned int i , IfcFile * f , boost : : optional < unsigned > offset ) {
2023-09-17 12:30:17 +02:00
if ( offset ) {
f - > tokens - > stream - > Seek ( * offset ) ;
}
Token datatype = f - > tokens - > Next ( ) ;
if ( ! TokenFunc : : isKeyword ( datatype ) ) {
throw IfcException ( " Unexpected token while parsing entity " ) ;
}
const IfcParse : : declaration * ty = f - > schema ( ) - > declaration_by_name ( TokenFunc : : asStringRef ( datatype ) ) ;
IfcEntityInstanceData * e = new IfcEntityInstanceData ( ty , f , i , offset . get_value_or ( 0 ) ) ;
return e ;
2011-05-08 11:04:32 +00:00
}
2011-07-11 09:33:18 +00:00
2019-07-03 12:10:27 +02:00
void IfcParse : : IfcFile : : seek_to ( const IfcEntityInstanceData & data ) {
2023-09-17 12:30:17 +02:00
if ( tokens - > stream - > Tell ( ) ! = data . offset_in_file ( ) ) {
tokens - > stream - > Seek ( data . offset_in_file ( ) ) ;
Token datatype = tokens - > Next ( ) ;
if ( ! TokenFunc : : isKeyword ( datatype ) ) {
throw IfcException ( " Unexpected token while parsing entity instance " ) ;
}
}
tokens - > Next ( ) ;
2019-07-03 12:10:27 +02:00
}
void IfcParse : : IfcFile : : try_read_semicolon ( ) {
2023-09-17 12:30:17 +02:00
unsigned int old_offset = tokens - > stream - > Tell ( ) ;
Token semilocon = tokens - > Next ( ) ;
if ( ! TokenFunc : : isOperator ( semilocon , ' ; ' ) ) {
tokens - > stream - > Seek ( old_offset ) ;
}
2011-05-08 11:04:32 +00:00
}
2011-07-11 09:33:18 +00:00
2021-08-22 12:22:44 +02:00
void IfcParse : : IfcFile : : register_inverse ( unsigned id_from , const IfcParse : : entity * from_entity , Token t , int attribute_index ) {
2023-09-17 12:30:17 +02:00
// Assume a check on token type has already been performed
auto e = from_entity ;
byref_excl [ t . value_int ] . push_back ( id_from ) ;
2023-10-24 14:16:26 +02:00
while ( e ! = nullptr ) {
2023-09-17 12:30:17 +02:00
byref [ { t . value_int , e - > index_in_schema ( ) , attribute_index } ] . push_back ( id_from ) ;
e = e - > supertype ( ) ;
}
2011-05-08 11:04:32 +00:00
}
2011-07-11 09:33:18 +00:00
2021-08-22 12:22:44 +02:00
void IfcParse : : IfcFile : : register_inverse ( unsigned id_from , const IfcParse : : entity * from_entity , IfcUtil : : IfcBaseClass * inst , int attribute_index ) {
2023-09-17 12:30:17 +02:00
auto e = from_entity ;
byref_excl [ inst - > data ( ) . id ( ) ] . push_back ( id_from ) ;
2023-10-24 14:16:26 +02:00
while ( e ! = nullptr ) {
2023-09-17 12:30:17 +02:00
byref [ { inst - > data ( ) . id ( ) , e - > index_in_schema ( ) , attribute_index } ] . push_back ( id_from ) ;
e = e - > supertype ( ) ;
}
2017-07-03 14:18:56 +02:00
}
2021-08-22 12:22:44 +02:00
void IfcParse : : IfcFile : : unregister_inverse ( unsigned id_from , const IfcParse : : entity * from_entity , IfcUtil : : IfcBaseClass * inst , int attribute_index ) {
2023-09-17 12:30:17 +02:00
auto e = from_entity ;
2023-10-24 14:16:26 +02:00
while ( e ! = nullptr ) {
2023-09-17 12:30:17 +02:00
std : : vector < int > & ids = byref [ { inst - > data ( ) . id ( ) , e - > index_in_schema ( ) , attribute_index } ] ;
std : : vector < int > : : iterator it = std : : find ( ids . begin ( ) , ids . end ( ) , id_from ) ;
if ( it = = ids . end ( ) ) {
// @todo inverses also need to be populated when multiple instances are added to a new file.
// throw IfcParse::IfcException("Instance not found among inverses");
} else {
ids . erase ( it ) ;
}
e = e - > supertype ( ) ;
}
2021-09-23 13:46:37 +02:00
2023-09-17 12:30:17 +02:00
std : : vector < int > & ids = byref_excl [ inst - > data ( ) . id ( ) ] ;
std : : vector < int > : : iterator it = std : : find ( ids . begin ( ) , ids . end ( ) , id_from ) ;
if ( it = = ids . end ( ) ) {
// @todo inverses also need to be populated when multiple instances are added to a new file.
// throw IfcParse::IfcException("Instance not found among inverses");
} else {
ids . erase ( it ) ;
}
2017-07-03 14:18:56 +02:00
}
2011-07-11 09:33:18 +00:00
//
// Returns a string representation of the entity
// Note that this initializes the entity if it is not initialized
//
2017-06-05 17:26:58 +02:00
std : : string IfcEntityInstanceData : : toString ( bool upper ) const {
2023-09-17 12:30:17 +02:00
if ( attributes_ = = 0 ) {
load ( ) ;
}
2015-01-25 10:50:12 +00:00
2023-09-17 12:30:17 +02:00
std : : stringstream ss ;
ss . imbue ( std : : locale : : classic ( ) ) ;
2015-01-25 10:50:12 +00:00
2023-09-17 12:30:17 +02:00
std : : string dt ;
2023-10-24 14:16:26 +02:00
if ( type_ ! = nullptr ) {
2023-09-17 12:30:17 +02:00
dt = type ( ) - > name ( ) ;
if ( upper ) {
boost : : to_upper ( dt ) ;
}
2023-10-24 14:16:26 +02:00
if ( ( type ( ) - > as_entity ( ) ! = nullptr ) | | id_ ! = 0 ) {
2023-09-17 12:30:17 +02:00
ss < < " # " < < id_ < < " = " ;
}
}
ss < < dt < < " ( " ;
for ( size_t i = 0 ; i < getArgumentCount ( ) ; + + i ) {
if ( i ! = 0 ) {
ss < < " , " ;
}
if ( attributes_ [ i ] = = 0 ) {
ss < < " $ " ;
} else {
ss < < attributes_ [ i ] - > toString ( upper ) ;
}
}
ss < < " ) " ;
2015-01-25 10:50:12 +00:00
2023-09-17 12:30:17 +02:00
return ss . str ( ) ;
2011-05-08 11:04:32 +00:00
}
2011-07-11 09:33:18 +00:00
2023-09-17 12:30:17 +02:00
void IfcEntityInstanceData : : clearArguments ( ) {
if ( attributes_ ! = NULL ) {
for ( size_t i = 0 ; i < getArgumentCount ( ) ; + + i ) {
delete attributes_ [ i ] ;
}
delete [ ] attributes_ ;
attributes_ = NULL ;
}
2017-06-05 17:26:58 +02:00
}
2022-04-28 10:33:54 +02:00
IfcEntityInstanceData : : ~ IfcEntityInstanceData ( ) {
2023-09-17 12:30:17 +02:00
clearArguments ( ) ;
2022-04-28 10:33:54 +02:00
}
2017-06-05 17:26:58 +02:00
unsigned IfcEntityInstanceData : : set_id ( boost : : optional < unsigned > i ) {
2023-09-17 12:30:17 +02:00
if ( i ) {
return id_ = * i ;
}
2023-10-24 12:21:32 +02:00
return id_ = file - > FreshId ( ) ;
2011-09-03 13:58:59 +00:00
}
2011-07-11 09:33:18 +00:00
//
2015-01-10 22:13:52 +00:00
// Returns the entities of Entity type that have this entity in their ArgumentList
2011-07-11 09:33:18 +00:00
//
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcEntityInstanceData : : getInverse ( const IfcParse : : declaration * type , int attribute_index ) const {
2023-09-17 12:30:17 +02:00
static std : : mutex m ;
std : : lock_guard < std : : mutex > lk ( m ) ;
2019-07-03 12:10:27 +02:00
2023-09-17 12:30:17 +02:00
return file - > getInverse ( id_ , type , attribute_index ) ;
2012-07-18 19:32:58 +00:00
}
2017-06-24 21:35:38 +02:00
void IfcEntityInstanceData : : load ( ) const {
2023-09-17 12:30:17 +02:00
static std : : recursive_mutex m ;
std : : lock_guard < std : : recursive_mutex > lk ( m ) ;
Argument * * tmp_data = nullptr ;
if ( file - > parsing_complete ( ) ) {
// only when parsing is fully complete we need to seek to the instance, otherwise
// we know the token cursor is currently at the keyword token
file - > seek_to ( * this ) ;
} else {
// Apparently the load() function assumes one token later after the opening parenthesis
file - > tokens - > Next ( ) ;
}
2021-09-13 13:17:08 +02:00
2023-09-17 12:30:17 +02:00
// type_ is 0 for header entities which have their size predetermined in code
// in that we have attributes_ pre-constructed to the correct size in the constructor
// in the other case load() will use a vector internally to grow to the size found in the file
2023-10-24 14:16:26 +02:00
size_t n = file - > load ( id ( ) , type_ ! = nullptr ? type_ - > as_entity ( ) : nullptr , type_ ! = nullptr ? tmp_data : attributes_ , getArgumentCount ( ) ) ;
2023-09-17 12:30:17 +02:00
if ( n ! = getArgumentCount ( ) ) {
Logger : : Error ( " Wrong number of attributes on instance with id # " + std : : to_string ( id_ ) +
" at offset " + std : : to_string ( this - > offset_in_file ( ) ) +
" expected " + std : : to_string ( getArgumentCount ( ) ) +
" got " + std : : to_string ( n ) ) ;
}
2022-10-03 14:03:04 +02:00
2023-09-17 12:30:17 +02:00
file - > try_read_semicolon ( ) ;
// @todo does this need to be atomic somehow?
2023-10-24 14:16:26 +02:00
if ( tmp_data ! = nullptr ) {
2023-09-17 12:30:17 +02:00
attributes_ = tmp_data ;
}
2017-06-05 17:26:58 +02:00
}
2021-11-19 09:38:28 +01:00
namespace {
2023-09-17 12:30:17 +02:00
// @todo remove redundancy with python wrapper code (which is not identical due to
// different handling of enumerations)
IfcUtil : : ArgumentType get_argument_type ( const IfcParse : : declaration * decl , size_t i ) {
const IfcParse : : parameter_type * pt = 0 ;
2023-10-24 14:16:26 +02:00
if ( decl - > as_entity ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
pt = decl - > as_entity ( ) - > attribute_by_index ( i ) - > type_of_attribute ( ) ;
if ( decl - > as_entity ( ) - > derived ( ) [ i ] ) {
return IfcUtil : : Argument_DERIVED ;
}
2023-10-24 14:16:26 +02:00
} else if ( ( decl - > as_type_declaration ( ) ! = nullptr ) & & i = = 0 ) {
2023-09-17 12:30:17 +02:00
pt = decl - > as_type_declaration ( ) - > declared_type ( ) ;
2023-10-24 14:16:26 +02:00
} else if ( ( decl - > as_enumeration_type ( ) ! = nullptr ) & & i = = 0 ) {
2023-09-17 12:30:17 +02:00
return IfcUtil : : Argument_ENUMERATION ;
}
if ( pt = = 0 ) {
return IfcUtil : : Argument_UNKNOWN ;
}
2023-10-24 12:21:32 +02:00
return IfcUtil : : from_parameter_type ( pt ) ;
2021-11-19 09:38:28 +01:00
}
2023-09-17 12:30:17 +02:00
} // namespace
2021-11-19 09:38:28 +01:00
2017-06-05 17:26:58 +02:00
IfcEntityInstanceData : : IfcEntityInstanceData ( const IfcEntityInstanceData & e ) {
2023-09-17 12:30:17 +02:00
file = 0 ;
type_ = e . type_ ;
id_ = 0 ;
2017-06-05 17:26:58 +02:00
2023-09-17 12:30:17 +02:00
const size_t count = e . getArgumentCount ( ) ;
2018-01-29 12:11:50 +01:00
2023-09-17 12:30:17 +02:00
// In order not to have the instance read from file
attributes_ = new Argument * [ count ] ;
2017-08-02 16:09:01 +02:00
2023-09-17 12:30:17 +02:00
for ( unsigned int i = 0 ; i < count ; + + i ) {
attributes_ [ i ] = 0 ;
this - > setArgument ( i , e . getArgument ( i ) , get_argument_type ( e . type ( ) , i ) , true ) ;
}
2017-06-05 17:26:58 +02:00
}
2018-08-29 12:54:25 +02:00
static IfcParse : : NullArgument static_null_attribute ;
2017-06-05 17:26:58 +02:00
2021-05-13 21:11:19 +02:00
Argument * IfcEntityInstanceData : : getArgument ( size_t i ) const {
2023-09-17 12:30:17 +02:00
if ( attributes_ = = 0 ) {
load ( ) ;
}
if ( i < getArgumentCount ( ) ) {
if ( attributes_ [ i ] = = nullptr ) {
return & static_null_attribute ;
}
2023-10-24 12:21:32 +02:00
return attributes_ [ i ] ;
2023-09-17 12:30:17 +02:00
}
2023-10-24 12:21:32 +02:00
throw IfcParse : : IfcException ( " Attribute index out of range " ) ;
2017-06-05 17:26:58 +02:00
}
2017-07-03 14:18:56 +02:00
class unregister_inverse_visitor {
2023-09-17 12:30:17 +02:00
private :
IfcFile & file_ ;
const IfcEntityInstanceData & data_ ;
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
public :
unregister_inverse_visitor ( IfcFile & file , const IfcEntityInstanceData & data )
: file_ ( file ) ,
data_ ( data ) { }
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
void operator ( ) ( IfcUtil : : IfcBaseClass * inst , int index ) {
file_ . unregister_inverse ( data_ . id ( ) , data_ . type ( ) - > as_entity ( ) , inst , index ) ;
}
2017-07-03 14:18:56 +02:00
} ;
class register_inverse_visitor {
2023-09-17 12:30:17 +02:00
private :
IfcFile & file_ ;
const IfcEntityInstanceData & data_ ;
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
public :
register_inverse_visitor ( IfcFile & file , const IfcEntityInstanceData & data )
: file_ ( file ) ,
data_ ( data ) { }
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
void operator ( ) ( IfcUtil : : IfcBaseClass * inst , int index ) {
file_ . register_inverse ( data_ . id ( ) , data_ . type ( ) - > as_entity ( ) , inst , index ) ;
}
2017-07-03 14:18:56 +02:00
} ;
class add_to_instance_list_visitor {
2023-09-17 12:30:17 +02:00
private :
aggregate_of_instance : : ptr & list_ ;
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
public :
add_to_instance_list_visitor ( aggregate_of_instance : : ptr & list )
: list_ ( list ) { }
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
void operator ( ) ( IfcUtil : : IfcBaseClass * inst ) {
list_ - > push ( inst ) ;
}
2017-07-03 14:18:56 +02:00
} ;
class apply_individual_instance_visitor {
2023-09-17 12:30:17 +02:00
private :
Argument * attribute_ ;
IfcEntityInstanceData * data_ ;
int attribute_index_ ;
template < typename T >
void apply_attribute_ ( T & t , Argument * attr , int index ) const {
if ( ! attr ) {
return ;
}
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
if ( attr - > type ( ) = = IfcUtil : : Argument_ENTITY_INSTANCE ) {
IfcUtil : : IfcBaseClass * inst = * attr ;
t ( inst , index ) ;
} else if ( attr - > type ( ) = = IfcUtil : : Argument_AGGREGATE_OF_ENTITY_INSTANCE ) {
aggregate_of_instance : : ptr entity_list_attribute = * attr ;
for ( aggregate_of_instance : : it it = entity_list_attribute - > begin ( ) ; it ! = entity_list_attribute - > end ( ) ; + + it ) {
t ( * it , index ) ;
}
} else if ( attr - > type ( ) = = IfcUtil : : Argument_AGGREGATE_OF_AGGREGATE_OF_ENTITY_INSTANCE ) {
aggregate_of_aggregate_of_instance : : ptr entity_list_attribute = * attr ;
for ( aggregate_of_aggregate_of_instance : : outer_it it = entity_list_attribute - > begin ( ) ; it ! = entity_list_attribute - > end ( ) ; + + it ) {
for ( aggregate_of_aggregate_of_instance : : inner_it jt = it - > begin ( ) ; jt ! = it - > end ( ) ; + + jt ) {
t ( * jt , index ) ;
}
}
}
} ;
public :
apply_individual_instance_visitor ( Argument * attribute , int idx )
: attribute_ ( attribute ) ,
data_ ( 0 ) ,
attribute_index_ ( idx ) { }
apply_individual_instance_visitor ( IfcEntityInstanceData * data )
: attribute_ ( 0 ) ,
data_ ( data ) { }
template < typename T >
void apply ( T & t ) const {
if ( attribute_ ) {
apply_attribute_ ( t , attribute_ , attribute_index_ ) ;
} else {
for ( size_t i = 0 ; i < data_ - > getArgumentCount ( ) ; + + i ) {
Argument * attr = data_ - > getArgument ( i ) ;
apply_attribute_ ( t , attr , i ) ;
}
}
} ;
2017-07-03 14:18:56 +02:00
} ;
2022-04-28 10:33:54 +02:00
void IfcEntityInstanceData : : setArgument ( size_t i , Argument * a , IfcUtil : : ArgumentType attr_type , bool make_copy ) {
2023-09-17 12:30:17 +02:00
if ( attributes_ = = 0 ) {
load ( ) ;
}
Argument * new_attribute = a ;
if ( make_copy ) {
if ( attr_type = = IfcUtil : : Argument_UNKNOWN ) {
attr_type = a - > type ( ) ;
} else if ( a - > isNull ( ) ) {
attr_type = IfcUtil : : Argument_NULL ;
}
2017-08-02 16:09:01 +02:00
2023-09-17 12:30:17 +02:00
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
switch ( attr_type ) {
case IfcUtil : : Argument_NULL :
copy - > set ( boost : : blank ( ) ) ;
break ;
case IfcUtil : : Argument_DERIVED :
copy - > set ( IfcWrite : : IfcWriteArgument : : Derived ( ) ) ;
break ;
case IfcUtil : : Argument_INT :
copy - > set ( static_cast < int > ( * a ) ) ;
break ;
case IfcUtil : : Argument_BOOL :
copy - > set ( static_cast < bool > ( * a ) ) ;
break ;
case IfcUtil : : Argument_LOGICAL : {
boost : : logic : : tribool tb = * a ;
copy - > set ( tb ) ;
break ;
}
case IfcUtil : : Argument_DOUBLE :
copy - > set ( static_cast < double > ( * a ) ) ;
break ;
case IfcUtil : : Argument_STRING :
copy - > set ( static_cast < std : : string > ( * a ) ) ;
break ;
case IfcUtil : : Argument_BINARY : {
boost : : dynamic_bitset < > attr_value = * a ;
copy - > set ( attr_value ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_INT : {
std : : vector < int > attr_value = * a ;
copy - > set ( attr_value ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_DOUBLE : {
std : : vector < double > attr_value = * a ;
copy - > set ( attr_value ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_STRING : {
std : : vector < std : : string > attr_value = * a ;
copy - > set ( attr_value ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_BINARY : {
std : : vector < boost : : dynamic_bitset < > > attr_value = * a ;
copy - > set ( attr_value ) ;
break ;
}
case IfcUtil : : Argument_ENUMERATION : {
std : : string enum_literal = a - > toString ( ) ;
// Remove leading and trailing '.'
enum_literal = enum_literal . substr ( 1 , enum_literal . size ( ) - 2 ) ;
2023-10-24 14:16:26 +02:00
const IfcParse : : enumeration_type * enum_type = type ( ) - > as_enumeration_type ( ) ! = nullptr
2023-09-17 12:30:17 +02:00
? type ( ) - > as_enumeration_type ( )
: type ( ) - > as_entity ( ) - > attribute_by_index ( i ) - > type_of_attribute ( ) - > as_named_type ( ) - > declared_type ( ) - > as_enumeration_type ( ) ;
std : : vector < std : : string > : : const_iterator it = std : : find (
enum_type - > enumeration_items ( ) . begin ( ) ,
enum_type - > enumeration_items ( ) . end ( ) ,
enum_literal ) ;
if ( it = = enum_type - > enumeration_items ( ) . end ( ) ) {
throw IfcParse : : IfcException ( enum_literal + " does not name a valid item for " + enum_type - > name ( ) ) ;
}
copy - > set ( IfcWrite : : IfcWriteArgument : : EnumerationReference ( it - enum_type - > enumeration_items ( ) . begin ( ) , it - > c_str ( ) ) ) ;
break ;
}
case IfcUtil : : Argument_ENTITY_INSTANCE : {
copy - > set ( static_cast < IfcUtil : : IfcBaseClass * > ( * a ) ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_ENTITY_INSTANCE : {
aggregate_of_instance : : ptr instances = * a ;
aggregate_of_instance : : ptr mapped_instances ( new aggregate_of_instance ) ;
// @todo mapped_instances are not actually mapped to the file using add().
for ( aggregate_of_instance : : it it = instances - > begin ( ) ; it ! = instances - > end ( ) ; + + it ) {
mapped_instances - > push ( * it ) ;
}
copy - > set ( mapped_instances ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_AGGREGATE_OF_INT : {
std : : vector < std : : vector < int > > attr_value = * a ;
copy - > set ( attr_value ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_AGGREGATE_OF_DOUBLE : {
std : : vector < std : : vector < double > > attr_value = * a ;
copy - > set ( attr_value ) ;
break ;
}
case IfcUtil : : Argument_AGGREGATE_OF_AGGREGATE_OF_ENTITY_INSTANCE : {
aggregate_of_aggregate_of_instance : : ptr instances = * a ;
aggregate_of_aggregate_of_instance : : ptr mapped_instances ( new aggregate_of_aggregate_of_instance ) ;
for ( aggregate_of_aggregate_of_instance : : outer_it it = instances - > begin ( ) ; it ! = instances - > end ( ) ; + + it ) {
std : : vector < IfcUtil : : IfcBaseClass * > inner ;
for ( aggregate_of_aggregate_of_instance : : inner_it jt = it - > begin ( ) ; jt ! = it - > end ( ) ; + + jt ) {
inner . push_back ( * jt ) ;
}
mapped_instances - > push ( inner ) ;
}
copy - > set ( mapped_instances ) ;
break ;
}
case IfcUtil : : Argument_EMPTY_AGGREGATE :
case IfcUtil : : Argument_AGGREGATE_OF_EMPTY_AGGREGATE : {
IfcUtil : : ArgumentType t2 = IfcUtil : : from_parameter_type ( type ( ) - > as_entity ( ) - > attribute_by_index ( i ) - > type_of_attribute ( ) ) ;
delete copy ;
copy = 0 ;
setArgument ( i , a , t2 , make_copy ) ;
break ;
}
default :
case IfcUtil : : Argument_UNKNOWN :
throw IfcParse : : IfcException ( std : : string ( " Unknown attribute encountered: ' " ) + a - > toString ( ) + " ' at index ' " + boost : : lexical_cast < std : : string > ( i ) + " ' " ) ;
break ;
}
2017-07-03 14:18:56 +02:00
2023-10-24 14:16:26 +02:00
if ( copy = = nullptr ) {
2023-09-17 12:30:17 +02:00
return ;
}
2017-07-03 14:18:56 +02:00
2023-09-17 12:30:17 +02:00
new_attribute = copy ;
}
if ( attributes_ [ i ] ! = 0 ) {
Argument * current_attribute = attributes_ [ i ] ;
2023-10-24 14:16:26 +02:00
if ( this - > file ! = nullptr ) {
2023-09-17 12:30:17 +02:00
// Deregister old attribute guid in file guid map.
2023-10-24 14:16:26 +02:00
if ( i = = 0 & & ( this - > type ( ) ! = nullptr ) & & ( this - > file - > ifcroot_type ( ) ! = nullptr ) & & this - > type ( ) - > is ( * this - > file - > ifcroot_type ( ) ) ) {
2023-09-17 12:30:17 +02:00
try {
auto guid = ( std : : string ) * current_attribute ;
auto it = this - > file - > internal_guid_map ( ) . find ( guid ) ;
if ( it ! = this - > file - > internal_guid_map ( ) . end ( ) & & & it - > second - > data ( ) = = this ) {
this - > file - > internal_guid_map ( ) . erase ( it ) ;
}
} catch ( IfcParse : : IfcException & e ) {
Logger : : Error ( e ) ;
}
}
// Deregister inverse indices in file
unregister_inverse_visitor visitor ( * this - > file , * this ) ;
apply_individual_instance_visitor ( current_attribute , i ) . apply ( visitor ) ;
}
delete attributes_ [ i ] ;
}
2023-10-24 14:16:26 +02:00
if ( this - > file ! = nullptr ) {
2023-09-17 12:30:17 +02:00
// Register inverse indices in file
register_inverse_visitor visitor ( * this - > file , * this ) ;
apply_individual_instance_visitor ( new_attribute , i ) . apply ( visitor ) ;
}
attributes_ [ i ] = new_attribute ;
// Register new attribute guid in guid map
2023-10-24 14:16:26 +02:00
if ( this - > file ! = nullptr ) {
if ( i = = 0 & & ( this - > type ( ) ! = nullptr ) & & ( this - > file - > ifcroot_type ( ) ! = nullptr ) & & this - > type ( ) - > is ( * this - > file - > ifcroot_type ( ) ) ) {
2023-09-17 12:30:17 +02:00
try {
auto guid = ( std : : string ) * new_attribute ;
auto it = this - > file - > internal_guid_map ( ) . find ( guid ) ;
if ( it ! = this - > file - > internal_guid_map ( ) . end ( ) ) {
Logger : : Warning ( " Duplicate guid " + guid ) ;
}
this - > file - > internal_guid_map ( ) [ guid ] = this - > file - > instance_by_id ( this - > id ( ) ) ;
} catch ( IfcParse : : IfcException & e ) {
Logger : : Error ( e ) ;
}
}
}
2017-06-05 17:26:58 +02:00
}
2011-07-11 09:33:18 +00:00
//
// Parses the IFC file in fn
// Creates the maps
//
2017-07-23 14:52:04 +02:00
# ifdef USE_MMAP
2017-12-12 10:33:24 +01:00
IfcFile : : IfcFile ( const std : : string & fn , bool mmap ) {
2023-09-17 12:30:17 +02:00
initialize_ ( new IfcSpfStream ( fn , mmap ) ) ;
2017-07-23 14:52:04 +02:00
}
# else
2017-12-12 10:33:24 +01:00
IfcFile : : IfcFile ( const std : : string & fn ) {
2023-09-17 12:30:17 +02:00
initialize_ ( new IfcSpfStream ( fn ) ) ;
2011-08-20 09:08:56 +00:00
}
2017-07-23 14:52:04 +02:00
# endif
2017-08-22 10:16:07 +02:00
2017-12-12 10:33:24 +01:00
IfcFile : : IfcFile ( std : : istream & f , int len ) {
2023-09-17 12:30:17 +02:00
initialize_ ( new IfcSpfStream ( f , len ) ) ;
2011-08-20 09:08:56 +00:00
}
2015-02-17 19:48:41 +00:00
2017-12-12 10:33:24 +01:00
IfcFile : : IfcFile ( void * data , int len ) {
2023-09-17 12:30:17 +02:00
initialize_ ( new IfcSpfStream ( data , len ) ) ;
2017-12-12 10:33:24 +01:00
}
IfcFile : : IfcFile ( IfcParse : : IfcSpfStream * s ) {
2023-09-17 12:30:17 +02:00
initialize_ ( s ) ;
2017-12-12 10:33:24 +01:00
}
IfcFile : : IfcFile ( const IfcParse : : schema_definition * schema )
2023-09-17 12:30:17 +02:00
: parsing_complete_ ( true ) ,
schema_ ( schema ) ,
ifcroot_type_ ( schema_ - > declaration_by_name ( " IfcRoot " ) ) ,
MaxId ( 0 ) ,
tokens ( 0 ) ,
stream ( 0 ) {
setDefaultHeaderValues ( ) ;
2011-10-27 16:13:29 +00:00
}
2015-02-17 19:48:41 +00:00
2017-12-12 10:33:24 +01:00
void IfcFile : : initialize_ ( IfcParse : : IfcSpfStream * s ) {
2023-09-17 12:30:17 +02:00
// Initialize a "C" locale for locale-independent
// number parsing. See comment above on line 41.
init_locale ( ) ;
// prevent heap allocations during parse
internal_attribute_vector_ . reserve ( 64 ) ;
internal_attribute_vector_simple_type_ . reserve ( 16 ) ;
parsing_complete_ = false ;
MaxId = 0 ;
tokens = 0 ;
stream = 0 ;
schema_ = 0 ;
setDefaultHeaderValues ( ) ;
stream = s ;
if ( ! stream - > valid ) {
good_ = file_open_status : : READ_ERROR ;
return ;
}
2015-01-05 17:46:23 +00:00
2023-09-17 12:30:17 +02:00
tokens = new IfcSpfLexer ( stream , this ) ;
2017-08-01 16:45:11 +02:00
2023-09-17 12:30:17 +02:00
std : : vector < std : : string > schemas ;
2017-12-12 10:33:24 +01:00
2023-09-17 12:30:17 +02:00
_header . file ( this ) ;
if ( _header . tryRead ( ) ) {
try {
schemas = _header . file_schema ( ) . schema_identifiers ( ) ;
} catch ( . . . ) {
// Purposely empty catch block
}
} else {
good_ = file_open_status : : NO_HEADER ;
}
if ( schemas . size ( ) = = 1 ) {
try {
schema_ = IfcParse : : schema_by_name ( schemas . front ( ) ) ;
} catch ( const IfcParse : : IfcException & e ) {
good_ = file_open_status : : UNSUPPORTED_SCHEMA ;
Logger : : Error ( e ) ;
}
}
if ( schema_ = = 0 ) {
Logger : : Message ( Logger : : LOG_ERROR , " No support for file schema encountered ( " + boost : : algorithm : : join ( schemas , " , " ) + " ) " ) ;
return ;
}
ifcroot_type_ = schema_ - > declaration_by_name ( " IfcRoot " ) ;
boost : : circular_buffer < Token > token_stream ( 3 , Token ( ) ) ;
IfcEntityInstanceData * data ;
IfcUtil : : IfcBaseClass * instance = 0 ;
unsigned current_id = 0 ;
int progress = 0 ;
Logger : : Status ( " Scanning file... " ) ;
2015-01-05 17:46:23 +00:00
2023-09-17 12:30:17 +02:00
int paren_stack_depth = 0 ;
int attribute_index = - 1 ;
while ( ! stream - > eof ) {
if ( token_stream [ 0 ] . type = = IfcParse : : Token_IDENTIFIER & &
token_stream [ 1 ] . type = = IfcParse : : Token_OPERATOR & &
token_stream [ 1 ] . value_char = = ' = ' & &
token_stream [ 2 ] . type = = IfcParse : : Token_KEYWORD ) {
attribute_index = 0 ;
current_id = ( unsigned ) TokenFunc : : asIdentifier ( token_stream [ 0 ] ) ;
const IfcParse : : declaration * entity_type ;
try {
entity_type = schema_ - > declaration_by_name ( TokenFunc : : asStringRef ( token_stream [ 2 ] ) ) ;
} catch ( const IfcException & ex ) {
Logger : : Message ( Logger : : LOG_ERROR , std : : string ( ex . what ( ) ) + " at offset " + std : : to_string ( token_stream [ 2 ] . startPos ) ) ;
goto advance ;
}
data = new IfcEntityInstanceData ( entity_type , this , current_id , token_stream [ 2 ] . startPos ) ;
instance = schema ( ) - > instantiate ( data ) ;
2017-06-05 17:26:58 +02:00
2017-05-23 20:06:35 +03:00
/// @todo Printing to stdout in a library class feels weird. Maybe move the progress prints to the client code?
2023-09-17 12:30:17 +02:00
// Update the status after every 1000 instances parsed
2023-10-24 14:16:26 +02:00
if ( ( ( + + progress ) % 1000 ) = = 0 ) {
2023-09-17 12:30:17 +02:00
std : : stringstream ss ;
ss < < " \r # " < < current_id ;
Logger : : Status ( ss . str ( ) , false ) ;
}
if ( ! lazy_load_ ) {
data - > load ( ) ;
}
if ( instance - > declaration ( ) . is ( * ifcroot_type_ ) ) {
try {
const std : : string guid = * instance - > data ( ) . getArgument ( 0 ) ;
if ( byguid . find ( guid ) ! = byguid . end ( ) ) {
std : : stringstream ss ;
ss < < " Instance encountered with non-unique GlobalId " < < guid ;
Logger : : Message ( Logger : : LOG_WARNING , ss . str ( ) ) ;
}
byguid [ guid ] = instance ;
} catch ( const IfcException & ex ) {
Logger : : Message ( Logger : : LOG_ERROR , ex . what ( ) ) ;
}
// this has consumed the instance tokens, set stack depth to 0
paren_stack_depth = 0 ;
attribute_index = - 1 ;
}
const IfcParse : : declaration * ty = & instance - > declaration ( ) ;
{
aggregate_of_instance : : ptr insts = instances_by_type_excl_subtypes ( ty ) ;
if ( ! insts ) {
insts = aggregate_of_instance : : ptr ( new aggregate_of_instance ( ) ) ;
bytype_excl [ ty ] = insts ;
}
insts - > push ( instance ) ;
}
for ( ; ; ) {
aggregate_of_instance : : ptr insts = instances_by_type ( ty ) ;
if ( ! insts ) {
insts = aggregate_of_instance : : ptr ( new aggregate_of_instance ( ) ) ;
bytype [ ty ] = insts ;
}
insts - > push ( instance ) ;
const IfcParse : : declaration * pt = ty - > as_entity ( ) - > supertype ( ) ;
2023-10-24 14:16:26 +02:00
if ( pt ! = nullptr ) {
2023-09-17 12:30:17 +02:00
ty = pt ;
} else {
break ;
}
}
if ( byid . find ( current_id ) ! = byid . end ( ) ) {
std : : stringstream ss ;
ss < < " Overwriting instance with name # " < < current_id ;
Logger : : Message ( Logger : : LOG_WARNING , ss . str ( ) ) ;
}
byid [ current_id ] = instance ;
MaxId = ( std : : max ) ( MaxId , current_id ) ;
2023-10-24 14:16:26 +02:00
} else if ( token_stream [ 0 ] . type = = IfcParse : : Token_IDENTIFIER & & ( instance ! = nullptr ) ) {
2023-09-17 12:30:17 +02:00
register_inverse ( current_id , instance - > declaration ( ) . as_entity ( ) , token_stream [ 0 ] , attribute_index ) ;
} else if ( token_stream [ 0 ] . type = = IfcParse : : Token_OPERATOR & & token_stream [ 0 ] . value_char = = ' ( ' ) {
paren_stack_depth + + ;
} else if ( token_stream [ 0 ] . type = = IfcParse : : Token_OPERATOR & & token_stream [ 0 ] . value_char = = ' ) ' ) {
paren_stack_depth - - ;
if ( paren_stack_depth = = 0 ) {
attribute_index = - 1 ;
}
} else if ( paren_stack_depth = = 1 & & token_stream [ 0 ] . type = = IfcParse : : Token_OPERATOR & & token_stream [ 0 ] . value_char = = ' , ' ) {
attribute_index + + ;
}
advance :
Token next_token ;
try {
next_token = tokens - > Next ( ) ;
} catch ( const IfcException & e ) {
Logger : : Message ( Logger : : LOG_ERROR , std : : string ( e . what ( ) ) + " . Parsing terminated " ) ;
} catch ( . . . ) {
Logger : : Message ( Logger : : LOG_ERROR , " Parsing terminated " ) ;
}
if ( next_token . type = = Token_NONE ) {
break ;
}
2017-06-05 17:26:58 +02:00
2023-09-17 12:30:17 +02:00
token_stream . push_back ( next_token ) ;
}
2017-06-05 17:26:58 +02:00
2023-09-17 12:30:17 +02:00
Logger : : Status ( " \r Done scanning file " ) ;
2017-06-05 17:26:58 +02:00
2023-09-17 12:30:17 +02:00
parsing_complete_ = true ;
return ;
2011-07-11 09:33:18 +00:00
}
2015-02-17 19:48:41 +00:00
2021-06-26 13:23:55 +02:00
void IfcFile : : recalculate_id_counter ( ) {
2023-09-17 12:30:17 +02:00
entity_by_id_t : : key_type k = 0 ;
for ( auto & p : byid ) {
if ( p . first > k ) {
k = p . first ;
}
}
MaxId = ( unsigned int ) k ;
2021-06-26 13:23:55 +02:00
}
2021-08-12 13:35:45 +02:00
class traversal_recorder {
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr list_ ;
std : : map < int , aggregate_of_instance : : ptr > instances_by_level_ ;
int mode_ ;
public :
traversal_recorder ( int mode ) : mode_ ( mode ) {
if ( mode = = 0 ) {
list_ . reset ( new aggregate_of_instance ) ;
}
} ;
void push_back ( int level , IfcUtil : : IfcBaseClass * instance ) {
if ( mode_ = = 0 ) {
list_ - > push ( instance ) ;
} else {
auto & l = instances_by_level_ [ level ] ;
if ( ! l ) {
l . reset ( new aggregate_of_instance ) ;
}
l - > push ( instance ) ;
}
}
2021-08-12 13:35:45 +02:00
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr get_list ( ) const {
if ( mode_ = = 0 ) {
return list_ ;
}
2023-10-24 12:21:32 +02:00
aggregate_of_instance : : ptr l ( new aggregate_of_instance ) ;
for ( auto & p : instances_by_level_ ) {
l - > push ( p . second ) ;
}
return l ;
2023-09-17 12:30:17 +02:00
}
2021-08-12 13:35:45 +02:00
} ;
2017-08-01 19:07:46 +02:00
class traversal_visitor {
2023-09-17 12:30:17 +02:00
private :
std : : set < IfcUtil : : IfcBaseClass * > & visited_ ;
traversal_recorder & list_ ;
int level_ ;
int max_level_ ;
public :
traversal_visitor ( std : : set < IfcUtil : : IfcBaseClass * > & visited , traversal_recorder & list , int level , int max_level )
: visited_ ( visited ) ,
list_ ( list ) ,
level_ ( level ) ,
max_level_ ( max_level ) { }
void operator ( ) ( IfcUtil : : IfcBaseClass * inst , int index ) ;
2017-08-01 19:07:46 +02:00
} ;
2021-08-12 13:35:45 +02:00
void traverse_ ( IfcUtil : : IfcBaseClass * instance , std : : set < IfcUtil : : IfcBaseClass * > & visited , traversal_recorder & list , int level , int max_level ) {
2023-09-17 12:30:17 +02:00
if ( visited . find ( instance ) ! = visited . end ( ) ) {
return ;
}
visited . insert ( instance ) ;
list . push_back ( level , instance ) ;
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
if ( level > = max_level & & max_level > 0 ) {
return ;
}
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
traversal_visitor visit ( visited , list , level + 1 , max_level ) ;
apply_individual_instance_visitor ( & instance - > data ( ) ) . apply ( visit ) ;
2015-02-17 19:48:41 +00:00
}
2021-08-22 12:22:44 +02:00
void traversal_visitor : : operator ( ) ( IfcUtil : : IfcBaseClass * inst , int /* index */ ) {
2023-09-17 12:30:17 +02:00
traverse_ ( inst , visited_ , list_ , level_ , max_level_ ) ;
2017-08-01 19:07:46 +02:00
}
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcParse : : traverse ( IfcUtil : : IfcBaseClass * instance , int max_level ) {
2023-09-17 12:30:17 +02:00
std : : set < IfcUtil : : IfcBaseClass * > visited ;
traversal_recorder r ( 0 ) ;
traverse_ ( instance , visited , r , 0 , max_level ) ;
return r . get_list ( ) ;
2021-08-12 13:35:45 +02:00
}
// I'm cheating this isn't breadth-first, but rather we record visited instances
// keeping track of their rank and return a list ordered by rank. Is this equivalent?
2021-08-15 20:40:55 +02:00
aggregate_of_instance : : ptr IfcParse : : traverse_breadth_first ( IfcUtil : : IfcBaseClass * instance , int max_level ) {
2023-09-17 12:30:17 +02:00
std : : set < IfcUtil : : IfcBaseClass * > visited ;
traversal_recorder r ( 1 ) ;
traverse_ ( instance , visited , r , 0 , max_level ) ;
return r . get_list ( ) ;
2015-02-17 19:48:41 +00:00
}
2017-06-20 18:54:30 +02:00
/// @note: for backwards compatibility
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcFile : : traverse ( IfcUtil : : IfcBaseClass * instance , int max_level ) {
2023-09-17 12:30:17 +02:00
return IfcParse : : traverse ( instance , max_level ) ;
2017-06-20 18:54:30 +02:00
}
2021-08-12 13:35:45 +02:00
/// @note: for backwards compatibility
2021-08-15 20:40:55 +02:00
aggregate_of_instance : : ptr IfcFile : : traverse_breadth_first ( IfcUtil : : IfcBaseClass * instance , int max_level ) {
2023-09-17 12:30:17 +02:00
return IfcParse : : traverse_breadth_first ( instance , max_level ) ;
2021-08-12 13:35:45 +02:00
}
2021-07-29 14:54:51 +02:00
void IfcFile : : addEntities ( aggregate_of_instance : : ptr es ) {
2023-09-17 12:30:17 +02:00
for ( aggregate_of_instance : : it i = es - > begin ( ) ; i ! = es - > end ( ) ; + + i ) {
addEntity ( * i ) ;
}
2012-08-11 13:24:08 +00:00
}
2015-02-17 19:48:41 +00:00
2021-06-27 11:14:26 +02:00
IfcUtil : : IfcBaseClass * IfcFile : : addEntity ( IfcUtil : : IfcBaseClass * entity , int id ) {
2023-09-17 12:30:17 +02:00
if ( id ! = - 1 & & byid . find ( ( unsigned ) id ) ! = byid . end ( ) ) {
throw IfcParse : : IfcException ( " An instance with id " + boost : : lexical_cast < std : : string > ( id ) + " is already part of this file " ) ;
}
2021-06-27 11:14:26 +02:00
2023-09-17 12:30:17 +02:00
if ( entity - > declaration ( ) . schema ( ) ! = schema ( ) ) {
throw IfcParse : : IfcException ( " Unabled to add instance from " + entity - > declaration ( ) . schema ( ) - > name ( ) + " schema to file with " + schema ( ) - > name ( ) + " schema " ) ;
}
2018-11-04 15:16:08 +01:00
2023-09-17 12:30:17 +02:00
// If this instance has been inserted before, return
// a reference to the copy that was created from it.
entity_entity_map_t : : iterator mit = entity_file_map . find ( entity - > identity ( ) ) ;
if ( mit ! = entity_file_map . end ( ) ) {
return mit - > second ;
}
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
IfcUtil : : IfcBaseClass * new_entity = entity ;
// Obtain all forward references by a depth-first
// traversal and add them to the file.
if ( parsing_complete_ ) {
try {
aggregate_of_instance : : ptr entity_attributes = traverse ( entity , 1 ) ;
for ( aggregate_of_instance : : it it = entity_attributes - > begin ( ) ; it ! = entity_attributes - > end ( ) ; + + it ) {
if ( * it ! = entity ) {
entity_entity_map_t : : iterator mit2 = entity_file_map . find ( ( * it ) - > identity ( ) ) ;
if ( mit2 = = entity_file_map . end ( ) ) {
entity_file_map . insert ( entity_entity_map_t : : value_type ( ( * it ) - > identity ( ) , addEntity ( * it ) ) ) ;
}
}
}
} catch ( . . . ) {
Logger : : Message ( Logger : : LOG_ERROR , " Failed to visit forward references of " , entity ) ;
}
}
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
// See whether the instance is already part of a file
if ( entity - > data ( ) . file ! = 0 ) {
if ( entity - > data ( ) . file = = this ) {
2023-10-24 14:16:26 +02:00
if ( entity - > declaration ( ) . as_entity ( ) = = nullptr ) {
2023-09-17 12:30:17 +02:00
// While not a mapping that can be queried, we do need to free the instance later on
byidentity [ new_entity - > identity ( ) ] = new_entity ;
}
// If it is part of this file
// nothing else needs to be done.
return entity ;
}
// An instance is being added from another file. A copy of the
// container and entity is created. The attribute references
// need to be updated to point to instances in this file.
IfcFile * other_file = entity - > data ( ) . file ;
IfcEntityInstanceData * we = new IfcEntityInstanceData ( entity - > data ( ) ) ;
new_entity = schema ( ) - > instantiate ( we ) ;
// In case an entity is added that contains geometry, the unit
// information needs to be accounted for for IfcLengthMeasures.
2016-07-15 06:49:32 +03:00
double conversion_factor = std : : numeric_limits < double > : : quiet_NaN ( ) ;
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
for ( size_t i = 0 ; i < we - > getArgumentCount ( ) ; + + i ) {
Argument * attr = we - > getArgument ( i ) ;
IfcUtil : : ArgumentType attr_type = attr - > type ( ) ;
IfcParse : : declaration * decl = 0 ;
2023-10-24 14:16:26 +02:00
if ( entity - > declaration ( ) . as_entity ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
decl = 0 ;
const parameter_type * pt = entity - > declaration ( ) . as_entity ( ) - > attribute_by_index ( i ) - > type_of_attribute ( ) ;
2023-10-24 14:16:26 +02:00
while ( pt - > as_aggregation_type ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
pt = pt - > as_aggregation_type ( ) - > type_of_element ( ) ;
}
2023-10-24 14:16:26 +02:00
if ( pt - > as_named_type ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
decl = pt - > as_named_type ( ) - > declared_type ( ) ;
}
}
if ( attr_type = = IfcUtil : : Argument_ENTITY_INSTANCE ) {
entity_entity_map_t : : const_iterator eit = entity_file_map . find ( ( ( IfcUtil : : IfcBaseClass * ) ( * attr ) ) - > identity ( ) ) ;
if ( eit = = entity_file_map . end ( ) ) {
throw IfcParse : : IfcException ( " Unable to map instance to file " ) ;
}
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( eit - > second ) ;
we - > setArgument ( i , copy ) ;
} else if ( attr_type = = IfcUtil : : Argument_AGGREGATE_OF_ENTITY_INSTANCE ) {
aggregate_of_instance : : ptr instances = * attr ;
aggregate_of_instance : : ptr new_instances ( new aggregate_of_instance ) ;
for ( aggregate_of_instance : : it it = instances - > begin ( ) ; it ! = instances - > end ( ) ; + + it ) {
entity_entity_map_t : : const_iterator eit = entity_file_map . find ( ( * it ) - > identity ( ) ) ;
if ( eit = = entity_file_map . end ( ) ) {
throw IfcParse : : IfcException ( " Unable to map instance to file " ) ;
}
new_instances - > push ( eit - > second ) ;
}
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( new_instances ) ;
we - > setArgument ( i , copy ) ;
} else if ( attr_type = = IfcUtil : : Argument_AGGREGATE_OF_AGGREGATE_OF_ENTITY_INSTANCE ) {
aggregate_of_aggregate_of_instance : : ptr instances = * attr ;
aggregate_of_aggregate_of_instance : : ptr new_instances ( new aggregate_of_aggregate_of_instance ) ;
for ( aggregate_of_aggregate_of_instance : : outer_it it = instances - > begin ( ) ; it ! = instances - > end ( ) ; + + it ) {
std : : vector < IfcUtil : : IfcBaseClass * > list ;
for ( aggregate_of_aggregate_of_instance : : inner_it jt = it - > begin ( ) ; jt ! = it - > end ( ) ; + + jt ) {
entity_entity_map_t : : const_iterator eit = entity_file_map . find ( ( * jt ) - > identity ( ) ) ;
if ( eit = = entity_file_map . end ( ) ) {
throw IfcParse : : IfcException ( " Unable to map instance to file " ) ;
}
list . push_back ( eit - > second ) ;
}
new_instances - > push ( list ) ;
}
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( new_instances ) ;
we - > setArgument ( i , copy ) ;
2023-10-24 14:16:26 +02:00
} else if ( ( decl ! = nullptr ) & & decl - > is ( * schema ( ) - > declaration_by_name ( " IfcLengthMeasure " ) ) ) {
2023-09-17 12:30:17 +02:00
if ( boost : : math : : isnan ( conversion_factor ) ) {
std : : pair < IfcUtil : : IfcBaseClass * , double > this_file_unit = { nullptr , 1.0 } ;
std : : pair < IfcUtil : : IfcBaseClass * , double > other_file_unit = { nullptr , 1.0 } ;
try {
this_file_unit = getUnit ( " LENGTHUNIT " ) ;
other_file_unit = other_file - > getUnit ( " LENGTHUNIT " ) ;
} catch ( IfcParse : : IfcException & ) {
}
2023-10-24 14:16:26 +02:00
if ( ( this_file_unit . first ! = nullptr ) & & ( other_file_unit . first ! = nullptr ) ) {
2023-09-17 12:30:17 +02:00
conversion_factor = other_file_unit . second / this_file_unit . second ;
} else {
conversion_factor = 1. ;
}
}
if ( attr_type = = IfcUtil : : Argument_DOUBLE ) {
double v = * attr ;
v * = conversion_factor ;
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( v ) ;
we - > setArgument ( i , copy ) ;
} else if ( attr_type = = IfcUtil : : Argument_AGGREGATE_OF_DOUBLE ) {
std : : vector < double > v = * attr ;
for ( std : : vector < double > : : iterator it = v . begin ( ) ; it ! = v . end ( ) ; + + it ) {
( * it ) * = conversion_factor ;
}
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( v ) ;
we - > setArgument ( i , copy ) ;
} else if ( attr_type = = IfcUtil : : Argument_AGGREGATE_OF_AGGREGATE_OF_DOUBLE ) {
std : : vector < std : : vector < double > > v = * attr ;
for ( std : : vector < std : : vector < double > > : : iterator it = v . begin ( ) ; it ! = v . end ( ) ; + + it ) {
std : : vector < double > & v2 = ( * it ) ;
for ( std : : vector < double > : : iterator jt = v2 . begin ( ) ; jt ! = v2 . end ( ) ; + + jt ) {
( * jt ) * = conversion_factor ;
}
}
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( v ) ;
we - > setArgument ( i , copy ) ;
}
}
}
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
// A new entity instance name is generated and
// the instance is pointed to this file.
we - > file = this ;
2023-10-24 14:16:26 +02:00
if ( we - > type ( ) - > as_entity ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
if ( id = = - 1 ) {
we - > set_id ( FreshId ( ) ) ;
} else {
we - > set_id ( ( unsigned int ) id ) ;
if ( ( unsigned ) id > MaxId ) {
MaxId = ( unsigned ) id ;
}
}
}
2014-11-04 12:02:28 +00:00
2023-09-17 12:30:17 +02:00
entity_file_map . insert ( entity_entity_map_t : : value_type ( entity - > identity ( ) , new_entity ) ) ;
}
2017-08-16 21:46:39 +02:00
2023-09-17 12:30:17 +02:00
// For subtypes of IfcRoot, the GUID mapping needs to be updated.
if ( new_entity - > declaration ( ) . is ( * ifcroot_type_ ) ) {
try {
const std : : string guid = * new_entity - > data ( ) . getArgument ( 0 ) ;
if ( byguid . find ( guid ) ! = byguid . end ( ) ) {
std : : stringstream ss ;
ss < < " Overwriting entity with guid " < < guid ;
Logger : : Message ( Logger : : LOG_WARNING , ss . str ( ) ) ;
}
byguid [ guid ] = new_entity ;
} catch ( const IfcException & ex ) {
Logger : : Message ( Logger : : LOG_ERROR , ex . what ( ) ) ;
}
}
2017-08-16 21:46:39 +02:00
2023-09-17 12:30:17 +02:00
// The mapping by entity type is updated.
const IfcParse : : declaration * ty = & new_entity - > declaration ( ) ;
2012-07-18 19:32:58 +00:00
2023-10-24 14:16:26 +02:00
if ( ty - > as_entity ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr insts = instances_by_type_excl_subtypes ( ty ) ;
if ( ! insts ) {
insts = aggregate_of_instance : : ptr ( new aggregate_of_instance ( ) ) ;
bytype_excl [ ty ] = insts ;
}
insts - > push ( new_entity ) ;
}
2021-03-13 12:16:40 +01:00
2023-10-24 14:16:26 +02:00
for ( ; ty - > as_entity ( ) ! = nullptr ; ) {
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr insts = instances_by_type ( ty ) ;
if ( ! insts ) {
insts = aggregate_of_instance : : ptr ( new aggregate_of_instance ( ) ) ;
bytype [ ty ] = insts ;
}
insts - > push ( new_entity ) ;
const IfcParse : : declaration * pt = ty - > as_entity ( ) - > supertype ( ) ;
2023-10-24 14:16:26 +02:00
if ( pt ! = nullptr ) {
2023-09-17 12:30:17 +02:00
ty = pt ;
} else {
break ;
}
}
2023-10-24 14:16:26 +02:00
if ( ty - > as_entity ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
int new_id = - 1 ;
2023-10-24 14:16:26 +02:00
if ( new_entity - > data ( ) . file = = nullptr ) {
2023-09-17 12:30:17 +02:00
// For newly created entities ensure a valid ENTITY_INSTANCE_NAME is set
new_entity - > data ( ) . file = this ;
boost : : optional < unsigned > id_value ;
if ( id ! = - 1 ) {
id_value = ( unsigned ) id ;
if ( ( unsigned ) id > MaxId ) {
MaxId = ( unsigned ) id ;
}
}
new_id = new_entity - > data ( ) . set_id ( id_value ) ;
} else {
new_id = new_entity - > data ( ) . id ( ) ;
}
if ( byid . find ( new_id ) ! = byid . end ( ) ) {
// This should not happen
std : : stringstream ss ;
ss < < " Overwriting entity with id " < < new_id ;
Logger : : Message ( Logger : : LOG_WARNING , ss . str ( ) ) ;
}
// The mapping by entity instance name is updated.
byid [ new_id ] = new_entity ;
2023-10-24 14:16:26 +02:00
} else if ( new_entity - > data ( ) . file = = nullptr ) {
2023-09-17 12:30:17 +02:00
// For non-entity instances, no mappings are updated, but the file
// pointer has to be set, so that actual copies are created in subsequent
// times.
new_entity - > data ( ) . file = this ;
// While not a mapping that can be queried, we do need to free the instance
byidentity [ new_entity - > identity ( ) ] = new_entity ;
}
2021-03-13 12:16:40 +01:00
2023-10-24 14:16:26 +02:00
if ( parsing_complete_ & & ( ty - > as_entity ( ) ! = nullptr ) ) {
2023-09-17 12:30:17 +02:00
build_inverses_ ( new_entity ) ;
}
return new_entity ;
2015-02-17 19:48:41 +00:00
}
void IfcFile : : removeEntity ( IfcUtil : : IfcBaseClass * entity ) {
2023-09-17 12:30:17 +02:00
const unsigned id = entity - > data ( ) . id ( ) ;
IfcUtil : : IfcBaseClass * file_entity = instance_by_id ( id ) ;
// Attention when running removeEntity inside a loop over a list of entities to be removed.
// This invalidates the iterator. A workaround is to reverse the loop:
// boost::shared_ptr<aggregate_of_instance> entities = ...;
// for (auto it = entities->end() - 1; it >= entities->begin(); --it) {
// IfcUtil::IfcBaseClass *const inst = *it;
// model->removeEntity(inst);
// }
// TODO: Create a set of weak relations. Inverse relations that do not dictate an
// instance to be retained. For example: when deleting an IfcRepresentation, the
// individual IfcRepresentationItems can not be deleted if an IfcStyledItem is
// related. Hence, the IfcRepresentationItem::StyledByItem relation could be
// characterized as weak.
// std::set<IfcSchema::Type::Enum> weak_roots;
if ( entity ! = file_entity ) {
throw IfcParse : : IfcException ( " Instance not part of this file " ) ;
}
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
batch_deletion_ids_ . push_back ( id ) ;
2015-02-17 19:48:41 +00:00
2023-09-17 12:30:17 +02:00
if ( ! batch_mode_ ) {
process_deletion_ ( ) ;
}
2021-03-13 12:16:40 +01:00
}
2017-12-11 14:47:30 +01:00
2021-03-13 12:16:40 +01:00
void IfcFile : : process_deletion_ ( ) {
2017-12-11 14:47:30 +01:00
2023-09-17 12:30:17 +02:00
for ( auto & id : batch_deletion_ids_ . get < 0 > ( ) ) {
auto entity = instance_by_id ( id ) ;
aggregate_of_instance : : ptr references = instances_by_reference ( id ) ;
// Alter entity instances with INVERSE relations to the entity being
// deleted. This is necessary to maintain a valid IFC file, because
// dangling references to it's entities name should be removed. At this
// moment, inversely related instances affected by the removal of the
// entity being deleted are not deleted themselves.
if ( references ) {
for ( aggregate_of_instance : : it iit = references - > begin ( ) ; iit ! = references - > end ( ) ; + + iit ) {
IfcUtil : : IfcBaseEntity * related_instance = ( IfcUtil : : IfcBaseEntity * ) * iit ;
if ( std : : find ( batch_deletion_ids_ . begin ( ) , batch_deletion_ids_ . end ( ) , related_instance - > data ( ) . id ( ) ) ! = batch_deletion_ids_ . end ( ) ) {
continue ;
}
for ( size_t i = 0 ; i < related_instance - > data ( ) . getArgumentCount ( ) ; + + i ) {
Argument * attr = related_instance - > data ( ) . getArgument ( i ) ;
if ( attr - > isNull ( ) ) {
continue ;
}
IfcUtil : : ArgumentType attr_type = attr - > type ( ) ;
switch ( attr_type ) {
case IfcUtil : : Argument_ENTITY_INSTANCE : {
IfcUtil : : IfcBaseClass * instance_attribute = * attr ;
if ( instance_attribute = = entity ) {
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( boost : : blank ( ) ) ;
related_instance - > data ( ) . setArgument ( i , copy ) ;
}
} break ;
case IfcUtil : : Argument_AGGREGATE_OF_ENTITY_INSTANCE : {
aggregate_of_instance : : ptr instance_list = * attr ;
if ( instance_list - > contains ( entity ) ) {
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
instance_list - > remove ( entity ) ;
2023-10-24 14:16:26 +02:00
if ( ( instance_list - > size ( ) = = 0U ) & & related_instance - > declaration ( ) . as_entity ( ) - > attribute_by_index ( i ) - > optional ( ) ) {
2023-09-17 12:30:17 +02:00
// @todo we can also check the lower bound of the attribute type before setting to null.
copy - > set ( boost : : blank ( ) ) ;
} else {
copy - > set ( instance_list ) ;
}
related_instance - > data ( ) . setArgument ( i , copy ) ;
}
} break ;
case IfcUtil : : Argument_AGGREGATE_OF_AGGREGATE_OF_ENTITY_INSTANCE : {
aggregate_of_aggregate_of_instance : : ptr instance_list_list = * attr ;
if ( instance_list_list - > contains ( entity ) ) {
aggregate_of_aggregate_of_instance : : ptr new_list ( new aggregate_of_aggregate_of_instance ) ;
for ( aggregate_of_aggregate_of_instance : : outer_it it = instance_list_list - > begin ( ) ; it ! = instance_list_list - > end ( ) ; + + it ) {
std : : vector < IfcUtil : : IfcBaseClass * > instances = * it ;
std : : vector < IfcUtil : : IfcBaseClass * > : : iterator jt ;
while ( ( jt = std : : find ( instances . begin ( ) , instances . end ( ) , entity ) ) ! = instances . end ( ) ) {
instances . erase ( jt ) ;
}
new_list - > push ( instances ) ;
}
IfcWrite : : IfcWriteArgument * copy = new IfcWrite : : IfcWriteArgument ( ) ;
copy - > set ( new_list ) ;
related_instance - > data ( ) . setArgument ( i , copy ) ;
}
} break ;
default :
break ;
}
}
}
}
2021-03-13 12:16:40 +01:00
2023-09-17 12:30:17 +02:00
if ( ! batch_mode_ ) {
byref . erase (
byref . lower_bound ( { id , - 1 , - 1 } ) ,
byref . upper_bound ( { id , std : : numeric_limits < int > : : max ( ) , std : : numeric_limits < int > : : max ( ) } ) ) ;
byref_excl . erase ( id ) ;
// This is based on traversal which needs instances to still be contained in the map.
// another option would be to keep byid intact for the remainder of this loop
aggregate_of_instance : : ptr entity_attributes = traverse ( entity , 1 ) ;
for ( aggregate_of_instance : : it it = entity_attributes - > begin ( ) ; it ! = entity_attributes - > end ( ) ; + + it ) {
IfcUtil : : IfcBaseClass * entity_attribute = * it ;
if ( entity_attribute = = entity ) {
continue ;
}
const unsigned int name = entity_attribute - > data ( ) . id ( ) ;
// Do not update inverses for simple types (which have id()==0 in IfcOpenShell).
if ( name ! = 0 ) {
{
auto lower = byref . lower_bound ( { name , - 1 , - 1 } ) ;
auto upper = byref . upper_bound ( { name , std : : numeric_limits < int > : : max ( ) , std : : numeric_limits < int > : : max ( ) } ) ;
for ( auto byref_it = lower ; byref_it ! = upper ; + + byref_it ) {
auto & ids = byref_it - > second ;
ids . erase ( std : : remove ( ids . begin ( ) , ids . end ( ) , id ) , ids . end ( ) ) ;
}
}
{
auto byref_it = byref_excl . find ( name ) ;
if ( byref_it ! = byref_excl . end ( ) ) {
auto & ids = byref_it - > second ;
ids . erase ( std : : remove ( ids . begin ( ) , ids . end ( ) , id ) , ids . end ( ) ) ;
}
}
}
}
}
if ( entity - > declaration ( ) . is ( * ifcroot_type_ ) ) {
const std : : string global_id = * entity - > data ( ) . getArgument ( 0 ) ;
auto it = byguid . find ( global_id ) ;
if ( it ! = byguid . end ( ) ) {
byguid . erase ( it ) ;
} else {
Logger : : Warning ( " GlobalId on rooted instance not encountered in map " ) ;
}
}
byid . erase ( byid . find ( id ) ) ;
const IfcParse : : declaration * ty = & entity - > declaration ( ) ;
{
aggregate_of_instance : : ptr instances_of_same_type = instances_by_type_excl_subtypes ( ty ) ;
instances_of_same_type - > remove ( entity ) ;
if ( instances_of_same_type - > size ( ) = = 0 ) {
bytype_excl . erase ( ty ) ;
}
}
for ( ; ; ) {
aggregate_of_instance : : ptr instances_of_same_type = instances_by_type ( ty ) ;
if ( instances_of_same_type ) {
instances_of_same_type - > remove ( entity ) ;
}
if ( instances_of_same_type - > size ( ) = = 0 ) {
bytype . erase ( ty ) ;
}
const IfcParse : : declaration * pt = ty - > as_entity ( ) - > supertype ( ) ;
2023-10-24 14:16:26 +02:00
if ( pt ! = nullptr ) {
2023-09-17 12:30:17 +02:00
ty = pt ;
} else {
break ;
}
}
// entity_file_map is in place to prevent duplicate definitions with usage of add().
// Upon deletion the pairs need to be erased.
for ( auto it = entity_file_map . begin ( ) ; it ! = entity_file_map . end ( ) ; ) {
if ( it - > second = = entity ) {
it = entity_file_map . erase ( it ) ;
} else {
+ + it ;
}
}
delete entity ;
}
if ( batch_mode_ ) {
for ( auto it = byref . begin ( ) ; it ! = byref . end ( ) ; ) {
bool do_delete = batch_deletion_ids_ . get < 1 > ( ) . find ( std : : get < INSTANCE_ID > ( it - > first ) ) ! = batch_deletion_ids_ . get < 1 > ( ) . end ( ) ;
if ( ! do_delete ) {
it - > second . erase ( std : : remove_if ( it - > second . begin ( ) , it - > second . end ( ) , [ this ] ( int x ) {
return batch_deletion_ids_ . get < 1 > ( ) . find ( x ) ! = batch_deletion_ids_ . get < 1 > ( ) . end ( ) ;
} ) ,
it - > second . end ( ) ) ;
do_delete = it - > second . empty ( ) ;
}
if ( do_delete ) {
it = byref . erase ( it ) ;
} else {
+ + it ;
}
}
2021-03-13 12:16:40 +01:00
2023-09-17 12:30:17 +02:00
for ( auto it = byref_excl . begin ( ) ; it ! = byref_excl . end ( ) ; ) {
bool do_delete = batch_deletion_ids_ . get < 1 > ( ) . find ( it - > first ) ! = batch_deletion_ids_ . get < 1 > ( ) . end ( ) ;
if ( ! do_delete ) {
it - > second . erase ( std : : remove_if ( it - > second . begin ( ) , it - > second . end ( ) , [ this ] ( int x ) {
return batch_deletion_ids_ . get < 1 > ( ) . find ( x ) ! = batch_deletion_ids_ . get < 1 > ( ) . end ( ) ;
} ) ,
it - > second . end ( ) ) ;
do_delete = it - > second . empty ( ) ;
}
if ( do_delete ) {
it = byref_excl . erase ( it ) ;
} else {
+ + it ;
}
}
}
batch_deletion_ids_ . clear ( ) ;
2012-07-18 19:32:58 +00:00
}
2015-02-17 19:48:41 +00:00
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcFile : : instances_by_type ( const IfcParse : : declaration * t ) {
2023-09-17 12:30:17 +02:00
entities_by_type_t : : const_iterator it = bytype . find ( t ) ;
return ( it = = bytype . end ( ) ) ? aggregate_of_instance : : ptr ( ) : it - > second ;
2011-05-08 11:04:32 +00:00
}
2015-02-17 19:48:41 +00:00
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcFile : : instances_by_type_excl_subtypes ( const IfcParse : : declaration * t ) {
2023-09-17 12:30:17 +02:00
entities_by_type_t : : const_iterator it = bytype_excl . find ( t ) ;
return ( it = = bytype_excl . end ( ) ) ? aggregate_of_instance : : ptr ( ) : it - > second ;
2017-08-16 21:46:39 +02:00
}
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcFile : : instances_by_type ( const std : : string & t ) {
2023-09-17 12:30:17 +02:00
return instances_by_type ( schema ( ) - > declaration_by_name ( t ) ) ;
2012-07-19 00:14:01 +00:00
}
2015-02-17 19:48:41 +00:00
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcFile : : instances_by_type_excl_subtypes ( const std : : string & t ) {
2023-09-17 12:30:17 +02:00
return instances_by_type_excl_subtypes ( schema ( ) - > declaration_by_name ( t ) ) ;
2020-07-11 11:55:00 +02:00
}
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcFile : : instances_by_reference ( int t ) {
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr ret ( new aggregate_of_instance ) ;
for ( auto & i : byref_excl [ t ] ) {
ret - > push ( instance_by_id ( i ) ) ;
}
return ret ;
2011-05-08 11:04:32 +00:00
}
2015-02-17 19:48:41 +00:00
2017-12-13 13:16:49 +01:00
IfcUtil : : IfcBaseClass * IfcFile : : instance_by_id ( int id ) {
2023-09-17 12:30:17 +02:00
entity_by_id_t : : const_iterator it = byid . find ( id ) ;
if ( it = = byid . end ( ) ) {
throw IfcException ( " Instance # " + boost : : lexical_cast < std : : string > ( id ) + " not found " ) ;
}
return it - > second ;
2011-05-08 11:04:32 +00:00
}
2015-02-17 19:48:41 +00:00
2017-12-13 13:58:58 +01:00
IfcUtil : : IfcBaseClass * IfcFile : : instance_by_guid ( const std : : string & guid ) {
2023-09-17 12:30:17 +02:00
entity_by_guid_t : : const_iterator it = byguid . find ( guid ) ;
if ( it = = byguid . end ( ) ) {
throw IfcException ( " Instance with GlobalId ' " + guid + " ' not found " ) ;
}
2023-10-24 12:21:32 +02:00
return it - > second ;
2012-03-13 11:56:52 +00:00
}
2011-07-25 14:56:40 +00:00
2012-08-11 13:24:08 +00:00
// FIXME: Test destructor to delete entity and arg allocations
IfcFile : : ~ IfcFile ( ) {
2023-09-17 12:30:17 +02:00
std : : set < IfcUtil : : IfcBaseClass * > entities_to_delete ;
for ( const auto & pair : byid ) {
entities_to_delete . insert ( pair . second ) ;
}
for ( const auto & pair : byidentity ) {
entities_to_delete . insert ( pair . second ) ;
}
for ( auto entity : entities_to_delete ) {
delete entity ;
}
delete stream ;
delete tokens ;
2012-08-11 13:24:08 +00:00
}
2015-01-05 14:11:42 +00:00
IfcFile : : entity_by_id_t : : const_iterator IfcFile : : begin ( ) const {
2023-09-17 12:30:17 +02:00
return byid . begin ( ) ;
2012-03-16 15:21:52 +00:00
}
2015-02-17 19:48:41 +00:00
2015-01-05 14:11:42 +00:00
IfcFile : : entity_by_id_t : : const_iterator IfcFile : : end ( ) const {
2023-09-17 12:30:17 +02:00
return byid . end ( ) ;
2012-03-16 15:21:52 +00:00
}
2012-08-11 13:24:08 +00:00
2017-08-16 21:46:39 +02:00
IfcFile : : type_iterator IfcFile : : types_begin ( ) const {
2023-09-17 12:30:17 +02:00
return bytype_excl . begin ( ) ;
2017-08-16 21:46:39 +02:00
}
IfcFile : : type_iterator IfcFile : : types_end ( ) const {
2023-09-17 12:30:17 +02:00
return bytype_excl . end ( ) ;
2017-08-16 21:46:39 +02:00
}
IfcFile : : type_iterator IfcFile : : types_incl_super_begin ( ) const {
2023-09-17 12:30:17 +02:00
return bytype . begin ( ) ;
2017-08-16 21:46:39 +02:00
}
IfcFile : : type_iterator IfcFile : : types_incl_super_end ( ) const {
2023-09-17 12:30:17 +02:00
return bytype . end ( ) ;
2017-08-16 21:46:39 +02:00
}
2018-11-13 14:46:15 +01:00
namespace {
2023-09-17 12:30:17 +02:00
struct id_instance_pair_sorter {
bool operator ( ) ( const IfcParse : : IfcFile : : entity_by_id_t : : value_type & a , const IfcParse : : IfcFile : : entity_by_id_t : : value_type & b ) const {
return a . first < b . first ;
}
} ;
} // namespace
2018-11-13 14:46:15 +01:00
2023-09-17 12:30:17 +02:00
std : : ostream & operator < < ( std : : ostream & os , const IfcParse : : IfcFile & f ) {
f . header ( ) . write ( os ) ;
2012-07-18 19:32:58 +00:00
2023-09-17 12:30:17 +02:00
typedef std : : vector < std : : pair < unsigned int , IfcUtil : : IfcBaseClass * > > vector_t ;
vector_t sorted ( f . begin ( ) , f . end ( ) ) ;
std : : sort ( sorted . begin ( ) , sorted . end ( ) , id_instance_pair_sorter ( ) ) ;
2018-11-13 14:46:15 +01:00
2023-09-17 12:30:17 +02:00
for ( vector_t : : const_iterator it = sorted . begin ( ) ; it ! = sorted . end ( ) ; + + it ) {
const IfcUtil : : IfcBaseClass * e = it - > second ;
2023-10-24 14:16:26 +02:00
if ( e - > declaration ( ) . as_entity ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
os < < e - > data ( ) . toString ( true ) < < " ; " < < std : : endl ;
}
}
2012-07-18 19:32:58 +00:00
2023-09-17 12:30:17 +02:00
os < < " ENDSEC; " < < std : : endl ;
os < < " END-ISO-10303-21; " < < std : : endl ;
2012-08-11 13:24:08 +00:00
2023-09-17 12:30:17 +02:00
return os ;
2012-08-11 13:24:08 +00:00
}
2012-11-17 15:19:38 +00:00
2015-01-16 16:26:40 +00:00
std : : string IfcFile : : createTimestamp ( ) const {
2023-09-17 12:30:17 +02:00
char buf [ 255 ] ;
time_t t ;
time ( & t ) ;
struct tm * ti = localtime ( & t ) ;
2015-01-16 16:26:40 +00:00
2023-09-17 12:30:17 +02:00
std : : string result = " " ;
2023-10-24 14:16:26 +02:00
if ( strftime ( buf , 255 , " %Y-%m-%dT%H:%M:%S " , ti ) ! = 0U ) {
2023-09-17 12:30:17 +02:00
result = std : : string ( buf ) ;
}
return result ;
2015-01-10 22:13:52 +00:00
}
2022-10-20 13:35:02 +02:00
std : : vector < int > IfcFile : : get_inverse_indices ( int instance_id ) {
2023-09-17 12:30:17 +02:00
std : : vector < int > return_value ;
2022-10-20 13:35:02 +02:00
2023-09-17 12:30:17 +02:00
auto lower = byref . lower_bound ( { instance_id , - 1 , - 1 } ) ;
auto upper = byref . upper_bound ( { instance_id , std : : numeric_limits < int > : : max ( ) , std : : numeric_limits < int > : : max ( ) } ) ;
2022-10-20 13:35:02 +02:00
2023-09-17 12:30:17 +02:00
// Mapping of instance id to attribute offset.
std : : map < int , std : : vector < int > > mapping ;
for ( auto it = lower ; it ! = upper ; + + it ) {
for ( auto & i : it - > second ) {
// We only take the tuple for the type that id=i actually is, in order not
// to count double. Because byref contains mappings for every supertype of id=i.
if ( instance_by_id ( i ) - > declaration ( ) . index_in_schema ( ) = = std : : get < 1 > ( it - > first ) ) {
mapping [ i ] . push_back ( std : : get < 2 > ( it - > first ) ) ;
}
}
}
auto refs = instances_by_reference ( instance_id ) ;
for ( auto & r : * refs ) {
auto it = mapping . find ( r - > data ( ) . id ( ) ) ;
if ( it = = mapping . end ( ) | | it - > second . empty ( ) ) {
throw IfcException ( " Internal error " ) ;
}
return_value . push_back ( it - > second . front ( ) ) ;
it - > second . erase ( it - > second . begin ( ) ) ;
if ( it - > second . empty ( ) ) {
mapping . erase ( it ) ;
}
}
2022-10-20 13:35:02 +02:00
2023-09-17 12:30:17 +02:00
// Test whether all mappings where indeed used.
if ( ! mapping . empty ( ) ) {
throw IfcException ( " Internal error " ) ;
}
return return_value ;
2022-10-20 13:35:02 +02:00
}
2021-07-29 14:54:51 +02:00
aggregate_of_instance : : ptr IfcFile : : getInverse ( int instance_id , const IfcParse : : declaration * type , int attribute_index ) {
2023-09-17 12:30:17 +02:00
if ( type = = nullptr & & attribute_index = = - 1 ) {
return instances_by_reference ( instance_id ) ;
}
2015-01-10 22:13:52 +00:00
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr return_value ( new aggregate_of_instance ) ;
2021-08-22 12:22:44 +02:00
2023-09-17 12:30:17 +02:00
if ( attribute_index = = - 1 ) {
auto lower = byref . lower_bound ( { instance_id , type - > index_in_schema ( ) , - 1 } ) ;
auto upper = byref . upper_bound ( { instance_id , type - > index_in_schema ( ) , std : : numeric_limits < int > : : max ( ) } ) ;
2015-01-16 16:26:40 +00:00
2023-09-17 12:30:17 +02:00
for ( auto it = lower ; it ! = upper ; + + it ) {
for ( auto & i : it - > second ) {
return_value - > push ( instance_by_id ( i ) ) ;
}
}
} else {
auto it = byref . find ( { instance_id , type - > index_in_schema ( ) , attribute_index } ) ;
if ( it ! = byref . end ( ) ) {
for ( auto & i : it - > second ) {
return_value - > push ( instance_by_id ( i ) ) ;
}
}
}
2021-11-30 13:25:02 +11:00
2023-09-17 12:30:17 +02:00
return return_value ;
2021-11-30 13:25:02 +11:00
}
2023-09-17 12:30:17 +02:00
int IfcFile : : getTotalInverses ( int instance_id ) {
return byref_excl [ instance_id ] . size ( ) ;
}
2021-11-30 13:25:02 +11:00
2015-01-16 16:26:40 +00:00
void IfcFile : : setDefaultHeaderValues ( ) {
2023-09-17 12:30:17 +02:00
const std : : string empty_string = " " ;
std : : vector < std : : string > file_description , schema_identifiers , empty_vector ;
2015-01-16 16:26:40 +00:00
2023-09-17 12:30:17 +02:00
file_description . push_back ( " ViewDefinition [CoordinationView] " ) ;
2023-10-24 14:16:26 +02:00
if ( schema ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
schema_identifiers . push_back ( schema ( ) - > name ( ) ) ;
}
2015-01-16 16:26:40 +00:00
2023-09-17 12:30:17 +02:00
header ( ) . file_description ( ) . description ( file_description ) ;
header ( ) . file_description ( ) . implementation_level ( " 2;1 " ) ;
2015-01-16 16:26:40 +00:00
2023-09-17 12:30:17 +02:00
header ( ) . file_name ( ) . name ( empty_string ) ;
header ( ) . file_name ( ) . time_stamp ( createTimestamp ( ) ) ;
header ( ) . file_name ( ) . author ( empty_vector ) ;
header ( ) . file_name ( ) . organization ( empty_vector ) ;
header ( ) . file_name ( ) . preprocessor_version ( " IfcOpenShell " IFCOPENSHELL_VERSION ) ;
header ( ) . file_name ( ) . originating_system ( " IfcOpenShell " IFCOPENSHELL_VERSION ) ;
header ( ) . file_name ( ) . authorization ( empty_string ) ;
2015-01-16 16:26:40 +00:00
2023-09-17 12:30:17 +02:00
header ( ) . file_schema ( ) . schema_identifiers ( schema_identifiers ) ;
2015-01-16 16:26:40 +00:00
}
2017-12-13 18:05:10 +01:00
std : : pair < IfcUtil : : IfcBaseClass * , double > IfcFile : : getUnit ( const std : : string & unit_type ) {
2023-09-17 12:30:17 +02:00
std : : pair < IfcUtil : : IfcBaseClass * , double > return_value ( 0 , 1. ) ;
2021-09-24 09:03:58 +02:00
2023-09-17 12:30:17 +02:00
aggregate_of_instance : : ptr projects = instances_by_type ( schema ( ) - > declaration_by_name ( " IfcProject " ) ) ;
if ( ! projects | | projects - > size ( ) = = 0 ) {
try {
projects = instances_by_type ( schema ( ) - > declaration_by_name ( " IfcContext " ) ) ;
} catch ( IfcException & e ) {
}
}
2017-12-13 18:05:10 +01:00
2023-09-17 12:30:17 +02:00
if ( projects & & projects - > size ( ) = = 1 ) {
IfcUtil : : IfcBaseClass * project = * projects - > begin ( ) ;
IfcUtil : : IfcBaseClass * unit_assignment = * project - > data ( ) . getArgument (
project - > declaration ( ) . as_entity ( ) - > attribute_index ( " UnitsInContext " ) ) ;
aggregate_of_instance : : ptr units = * unit_assignment - > data ( ) . getArgument (
unit_assignment - > declaration ( ) . as_entity ( ) - > attribute_index ( " Units " ) ) ;
for ( aggregate_of_instance : : it it = units - > begin ( ) ; it ! = units - > end ( ) ; + + it ) {
IfcUtil : : IfcBaseClass * unit = * it ;
if ( unit - > declaration ( ) . is ( " IfcNamedUnit " ) ) {
const std : : string file_unit_type = * unit - > data ( ) . getArgument (
unit - > declaration ( ) . as_entity ( ) - > attribute_index ( " UnitType " ) ) ;
if ( file_unit_type ! = unit_type ) {
continue ;
}
IfcUtil : : IfcBaseClass * siunit = 0 ;
if ( unit - > declaration ( ) . is ( " IfcConversionBasedUnit " ) ) {
IfcUtil : : IfcBaseClass * mu = * unit - > data ( ) . getArgument (
unit - > declaration ( ) . as_entity ( ) - > attribute_index ( " ConversionFactor " ) ) ;
2017-12-13 18:05:10 +01:00
2023-09-17 12:30:17 +02:00
IfcUtil : : IfcBaseClass * vlc = * mu - > data ( ) . getArgument (
mu - > declaration ( ) . as_entity ( ) - > attribute_index ( " ValueComponent " ) ) ;
IfcUtil : : IfcBaseClass * unc = * mu - > data ( ) . getArgument (
mu - > declaration ( ) . as_entity ( ) - > attribute_index ( " UnitComponent " ) ) ;
return_value . second * = static_cast < double > ( * vlc - > data ( ) . getArgument ( 0 ) ) ;
return_value . first = unit ;
if ( unc - > declaration ( ) . is ( " IfcSIUnit " ) ) {
siunit = unc ;
}
} else if ( unit - > declaration ( ) . is ( " IfcSIUnit " ) ) {
return_value . first = siunit = unit ;
}
2023-10-24 14:16:26 +02:00
if ( siunit ! = nullptr ) {
2023-09-17 12:30:17 +02:00
Argument * prefix = siunit - > data ( ) . getArgument (
siunit - > declaration ( ) . as_entity ( ) - > attribute_index ( " Prefix " ) ) ;
if ( ! prefix - > isNull ( ) ) {
return_value . second * = IfcSIPrefixToValue ( * prefix ) ;
}
}
}
}
}
return return_value ;
2016-05-15 15:47:21 +02:00
}
2018-08-29 12:54:25 +02:00
void IfcParse : : IfcFile : : build_inverses_ ( IfcUtil : : IfcBaseClass * inst ) {
2023-09-17 12:30:17 +02:00
std : : function < void ( IfcUtil : : IfcBaseClass * , int ) > fn = [ this , inst ] ( IfcUtil : : IfcBaseClass * attr , int idx ) {
2023-10-24 14:16:26 +02:00
if ( attr - > declaration ( ) . as_entity ( ) ! = nullptr ) {
2023-09-17 12:30:17 +02:00
unsigned entity_attribute_id = attr - > data ( ) . id ( ) ;
auto decl = inst - > declaration ( ) . as_entity ( ) ;
byref_excl [ entity_attribute_id ] . push_back ( inst - > data ( ) . id ( ) ) ;
2023-10-24 14:16:26 +02:00
while ( decl ! = nullptr ) {
2023-09-17 12:30:17 +02:00
byref [ { entity_attribute_id , decl - > index_in_schema ( ) , idx } ] . push_back ( inst - > data ( ) . id ( ) ) ;
decl = decl - > supertype ( ) ;
}
}
} ;
apply_individual_instance_visitor ( & inst - > data ( ) ) . apply ( fn ) ;
2018-08-29 12:54:25 +02:00
}
void IfcParse : : IfcFile : : build_inverses ( ) {
2023-09-17 12:30:17 +02:00
for ( auto & pair : * this ) {
build_inverses_ ( pair . second ) ;
}
2020-09-30 09:38:01 +02:00
}
2021-09-11 12:59:48 +02:00
2021-09-13 13:17:08 +02:00
std : : atomic_uint32_t IfcUtil : : IfcBaseClass : : counter_ ( 0 ) ;
bool IfcParse : : IfcFile : : lazy_load_ = true ;
bool IfcParse : : IfcFile : : guid_map_ = true ;