Salome HOME
Updated copyright comment
[modules/geom.git] / src / CurveCreator / CurveCreator_Curve.cxx
index e6be346fa1e7f9781004835cd385ff69f48e97b3..856885bfba258fde7a2b9af5453c65b7b2256bd1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2013-2024  CEA, EDF, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #include <AIS_Shape.hxx>
 #include <AIS_InteractiveObject.hxx>
 #include <Geom_CartesianPoint.hxx>
+#include <TopoDS_Iterator.hxx>
 #include <gp_Pnt.hxx>
 #include <gp_Lin.hxx>
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Face.hxx>
 #include <TopoDS_Wire.hxx>
+#include <TopoDS_Shape.hxx>
+#include <AIS_ColoredShape.hxx>
 
 #include <Prs3d_PointAspect.hxx>
-#include <Handle_Prs3d_PointAspect.hxx>
 #include <iostream>
 #define DEBTRACE(msg) {std::cerr<<std::flush<<__FILE__<<" ["<<__LINE__<<"] : "<<msg<<std::endl<<std::flush;}
 
 // purpose:
 //=======================================================================
 CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimension )
-: myIsLocked  (false),
+: mySkipSorting (false),
+  myIsLocked (false),
   myDimension (theDimension),
   myDisplayer (NULL),
-  myAISShape  (NULL),
-  myNbUndos   (0),
-  myNbRedos   (0),
+  myPointAspectColor (Quantity_NOC_ROYALBLUE4),
+  //myCurveColor (Quantity_NOC_RED),
+  myLineWidth (1),
+  myNbUndos (0),
+  myNbRedos (0),
   myUndoDepth (-1),
-  myOpLevel(0),
-  mySkipSorting(false)
+  myOpLevel (0),
+  myAISShape (NULL),
+  myEraseAll (true)
 {
 }
 
@@ -92,12 +98,12 @@ std::string CurveCreator_Curve::getUniqSectionName() const
         sprintf( aBuffer, "Section_%d", i+1 );
         std::string aName(aBuffer);
         int j;
-        for( j = 0 ; j < mySections.size() ; j++ ){
+        for( j = 0 ; j < (int)mySections.size() ; j++ ){
             aSection = (CurveCreator_Section*)getSection( j );
             if ( aSection && aSection->myName == aName )
               break;
         }
-        if( j == mySections.size() )
+        if( j == (int)mySections.size() )
             return aName;
     }
     return "";
@@ -250,13 +256,16 @@ void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double&
   }
 }
 
-void CurveCreator_Curve::redisplayCurve()
+void CurveCreator_Curve::redisplayCurve(bool preEraseAllObjects)
 {
   //DEBTRACE("redisplayCurve");
-  if( myDisplayer ) {
-    myDisplayer->eraseAll( false );
+  if( myDisplayer )
+  {
+    if (preEraseAllObjects)
+      myDisplayer->eraseAll( false );
+    else
+      myDisplayer->erase( myAISShape, false);
     myAISShape = NULL;
-
     myDisplayer->display( getAISObject( true ), true );
   }
 }
@@ -358,8 +367,12 @@ bool CurveCreator_Curve::redo()
 bool CurveCreator_Curve::clearInternal()
 {
   // erase curve from the viewer
-  if( myDisplayer ) {
-    myDisplayer->eraseAll( true );
+  if( myDisplayer )
+  {
+    if (myEraseAll)
+      myDisplayer->eraseAll( true );
+    else
+      myDisplayer->erase(myAISShape, false);
     myAISShape = NULL;
   }
   // Delete all allocated data.
@@ -374,7 +387,7 @@ bool CurveCreator_Curve::clearInternal()
   }
 
   mySections.clear();
-  
+
   return true;
 }
 
@@ -395,6 +408,23 @@ bool CurveCreator_Curve::clear()
   return res;
 }
 
