2026-03-31 15:32:36 +02:00
|
|
|
/********************************************************************************
|
2021-01-21 22:22:34 +01: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/>. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
|
#define NOMINMAX
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOMSG
|
|
|
|
|
#define NOMSG NOMSG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NODRAWTEXT
|
|
|
|
|
#define NODRAWTEXT NODRAWTEXT
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOGDI
|
|
|
|
|
#define NOGDI NOGDI
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOSERVICE
|
2023-09-17 12:30:17 +02:00
|
|
|
#define NOSERVICE NOSERVICE
|
2021-01-21 22:22:34 +01:00
|
|
|
#endif
|
|
|
|
|
#ifndef NOKERNEL
|
|
|
|
|
#define NOKERNEL NOKERNEL
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOUSER
|
|
|
|
|
#define NOUSER NOUSER
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOMCX
|
|
|
|
|
#define NOMCX NOMCX
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef NOIME
|
|
|
|
|
#define NOIME NOIME
|
|
|
|
|
#endif
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
#include "argument.h"
|
2026-01-04 10:40:02 +01:00
|
|
|
#include "express.h"
|
2026-03-31 15:32:36 +02:00
|
|
|
#include "exception.h"
|
2023-09-17 12:30:17 +02:00
|
|
|
#include "utils.h"
|
2026-03-31 15:32:36 +02:00
|
|
|
#include "file.h"
|
2021-01-21 22:22:34 +01:00
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
#include <algorithm>
|
2021-01-21 22:22:34 +01:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
|
|
|
|
|
static const char* const argument_type_string[] = {
|
2023-09-17 12:30:17 +02:00
|
|
|
"NULL",
|
|
|
|
|
"DERIVED",
|
|
|
|
|
"INT",
|
|
|
|
|
"BOOL",
|
|
|
|
|
"LOGICAL",
|
|
|
|
|
"DOUBLE",
|
|
|
|
|
"STRING",
|
|
|
|
|
"BINARY",
|
|
|
|
|
"ENUMERATION",
|
|
|
|
|
"ENTITY INSTANCE",
|
|
|
|
|
|
|
|
|
|
"EMPTY AGGREGATE",
|
|
|
|
|
"AGGREGATE OF INT",
|
|
|
|
|
"AGGREGATE OF DOUBLE",
|
|
|
|
|
"AGGREGATE OF STRING",
|
|
|
|
|
"AGGREGATE OF BINARY",
|
|
|
|
|
"AGGREGATE OF ENTITY INSTANCE",
|
|
|
|
|
|
|
|
|
|
"AGGREGATE OF EMPTY AGGREGATE",
|
|
|
|
|
"AGGREGATE OF AGGREGATE OF INT",
|
|
|
|
|
"AGGREGATE OF AGGREGATE OF DOUBLE",
|
|
|
|
|
"AGGREGATE OF AGGREGATE OF ENTITY INSTANCE",
|
|
|
|
|
|
|
|
|
|
"UNKNOWN"};
|
2021-01-21 22:22:34 +01:00
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
const char* ifcopenshell::argument_type_to_string(argument_type argument_type) {
|
2023-09-17 12:30:17 +02:00
|
|
|
return argument_type_string[static_cast<int>(argument_type)];
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
bool ifcopenshell::valid_binary_string(const std::string& str) {
|
2023-11-06 20:29:00 +01:00
|
|
|
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
|
2023-09-17 12:30:17 +02:00
|
|
|
if (*it != '0' && *it != '1') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
void ifcopenshell::sanitate_material_name(std::string& str) {
|
2021-01-21 22:22:34 +01:00
|
|
|
// 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, " ", "_");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
void ifcopenshell::escape_xml(std::string& str) {
|
2026-07-10 16:03:53 +03:00
|
|
|
// Strip characters that are illegal in XML 1.0. Control characters other
|
|
|
|
|
// than tab (0x09), newline (0x0A) and carriage return (0x0D) are not valid
|
|
|
|
|
// XML 1.0 characters and cannot even be represented as numeric character
|
|
|
|
|
// references, so they would otherwise make the serialized XML/SVG output
|
|
|
|
|
// non-well-formed. Bytes belonging to a valid UTF-8 multibyte sequence are
|
|
|
|
|
// always >= 0x80, so filtering on the low control range leaves them intact.
|
|
|
|
|
str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char c) {
|
|
|
|
|
return c < 0x20 && c != '\t' && c != '\n' && c != '\r';
|
|
|
|
|
}), str.end());
|
2023-09-17 12:30:17 +02:00
|
|
|
boost::replace_all(str, "&", "&");
|
2021-01-21 22:22:34 +01:00
|
|
|
boost::replace_all(str, "\"", """);
|
|
|
|
|
boost::replace_all(str, "'", "'");
|
|
|
|
|
boost::replace_all(str, "<", "<");
|
|
|
|
|
boost::replace_all(str, ">", ">");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
void ifcopenshell::unescape_xml(std::string& str) {
|
2023-09-17 12:30:17 +02:00
|
|
|
boost::replace_all(str, "&", "&");
|
2021-01-21 22:22:34 +01:00
|
|
|
boost::replace_all(str, """, "\"");
|
|
|
|
|
boost::replace_all(str, "'", "'");
|
|
|
|
|
boost::replace_all(str, "<", "<");
|
|
|
|
|
boost::replace_all(str, ">", ">");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
void instance_data::populate_derived_() {
|
2026-01-15 19:24:55 +01:00
|
|
|
if (auto* ent = declaration_->as_entity()) {
|
|
|
|
|
for (auto it = ent->derived().begin(); it != ent->derived().end(); ++it) {
|
|
|
|
|
if (*it) {
|
|
|
|
|
set_attribute_value(
|
|
|
|
|
std::distance(ent->derived().begin(), it),
|
2026-03-31 15:32:36 +02:00
|
|
|
derived{});
|
2026-01-15 19:24:55 +01:00
|
|
|
}
|
2024-10-07 20:41:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
2024-08-23 20:29:07 +02:00
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
attribute_value express::Entity::get(const std::string& name) const {
|
2024-08-23 20:29:07 +02:00
|
|
|
auto attrs = declaration().as_entity()->all_attributes();
|
|
|
|
|
auto iter = attrs.begin();
|
|
|
|
|
size_t idx = 0;
|
|
|
|
|
for (; iter != attrs.end(); ++iter, ++idx) {
|
|
|
|
|
if ((*iter)->name() == name) {
|
2025-02-27 22:07:31 +01:00
|
|
|
return get_attribute_value(idx);
|
2024-08-23 20:29:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-31 15:32:36 +02:00
|
|
|
throw ifcopenshell::exception(name + " not found on " + declaration().name());
|
2024-08-23 20:29:07 +02:00
|
|
|
}
|
2021-01-21 22:22:34 +01:00
|
|
|
|
2026-01-04 10:40:02 +01:00
|
|
|
std::vector<express::Entity> express::Entity::get_inverse(const std::string& name) const {
|
2026-03-31 15:32:36 +02:00
|
|
|
const std::vector<const ifcopenshell::inverse_attribute*> attrs = declaration().as_entity()->all_inverse_attributes();
|
|
|
|
|
std::vector<const ifcopenshell::inverse_attribute*>::const_iterator iter = attrs.begin();
|
2023-11-06 20:29:00 +01:00
|
|
|
for (; iter != attrs.end(); ++iter) {
|
|
|
|
|
if ((*iter)->name() == name) {
|
2026-03-31 15:32:36 +02:00
|
|
|
return file()->get_inverse(
|
2026-01-04 10:40:02 +01:00
|
|
|
id(),
|
2023-11-06 20:29:00 +01:00
|
|
|
(*iter)->entity_reference(),
|
|
|
|
|
(int)(*iter)->entity_reference()->attribute_index((*iter)->attribute_reference()));
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-31 15:32:36 +02:00
|
|
|
throw ifcopenshell::exception(name + " not found on " + declaration().name());
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-23 20:29:07 +02:00
|
|
|
/*
|
2026-03-31 15:32:36 +02:00
|
|
|
void ifcopenshell::IfcBaseClass::data(instance_data* data) {
|
2023-09-17 12:30:17 +02:00
|
|
|
delete data_;
|
2023-11-06 20:29:00 +01:00
|
|
|
data_ = data;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
2024-08-23 20:29:07 +02:00
|
|
|
*/
|
2021-01-21 22:22:34 +01:00
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
ifcopenshell::argument_type ifcopenshell::make_aggregate(ifcopenshell::argument_type elem_type) {
|
2023-10-24 12:21:32 +02:00
|
|
|
switch (elem_type) {
|
2026-03-31 15:32:36 +02:00
|
|
|
case ifcopenshell::Argument_INT:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_INT;
|
|
|
|
|
case ifcopenshell::Argument_DOUBLE:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_DOUBLE;
|
|
|
|
|
case ifcopenshell::Argument_STRING:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_STRING;
|
|
|
|
|
case ifcopenshell::Argument_BINARY:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_BINARY;
|
|
|
|
|
case ifcopenshell::Argument_ENTITY_INSTANCE:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_ENTITY_INSTANCE;
|
|
|
|
|
case ifcopenshell::Argument_AGGREGATE_OF_INT:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_AGGREGATE_OF_INT;
|
|
|
|
|
case ifcopenshell::Argument_AGGREGATE_OF_DOUBLE:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_AGGREGATE_OF_DOUBLE;
|
|
|
|
|
case ifcopenshell::Argument_AGGREGATE_OF_ENTITY_INSTANCE:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_AGGREGATE_OF_ENTITY_INSTANCE;
|
|
|
|
|
case ifcopenshell::Argument_EMPTY_AGGREGATE:
|
|
|
|
|
return ifcopenshell::Argument_AGGREGATE_OF_EMPTY_AGGREGATE;
|
2023-10-24 12:21:32 +02:00
|
|
|
default:
|
2026-03-31 15:32:36 +02:00
|
|
|
return ifcopenshell::Argument_UNKNOWN;
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
ifcopenshell::argument_type ifcopenshell::from_parameter_type(const ifcopenshell::parameter_type* pt) {
|
2023-09-17 12:30:17 +02:00
|
|
|
// TODO: How to detect derived types here without a reference to the refering entity?
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
const ifcopenshell::aggregation_type* at = pt->as_aggregation_type();
|
|
|
|
|
const ifcopenshell::named_type* nt = pt->as_named_type();
|
|
|
|
|
const ifcopenshell::simple_type* st = pt->as_simple_type();
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2023-10-24 14:16:26 +02:00
|
|
|
if (at != nullptr) {
|
2023-09-17 12:30:17 +02:00
|
|
|
return make_aggregate(from_parameter_type(at->type_of_element()));
|
2023-10-24 12:21:32 +02:00
|
|
|
}
|
2023-10-24 14:16:26 +02:00
|
|
|
if (nt != nullptr) {
|
|
|
|
|
if (nt->declared_type()->as_entity() != nullptr) {
|
2026-03-31 15:32:36 +02:00
|
|
|
return ifcopenshell::Argument_ENTITY_INSTANCE;
|
2023-10-24 12:21:32 +02:00
|
|
|
}
|
2023-10-24 14:16:26 +02:00
|
|
|
if (nt->declared_type()->as_enumeration_type() != nullptr) {
|
2026-03-31 15:32:36 +02:00
|
|
|
return ifcopenshell::Argument_ENUMERATION;
|
2023-10-24 12:21:32 +02:00
|
|
|
}
|
2023-10-24 14:16:26 +02:00
|
|
|
if (nt->declared_type()->as_select_type() != nullptr) {
|
2026-03-31 15:32:36 +02:00
|
|
|
return ifcopenshell::Argument_ENTITY_INSTANCE;
|
2023-10-24 12:21:32 +02:00
|
|
|
}
|
2023-10-24 14:16:26 +02:00
|
|
|
if (nt->declared_type()->as_type_declaration() != nullptr) {
|
2023-09-17 12:30:17 +02:00
|
|
|
return from_parameter_type(nt->declared_type()->as_type_declaration()->declared_type());
|
|
|
|
|
}
|
2023-10-24 14:16:26 +02:00
|
|
|
} else if (st != nullptr) {
|
2023-09-17 12:30:17 +02:00
|
|
|
switch (st->declared_type()) {
|
2026-03-31 15:32:36 +02:00
|
|
|
case ifcopenshell::simple_type::binary_type:
|
|
|
|
|
return ifcopenshell::Argument_BINARY;
|
|
|
|
|
case ifcopenshell::simple_type::boolean_type:
|
|
|
|
|
return ifcopenshell::Argument_BOOL;
|
|
|
|
|
case ifcopenshell::simple_type::integer_type:
|
|
|
|
|
return ifcopenshell::Argument_INT;
|
|
|
|
|
case ifcopenshell::simple_type::logical_type:
|
|
|
|
|
return ifcopenshell::Argument_LOGICAL;
|
|
|
|
|
case ifcopenshell::simple_type::number_type:
|
|
|
|
|
return ifcopenshell::Argument_DOUBLE;
|
|
|
|
|
case ifcopenshell::simple_type::real_type:
|
|
|
|
|
return ifcopenshell::Argument_DOUBLE;
|
|
|
|
|
case ifcopenshell::simple_type::string_type:
|
|
|
|
|
return ifcopenshell::Argument_STRING;
|
|
|
|
|
case ifcopenshell::simple_type::datatype_COUNT:
|
|
|
|
|
throw ifcopenshell::exception("Invalid simple type encountered");
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
return ifcopenshell::Argument_UNKNOWN;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2026-03-31 15:32:36 +02:00
|
|
|
std::string ifcopenshell::path::to_utf8(const std::wstring& str) {
|
2023-09-17 12:30:17 +02:00
|
|
|
int buffer_size = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, 0, 0, 0, 0);
|
|
|
|
|
char* buffer = new char[buffer_size];
|
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, buffer, buffer_size, 0, 0);
|
|
|
|
|
std::string str_utf8(buffer);
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
return str_utf8;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
std::wstring ifcopenshell::path::from_utf8(const std::string& str) {
|
2023-09-17 12:30:17 +02:00
|
|
|
int buffer_size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0);
|
|
|
|
|
wchar_t* buffer = new wchar_t[buffer_size];
|
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buffer, buffer_size);
|
|
|
|
|
std::wstring str_wide(buffer);
|
|
|
|
|
delete[] buffer;
|
|
|
|
|
return str_wide;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
IFC_PARSE_API bool ifcopenshell::path::rename_file(const std::string& old_filename, const std::string& new_filename) {
|
2023-09-17 12:30:17 +02:00
|
|
|
std::wstring old_filename_w = from_utf8(old_filename);
|
|
|
|
|
std::wstring new_filename_w = from_utf8(new_filename);
|
|
|
|
|
delete_file(new_filename);
|
|
|
|
|
const bool success = !!MoveFileW(old_filename_w.c_str(), new_filename_w.c_str());
|
|
|
|
|
return success;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-09 13:30:48 +02:00
|
|
|
IFC_PARSE_API bool ifcopenshell::path::atomic_rename_file(const std::string& old_filename, const std::string& new_filename) {
|
2026-07-09 13:21:39 +02:00
|
|
|
std::wstring old_filename_w = from_utf8(old_filename);
|
|
|
|
|
std::wstring new_filename_w = from_utf8(new_filename);
|
|
|
|
|
// MOVEFILE_REPLACE_EXISTING makes the replace atomic on NTFS (no unlink
|
|
|
|
|
// of the destination first). MOVEFILE_WRITE_THROUGH waits until the move
|
|
|
|
|
// is flushed to disk before returning.
|
|
|
|
|
const bool success = !!MoveFileExW(old_filename_w.c_str(), new_filename_w.c_str(),
|
|
|
|
|
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
IFC_PARSE_API bool ifcopenshell::path::delete_file(const std::string& filename) {
|
2023-09-17 12:30:17 +02:00
|
|
|
std::wstring filename_w = from_utf8(filename);
|
|
|
|
|
const bool success = !!DeleteFileW(filename_w.c_str());
|
|
|
|
|
return success;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
IFC_PARSE_API bool ifcopenshell::path::rename_file(const std::string& old_filename, const std::string& new_filename) {
|
2023-09-17 12:30:17 +02:00
|
|
|
// Whether or not rename() replaces an existing file is implementation-specific,
|
|
|
|
|
// so remove() possible existing file always.
|
|
|
|
|
delete_file(new_filename);
|
|
|
|
|
return std::rename(old_filename.c_str(), new_filename.c_str()) == 0;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-07-09 13:30:48 +02:00
|
|
|
IFC_PARSE_API bool ifcopenshell::path::atomic_rename_file(const std::string& old_filename, const std::string& new_filename) {
|
2026-07-09 13:21:39 +02:00
|
|
|
// POSIX rename() atomically replaces an existing destination on the same
|
|
|
|
|
// filesystem, so there is no window in which new_filename is missing.
|
|
|
|
|
return std::rename(old_filename.c_str(), new_filename.c_str()) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
IFC_PARSE_API bool ifcopenshell::path::delete_file(const std::string& filename) {
|
2023-10-24 14:16:26 +02:00
|
|
|
return std::remove(filename.c_str()) != 0;
|
2021-01-21 22:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|