Salome HOME
OCC functionality moving out from the widget
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_Utils.cxx
index 12d7ee7d84cf92d6a17a80aef4630e1eaf760122..f4e49253ac69e7ff38c073248b656cbf1622a1b5 100644 (file)
@@ -18,6 +18,8 @@
 //
 
 #include "CurveCreator_Utils.h"
+#include "CurveCreator.hxx"
+#include "CurveCreator_UtilsICurve.hxx"
 
 #include <GEOMUtils.hxx>
 
@@ -27,6 +29,7 @@
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS_Wire.hxx>
 #include <TopoDS_Edge.hxx>
+#include <TopoDS_Compound.hxx>
 
 #include <AIS_ListOfInteractive.hxx>
 #include <AIS_ListIteratorOfListOfInteractive.hxx>
 #include <Geom_Line.hxx>
 
 #include <TopExp_Explorer.hxx>
-#include <BRep_Tool.hxx>
 #include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <SelectMgr_EntityOwner.hxx>
 
+#include <BRep_Tool.hxx>
+#include <BRep_Builder.hxx>
+#include <BRepBuilderAPI_MakeVertex.hxx>
+#include <BRepBuilderAPI_MakeEdge.hxx>
+#include <BRepBuilderAPI_MakeWire.hxx>
+
+#include <TColgp_HArray1OfPnt.hxx>
+#include <GeomAPI_Interpolate.hxx>
+
 #include <ProjLib.hxx>
 #include <ElSLib.hxx>
 
+#include "CurveCreator_ICurve.hxx"
+
 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
 const int    SCENE_PIXEL_TOLERANCE = 10;
 
@@ -71,6 +84,135 @@ gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) a
   return GEOMUtils::ConvertClickToPoint( x, y, aView );
 }
 