+//=======================================================================
+// function: clear
+// purpose:
+//=======================================================================
+void CurveCreator_Curve::SetEraseAllState(bool toEraseAll)
+{
+  myEraseAll = toEraseAll;
+}
+//=======================================================================
+// function: clear
+// purpose:
+//=======================================================================
+bool CurveCreator_Curve::GetEraseAllState() const
+{
+  return myEraseAll;
+}
+
 //! For internal use only! Undo/Redo are not used here.
 bool CurveCreator_Curve::joinInternal( const std::list<int>& theSections )
 {
@@ -423,7 +453,7 @@ bool CurveCreator_Curve::joinInternal( const std::list<int>& theSections )
       break;
   }
 
-  redisplayCurve();
+  redisplayCurve(false);
   return res;
 }
 
@@ -453,7 +483,8 @@ int CurveCreator_Curve::getNbSections() const
 //! For internal use only! Undo/Redo are not used here.
 int CurveCreator_Curve::addSectionInternal
         (const std::string& theName, const CurveCreator::SectionType theType,
-         const bool theIsClosed, const CurveCreator::Coordinates &thePoints)
+         const bool theIsClosed, const CurveCreator::Coordinates &thePoints,
+         const Quantity_Color& aColor)
 {
   CurveCreator_Section *aSection = new CurveCreator_Section;
 
@@ -465,8 +496,9 @@ int CurveCreator_Curve::addSectionInternal
   aSection->myType     = theType;
   aSection->myIsClosed = theIsClosed;
   aSection->myPoints   = thePoints;
+  aSection->myColor    = aColor;//getRandColor();  //TODO temp
   mySections.push_back(aSection);
-  redisplayCurve();
+  redisplayCurve(false);
   return mySections.size()-1;
 }
 
@@ -487,7 +519,9 @@ int CurveCreator_Curve::addSection
                             theName, aCoords, theType, theIsClosed);
   }
 
-  resISection = addSectionInternal(theName, theType, theIsClosed, aCoords);
+  Quantity_Color aRColor = CurveCreator_Utils::getRandColor();
+
+  resISection = addSectionInternal(theName, theType, theIsClosed, aCoords, aRColor);
 
   finishOperation();
   return resISection;
@@ -508,7 +542,7 @@ int CurveCreator_Curve::addSection
                             theName, thePoints, theType, theIsClosed);
   }
 
-  resISection = addSectionInternal(theName, theType, theIsClosed, thePoints);
+  resISection = addSectionInternal(theName, theType, theIsClosed, thePoints, Quantity_NOC_YELLOW);
 
   finishOperation();
   return resISection;
@@ -518,18 +552,21 @@ int CurveCreator_Curve::addSection
 bool CurveCreator_Curve::removeSectionInternal( const int theISection )
 {
   if (theISection == -1) {
+    myRemColors.push_back(mySections.back()->myColor);
     delete mySections.back();
     mySections.pop_back();
   } else {
     CurveCreator::Sections::iterator anIterRm = mySections.begin() + theISection;
 
+    myRemColors.push_back((*anIterRm)->myColor);
+
     delete *anIterRm;
     mySections.erase(anIterRm);
   }
-  redisplayCurve();
+  redisplayCurve(false);
   return true;
 }
-  
+
 //! Removes the given sections.
 bool CurveCreator_Curve::removeSection( const int theISection )
 {
@@ -545,6 +582,49 @@ bool CurveCreator_Curve::removeSection( const int theISection )
   return res;
 }
 
