From 78f115fd8a969ad2548563f20ed64726b3710c84 Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Mon, 8 Dec 2025 18:24:13 +0100 Subject: [PATCH] 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 --- src/Mod/Part/App/TopoShape.cpp | 4 +++- tests/src/Mod/Part/App/TopoShape.cpp | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Mod/Part/App/TopoShape.cpp b/src/Mod/Part/App/TopoShape.cpp index a3852749d7..dba69ae4fe 100644 --- a/src/Mod/Part/App/TopoShape.cpp +++ b/src/Mod/Part/App/TopoShape.cpp @@ -299,7 +299,9 @@ std::pair TopoShape::getElementTypeAndIndex(const ch { int index = 0; 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; if (Name && boost::regex_match(Name, what, ex)) { diff --git a/tests/src/Mod/Part/App/TopoShape.cpp b/tests/src/Mod/Part/App/TopoShape.cpp index a133e66381..d22d3b0190 100644 --- a/tests/src/Mod/Part/App/TopoShape.cpp +++ b/tests/src/Mod/Part/App/TopoShape.cpp @@ -78,6 +78,27 @@ TEST_F(TopoShapeTest, TestElementTypeNull) 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) { EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Face1"),