From f028bde0df13db6e85fa966734e1d380154aac3f Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Thu, 9 Mar 2017 12:12:41 +0800 Subject: [PATCH] Path.Area: improved code readablity --- src/Mod/Path/App/Area.cpp | 91 +++++++++++++++++++-------------------- src/Mod/Path/App/Area.h | 15 +++++-- 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/Mod/Path/App/Area.cpp b/src/Mod/Path/App/Area.cpp index 60544222b2..8d14edd68e 100644 --- a/src/Mod/Path/App/Area.cpp +++ b/src/Mod/Path/App/Area.cpp @@ -65,7 +65,6 @@ #include #include -#include #include #include #include @@ -172,7 +171,7 @@ bool Area::isCoplanar(const TopoDS_Shape &s1, const TopoDS_Shape &s2) { return planeFinder.Found(); } -int Area::add(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf, +int Area::addShape(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf, double deflection, const TopoDS_Shape *plane, bool force_coplanar, CArea *areaOpen, bool to_edges, bool reorient) { @@ -186,7 +185,7 @@ int Area::add(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf, if(force_coplanar) continue; } for (TopExp_Explorer it(face, TopAbs_WIRE); it.More(); it.Next()) - add(area,TopoDS::Wire(it.Current()),trsf,deflection); + addWire(area,TopoDS::Wire(it.Current()),trsf,deflection); } if(haveShape) return skipped; @@ -202,13 +201,13 @@ int Area::add(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf, if(force_coplanar) continue; } if(BRep_Tool::IsClosed(wire)) - add(_area,wire,trsf,deflection); + addWire(_area,wire,trsf,deflection); else if(to_edges) { for (TopExp_Explorer it(wire, TopAbs_EDGE); it.More(); it.Next()) - add(_areaOpen,BRepBuilderAPI_MakeWire( + addWire(_areaOpen,BRepBuilderAPI_MakeWire( TopoDS::Edge(it.Current())).Wire(),trsf,deflection,true); }else - add(_areaOpen,wire,trsf,deflection); + addWire(_areaOpen,wire,trsf,deflection); } if(!haveShape) { @@ -219,7 +218,7 @@ int Area::add(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf, } TopoDS_Wire wire = BRepBuilderAPI_MakeWire( TopoDS::Edge(it.Current())).Wire(); - add(BRep_Tool::IsClosed(wire)?_area:_areaOpen,wire,trsf,deflection); + addWire(BRep_Tool::IsClosed(wire)?_area:_areaOpen,wire,trsf,deflection); } } @@ -233,7 +232,7 @@ int Area::add(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf, return skipped; } -void Area::add(CArea &area, const TopoDS_Wire& wire, +void Area::addWire(CArea &area, const TopoDS_Wire& wire, const gp_Trsf *trsf, double deflection, bool to_edges) { CCurve ccurve; @@ -334,33 +333,33 @@ void Area::clean(bool deleteShapes) { } } -void Area::add(const TopoDS_Shape &shape,short op) { -#define AREA_CONVERT_OP \ - ClipperLib::ClipType Operation;\ - switch(op){\ - case OperationUnion:\ - Operation = ClipperLib::ctUnion;\ - break;\ - case OperationDifference:\ - Operation = ClipperLib::ctDifference;\ - break;\ - case OperationIntersection:\ - Operation = ClipperLib::ctIntersection;\ - break;\ - case OperationXor:\ - Operation = ClipperLib::ctXor;\ - break;\ - default:\ - throw Base::ValueError("invalid Operation");\ +static inline ClipperLib::ClipType toClipperOp(short op) { + switch(op){ + case Area::OperationUnion: + return ClipperLib::ctUnion; + break; + case Area::OperationDifference: + return ClipperLib::ctDifference; + break; + case Area::OperationIntersection: + return ClipperLib::ctIntersection; + break; + case Area::OperationXor: + return ClipperLib::ctXor; + break; + default: + throw Base::ValueError("invalid Operation"); } +} + +void Area::add(const TopoDS_Shape &shape,short op) { if(shape.IsNull()) throw Base::ValueError("null shape"); - if(op!=OperationCompound) { - AREA_CONVERT_OP; - Q_UNUSED(Operation); - } + if(op!=OperationCompound) + toClipperOp(op); + bool haveSolid = false; for(TopExp_Explorer it(shape, TopAbs_SOLID);it.More();) { haveSolid = true; @@ -397,7 +396,7 @@ void Area::addToBuild(CArea &area, const TopoDS_Shape &shape) { } TopoDS_Shape plane = getPlane(); CArea areaOpen; - mySkippedShapes += add(area,shape,&myTrsf,myParams.Deflection, + mySkippedShapes += addShape(area,shape,&myTrsf,myParams.Deflection, myParams.Coplanar==CoplanarNone?NULL:&plane, myHaveSolid||myParams.Coplanar==CoplanarForce,&areaOpen, myParams.OpenMode==OpenModeEdges,myParams.Reorient); @@ -425,7 +424,7 @@ void Area::explode(const TopoDS_Shape &shape) { } for(TopExp_Explorer itw(it.Current(), TopAbs_WIRE); itw.More(); itw.Next()) { for(BRepTools_WireExplorer xp(TopoDS::Wire(itw.Current()));xp.More();xp.Next()) - add(*myArea,BRepBuilderAPI_MakeWire( + addWire(*myArea,BRepBuilderAPI_MakeWire( TopoDS::Edge(xp.Current())).Wire(),&myTrsf,myParams.Deflection,true); } } @@ -436,7 +435,7 @@ void Area::explode(const TopoDS_Shape &shape) { if(myParams.Coplanar == CoplanarForce) continue; } - add(*myArea,BRepBuilderAPI_MakeWire( + addWire(*myArea,BRepBuilderAPI_MakeWire( TopoDS::Edge(it.Current())).Wire(),&myTrsf,myParams.Deflection,true); } } @@ -759,8 +758,7 @@ void Area::build() { if(op == OperationCompound) myArea->m_curves.splice(myArea->m_curves.end(),areaClip.m_curves); else{ - AREA_CONVERT_OP; - myArea->Clip(Operation,&areaClip,SubjectFill,ClipFill); + myArea->Clip(toClipperOp(op),&areaClip,SubjectFill,ClipFill); areaClip.m_curves.clear(); } } @@ -779,8 +777,7 @@ void Area::build() { if(op == OperationCompound) myArea->m_curves.splice(myArea->m_curves.end(),areaClip.m_curves); else{ - AREA_CONVERT_OP; - myArea->Clip(Operation,&areaClip,SubjectFill,ClipFill); + myArea->Clip(toClipperOp(op),&areaClip,SubjectFill,ClipFill); } } myArea->m_curves.splice(myArea->m_curves.end(),myAreaOpen->m_curves); @@ -1223,7 +1220,7 @@ TopoDS_Wire Area::toShape(const CCurve &c, const gp_Trsf *trsf) { newCenter.SetY(y - dy); } AREA_WARN("Arc correction: "<"<"< sortWires3D(gp_Pnt &pend,double min_dist) { std::list wires; while(true) { - AREA_TRACE("3D sort pt " << AREA_PT(myBestPt)); + AREA_TRACE("3D sort pt " << AREA_XYZ(myBestPt)); if(myRebase) { AREA_TRACE("3D sort rebase"); pend = myBestPt; @@ -1504,7 +1501,7 @@ struct ShapeInfo{ wires.push_back(myBestWire->wire); pend = myBestWire->pend; } - AREA_TRACE("3D sort end " << AREA_PT(pend)); + AREA_TRACE("3D sort end " << AREA_XYZ(pend)); myWires.erase(myBestWire); if(myWires.empty()) break; nearest(pend); @@ -1628,7 +1625,7 @@ std::list Area::sortWires(const std::list &shapes, } bool has_2d5=false,has_3d=false; while(shape_list.size()) { - AREA_TRACE("start " << shape_list.size() << ' ' << AREA_PT(pstart)); + AREA_TRACE("start " << shape_list.size() << ' ' << AREA_XYZ(pstart)); double best_d; auto best_it = shape_list.begin(); bool first = true; diff --git a/src/Mod/Path/App/Area.h b/src/Mod/Path/App/Area.h index fa67c90c70..c035a49f1a 100644 --- a/src/Mod/Path/App/Area.h +++ b/src/Mod/Path/App/Area.h @@ -34,9 +34,14 @@ #include #include +#include #include "Path.h" #include "AreaParams.h" +namespace Part { +extern PartExport Py::Object shape2pyshape(const TopoDS_Shape &shape); +} + #define _AREA_LOG(_l,_msg) do {\ if(Area::_l##Enabled()){\ std::stringstream str;\ @@ -53,13 +58,15 @@ #define AREA_LOG(_msg) _AREA_LOG(Log,_msg) #define AREA_WARN(_msg) _AREA_LOG(Warning,_msg) #define AREA_ERR(_msg) _AREA_LOG(Error,_msg) -#define AREA_PT(_pt) '('<<(_pt).X()<<", " << (_pt).Y()<<", " << (_pt).Z()<<')' -#define AREA_PT2(_pt) '('<<(_pt).x<<", " << (_pt).y<<')' +#define AREA_XYZ(_pt) '('<<(_pt).X()<<", " << (_pt).Y()<<", " << (_pt).Z()<<')' +#define AREA_XY(_pt) '('<<(_pt).x<<", " << (_pt).y<<')' #define AREA_TRACE(_msg) do{\ if(Area::TraceEnabled()) AREA_LOG('('<<__LINE__<<"): " <<_msg);\ }while(0) +#define AREA_DBG AREA_WARN + #define AREA_TIME_ENABLE #ifdef AREA_TIME_ENABLE @@ -346,7 +353,7 @@ public: * \arg \c to_edges: if true, discretize all curves, and insert as open * line segments * */ - static void add(CArea &area, const TopoDS_Wire &wire, const gp_Trsf *trsf=NULL, + static void addWire(CArea &area, const TopoDS_Wire &wire, const gp_Trsf *trsf=NULL, double deflection=0.01, bool to_edges=false); /** Output a list or sorted wire with minimize traval distance @@ -385,7 +392,7 @@ public: * \return Returns the number of non coplaner. Planar testing only happens * if \c plane is supplied * */ - static int add(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf=NULL, + static int addShape(CArea &area, const TopoDS_Shape &shape, const gp_Trsf *trsf=NULL, double deflection=0.01,const TopoDS_Shape *plane = NULL, bool force_coplanar=true, CArea *areaOpen=NULL, bool to_edges=false, bool reorient=true);