+bool CurveCreator_Curve::setColorSection( int SectInd, Quantity_Color theNewColor )
+{
+  bool res = false;
+  // Set the difference.
+  startOperation();
+
+  int ColorParam[3] = { (int)( theNewColor.Red() * 255 ),
+    (int)( theNewColor.Green() * 255 ),
+    (int)( theNewColor.Blue() * 255 ) };
+
+  if (addEmptyDiff())
+    myListDiffs.back().init(this, CurveCreator_Operation::SetColorSection, SectInd, ColorParam);
+
+  setColorSectionInternal(SectInd, theNewColor);
+
+  finishOperation();
+  return res;
+}
+
+void CurveCreator_Curve::setColorSectionInternal( int SectInd, Quantity_Color theNewColor )
+{
+  CurveCreator_Section* aSec = (CurveCreator_Section*)(getSection(SectInd));
+  aSec->myColor = theNewColor;
+
+  redisplayCurve(false);
+}
+
+Quantity_Color CurveCreator_Curve::getColorSection( int SectInd ) const
+{
+  CurveCreator_Section* aSec = (CurveCreator_Section*)(getSection(SectInd));
+  return aSec->myColor;
+}
+
+Quantity_Color CurveCreator_Curve::getLastRemovedColor() const
+{
+  return myRemColors.empty() ? Quantity_NOC_BLACK : myRemColors.back();
+}
+
+void CurveCreator_Curve::popLastRemovedColor()
+{
+  myRemColors.pop_back();
+}
+
 /**
  *  Get number of points in specified section or (the total number of points
  *  in Curve if theISection is equal to -1).
@@ -605,7 +685,7 @@ bool CurveCreator_Curve::isClosed( const int theISection ) const
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::setClosedInternal( const int theISection, 
+bool CurveCreator_Curve::setClosedInternal( const int theISection,
                                             const bool theIsClosed )
 {
   CurveCreator_Section* aSection = 0;
@@ -617,14 +697,14 @@ bool CurveCreator_Curve::setClosedInternal( const int theISection,
       aSection = (CurveCreator_Section*)getSection( i );
       if( aSection ) {
         aSection->myIsClosed = theIsClosed;
-        redisplayCurve();
+        redisplayCurve(false);
       }
     }
   } else {
     aSection = (CurveCreator_Section*)getSection( theISection );
     if ( aSection ) {
       aSection->myIsClosed = theIsClosed;
-      redisplayCurve();
+      redisplayCurve(false);
     }
   }
   return true;
@@ -634,7 +714,7 @@ bool CurveCreator_Curve::setClosedInternal( const int theISection,
  *  Set "closed" flag of the specified section (all sections if
  *  \a theISection is -1).
  */
