From e381b2332a6a5244fe82ffcdd5bb4d9ca1c227c1 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Tue, 3 Jan 2017 16:59:16 +0800 Subject: [PATCH] Added Part.sortEdges Unlike Part.__sortEdges__ which only return a list of connected edges, and discard the rest. Part.sortEdges return a list of list of connected edges, which includes all input edges. --- src/Mod/Part/App/AppPartPy.cpp | 51 ++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Mod/Part/App/AppPartPy.cpp b/src/Mod/Part/App/AppPartPy.cpp index 6928a3d0c..1a5b24493 100644 --- a/src/Mod/Part/App/AppPartPy.cpp +++ b/src/Mod/Part/App/AppPartPy.cpp @@ -143,20 +143,22 @@ extern const char* BRepBuilderAPI_FaceErrorText(BRepBuilderAPI_FaceError fe); namespace Part { struct EdgePoints { gp_Pnt v1, v2; + std::list::iterator it; TopoDS_Edge edge; }; -static std::list sort_Edges(double tol3d, const std::vector& edges) +static std::list sort_Edges(double tol3d, std::list& edges) { tol3d = tol3d * tol3d; std::list edge_points; TopExp_Explorer xp; - for (std::vector::const_iterator it = edges.begin(); it != edges.end(); ++it) { + for (std::list::iterator it = edges.begin(); it != edges.end(); ++it) { EdgePoints ep; xp.Init(*it,TopAbs_VERTEX); ep.v1 = BRep_Tool::Pnt(TopoDS::Vertex(xp.Current())); xp.Next(); ep.v2 = BRep_Tool::Pnt(TopoDS::Vertex(xp.Current())); + ep.it = it; ep.edge = *it; edge_points.push_back(ep); } @@ -170,6 +172,7 @@ static std::list sort_Edges(double tol3d, const std::vector sort_Edges(double tol3d, const std::vectorv1.SquareDistance(last) <= tol3d) { last = pEI->v2; sorted.push_back(pEI->edge); + edges.erase(pEI->it); edge_points.erase(pEI); pEI = edge_points.begin(); break; @@ -186,6 +190,7 @@ static std::list sort_Edges(double tol3d, const std::vectorv2.SquareDistance(first) <= tol3d) { first = pEI->v1; sorted.push_front(pEI->edge); + edges.erase(pEI->it); edge_points.erase(pEI); pEI = edge_points.begin(); break; @@ -198,6 +203,7 @@ static std::list sort_Edges(double tol3d, const std::vectorReversedParameter(last); TopoDS_Edge edgeReversed = BRepBuilderAPI_MakeEdge(curve->Reversed(), last, first); sorted.push_back(edgeReversed); + edges.erase(pEI->it); edge_points.erase(pEI); pEI = edge_points.begin(); break; @@ -210,6 +216,7 @@ static std::list sort_Edges(double tol3d, const std::vectorReversedParameter(last); TopoDS_Edge edgeReversed = BRepBuilderAPI_MakeEdge(curve->Reversed(), last, first); sorted.push_front(edgeReversed); + edges.erase(pEI->it); edge_points.erase(pEI); pEI = edge_points.begin(); break; @@ -357,6 +364,9 @@ public: "__sortEdges__(list of edges) -- Helper method to sort an unsorted list of edges so that afterwards\n" "two adjacent edges share a common vertex" ); + add_varargs_method("sortEdges",&Module::sortEdges2, + "sortEdges(list of edges) -- Helper method to sort a list of edges into a list of list of connected edges" + ); add_varargs_method("__toPythonOCC__",&Module::toPythonOCC, "__toPythonOCC__(shape) -- Helper method to convert an internal shape to pythonocc shape" ); @@ -1742,7 +1752,7 @@ private: } Py::Sequence list(obj); - std::vector edges; + std::list edges; for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { PyObject* item = (*it).ptr(); if (PyObject_TypeCheck(item, &(Part::TopoShapePy::Type))) { @@ -1766,6 +1776,41 @@ private: return sorted_list; } + Py::Object sortEdges2(const Py::Tuple& args) + { + PyObject *obj; + if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) { + throw Py::Exception(PartExceptionOCCError, "list of edges expected"); + } + + Py::Sequence list(obj); + std::list edges; + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + PyObject* item = (*it).ptr(); + if (PyObject_TypeCheck(item, &(Part::TopoShapePy::Type))) { + const TopoDS_Shape& sh = static_cast(item)->getTopoShapePtr()->getShape(); + if (sh.ShapeType() == TopAbs_EDGE) + edges.push_back(TopoDS::Edge(sh)); + else { + throw Py::TypeError("shape is not an edge"); + } + } + else { + throw Py::TypeError("item is not a shape"); + } + } + + Py::List root_list; + while(edges.size()) { + std::list sorted = sort_Edges(Precision::Confusion(), edges); + Py::List sorted_list; + for (std::list::iterator it = sorted.begin(); it != sorted.end(); ++it) { + sorted_list.append(Py::Object(new TopoShapeEdgePy(new TopoShape(*it)),true)); + } + root_list.append(sorted_list); + } + return root_list; + } Py::Object toPythonOCC(const Py::Tuple& args) { PyObject *pcObj;