mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-05 23:41:44 +00:00
Various improvements to USD serializer related to object naming
This commit is contained in:
@@ -1446,7 +1446,7 @@ std::vector<IfcGeom::filter_t> setup_filters(const std::vector<geom_filter>& fil
|
||||
layer_filter.populate(f.values);
|
||||
} else if (f.type == geom_filter::ENTITY_ARG) {
|
||||
attribute_filter.include = f.include;
|
||||
attribute_filter.traverse = f.traverse;
|
||||
attribute_filter.traverse = attribute_filter.traverse_openings = f.traverse;
|
||||
attribute_filter.attribute_name = f.arg;
|
||||
attribute_filter.populate(f.values);
|
||||
}
|
||||
|
||||
@@ -117,17 +117,28 @@ void USDSerializer::writeHeader() {
|
||||
stage_->GetRootLayer()->SetComment("File generated by IfcOpenShell " + std::string(IFCOPENSHELL_VERSION));
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::string nameObject(const IfcGeom::Element* o) {
|
||||
std::string name = o->name();
|
||||
const std::string type = o->type();
|
||||
const std::string id = std::to_string(o->id());
|
||||
if (name.empty()) {
|
||||
name = type + "_UnNamed_" + id;
|
||||
} else {
|
||||
name = type + "_" + usd_utils::toPath(name) + "_" + id;
|
||||
std::string USDSerializer::object_id_unique(const IfcGeom::Element* o) {
|
||||
auto it = element_names_.find(o->id());
|
||||
if (it != element_names_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
int postfix = 0;
|
||||
auto name = object_id(o);
|
||||
auto suffix = "-" + boost::to_lower_copy(o->context());
|
||||
if (name.size() > suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), name.rbegin())) {
|
||||
name = name.substr(0, name.size() - suffix.size());
|
||||
}
|
||||
while (true) {
|
||||
auto unique_name = name;
|
||||
if (postfix) {
|
||||
unique_name += "_" + std::to_string(postfix);
|
||||
}
|
||||
return name;
|
||||
unique_name = usd_utils::toPath(unique_name);
|
||||
if (emitted_names_.find(unique_name) == emitted_names_.end()) {
|
||||
emitted_names_.insert(unique_name);
|
||||
return element_names_[o->id()] = unique_name;
|
||||
}
|
||||
postfix += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +164,7 @@ T USDSerializer::writeNode(const IfcGeom::Element* o, const IfcGeom::Element* p)
|
||||
m = placements_[p->id()]->ccomponents().inverse() * m;
|
||||
} else {
|
||||
std::vector<std::string> names;
|
||||
std::transform(o->parents().begin(), o->parents().end(), std::back_inserter(names), nameObject);
|
||||
std::transform(o->parents().begin(), o->parents().end(), std::back_inserter(names), [this](auto& i) { return object_id_unique(i); });
|
||||
std::ostringstream oss;
|
||||
std::copy(names.begin(), names.end(), std::ostream_iterator<std::string>(oss, "/"));
|
||||
prefix += oss.str();
|
||||
@@ -161,12 +172,13 @@ T USDSerializer::writeNode(const IfcGeom::Element* o, const IfcGeom::Element* p)
|
||||
// std::ostringstream pss;
|
||||
// pss << "MATT" << std::endl << std::endl;
|
||||
// pss << m << std::endl << std::endl << o->parents().back()->transformation().data()->ccomponents() << std::endl << std::endl;
|
||||
if (!o->parents().empty())
|
||||
m = o->parents().back()->transformation().data()->ccomponents().inverse() * m;
|
||||
// pss << m << std::endl << std::endl;
|
||||
// auto psss = pss.str();
|
||||
// std::wcout << psss.c_str() << std::endl;
|
||||
}
|
||||
auto el_path = prefix + nameObject(o);
|
||||
auto el_path = prefix + object_id_unique(o);
|
||||
paths_[o->id()] = el_path;
|
||||
|
||||
T t = T::Define(stage_, pxr::SdfPath(el_path));
|
||||
@@ -189,7 +201,8 @@ void USDSerializer::write(const IfcGeom::TriangulationElement* o) {
|
||||
parents_.push_back({ *it, previous });
|
||||
previous = *it;
|
||||
}
|
||||
pxr::UsdGeomMesh usd_mesh = writeNode<pxr::UsdGeomMesh>(o);
|
||||
pxr::UsdGeomXform usd_mesh_container = writeNode<pxr::UsdGeomXform>(o); // writeNode<pxr::UsdGeomMesh>(o);
|
||||
auto usd_mesh = pxr::UsdGeomMesh::Define(stage_, pxr::SdfPath(usd_mesh_container.GetPath().GetString() + "/" + o->context()));
|
||||
const IfcGeom::Representation::Triangulation& mesh = o->geometry();
|
||||
const auto verts = mesh.verts();
|
||||
const auto faces = mesh.faces();
|
||||
|
||||
@@ -78,6 +78,8 @@ private:
|
||||
std::set<int> written_;
|
||||
std::map<int, std::string> paths_;
|
||||
std::map<int, ifcopenshell::geometry::taxonomy::matrix4::ptr> placements_;
|
||||
std::set<std::string> emitted_names_;
|
||||
std::map<int, std::string> element_names_;
|
||||
public:
|
||||
USDSerializer(const std::string&, const ifcopenshell::geometry::Settings&, const ifcopenshell::geometry::SerializerSettings&);
|
||||
virtual ~USDSerializer();
|
||||
@@ -89,6 +91,7 @@ public:
|
||||
bool isTesselated() const { return true; }
|
||||
void setUnitNameAndMagnitude(const std::string&, float) {}
|
||||
void setFile(IfcParse::IfcFile*) {}
|
||||
std::string object_id_unique(const IfcGeom::Element* o);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user