Salome HOME
Correct case when the weak-named attribute is dumped in Geom mode: geometrical repres...
[modules/shaper.git] / src / GeomAPI / GeomAPI_Wire.cpp
index 039be40a873ffb450f9b7ad609a0b1bffd9c2453..65729cbf7bed5f9d49b16703852d3c74f73a366e 100644 (file)
 //
 
 #include "GeomAPI_Wire.h"
+#include "GeomAPI_Pnt.h"
 
 #include <BRep_Builder.hxx>
+#include <BRepTools_WireExplorer.hxx>
+#include <Geom_Line.hxx>
+#include <Precision.hxx>
+#include <Standard_Type.hxx>
+#include <TopoDS.hxx>
 #include <TopoDS_Wire.hxx>
 
 //==================================================================================================
@@ -33,3 +39,96 @@ GeomAPI_Wire::GeomAPI_Wire()
 
   this->setImpl(aWire);
 }
+
+//==================================================================================================
+GeomAPI_Wire::GeomAPI_Wire(const std::shared_ptr<GeomAPI_Shape>& theShape)
+{
+  if (!theShape->isNull() && theShape->isWire()) {
+    setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
+  }
+}
+
+//==================================================================================================
+bool GeomAPI_Wire::isClosed() const
+{
+  return BRep_Tool::IsClosed(impl<TopoDS_Shape>());
+}
+
+//==================================================================================================
+bool GeomAPI_Wire::isPolygon(std::list<GeomPointPtr>& thePoints) const
+{
+  const TopoDS_Wire& aWire = TopoDS::Wire(impl<TopoDS_Shape>());
+
+  bool isPolygon = true;
+  const Handle(Standard_Type)& aLineType = STANDARD_TYPE(Geom_Line);
+  for (BRepTools_WireExplorer anExp(aWire); anExp.More() && isPolygon; anExp.Next()) {
+    const TopoDS_Edge& anEdge = anExp.Current();
+    double aT1, aT2;
+    Handle(Geom_Curve) aC3D = BRep_Tool::Curve(anEdge, aT1, aT2);
+    if (!aC3D.IsNull() && aC3D->IsKind(aLineType)) {
+      gp_Pnt aCorner = BRep_Tool::Pnt(anExp.CurrentVertex());
+      thePoints.push_back(GeomPointPtr(new GeomAPI_Pnt(aCorner.X(), aCorner.Y(), aCorner.Z())));
+    }
+    else
+      isPolygon = false;
+  }
+
+  if (!isPolygon)
+    thePoints.clear();
+  return isPolygon;
+}
+
+//==================================================================================================
+bool GeomAPI_Wire::isRectangle(std::list<GeomPointPtr>& thePoints) const
+{
+  thePoints.clear();
+
+  const TopoDS_Wire& aWire = TopoDS::Wire(impl<TopoDS_Shape>());
+  const Handle(Standard_Type)& aLineType = STANDARD_TYPE(Geom_Line);
+
+  gp_XYZ aPrevDir(0, 0, 0);
+
+  for (BRepTools_WireExplorer anExp(aWire); anExp.More(); anExp.Next()) {
+    const TopoDS_Edge& anEdge = anExp.Current();
+    double aT1, aT2;
+    Handle(Geom_Curve) aC3D = BRep_Tool::Curve(anEdge, aT1, aT2);
+    if (!aC3D.IsNull() && aC3D->IsKind(aLineType)) {
+      gp_Pnt aCorner = BRep_Tool::Pnt(anExp.CurrentVertex());
+      thePoints.push_back(GeomPointPtr(new GeomAPI_Pnt(aCorner.X(), aCorner.Y(), aCorner.Z())));
+    }
+    else
+      return false;
+
+    if (thePoints.size() > 4)
+      return false;
+
+    // collect length of the edge
+    gp_Pnt aStart = aC3D->Value(aT1);
+    gp_Pnt aEnd = aC3D->Value(aT2);
+
+    // check the edge is orthogonal to the previous
+    gp_XYZ aCurDir = (aEnd.XYZ() - aStart.XYZ()).Normalized();
+    if (aPrevDir.Dot(aCurDir) < Precision::Confusion())
+      aPrevDir = aCurDir;
+    else
+      return false;
+  }
+  return true;
+}
+
+//==================================================================================================
+GeomPointPtr GeomAPI_Wire::middlePoint() const
+{
+  // find middle edge in the wire
+  std::list<GeomShapePtr> aSubs = subShapes(EDGE);
+  size_t aNbSubs = aSubs.size();
+  if (aNbSubs == 0)
+    return GeomPointPtr();
+
+  aNbSubs /= 2;
+  for (; aNbSubs > 0; --aNbSubs)
+    aSubs.pop_front();
+
+  // compute middle point on the middle edge
+  return aSubs.front()->middlePoint();
+}