+
+//#define USE_COMPOUND
+#include "CurveCreator_Curve.hxx" // TODO - remove
+void CurveCreator_Utils::constructShape( const CurveCreator_ICurve* theCurve,
+                                         const int theISection, TopoDS_Shape& theShape,
+                                         std::vector<Handle_AIS_InteractiveObject>& aSectionRepresentation )
+{
+  CurveCreator::SectionType aSectType = theCurve->getSectionType( theISection );
+
+
+  int aPointSize = theCurve->getNbPoints( theISection );
+  bool aSectIsClosed = theCurve->isClosed( theISection );
+  if( aSectType == CurveCreator::Polyline )
+  {
+#ifdef USE_COMPOUND
+    BRep_Builder aBuilder;
+    TopoDS_Compound aComp;
+    aBuilder.MakeCompound(aComp);
+
+    int iPoint = 0;
+    gp_Pnt aPrevPoint, aPoint;
+    if ( aPointSize == 1 ) {
+      CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
+      TopoDS_Vertex aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
+      aBuilder.Add( aComp, aVertex );
+    }
+    else if ( aPointSize > 1 ) {
+      TopoDS_Edge aPointEdge;
+      TopoDS_Vertex aVertex;
+      CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPrevPoint );
+      aVertex = BRepBuilderAPI_MakeVertex( aPrevPoint ).Vertex();
+      aBuilder.Add( aComp, aVertex );
+      iPoint++;
+      for( ; iPoint < aPointSize; iPoint++ ) {
+        CurveCreator_UtilsICurve::getPoint( theCurve, theISection, iPoint, aPoint );
+        aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+        aBuilder.Add( aComp, aVertex );
+        aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
+        aBuilder.Add( aComp, aPointEdge );
+        aPrevPoint = aPoint;
+      }
+      if( aSectIsClosed && ( aPointSize > 2 ) ) {
+        CurveCreator_UtilsICurve::getPoint( theCurve, theISection, 0, aPoint );
+        aVertex = BRepBuilderAPI_MakeVertex( aPoint ).Vertex();
+        aBuilder.Add( aComp, aVertex );
+        aPointEdge = BRepBuilderAPI_MakeEdge( aPrevPoint, aPoint ).Edge();
+        aBuilder.Add( aComp, aPointEdge );
+      }
+      theShape = aComp;
+    }
+#endif
+  }
+  else if( aSectType == CurveCreator::Spline )
+  {
+    std::vector<double> aPoints;
+    for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
+    {
+      CurveCreator::Coordinates aCoords = theCurve->getPoint( theISection, iPoint );
+      double aX = aCoords[0];
+      double aY = aCoords[1];
+      double aZ = aCoords[2];
+      aPoints.push_back( aX );
+      aPoints.push_back( aY );
+    }
+
+    if( aPointSize > 1 )
+    {
+      Handle(Geom_BSplineCurve) aBSplineCurve;
+      // fill array for algorithm by the received coordinates
+      int aLen = aPoints.size() / 2;
+      Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
+      std::vector<double>::const_iterator aListIter = aPoints.begin();
+      for (int ind = 1; ind <= aLen; ind++) {
+        gp_Pnt aPnt(gp::Origin());
+        aPnt.SetX(*aListIter);
+        aListIter++;
+        aPnt.SetY(*aListIter);
+        aListIter++;
+        aPnt.SetZ(0);
+        aHCurvePoints->SetValue(ind, aPnt);
+      }
+      // compute BSpline
+      GeomAPI_Interpolate aGBC(aHCurvePoints, aSectIsClosed, gp::Resolution());
+      aGBC.Perform();
+      if (aGBC.IsDone()) {
+        aBSplineCurve = aGBC.Curve();
+      }
+      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSplineCurve ).Edge();
+
+      TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge ).Wire();
+      theShape = aWire;
+    }
+  }
+
+  const CurveCreator_Curve* aCurve = dynamic_cast<const CurveCreator_Curve*>( theCurve );
+  if ( !aCurve )
+    return;
+
+  if( aSectType == CurveCreator::Polyline )
+  {
+#ifndef USE_COMPOUND
+    int iPoint = 0; 
+    for( ; iPoint < ( aPointSize - 1 ) ; iPoint++ ){
+      Handle_AIS_Point anAISPnt = aCurve->getAISPoint(theISection, iPoint);
+      aSectionRepresentation.push_back( anAISPnt );
+      Handle_AIS_Line aLine = aCurve->getAISLine( theISection, iPoint, iPoint+1 );
+      aSectionRepresentation.push_back( aLine );
+    }
+    if( aPointSize != 0 ){
+      Handle_AIS_Point anAISPnt = aCurve->getAISPoint(theISection, iPoint); 
+      aSectionRepresentation.push_back( anAISPnt );
+      if( aSectIsClosed && ( aPointSize > 1 ) ){
+        Handle_AIS_Line aLine = aCurve->getAISLine( theISection, iPoint, 0 );
+        aSectionRepresentation.push_back( aLine );
+      }
+    }
+#endif
+  }
+  else if( aSectType == CurveCreator::Spline )
+  {
+    std::vector<double> aPoints;
+    for( int iPoint = 0; iPoint < aPointSize; iPoint++ )
+    {
+      Handle_AIS_Point anAISPnt = aCurve->getAISPoint( theISection, iPoint );
+      aSectionRepresentation.push_back( anAISPnt );
+    }
+  }
+}
+
 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
 {
   std::list<float> aSelectedPoints;
@@ -119,12 +261,12 @@ std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveCo
 // function : setLocalPointContext
 // purpose  : Open/close the viewer local context
 //=======================================================================
-//#define USE_COMPOUND
+//#define USE_GLOBAL_SELECTION
 void CurveCreator_Utils::setLocalPointContext(
                                               Handle(AIS_InteractiveContext) theContext,
                                               const bool theOpen )
 {
-#ifdef USE_COMPOUND
+#ifdef USE_GLOBAL_SELECTION
   return;
 #endif
   if ( !theContext )