Part: TopoShape make getElementTypeAndIndex more robust (#25913)

* Part: TopoShape make getElementTypeAndIndex more robust
* Part: Add unit tests for new regex

---------

Co-authored-by: Chris Hennes <chennes@gmail.com>
This commit is contained in:
PaddleStroke
2025-12-08 18:24:13 +01:00
committed by GitHub
parent 6f511f7911
commit 3608c9f8c4
2 changed files with 24 additions and 1 deletions

View File

@@ -299,7 +299,9 @@ std::pair<std::string, unsigned long> TopoShape::getElementTypeAndIndex(const ch
{ {
int index = 0; int index = 0;
std::string element; std::string element;
boost::regex ex("^(Face|Edge|Vertex)([1-9][0-9]*)$"); // Regex modified to allow a prefix ending in a separator (e.g. TNP hash or Dot notation)
// Matches "Face3", "Part.Face3", or ";#7:1;:G0...F.Face3"
boost::regex ex("^(?:.*[.;:,])?(Face|Edge|Vertex)([1-9][0-9]*)$");
boost::cmatch what; boost::cmatch what;
if (Name && boost::regex_match(Name, what, ex)) { if (Name && boost::regex_match(Name, what, ex)) {

View File

@@ -78,6 +78,27 @@ TEST_F(TopoShapeTest, TestElementTypeNull)
std::make_pair(std::string(), 0UL)); std::make_pair(std::string(), 0UL));
} }
TEST_F(TopoShapeTest, TestElementTypeWithHash)
{
EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex(";#7:1;:G0;XTR;:H11a6:8,F.Face3"),
std::make_pair(std::string("Face"), 3UL));
}
TEST_F(TopoShapeTest, TestElementTypeWithSubelements)
{
EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex("Part.Body.Pad.Face3"),
std::make_pair(std::string("Face"), 3UL));
}
TEST_F(TopoShapeTest, TestElementTypeNonMatching)
{
for (std::array elements = {"Face0", "Face01", "XFace3", "Face3extra"};
const auto& element : elements) {
EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex(element),
std::make_pair(std::string(), 0UL));
}
}
TEST_F(TopoShapeTest, TestTypeFace1) TEST_F(TopoShapeTest, TestTypeFace1)
{ {
EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Face1"), EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Face1"),