2012-07-19 18:26:47 +00:00
/********************************************************************************
2011-07-25 14:56:40 +00: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/>. *
* *
********************************************************************************/
2016-03-07 22:37:36 +02:00
# include "IfcUtil.h"
# include "../ifcparse/IfcException.h"
# include <boost/algorithm/string/replace.hpp>
2016-05-11 09:56:40 +01:00
# include <boost/optional.hpp>
2016-03-07 22:37:36 +02:00
2011-09-03 13:58:59 +00:00
# include <iostream>
2015-01-10 22:13:52 +00:00
# include <algorithm>
2011-07-25 14:56:40 +00:00
2015-01-05 14:11:42 +00:00
2014-05-04 14:42:55 +00:00
void IfcEntityList : : push ( IfcUtil : : IfcBaseClass * l ) {
2015-05-21 12:06:38 +00:00
if ( l ) {
ls . push_back ( l ) ;
}
2011-07-25 14:56:40 +00:00
}
2014-05-04 14:42:55 +00:00
void IfcEntityList : : push ( const IfcEntityList : : ptr & l ) {
2015-05-21 12:06:38 +00:00
if ( l ) {
for ( it i = l - > begin ( ) ; i ! = l - > end ( ) ; + + i ) {
if ( * i ) ls . push_back ( * i ) ;
}
2011-07-25 14:56:40 +00:00
}
}
2015-01-10 22:13:52 +00:00
unsigned int IfcEntityList : : size ( ) const { return ( unsigned int ) ls . size ( ) ; }
2011-07-25 14:56:40 +00:00
IfcEntityList : : it IfcEntityList : : begin ( ) { return ls . begin ( ) ; }
IfcEntityList : : it IfcEntityList : : end ( ) { return ls . end ( ) ; }
2014-05-04 14:42:55 +00:00
IfcUtil : : IfcBaseClass * IfcEntityList : : operator [ ] ( int i ) {
2011-07-25 14:56:40 +00:00
return ls [ i ] ;
}
2015-01-10 22:13:52 +00:00
bool IfcEntityList : : contains ( IfcUtil : : IfcBaseClass * instance ) const {
return std : : find ( ls . begin ( ) , ls . end ( ) , instance ) ! = ls . end ( ) ;
2011-07-25 14:56:40 +00:00
}
2015-02-17 19:48:41 +00:00
void IfcEntityList : : remove ( IfcUtil : : IfcBaseClass * instance ) {
2015-02-17 21:24:18 +00:00
std : : vector < IfcUtil : : IfcBaseClass * > : : iterator it ;
2015-02-17 19:48:41 +00:00
while ( ( it = std : : find ( ls . begin ( ) , ls . end ( ) , instance ) ) ! = ls . end ( ) ) {
ls . erase ( it ) ;
}
}
IfcEntityList : : ptr IfcEntityList : : filtered ( const std : : set < IfcSchema : : Type : : Enum > & entities ) {
IfcEntityList : : ptr return_value ( new IfcEntityList ) ;
for ( it it = begin ( ) ; it ! = end ( ) ; + + it ) {
bool contained = false ;
for ( std : : set < IfcSchema : : Type : : Enum > : : const_iterator jt = entities . begin ( ) ; jt ! = entities . end ( ) ; + + jt ) {
if ( ( * it ) - > is ( * jt ) ) {
contained = true ;
break ;
}
}
if ( ! contained ) {
return_value - > push ( * it ) ;
}
}
return return_value ;
}
2011-07-25 14:56:40 +00:00
2015-01-05 14:11:42 +00:00
unsigned int IfcUtil : : IfcBaseType : : getArgumentCount ( ) const { return 1 ; }
Argument * IfcUtil : : IfcBaseType : : getArgument ( unsigned int i ) const { return entity - > getArgument ( i ) ; }
2015-06-17 11:04:28 +00:00
const char * IfcUtil : : IfcBaseType : : getArgumentName ( unsigned int i ) const { if ( i = = 0 ) { return " wrappedValue " ; } else { throw IfcParse : : IfcAttributeOutOfRangeException ( " Argument index out of range " ) ; } }
2012-08-11 13:24:08 +00:00
2016-06-09 22:25:50 +06:00
//Note: some of these methods are overloaded in derived classes
Argument : : operator int ( ) const { throw IfcParse : : IfcException ( " Argument is not an integer " ) ; }
Argument : : operator bool ( ) const { throw IfcParse : : IfcException ( " Argument is not a boolean " ) ; }
Argument : : operator double ( ) const { throw IfcParse : : IfcException ( " Argument is not a number " ) ; }
Argument : : operator std : : string ( ) const { throw IfcParse : : IfcException ( " Argument is not a string " ) ; }
Argument : : operator boost : : dynamic_bitset < > ( ) const { throw IfcParse : : IfcException ( " Argument is not a binary " ) ; }
Argument : : operator IfcUtil : : IfcBaseClass * ( ) const { throw IfcParse : : IfcException ( " Argument is not an entity instance " ) ; }
Argument : : operator std : : vector < double > ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of floats " ) ; }
Argument : : operator std : : vector < int > ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of ints " ) ; }
Argument : : operator std : : vector < std : : string > ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of strings " ) ; }
Argument : : operator std : : vector < boost : : dynamic_bitset < > > ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of binaries " ) ; }
Argument : : operator IfcEntityList : : ptr ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of entity instances " ) ; }
Argument : : operator std : : vector < std : : vector < int > > ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of list of ints " ) ; }
Argument : : operator std : : vector < std : : vector < double > > ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of list of floats " ) ; }
Argument : : operator IfcEntityListList : : ptr ( ) const { throw IfcParse : : IfcException ( " Argument is not a list of list of entity instances " ) ; }
2015-01-05 14:11:42 +00:00
static const char * const argument_type_string [ ] = {
" NULL " ,
" DERIVED " ,
" INT " ,
" BOOL " ,
" DOUBLE " ,
2015-04-07 15:06:56 +00:00
" STRING " ,
" BINARY " ,
" ENUMERATION " ,
2015-06-16 14:47:03 +00:00
" ENTITY INSTANCE " ,
2015-04-07 15:06:56 +00:00
2015-06-16 14:47:03 +00:00
" AGGREGATE OF INT " ,
" AGGREGATE OF DOUBLE " ,
" AGGREGATE OF STRING " ,
" AGGREGATE OF BINARY " ,
" AGGREGATE OF ENTITY INSTANCE " ,
2015-04-07 15:06:56 +00:00
2015-06-16 14:47:03 +00:00
" AGGREGATE OF AGGREGATE OF INT " ,
" AGGREGATE OF AGGREGATE OF DOUBLE " ,
" AGGREGATE OF AGGREGATE OF ENTITY INSTANCE " ,
2015-04-07 15:06:56 +00:00
2015-01-05 14:11:42 +00:00
" UNKNOWN "
} ;
const char * IfcUtil : : ArgumentTypeToString ( ArgumentType argument_type ) {
return argument_type_string [ static_cast < int > ( argument_type ) ] ;
}
2015-06-17 11:27:02 +00:00
bool IfcUtil : : valid_binary_string ( const std : : string & s ) {
for ( std : : string : : const_iterator it = s . begin ( ) ; it ! = s . end ( ) ; + + it ) {
if ( * it ! = ' 0 ' & & * it ! = ' 1 ' ) return false ;
}
return true ;
2016-03-07 22:37:36 +02:00
}
2016-03-10 11:35:32 +02:00
void IfcUtil : : sanitate_material_name ( std : : string & str )
{
// Spaces in material names have been observed to cause problems with obj and dae importers.
// Handle other potential problematic characters here too if observing problems.
boost : : replace_all ( str , " " , " _ " ) ;
}
void IfcUtil : : escape_xml ( std : : string & str )
{
boost : : replace_all ( str , " \" " , " " " ) ;
boost : : replace_all ( str , " ' " , " ' " ) ;
boost : : replace_all ( str , " < " , " < " ) ;
boost : : replace_all ( str , " > " , " > " ) ;
boost : : replace_all ( str , " & " , " & " ) ;
}
void IfcUtil : : unescape_xml ( std : : string & str )
{
boost : : replace_all ( str , " " " , " \" " ) ;
boost : : replace_all ( str , " ' " , " ' " ) ;
boost : : replace_all ( str , " < " , " < " ) ;
boost : : replace_all ( str , " > " , " > " ) ;
boost : : replace_all ( str , " & " , " & " ) ;
}