Files
IfcOpenShell/src/bonsaiviewer/ElementRegistry.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
5.0 KiB
C++
Raw Normal View History

2026-05-06 10:47:03 +10:00
// This file was generated with the assistance of an AI coding tool.
/********************************************************************************
* *
2026-07-03 09:39:27 +10:00
* This file is part of Bonsai. *
2026-05-06 10:47:03 +10:00
* *
2026-07-03 09:39:27 +10:00
* Bonsai is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
2026-05-06 10:47:03 +10:00
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
2026-07-03 09:39:27 +10:00
* Bonsai is distributed in the hope that it will be useful, *
2026-05-06 10:47:03 +10:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
2026-07-03 09:39:27 +10:00
* GNU General Public License for more details. *
2026-05-06 10:47:03 +10:00
* *
2026-07-03 09:39:27 +10:00
* You should have received a copy of the GNU General Public License *
2026-05-06 10:47:03 +10:00
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include "ElementRegistry.h"
#include "../ifcviewer/GeometryStreamer.h"
#include "../ifcviewer/SceneLoader.h"
#include "../ifcviewer/SidecarCache.h"
2026-05-20 09:38:10 +10:00
namespace bonsaiviewer {
2026-05-06 10:47:03 +10:00
ElementRegistry::ElementRegistry(QObject* parent)
: QObject(parent)
{
}
void ElementRegistry::bindLoader(SceneLoader* loader) {
2026-05-06 12:36:28 +10:00
loader_ = loader;
2026-05-06 10:47:03 +10:00
connect(loader, &SceneLoader::sidecarElementsReady,
this, &ElementRegistry::onSidecarElementsReady);
connect(loader, &SceneLoader::streamedElementsReady,
this, &ElementRegistry::onStreamedElementsReady);
}
2026-05-07 12:21:55 +10:00
void ElementRegistry::clear() {
elements_.clear();
}
2026-05-07 17:10:04 +10:00
void ElementRegistry::removeModel(uint32_t model_id) {
for (auto it = elements_.begin(); it != elements_.end();) {
if (it->second.model_id == model_id) {
it = elements_.erase(it);
} else {
++it;
}
}
}
2026-05-14 10:06:19 +10:00
std::vector<BasicElementInfo> ElementRegistry::basicElementInfoForModel(uint32_t model_id) const {
std::vector<BasicElementInfo> result;
result.reserve(elements_.size());
for (const auto& [object_id, info] : elements_) {
(void)object_id;
if (info.model_id != model_id) continue;
result.push_back(info);
}
return result;
}
2026-05-06 12:36:28 +10:00
std::optional<BasicElementInfo> ElementRegistry::findBasicElementInfo(uint32_t object_id) const {
2026-05-06 10:47:03 +10:00
auto it = elements_.find(object_id);
if (it == elements_.end()) return std::nullopt;
return it->second;
}
2026-05-06 12:36:28 +10:00
std::optional<express::Base> ElementRegistry::findEntity(uint32_t object_id) const {
if (!loader_) return std::nullopt;
auto info = findBasicElementInfo(object_id);
if (!info) return std::nullopt;
auto* file = loader_->ifcFile(info->model_id);
if (!file) return std::nullopt;
try {
auto instance = file->instance_by_id(info->ifc_id);
if (!instance) return std::nullopt;
return instance;
} catch (...) {
return std::nullopt;
}
}
2026-07-03 08:38:57 +10:00
void ElementRegistry::onSidecarElementsReady(uint32_t /*model_id*/,
std::vector<ElementTableRecord> elements,
2026-05-06 10:47:03 +10:00
std::string string_table) {
2026-07-03 08:38:57 +10:00
auto string_from_table = [&](uint32_t offset, uint32_t length) -> QString {
2026-05-06 10:47:03 +10:00
if (length == 0 || offset + length > string_table.size()) return {};
return QString::fromStdString(string_table.substr(offset, length));
};
2026-07-03 08:38:57 +10:00
for (const auto& packed_element : elements) {
2026-05-06 10:47:03 +10:00
BasicElementInfo info;
2026-07-03 08:38:57 +10:00
info.object_id = packed_element.object_id;
info.model_id = packed_element.model_id;
info.ifc_id = packed_element.ifc_id;
info.guid = string_from_table(packed_element.guid_offset, packed_element.guid_length);
info.name = string_from_table(packed_element.name_offset, packed_element.name_length);
info.type = string_from_table(packed_element.type_offset, packed_element.type_length);
2026-05-06 10:47:03 +10:00
elements_[info.object_id] = info;
}
}
2026-07-03 08:38:57 +10:00
void ElementRegistry::onStreamedElementsReady(uint32_t /*model_id*/, std::vector<ElementInfo> elements) {
for (const auto& element : elements) {
2026-05-06 10:47:03 +10:00
BasicElementInfo info;
2026-07-03 08:38:57 +10:00
info.object_id = element.object_id;
info.model_id = element.model_id;
info.ifc_id = element.ifc_id;
info.guid = QString::fromStdString(element.guid);
info.name = QString::fromStdString(element.name);
info.type = QString::fromStdString(element.type);
2026-05-06 10:47:03 +10:00
elements_[info.object_id] = info;
}
}
2026-05-20 09:38:10 +10:00
} // namespace bonsaiviewer