From 07bce810aeedabc55fd677a18bb640e21cb945e2 Mon Sep 17 00:00:00 2001 From: Ajinkya Dahale Date: Thu, 3 Apr 2025 23:23:54 +0530 Subject: [PATCH] [Sketcher][WIP] Refactor `SketchObject::buildShape()` --- src/Mod/Sketcher/App/SketchObject.cpp | 111 ++++++++++++++------------ 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 2f8bf86bf0..888f2e0a80 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -324,36 +324,48 @@ static bool inline checkSmallEdge(const Part::TopoShape &s) { return GCPnts_AbscissaPoint::Length(adapt, Precision::Confusion()) <= Precision::Confusion(); } -void SketchObject::buildShape() { +// clang-format on +void SketchObject::buildShape() +{ // We use the following instead to map element names std::vector shapes; std::vector vertices; - int geoId =0; + int geoId = 0; + + auto addVertex = [this, &vertices](auto vertex, auto name) { + if (!vertex.hasElementMap()) { + vertex.resetElementMap(std::make_shared()); + } + vertex.setElementName(Data::IndexedName::fromConst("Vertex", 1), + Data::MappedName::fromRawData(name.c_str()), + 0L); + vertices.push_back(vertex); + vertices.back().copyElementMap(vertex, Part::OpCodes::Sketch); + }; + + auto addEdge = [this, &shapes](auto geo, auto indexedName) { + shapes.push_back(getEdge(geo, convertSubName(indexedName, false).c_str())); + if (checkSmallEdge(shapes.back())) { + FC_WARN("Edge too small: " << indexedName); + } + }; // get the geometry after running the solver auto geometries = solvedSketch.extractGeometry(); - for(auto geo : geometries) { + for (auto geo : geometries) { ++geoId; - if(GeometryFacade::getConstruction(geo)) { + if (GeometryFacade::getConstruction(geo)) { continue; } if (geo->isDerivedFrom()) { - Part::TopoShape vertex(TopoDS::Vertex(geo->toShape())); - int idx = getVertexIndexGeoPos(geoId -1, Sketcher::PointPos::start); - std::string name = convertSubName(Data::IndexedName::fromConst("Vertex", idx+1), false); - if (!vertex.hasElementMap()) { - vertex.resetElementMap(std::make_shared()); - } vertex.setElementName(Data::IndexedName::fromConst("Vertex", 1), - Data::MappedName::fromRawData(name.c_str()),0L); - vertices.push_back(vertex); - vertices.back().copyElementMap(vertex, Part::OpCodes::Sketch); - } else { + int idx = getVertexIndexGeoPos(geoId - 1, Sketcher::PointPos::start); + addVertex(Part::TopoShape {TopoDS::Vertex(geo->toShape())}, + convertSubName(Data::IndexedName::fromConst("Vertex", idx + 1), false)); + } + else { auto indexedName = Data::IndexedName::fromConst("Edge", geoId); - shapes.push_back(getEdge(geo,convertSubName(indexedName, false).c_str())); - if (checkSmallEdge(shapes.back())) { - FC_WARN("Edge too small: " << indexedName); - } + addEdge(geo, indexedName); } } @@ -361,57 +373,53 @@ void SketchObject::buildShape() { delete geo; } - for(int i=2;itestFlag(ExternalGeometryExtension::Defining)) + if (!egf->testFlag(ExternalGeometryExtension::Defining)) { continue; + } - auto indexedName = Data::IndexedName::fromConst("ExternalEdge", i-1); + auto indexedName = Data::IndexedName::fromConst("ExternalEdge", i - 1); if (geo->isDerivedFrom()) { - Part::TopoShape vertex(TopoDS::Vertex(geo->toShape())); - if (!vertex.hasElementMap()) { - vertex.resetElementMap(std::make_shared()); - } - vertex.setElementName(Data::IndexedName::fromConst("Vertex", 1), - Data::MappedName::fromRawData(convertSubName(indexedName, false).c_str()),0L); - vertices.push_back(vertex); - vertices.back().copyElementMap(vertex, Part::OpCodes::Sketch); - } else { + addVertex(Part::TopoShape {TopoDS::Vertex(geo->toShape())}, + convertSubName(indexedName, false)); + } + else { shapes.push_back(getEdge(geo, convertSubName(indexedName, false).c_str())); - if (checkSmallEdge(shapes.back())) { - FC_WARN("Edge too small: " << indexedName); - } + addEdge(geo, indexedName); } } internalElementMap.clear(); - if(shapes.empty() && vertices.empty()) { + if (shapes.empty() && vertices.empty()) { InternalShape.setValue(Part::TopoShape()); Shape.setValue(Part::TopoShape()); return; } Part::TopoShape result(0, getDocument()->getStringHasher()); if (vertices.empty()) { - // Notice here we supply op code Part::OpCodes::Sketch to makEWires(). - result.makeElementWires(shapes,Part::OpCodes::Sketch); - } else { - std::vector results; - if (!shapes.empty()) { - // Note, that we HAVE TO add the Part::OpCodes::Sketch op code to all - // geometry exposed through the Shape property, because - // SketchObject::getElementName() relies on this op code to - // differentiate geometries that are exposed with those in edit - // mode. - auto wires = Part::TopoShape().makeElementWires(shapes, Part::OpCodes::Sketch); - for (const auto &wire : wires.getSubTopoShapes(TopAbs_WIRE)) - results.push_back(wire); - } - results.insert(results.end(), vertices.begin(), vertices.end()); - result.makeElementCompound(results); - } + // Notice here we supply op code Part::OpCodes::Sketch to makEWires(). + result.makeElementWires(shapes, Part::OpCodes::Sketch); + } + else { + std::vector results; + if (!shapes.empty()) { + // Note, that we HAVE TO add the Part::OpCodes::Sketch op code to all + // geometry exposed through the Shape property, because + // SketchObject::getElementName() relies on this op code to + // differentiate geometries that are exposed with those in edit + // mode. + auto wires = Part::TopoShape().makeElementWires(shapes, Part::OpCodes::Sketch); + for (const auto& wire : wires.getSubTopoShapes(TopAbs_WIRE)) { + results.push_back(wire); + } + } + results.insert(results.end(), vertices.begin(), vertices.end()); + result.makeElementCompound(results); + } result.Tag = getID(); InternalShape.setValue(buildInternals(result.located(TopLoc_Location()))); // Must set Shape property after InternalShape so that @@ -419,6 +427,7 @@ void SketchObject::buildShape() { // property, because some reference may pointing to the InternalShape Shape.setValue(result); } +// clang-format off const std::map SketchObject::getInternalElementMap() const {