2022-01-19 12:18:33 +11:00
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 Thomas Krijnen <thomas@aecgeeks.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 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
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
2014-02-17 22:13:55 +00:00
header = """
#ifndef %(schema_name_upper)s _H
#define %(schema_name_upper)s _H
#include <string>
#include <vector>
#include <boost/optional.hpp>
2016-07-20 22:49:32 +03:00
#include " ../ifcparse/ifc_parse_api.h "
2016-05-29 18:12:57 +06:00
2021-07-29 14:53:26 +02:00
#include " ../ifcparse/aggregate_of_instance.h "
2017-06-05 17:26:58 +02:00
#include " ../ifcparse/IfcBaseClass.h "
2015-09-08 20:40:54 +02:00
#include " ../ifcparse/IfcSchema.h "
2014-02-17 22:13:55 +00:00
#include " ../ifcparse/IfcException.h "
2017-06-05 17:26:58 +02:00
#include " ../ifcparse/Argument.h "
2017-12-13 18:05:10 +01:00
struct %(schema_name)s {
2014-02-17 22:13:55 +00:00
2022-04-27 14:02:41 +02:00
IFC_PARSE_API static const IfcParse::schema_definition& get_schema();
2017-12-12 10:33:24 +01:00
2022-04-27 16:20:35 +02:00
IFC_PARSE_API static void clear_schema();
2017-12-22 16:45:02 +01:00
static const char* const Identifier;
2015-01-05 17:46:23 +00:00
2014-02-17 22:13:55 +00:00
// Forward definitions
%(forward_definitions)s
%(declarations)s
%(class_definitions)s
2017-12-13 18:05:10 +01:00
};
2014-02-17 22:13:55 +00:00
#endif
"""
enum_header = """
#ifndef %(schema_name_upper)s ENUM_H
#define %(schema_name_upper)s ENUM_H
2016-07-20 22:49:32 +03:00
#include " ../ifcparse/ifc_parse_api.h "
2016-05-29 18:12:57 +06:00
2017-06-05 17:26:58 +02:00
#include <string>
2016-03-18 11:14:14 +01:00
#include <boost/optional.hpp>
2014-02-17 22:13:55 +00:00
#endif
"""
2017-12-13 18:05:10 +01:00
lb_header = """ """
2015-01-05 14:11:42 +00:00
2020-11-01 20:08:27 +07:00
implementation = """
2014-02-17 22:13:55 +00:00
#include " ../ifcparse/ %(schema_name)s .h "
2015-09-08 20:40:54 +02:00
#include " ../ifcparse/IfcSchema.h "
2014-02-17 22:13:55 +00:00
#include " ../ifcparse/IfcException.h "
#include " ../ifcparse/IfcWrite.h "
2015-11-22 02:20:34 +02:00
#include <map>
2017-12-22 16:45:02 +01:00
const char* const %(schema_name)s ::Identifier = " %(schema_name_upper)s " ;
2014-02-17 22:13:55 +00:00
using namespace IfcParse;
using namespace IfcWrite;
2015-09-08 20:40:54 +02:00
// External definitions
%(external_definitions)s
2014-02-17 22:13:55 +00:00
%(enumeration_functions)s
2015-01-05 14:11:42 +00:00
%(simple_type_impl)s
2014-02-17 22:13:55 +00:00
%(entity_implementations)s
"""
2017-12-13 18:05:10 +01:00
lb_implementation = """ """
2015-01-05 14:11:42 +00:00
entity_descriptor = """ current = entity_descriptor_map[Type:: %(type)s ] = new IfcEntityDescriptor(Type:: %(type)s , %(parent_statement)s );
%(entity_descriptor_attributes)s """
entity_descriptor_parent = " entity_descriptor_map.find(Type:: %(type)s )->second "
2015-02-17 19:48:41 +00:00
entity_descriptor_attribute_without_entity = ' current->add( " %(name)s " , %(optional)s , %(type)s ); '
entity_descriptor_attribute_with_entity = ' current->add( " %(name)s " , %(optional)s , %(type)s ,Type:: %(entity_name)s ); '
2015-01-05 14:11:42 +00:00
enumeration_descriptor = """ values.clear(); values.reserve(128);
%(enumeration_descriptor_values)s
2016-07-15 06:49:32 +03:00
enumeration_descriptor_map[Type:: %(type)s ] = new IfcEnumerationDescriptor(Type:: %(type)s , values); """
2015-01-05 14:11:42 +00:00
enumeration_descriptor_value = ' values.push_back( " %(name)s " ); '
2020-11-01 20:08:27 +07:00
derived_field_statement = " { std::set<int> idxs; %(statements)s derived_map[Type:: %(type)s ] = idxs;} "
derived_field_statement_attrs = " idxs.insert( %d ); "
2015-01-05 14:11:42 +00:00
2014-02-17 22:13:55 +00:00
simpletype = """ %(documentation)s
2021-09-06 18:52:37 +02:00
class IFC_PARSE_API %(name)s : %(superclass)s {
2015-01-05 14:11:42 +00:00
public:
2015-09-08 20:40:54 +02:00
virtual const IfcParse::type_declaration& declaration() const;
2017-12-13 13:16:49 +01:00
static const IfcParse::type_declaration& Class();
2017-06-05 17:26:58 +02:00
explicit %(name)s (IfcEntityInstanceData* e);
2015-01-05 14:11:42 +00:00
%(name)s ( %(type)s v);
operator %(type)s () const;
};
2014-02-17 22:13:55 +00:00
"""
2015-01-05 14:11:42 +00:00
simpletype_impl_comment = " // Function implementations for %(name)s "
2020-11-01 20:08:27 +07:00
simpletype_impl_argument_type = ' if (i == 0) { return %(attr_type)s ; } else { throw IfcParse::IfcAttributeOutOfRangeException( " Argument index out of range " ); } '
2015-09-08 20:40:54 +02:00
simpletype_impl_argument = " return data_->getArgument(i); "
2017-12-13 13:16:49 +01:00
simpletype_impl_is_with_supertype = " return v == %(class_name)s _type || %(superclass)s ::is(v); "
simpletype_impl_is_without_supertype = " return v == %(class_name)s _type; "
2024-05-12 11:39:03 +02:00
simpletype_impl_type = " return *((IfcParse::type_declaration*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); "
simpletype_impl_class = " return *((IfcParse::type_declaration*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); "
2015-09-08 20:40:54 +02:00
simpletype_impl_explicit_constructor = " data_ = e; "
2020-11-01 20:08:27 +07:00
simpletype_impl_constructor = (
2024-05-12 16:57:34 +02:00
" data_ = new IfcEntityInstanceData( %(schema_name_upper)s _types[ %(index_in_schema)d ]); set_value(0, v); "
2020-11-01 20:08:27 +07:00
)
2024-05-12 16:57:34 +02:00
simpletype_impl_constructor_templated = " data_ = new IfcEntityInstanceData( %(schema_name_upper)s _types[ %(index_in_schema)d ]); set_value(0, v->generalize()); "
2015-09-08 20:40:54 +02:00
simpletype_impl_cast = " return *data_->getArgument(0); "
2020-11-01 20:08:27 +07:00
simpletype_impl_cast_templated = (
2021-07-29 14:53:26 +02:00
" aggregate_of_instance::ptr es = *data_->getArgument(0); return es->as< %(underlying_type)s >(); "
2020-11-01 20:08:27 +07:00
)
2024-05-12 11:39:03 +02:00
simpletype_impl_declaration = " return *((IfcParse::type_declaration*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); "
2020-11-01 20:08:27 +07:00
2014-02-17 22:13:55 +00:00
select = """ %(documentation)s
2022-02-10 13:06:04 +01:00
class IFC_PARSE_API %(name)s : public virtual IfcUtil::IfcBaseInterface {
public:
static const IfcParse::select_type& Class();
2023-10-17 12:12:37 +02:00
typedef aggregate_of< %(name)s > list;
2022-02-10 13:06:04 +01:00
};
2014-02-17 22:13:55 +00:00
"""
2021-02-14 12:19:06 +01:00
enumeration = """ class IFC_PARSE_API %(name)s : public IfcUtil::IfcBaseType {
2014-02-17 22:13:55 +00:00
%(documentation)s
2021-02-14 12:19:06 +01:00
public:
typedef enum { %(values)s } Value;
static const char* ToString(Value v);
static Value FromString(const std::string& s);
virtual const IfcParse::enumeration_type& declaration() const;
static const IfcParse::enumeration_type& Class();
%(name)s (IfcEntityInstanceData* e);
%(name)s (Value v);
%(name)s (const std::string& v);
operator Value() const;
2017-12-13 18:05:10 +01:00
};
2014-02-17 22:13:55 +00:00
"""
entity = """ %(documentation)s
2021-09-06 18:52:37 +02:00
class IFC_PARSE_API %(name)s : %(superclass)s {
2014-02-17 22:13:55 +00:00
public:
2015-09-08 20:40:54 +02:00
%(attributes)s %(inverse)s virtual const IfcParse::entity& declaration() const;
2017-12-13 13:16:49 +01:00
static const IfcParse::entity& Class();
2017-06-05 17:26:58 +02:00
%(name)s (IfcEntityInstanceData* e);
2014-02-17 22:13:55 +00:00
%(name)s ( %(constructor_arguments)s );
2021-07-29 14:53:26 +02:00
typedef aggregate_of< %(name)s > list;
2014-02-17 22:13:55 +00:00
};
"""
2022-02-10 13:06:04 +01:00
select_function = """
2024-05-12 11:39:03 +02:00
const IfcParse::select_type& %(schema_name)s :: %(name)s ::Class() { return *((IfcParse::select_type*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); }
2022-02-10 13:06:04 +01:00
"""
2020-11-01 20:08:27 +07:00
enumeration_function = """
2024-05-12 11:39:03 +02:00
const IfcParse::enumeration_type& %(schema_name)s :: %(name)s ::declaration() const { return *((IfcParse::enumeration_type*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); }
const IfcParse::enumeration_type& %(schema_name)s :: %(name)s ::Class() { return *((IfcParse::enumeration_type*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); }
2021-02-14 12:19:06 +01:00
%(schema_name)s :: %(name)s :: %(name)s (IfcEntityInstanceData* e) {
data_ = e;
}
%(schema_name)s :: %(name)s :: %(name)s (Value v) {
2024-05-12 11:39:03 +02:00
data_ = new IfcEntityInstanceData( %(schema_name_upper)s _types[ %(index_in_schema)d ]);
2024-05-12 16:57:34 +02:00
set_value(0, IfcWrite::IfcWriteArgument::EnumerationReference(v,ToString(v)));
2021-02-14 12:19:06 +01:00
}
%(schema_name)s :: %(name)s :: %(name)s (const std::string& v) {
2024-05-12 11:39:03 +02:00
data_ = new IfcEntityInstanceData( %(schema_name_upper)s _types[ %(index_in_schema)d ]);
2024-05-12 16:57:34 +02:00
set_value(0, IfcWrite::IfcWriteArgument::EnumerationReference(FromString(v),ToString(FromString(v))));
2021-02-14 12:19:06 +01:00
}
2017-12-13 18:05:10 +01:00
const char* %(schema_name)s :: %(name)s ::ToString(Value v) {
2024-05-12 14:51:45 +02:00
return %(schema_name)s :: %(name)s :: %(name)s ::Class().lookup_enum_value((size_t)v);
2014-02-17 22:13:55 +00:00
}
2017-12-13 18:05:10 +01:00
%(schema_name)s :: %(name)s ::Value %(schema_name)s :: %(name)s ::FromString(const std::string& s) {
2024-05-12 14:51:45 +02:00
return ( %(schema_name)s :: %(name)s ::Value) %(schema_name)s :: %(name)s :: %(name)s ::Class().lookup_enum_offset(s);
2014-02-17 22:13:55 +00:00
}
2021-07-01 11:13:50 +02:00
%(schema_name)s :: %(name)s ::operator %(schema_name)s :: %(name)s ::Value() const {
return FromString((std::string) *data_->getArgument(0));
}
2014-02-17 22:13:55 +00:00
"""
entity_implementation = """ // Function implementations for %(name)s
2015-09-08 20:40:54 +02:00
%(attributes)s
%(inverse)s
2024-05-12 11:39:03 +02:00
const IfcParse::entity& %(schema_name)s :: %(name)s ::declaration() const { return *((IfcParse::entity*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); }
const IfcParse::entity& %(schema_name)s :: %(name)s ::Class() { return *((IfcParse::entity*) %(schema_name_upper)s _types[ %(index_in_schema)d ]); }
2024-05-12 16:57:34 +02:00
%(schema_name)s :: %(name)s :: %(name)s (IfcEntityInstanceData* e) : %(superclass)s { data_ = e; }
2024-05-12 11:39:03 +02:00
%(schema_name)s :: %(name)s :: %(name)s ( %(constructor_arguments)s ) : %(superclass)s { data_ = new IfcEntityInstanceData( %(schema_name_upper)s _types[ %(index_in_schema)d ]); %(constructor_implementation)s }
2014-02-17 22:13:55 +00:00
"""
optional_attribute_description = " /// Whether the optional attribute %s is defined for this %s "
2017-12-13 18:05:10 +01:00
function = " %(return_type)s %(schema_name)s :: %(class_name)s :: %(name)s ( %(arguments)s ) { %(body)s } "
const_function = " %(return_type)s %(schema_name)s :: %(class_name)s :: %(name)s ( %(arguments)s ) const { %(body)s } "
constructor = " %(schema_name)s :: %(class_name)s :: %(class_name)s ( %(arguments)s ) { %(body)s } "
2020-11-01 20:08:27 +07:00
constructor_single_initlist = (
" %(schema_name)s :: %(class_name)s :: %(class_name)s ( %(arguments)s ) : %(superclass)s ( %(superclass_init)s ) { %(body)s } "
)
2017-12-13 18:05:10 +01:00
cast_function = " %(schema_name)s :: %(class_name)s ::operator %(return_type)s () const { %(body)s } "
2014-02-17 22:13:55 +00:00
array_type = " std::vector< %(instance_type)s > /*[ %(lower)s : %(upper)s ]*/ "
2015-06-16 12:58:07 +00:00
nested_array_type = " std::vector< std::vector< %(instance_type)s > > "
2021-07-29 14:53:26 +02:00
list_type = " aggregate_of< %(instance_type)s >::ptr "
list_list_type = " aggregate_of_aggregate_of< %(instance_type)s >::ptr "
untyped_list = " aggregate_of_instance::ptr "
inverse_attr = " aggregate_of< %(entity)s >::ptr %(name)s () const; // INVERSE %(entity)s :: %(attribute)s "
2014-02-17 22:13:55 +00:00
enum_from_string_stmt = ' if (s == " %(value)s " ) return :: %(schema_name)s :: %(name)s :: %(short_name)s _ %(value)s ; '
2020-11-01 20:08:27 +07:00
schema_entity_stmt = " case Type:: %(name)s : return new %(name)s (e); break; "
2014-02-17 22:13:55 +00:00
string_map_statement = ' string_map[ " %(uppercase_name)s " %(padding)s ] = Type:: %(name)s ; '
2020-11-01 20:08:27 +07:00
parent_type_stmt = " if(v== %(name)s %(padding)s ) { return %(parent)s ; } "
2014-02-17 22:13:55 +00:00
parent_type_test = " || %s ::is(v) "
2015-09-08 20:40:54 +02:00
optional_attr_stmt = " return !data_->getArgument( %(index)d )->isNull(); "
2014-02-17 22:13:55 +00:00
2021-07-29 14:53:26 +02:00
get_attr_stmt = " %(null_check)s %(non_optional_type)s v = *data_->getArgument( %(index)d ); return v; "
get_attr_stmt_enum = " %(null_check)s return %(non_optional_type)s ::FromString(*data_->getArgument( %(index)d )); "
2022-02-10 13:05:33 +01:00
get_attr_stmt_entity = " %(null_check)s return ((IfcUtil::IfcBaseClass*)(*data_->getArgument( %(index)d )))->as< %(non_optional_type_no_pointer)s >(true); "
2022-01-19 12:18:33 +11:00
get_attr_stmt_array = " %(null_check)s aggregate_of_instance::ptr es = *data_->getArgument( %(index)d ); return es->as< %(list_instance_type)s >(); "
get_attr_stmt_nested_array = " %(null_check)s aggregate_of_aggregate_of_instance::ptr es = *data_->getArgument( %(index)d ); return es->as< %(list_instance_type)s >(); "
2014-02-17 22:13:55 +00:00
2024-05-12 11:39:03 +02:00
get_inverse = " return data_->getInverse( %(schema_name_upper)s _types[ %(type_index)d ], %(index)d )->as< %(type)s >(); "
2014-02-17 22:13:55 +00:00
2020-11-01 20:08:27 +07:00
set_attr_stmt = (
2024-05-12 16:57:34 +02:00
" %(check_optional_set_begin)s set_value( %(index)d , %(star_if_optional)s v); %(check_optional_set_else)s unset_value( %(index)d ); %(check_optional_set_end)s "
2020-11-01 20:08:27 +07:00
)
2024-05-12 16:57:34 +02:00
set_attr_instance = (
" %(check_optional_set_begin)s set_value( %(index)d , v->as<IfcUtil::IfcBaseClass>()); %(check_optional_set_else)s unset_value( %(index)d ); %(check_optional_set_end)s "
)
set_attr_stmt_enum = " %(check_optional_set_begin)s set_value( %(index)d , IfcWrite::IfcWriteArgument::EnumerationReference( %(star_if_optional)s v, %(non_optional_type)s ::ToString( %(star_if_optional)s v))); %(check_optional_set_else)s unset_value( %(index)d ); %(check_optional_set_end)s "
2020-11-01 20:08:27 +07:00
set_attr_stmt_array = (
2024-05-12 16:57:34 +02:00
" %(check_optional_set_begin)s set_value( %(index)d , ( %(star_if_optional)s v)->generalize()); %(check_optional_set_else)s unset_value( %(index)d ); %(check_optional_set_end)s "
2020-11-01 20:08:27 +07:00
)
constructor_stmt = (
2024-05-12 16:57:34 +02:00
" set_value( %(index)d , ( %(name)s )); "
2020-11-01 20:08:27 +07:00
)
constructor_stmt_enum = (
2024-05-12 16:57:34 +02:00
" set_value( %(index)d , (IfcWrite::IfcWriteArgument::EnumerationReference( %(name)s , %(type)s ::ToString( %(name)s )))); "
2020-11-01 20:08:27 +07:00
)
constructor_stmt_array = (
2024-05-12 16:57:34 +02:00
" set_value( %(index)d , ( %(name)s )->generalize()); "
2020-11-01 20:08:27 +07:00
)
constructor_stmt_derived = (
2024-05-12 16:57:34 +02:00
" "
)
constructor_stmt_instance = (
" set_value( %(index)d , %(name)s ? %(name)s ->as<IfcUtil::IfcBaseClass>() : (IfcUtil::IfcBaseClass*) nullptr); "
2020-11-01 20:08:27 +07:00
)
2014-02-17 22:13:55 +00:00
2024-05-12 16:57:34 +02:00
constructor_stmt_optional = " if ( %(name)s ) { %(stmt)s } "
2014-02-17 22:13:55 +00:00
2020-11-01 20:08:27 +07:00
inverse_implementation = ' inverse_map[Type:: %(type)s ].insert(std::make_pair( " %(name)s " , std::make_pair(Type:: %(related_type)s , %(index)d ))); '
2015-01-05 14:11:42 +00:00
2014-02-17 22:13:55 +00:00
2020-11-01 20:08:27 +07:00
def multi_line_comment ( li ) :
return ( " /// %s " % ( " \n /// " . join ( li ) ) ) if len ( li ) else " "