Continue work on plug-in and tests

This commit is contained in:
Thomas Krijnen
2026-05-06 21:17:25 +02:00
parent 4670715ef3
commit 98ff457fd6
61 changed files with 16000 additions and 16377 deletions
+3 -3
View File
@@ -17,7 +17,7 @@ function(add_document_serializer_plugin target output_name)
target_compile_definitions(${target} PRIVATE SERIALIZERS_EXPORTS BOOST_DLL_USE_STD_FS ${PLUGIN_DEFINITIONS})
target_link_libraries(${target} PRIVATE ${plugin_when_not_wasm} IfcGeom IfcParse ${PLUGIN_LIBRARIES})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME "${output_name}"
OUTPUT_NAME "ifcopenshell.${output_name}"
RUNTIME_OUTPUT_DIRECTORY "${document_serializer_plugin_runtime_dir}"
LIBRARY_OUTPUT_DIRECTORY "${document_serializer_plugin_runtime_dir}"
)
@@ -43,7 +43,7 @@ function(add_geometry_serializer_plugin target output_name)
target_link_libraries(${target} PRIVATE proj::proj)
endif()
set_target_properties(${target} PROPERTIES
OUTPUT_NAME "${output_name}"
OUTPUT_NAME "ifcopenshell.${output_name}"
RUNTIME_OUTPUT_DIRECTORY "${geometry_serializer_plugin_runtime_dir}"
LIBRARY_OUTPUT_DIRECTORY "${geometry_serializer_plugin_runtime_dir}"
)
@@ -90,7 +90,7 @@ if(WITH_OPENCASCADE AND NOT WASM_BUILD)
add_geometry_serializer_plugin(geometry_serializer_svg "geometry.svg" SOURCES geometry_svg_plugin.cpp SvgSerializer.cpp util.cpp LIBRARIES ${opencascade_geometry_serializer_libraries})
if(HDF5_SUPPORT)
add_geometry_serializer_plugin(geometry_serializer_hdf "geometry.hdf" SOURCES geometry_hdf_plugin.cpp HdfSerializer.cpp LIBRARIES ${opencascade_geometry_serializer_libraries} ${HDF5_LIBRARIES})
add_geometry_serializer_plugin(geometry_serializer_hdf "geometry.h5" SOURCES geometry_hdf_plugin.cpp HdfSerializer.cpp LIBRARIES ${opencascade_geometry_serializer_libraries} ${HDF5_LIBRARIES})
endif()
endif()
+75 -2
View File
@@ -107,14 +107,24 @@ const ifcopenshell::serializers::document_serializer_registry::entry* ifcopenshe
}
const ifcopenshell::serializers::document_serializer_info* ifcopenshell::serializers::document_serializer_registry::find(const std::string& format, const std::string& schema_name) const {
auto* registry = const_cast<document_serializer_registry*>(this);
const auto* entry = find_entry_(format, schema_name);
if (!entry) {
load_document_serializer_plugin(*registry, format, schema_name);
entry = find_entry_(format, schema_name);
}
return entry ? &entry->info_ : nullptr;
}
boost::shared_ptr<Serializer> ifcopenshell::serializers::document_serializer_registry::create(const std::string& format, const document_serializer_context& context) const {
const auto schema_name = !context.schema_name.empty() ? context.schema_name :
(context.file ? context.file->schema()->name() : std::string());
auto* registry = const_cast<document_serializer_registry*>(this);
const auto* entry = find_entry_(format, schema_name);
if (!entry) {
load_document_serializer_plugin(*registry, format, schema_name);
entry = find_entry_(format, schema_name);
}
if (!entry) {
throw ifcopenshell::exception("No document serializer registered for " + format + (schema_name.empty() ? std::string() : " and schema " + schema_name));
}
@@ -135,6 +145,34 @@ std::vector<ifcopenshell::serializers::document_serializer_info> ifcopenshell::s
}
}
ifcopenshell::plugin::manager manager;
add_document_serializer_search_paths(manager);
for (const auto& path : manager.discover(document_serializer_plugin_prefix())) {
ifcopenshell::plugin::module module;
try {
module = manager.load(path);
} catch (const std::exception& e) {
logger::error(e);
continue;
}
if (module.meta().kind_ != ifcopenshell::plugin::kind::document_serializer) {
continue;
}
document_serializer_registry plugin_registry;
auto register_plugin = module.get_alias<register_document_serializer_plugin_fn>(document_serializer_plugin_registration_symbol());
register_plugin(plugin_registry, module);
for (const auto& pair : plugin_registry.entries_) {
for (const auto& entry : pair.second) {
const auto key = entry.info_.format + "|" + entry.info_.schema_name;
if (!seen.insert(key).second) {
continue;
}
result.push_back(entry.info_);
}
}
}
return result;
}
@@ -179,9 +217,44 @@ void ifcopenshell::serializers::load_document_serializer_plugins(document_serial
}
}
bool ifcopenshell::serializers::load_document_serializer_plugin(document_serializer_registry& registry, const std::string& format, const std::string& schema_name) {
const auto format_key = document_serializer_key(format);
if (format_key.empty()) {
return false;
}
const auto schema_key = document_serializer_schema_key(schema_name);
auto basename = document_serializer_plugin_prefix(format_key);
if (!schema_key.empty()) {
basename += "." + boost::to_lower_copy(schema_key);
}
ifcopenshell::plugin::manager manager;
add_document_serializer_search_paths(manager);
for (const auto& path : manager.discover_exact(basename)) {
ifcopenshell::plugin::module module;
try {
module = manager.load(path);
} catch (const std::exception& e) {
logger::error(e);
continue;
}
if (module.meta().kind_ != ifcopenshell::plugin::kind::document_serializer ||
module.meta().format != format_key ||
document_serializer_schema_key(module.meta().schema) != schema_key) {
continue;
}
auto register_plugin = module.get_alias<register_document_serializer_plugin_fn>(document_serializer_plugin_registration_symbol());
register_plugin(registry, module);
return registry.find_entry_(format_key, schema_key) != nullptr;
}
return false;
}
ifcopenshell::serializers::document_serializer_registry& ifcopenshell::serializers::document_serializer_registry_instance() {
static document_serializer_registry registry;
static std::once_flag once;
std::call_once(once, load_document_serializer_plugins, std::ref(registry));
return registry;
}
@@ -73,6 +73,8 @@ private:
const entry* find_entry_(const std::string& format, const std::string& schema_name) const;
friend SERIALIZERS_API bool load_document_serializer_plugin(document_serializer_registry& registry, const std::string& format, const std::string& schema_name);
std::map<std::string, std::vector<entry>> entries_;
};
@@ -82,6 +84,7 @@ SERIALIZERS_API const char* document_serializer_plugin_registration_symbol();
SERIALIZERS_API ifcopenshell::plugin::metadata document_serializer_plugin_metadata(const std::string& format, const std::string& schema_name = std::string());
SERIALIZERS_API std::filesystem::path document_serializer_plugin_directory();
SERIALIZERS_API void load_document_serializer_plugins(document_serializer_registry& registry);
SERIALIZERS_API bool load_document_serializer_plugin(document_serializer_registry& registry, const std::string& format, const std::string& schema_name = std::string());
SERIALIZERS_API document_serializer_registry& document_serializer_registry_instance();
}
+1 -1
View File
@@ -34,7 +34,7 @@ plugin::abi_info plugin_abi() {
}
plugin::metadata plugin_metadata() {
return geometry_serializer_plugin_metadata("hdf");
return geometry_serializer_plugin_metadata("h5");
}
boost::shared_ptr<GeometrySerializer> create_serializer(const geometry_serializer_context& context) {
+83 -6
View File
@@ -43,6 +43,14 @@ std::string geometry_serializer_key(const std::string& extension) {
return key;
}
std::string geometry_serializer_format_from_extension(const std::string& extension) {
auto key = geometry_serializer_key(extension);
if (!key.empty() && key.front() == '.') {
key.erase(key.begin());
}
return key;
}
void add_geometry_serializer_search_paths(ifcopenshell::plugin::manager& manager) {
const auto directory = ifcopenshell::serializers::geometry_serializer_plugin_directory();
manager.add_search_path(directory);
@@ -80,11 +88,19 @@ void ifcopenshell::serializers::geometry_serializer_registry::bind(const geometr
}
bool ifcopenshell::serializers::geometry_serializer_registry::has(const std::string& extension) const {
return entries_.find(geometry_serializer_key(extension)) != entries_.end();
const auto key = geometry_serializer_key(extension);
if (entries_.find(key) == entries_.end()) {
load_geometry_serializer_plugin(const_cast<geometry_serializer_registry&>(*this), extension);
}
return entries_.find(key) != entries_.end();
}
const ifcopenshell::serializers::geometry_serializer_info* ifcopenshell::serializers::geometry_serializer_registry::find(const std::string& extension) const {
const auto iter = entries_.find(geometry_serializer_key(extension));
const auto key = geometry_serializer_key(extension);
if (entries_.find(key) == entries_.end()) {
load_geometry_serializer_plugin(const_cast<geometry_serializer_registry&>(*this), extension);
}
const auto iter = entries_.find(key);
if (iter == entries_.end()) {
return nullptr;
}
@@ -92,7 +108,11 @@ const ifcopenshell::serializers::geometry_serializer_info* ifcopenshell::seriali
}
void ifcopenshell::serializers::geometry_serializer_registry::configure(const std::string& extension, geometry_serializer_context& context) const {
const auto iter = entries_.find(geometry_serializer_key(extension));
const auto key = geometry_serializer_key(extension);
if (entries_.find(key) == entries_.end()) {
load_geometry_serializer_plugin(const_cast<geometry_serializer_registry&>(*this), extension);
}
const auto iter = entries_.find(key);
if (iter == entries_.end()) {
throw ifcopenshell::exception("No geometry serializer registered for " + extension);
}
@@ -102,7 +122,11 @@ void ifcopenshell::serializers::geometry_serializer_registry::configure(const st
}
boost::shared_ptr<GeometrySerializer> ifcopenshell::serializers::geometry_serializer_registry::create(const std::string& extension, const geometry_serializer_context& context) const {
const auto iter = entries_.find(geometry_serializer_key(extension));
const auto key = geometry_serializer_key(extension);
if (entries_.find(key) == entries_.end()) {
load_geometry_serializer_plugin(const_cast<geometry_serializer_registry&>(*this), extension);
}
const auto iter = entries_.find(key);
if (iter == entries_.end()) {
throw ifcopenshell::exception("No geometry serializer registered for " + extension);
}
@@ -118,6 +142,31 @@ std::vector<ifcopenshell::serializers::geometry_serializer_info> ifcopenshell::s
}
result.push_back(pair.second.info_);
}
ifcopenshell::plugin::manager manager;
add_geometry_serializer_search_paths(manager);
for (const auto& path : manager.discover(geometry_serializer_plugin_prefix())) {
ifcopenshell::plugin::module module;
try {
module = manager.load(path);
} catch (const std::exception& e) {
logger::error(e);
continue;
}
if (module.meta().kind_ != ifcopenshell::plugin::kind::geometry_serializer) {
continue;
}
geometry_serializer_registry plugin_registry;
auto register_plugin = module.get_alias<register_geometry_serializer_plugin_fn>(geometry_serializer_plugin_registration_symbol());
register_plugin(plugin_registry, module);
for (const auto& pair : plugin_registry.entries_) {
if (!seen_formats.insert(pair.second.info_.format).second) {
continue;
}
result.push_back(pair.second.info_);
}
}
return result;
}
@@ -158,9 +207,37 @@ void ifcopenshell::serializers::load_geometry_serializer_plugins(geometry_serial
}
}
bool ifcopenshell::serializers::load_geometry_serializer_plugin(geometry_serializer_registry& registry, const std::string& extension) {
const auto format = geometry_serializer_format_from_extension(extension);
if (format.empty()) {
return false;
}
ifcopenshell::plugin::manager manager;
add_geometry_serializer_search_paths(manager);
for (const auto& path : manager.discover_exact(geometry_serializer_plugin_prefix(format))) {
ifcopenshell::plugin::module module;
try {
module = manager.load(path);
} catch (const std::exception& e) {
logger::error(e);
continue;
}
if (module.meta().kind_ != ifcopenshell::plugin::kind::geometry_serializer ||
module.meta().format != format) {
continue;
}
auto register_plugin = module.get_alias<register_geometry_serializer_plugin_fn>(geometry_serializer_plugin_registration_symbol());
register_plugin(registry, module);
return registry.entries_.find(geometry_serializer_key(extension)) != registry.entries_.end();
}
return false;
}
ifcopenshell::serializers::geometry_serializer_registry& ifcopenshell::serializers::geometry_serializer_registry_instance() {
static geometry_serializer_registry registry;
static std::once_flag once;
std::call_once(once, load_geometry_serializer_plugins, std::ref(registry));
return registry;
}
@@ -76,6 +76,8 @@ private:
ifcopenshell::plugin::module module_;
};
friend SERIALIZERS_API bool load_geometry_serializer_plugin(geometry_serializer_registry& registry, const std::string& extension);
std::map<std::string, entry> entries_;
};
@@ -85,6 +87,7 @@ SERIALIZERS_API const char* geometry_serializer_plugin_registration_symbol();
SERIALIZERS_API ifcopenshell::plugin::metadata geometry_serializer_plugin_metadata(const std::string& format);
SERIALIZERS_API std::filesystem::path geometry_serializer_plugin_directory();
SERIALIZERS_API void load_geometry_serializer_plugins(geometry_serializer_registry& registry);
SERIALIZERS_API bool load_geometry_serializer_plugin(geometry_serializer_registry& registry, const std::string& extension);
SERIALIZERS_API geometry_serializer_registry& geometry_serializer_registry_instance();
}
@@ -1,10 +1,22 @@
foreach(schema ${SCHEMA_VERSIONS})
add_document_serializer_plugin(document_serializer_xml_ifc${schema} "document.xml.ifc${schema}" SOURCES XmlSerializer.cpp xml_plugin.cpp)
if(NOT WASM_BUILD)
set(schema_plugin_library parse_schema_ifc${schema})
else()
set(schema_plugin_library)
endif()
add_document_serializer_plugin(document_serializer_xml_ifc${schema} "document.xml.ifc${schema}" SOURCES XmlSerializer.cpp xml_plugin.cpp LIBRARIES ${schema_plugin_library})
target_compile_definitions(document_serializer_xml_ifc${schema} PRIVATE IfcSchema=Ifc${schema})
if(WASM_BUILD)
target_link_options(document_serializer_xml_ifc${schema} PRIVATE "SHELL:-s ERROR_ON_UNDEFINED_SYMBOLS=0")
endif()
if(GLTF_SUPPORT)
add_document_serializer_plugin(document_serializer_json_ifc${schema} "document.json.ifc${schema}" SOURCES JsonSerializer.cpp json_plugin.cpp LIBRARIES nlohmann_json::nlohmann_json DEFINITIONS WITH_GLTF)
add_document_serializer_plugin(document_serializer_json_ifc${schema} "document.json.ifc${schema}" SOURCES JsonSerializer.cpp json_plugin.cpp LIBRARIES nlohmann_json::nlohmann_json ${schema_plugin_library} DEFINITIONS WITH_GLTF)
target_compile_definitions(document_serializer_json_ifc${schema} PRIVATE IfcSchema=Ifc${schema})
if(WASM_BUILD)
target_link_options(document_serializer_json_ifc${schema} PRIVATE "SHELL:-s ERROR_ON_UNDEFINED_SYMBOLS=0")
endif()
endif()
endforeach()