-bool CurveCreator_Curve::setClosed( const int theISection, 
+bool CurveCreator_Curve::setClosed( const int theISection,
                                     const bool theIsClosed )
 {
   bool res = false;
@@ -657,7 +737,7 @@ std::string CurveCreator_Curve::getSectionName( const int theISection ) const
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::setSectionNameInternal( const int theISection, 
+bool CurveCreator_Curve::setSectionNameInternal( const int theISection,
                                                  const std::string& theName )
 {
   bool res = false;
@@ -670,7 +750,7 @@ bool CurveCreator_Curve::setSectionNameInternal( const int theISection,
 }
 
 /** Set name of the specified section */
-bool CurveCreator_Curve::setSectionName( const int theISection, 
+bool CurveCreator_Curve::setSectionName( const int theISection,
                                          const std::string& theName )
 {
   bool res = false;
@@ -694,7 +774,7 @@ CurveCreator::SectionType CurveCreator_Curve::getSectionType
 }
 
 //! For internal use only! Undo/Redo are not used here.
-bool CurveCreator_Curve::setSectionTypeInternal( const int theISection, 
+bool CurveCreator_Curve::setSectionTypeInternal( const int theISection,
                                                  const CurveCreator::SectionType theType )
 {
   CurveCreator_Section* aSection;
@@ -707,12 +787,12 @@ bool CurveCreator_Curve::setSectionTypeInternal( const int theISection,
       if ( aSection )
         aSection->myType = theType;
     }
-    redisplayCurve();
+    redisplayCurve(false);
   } else {
     aSection = (CurveCreator_Section*)getSection( theISection );
     if ( aSection && aSection->myType != theType ){
       aSection->myType = theType;
-      redisplayCurve();
+      redisplayCurve(false);
     }
   }
   return true;
@@ -722,7 +802,7 @@ bool CurveCreator_Curve::setSectionTypeInternal( const int theISection,
  *  Set type of the specified section (or all sections
  *  if \a theISection is -1).
  */
-bool CurveCreator_Curve::setSectionType( const int theISection, 
+bool CurveCreator_Curve::setSectionType( const int theISection,
                                          const CurveCreator::SectionType theType )
 {
   bool res = false;
@@ -764,8 +844,8 @@ bool CurveCreator_Curve::addPointsInternal( const CurveCreator::SectionsMap &the
           anIterPosition = aSection->myPoints.end();
         else
           anIterPosition = aSection->myPoints.begin() + toICoord(anIPnt);
-        CurveCreator::Coordinates::const_iterator aFirstPosition = 
-          aCoords.begin();
+        /*CurveCreator::Coordinates::const_iterator aFirstPosition =
+          aCoords.begin();*/
         aSection->myPoints.insert(anIterPosition,
                                   aCoords.begin(), aCoords.end());
       }
@@ -773,7 +853,7 @@ bool CurveCreator_Curve::addPointsInternal( const CurveCreator::SectionsMap &the
     }
   }
   if(res)
-    redisplayCurve();
+    redisplayCurve(false);
   return res;
 }
 
@@ -818,7 +898,7 @@ bool CurveCreator_Curve::setPointInternal( const CurveCreator::SectionsMap &theS
   for ( ; anIt != theSectionsMap.end(); anIt++ ) {
     int anISection = anIt->first;
     aSection = (CurveCreator_Section*)getSection( anISection );
-    if( aSection ) { 
+    if( aSection ) {
       CurveCreator::PosPointsList aSectionPoints = anIt->second;
       CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
       for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
@@ -831,8 +911,8 @@ bool CurveCreator_Curve::setPointInternal( const CurveCreator::SectionsMap &theS
     }
   }
   if(res)
-    redisplayCurve();
-  
+    redisplayCurve(false);
+
   return res;
 }
 
@@ -857,13 +937,13 @@ bool CurveCreator_Curve::setPoint( const int theISection,
   aPoints.push_back( aPosPoint );
   aSectionsMap[theISection] = aPoints;
 
-  int aSize1 = getNbPoints( theISection );
+  /*int aSize1 = */getNbPoints( theISection ); // todo: unused variable
   res = setPointInternal( aSectionsMap );
-  int aSize2 = getNbPoints( theISection );
+  /*int aSize2 = */getNbPoints( theISection ); // todo: unused variable
 
   finishOperation();
 
-  return res; 
+  return res;
 }
 
 //! Set coordinates of specified points from different sections
@@ -880,7 +960,7 @@ bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSe
   }
   CurveCreator::SectionsMap aSectionsMap;
   CurveCreator::PosPointsList aPosPoints;
-  CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt = 
+  CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
     theSectionToPntCoords.begin(), aLast = theSectionToPntCoords.end();
   int aSectionId, aPointId;
   for ( ; anIt != aLast; anIt++ ) {
@@ -888,18 +968,18 @@ bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSe
     aSectionId = anIt->first.first;
     aPointId = anIt->first.second;
     CurveCreator::Coordinates aNewCoords = anIt->second;
-    CurveCreator_PosPoint* aPosPoint = 
+    CurveCreator_PosPoint* aPosPoint =
       new CurveCreator_PosPoint( aPointId, aNewCoords );
     if( aSectionsMap.find(aSectionId) != aSectionsMap.end() )
       aPosPoints = aSectionsMap[aSectionId];
     aPosPoints.push_back( aPosPoint );
     aSectionsMap[aSectionId] = aPosPoints;
-    
+
   }
   res = setPointInternal( aSectionsMap );
   finishOperation();
 
-  return res; 
+  return res;
 }
 
 //! For internal use only! Undo/Redo are not used here.
@@ -915,7 +995,7 @@ bool CurveCreator_Curve::removePointsInternal( const SectionToPointList &thePoin
     aRes = removeSectionPoints(aSectionId, anIt->second);
   }
   if( aRes)
-    redisplayCurve();
+    redisplayCurve(false);
 
   return aRes;
 }
@@ -972,32 +1052,58 @@ CurveCreator::Coordinates CurveCreator_Curve::getPoint( const int theISection,
 // function: getPoints
 // purpose:
 //=======================================================================
-CurveCreator::Coordinates CurveCreator_Curve::getPoints( const int theISection ) const
+CurveCreator::Coordinates CurveCreator_Curve::getCoords( int theISection ) const
 {
-  //DEBTRACE("getPoints");
   CurveCreator_Section* aSection = (CurveCreator_Section*)getSection( theISection );
   return aSection ? aSection->myPoints : CurveCreator::Coordinates();
 }
 
+Handle(TColgp_HArray1OfPnt) CurveCreator_Curve::GetDifferentPoints( int theISection ) const
+{
+  //DEBTRACE("getPoints");
+  CurveCreator_Section* aSection = (CurveCreator_Section*)getSection( theISection );
+  return aSection ? aSection->GetDifferentPoints( (int)myDimension ) : Handle(TColgp_HArray1OfPnt)();
+}
+
 void CurveCreator_Curve::constructAISObject()
 {
   //DEBTRACE("constructAISObject");
   TopoDS_Shape aShape;
-  CurveCreator_Utils::constructShape( this, aShape );
+  mySect2Shape.Clear();
+  CurveCreator_Utils::constructShape( this, aShape, &mySect2Shape );
+  myAISShape = new AIS_ColoredShape( aShape );
+  AIS_ColoredShape* AISColoredShape = dynamic_cast<AIS_ColoredShape*>(myAISShape);
+
+  std::map<int, TopoDS_Shape>::iterator it;
+
+  //for ( it = mySect2Shape.begin(); it != mySect2Shape.end(); it++ )
+  for (int i = 1; i <= mySect2Shape.Extent(); i++ )
+  {
+    CurveCreator_Section* aSect = (CurveCreator_Section*)getSection(mySect2Shape.FindKey(i));
+    Quantity_Color aColor = aSect->myColor;
+    const TopoDS_Shape& aShape = mySect2Shape.FindFromIndex(i); //should contain: one wire + vertices
+    TopoDS_Iterator it(aShape);
+    for (;it.More();it.Next())
+    {
+      if (it.Value().ShapeType() == TopAbs_WIRE)
+        AISColoredShape->SetCustomColor(it.Value(), aColor);
+    }
+  }
 
-  myAISShape = new AIS_Shape( aShape );
+  // myAISShape->SetColor( myCurveColor );
+  myAISShape->SetWidth( myLineWidth );
   Handle(Prs3d_PointAspect) anAspect = myAISShape->Attributes()->PointAspect();
   anAspect->SetScale( 3.0 );
   anAspect->SetTypeOfMarker(Aspect_TOM_O_POINT);
-  anAspect->SetColor(Quantity_NOC_ROYALBLUE4);
+  anAspect->SetColor(myPointAspectColor);
   myAISShape->Attributes()->SetPointAspect( anAspect );
-
 }
 
 Handle(AIS_InteractiveObject) CurveCreator_Curve::getAISObject( const bool theNeedToBuild ) const
 {
   //DEBTRACE("getAISObject");
-  if ( !myAISShape && theNeedToBuild ) {
+  if ( !myAISShape && theNeedToBuild )
+  {
     CurveCreator_Curve* aCurve = (CurveCreator_Curve*)this;
     aCurve->constructAISObject();
   }