1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: CurveCreator_Curve.cxx
21 // Author: Sergey KHROMOV
23 #include "CurveCreator_Curve.hxx"
25 #include "CurveCreator.hxx"
26 #include "CurveCreator_PosPoint.hxx"
27 #include "CurveCreator_Section.hxx"
28 #include "CurveCreator_Displayer.h"
29 #include "CurveCreator_Utils.h"
31 #include <AIS_Shape.hxx>
32 #include <AIS_InteractiveObject.hxx>
33 #include <Geom_CartesianPoint.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Wire.hxx>
42 //=======================================================================
43 // function: Constructor
45 //=======================================================================
46 CurveCreator_Curve::CurveCreator_Curve( const CurveCreator::Dimension theDimension )
48 myDimension (theDimension),
58 //=======================================================================
59 // function: Destructor
61 //=======================================================================
62 CurveCreator_Curve::~CurveCreator_Curve()
64 // Delete all allocated data.
68 //=======================================================================
69 // function: getDimension
71 //=======================================================================
72 CurveCreator::Dimension CurveCreator_Curve::getDimension() const
77 //=======================================================================
78 // function: getUniqSectionName
79 // purpose: return unique section name
80 //=======================================================================
81 std::string CurveCreator_Curve::getUniqSectionName() const
83 for( int i = 0 ; i < 1000000 ; i++ ){
85 sprintf( aBuffer, "Section_%d", i+1 );
86 std::string aName(aBuffer);
88 for( j = 0 ; j < mySections.size() ; j++ ){
89 if( mySections[j]->myName == aName )
92 if( j == mySections.size() )
98 //=======================================================================
99 // function: setDisplayer
100 // purpose: set curve changes Displayer
101 //=======================================================================
102 void CurveCreator_Curve::setDisplayer( CurveCreator_Displayer* theDisplayer )
104 myDisplayer = theDisplayer;
107 //=======================================================================
108 // function: getDisplayer
109 // purpose: get curve changes Displayer
110 //=======================================================================
111 CurveCreator_Displayer* CurveCreator_Curve::getDisplayer()
116 //=======================================================================
117 // function: removeDisplayer
118 // purpose: remove the attached Displayer
119 //=======================================================================
120 void CurveCreator_Curve::removeDisplayer()
125 //=======================================================================
128 //=======================================================================
129 bool CurveCreator_Curve::addEmptyDiff()
131 bool isEnabled = false;
133 if (myUndoDepth != 0) {
134 // Forget all Redos after the current one.
137 myListDiffs.erase(myCurrenPos, myListDiffs.end());
140 if (myUndoDepth == -1 || myNbUndos < myUndoDepth) {
141 // Increase the number of undos.
144 // If there are too many differences, remove the first one.
145 myListDiffs.pop_front();
148 // Add new difference.
149 myListDiffs.push_back(CurveCreator_Diff());
150 myCurrenPos = myListDiffs.end();
157 void CurveCreator_Curve::startOperation()
162 void CurveCreator_Curve::finishOperation()
167 //=======================================================================
168 // function: toICoord
170 //=======================================================================
171 int CurveCreator_Curve::toICoord(const int theIPnt) const
173 return theIPnt * myDimension;
176 //=======================================================================
177 // function: setUndoDepth
179 //=======================================================================
180 void CurveCreator_Curve::setUndoDepth(const int theDepth)
183 // Reset all undo/redo data.
187 myCurrenPos = myListDiffs.end();
189 } else if (theDepth == -1) {
190 // There is nothing to do as the depth become unlimited.
192 } else if (theDepth > 0) {
193 // The new "real" depth is set.
194 if (theDepth < myNbRedos) {
195 // The new depth is less then number of redos. Remove the latest redos.
196 int aShift = (myNbRedos - theDepth);
197 ListDiff::iterator aFromPos = myListDiffs.end();
203 myListDiffs.erase(aFromPos, myListDiffs.end());
204 myNbRedos = theDepth;
207 if (theDepth < myNbUndos + myNbRedos) {
208 // The new depth is less then the total number of differences.
209 // Remove the first undos.
210 int aShift = (myNbUndos + myNbRedos - theDepth);
211 ListDiff::iterator aToPos = myListDiffs.begin();
217 myListDiffs.erase(myListDiffs.begin(), aToPos);
218 myNbUndos = theDepth - myNbRedos;
221 myUndoDepth = theDepth;
225 //=======================================================================
226 // function: getUndoDepth
228 //=======================================================================
229 int CurveCreator_Curve::getUndoDepth() const
234 void CurveCreator_Curve::getCoordinates( int theISection, int theIPoint, double& theX, double& theY, double& theZ ) const
236 CurveCreator::Coordinates aCoords = getPoint( theISection, theIPoint );
240 if( getDimension() == CurveCreator::Dim3d ){
245 void CurveCreator_Curve::redisplayCurve()
248 myDisplayer->eraseAll( false );
251 myDisplayer->display( getAISObject( true ), true );
255 //! For internal use only! Undo/Redo are not used here.
256 bool CurveCreator_Curve::moveSectionInternal(const int theISection,
257 const int theNewIndex)
260 if (theISection != theNewIndex) {
261 CurveCreator_Section *aSection = mySections.at(theISection);
264 CurveCreator::Sections::iterator anIter = mySections.begin() + theISection;
266 mySections.erase(anIter);
269 anIter = mySections.begin() + theNewIndex;
270 mySections.insert(anIter, aSection);
276 //=======================================================================
277 // function: moveSection
279 //=======================================================================
280 bool CurveCreator_Curve::moveSection(const int theISection,
281 const int theNewIndex)
284 // Set the difference.
286 if (addEmptyDiff()) {
287 myListDiffs.back().init(this, CurveCreator_Operation::MoveSection,
288 theISection, theNewIndex);
292 res = moveSectionInternal(theISection, theNewIndex);
297 void CurveCreator_Curve::convert( const SectionToPointList& thePoints,
298 std::map< int, std::list<int> >& theConvPoints )
300 theConvPoints.clear();
302 SectionToPointList::const_iterator anIt = thePoints.begin(), aLast = thePoints.end();
303 std::list<int> aPoints;
304 int aSectionId, aPointId;
305 for ( ; anIt != aLast; anIt++ ) {
306 aSectionId = anIt->first;
307 aPointId = anIt->second;
309 if ( theConvPoints.find( aSectionId ) != theConvPoints.end() )
310 aPoints = theConvPoints[aSectionId];
311 aPoints.push_back( aPointId );
312 theConvPoints[aSectionId] = aPoints;
316 /************ Implementation of INTERFACE methods ************/
318 /***********************************************/
319 /*** Undo/Redo methods ***/
320 /***********************************************/
322 //! Get number of available undo operations
323 int CurveCreator_Curve::getNbUndo() const
328 //! Undo previous operation
329 bool CurveCreator_Curve::undo()
336 myCurrenPos->applyUndo(this);
342 //! Get number of available redo operations
343 int CurveCreator_Curve::getNbRedo() const
348 //! Redo last previously "undone" operation
349 bool CurveCreator_Curve::redo()
353 myCurrenPos->applyRedo(this);
362 /***********************************************/
363 /*** Section methods ***/
364 /***********************************************/
365 //! For internal use only! Undo/Redo are not used here.
366 bool CurveCreator_Curve::clearInternal()
368 // erase curve from the viewer
370 myDisplayer->eraseAll( true );
373 // Delete all allocated data.
375 const int aNbSections = getNbSections();
377 for (; i < aNbSections; i++) {
378 delete mySections[i];
386 //=======================================================================
389 //=======================================================================
390 bool CurveCreator_Curve::clear()
394 // Set the difference.
395 if (addEmptyDiff()) {
396 myListDiffs.back().init(this, CurveCreator_Operation::Clear);
398 res = clearInternal();
403 //! For internal use only! Undo/Redo are not used here.
404 bool CurveCreator_Curve::joinInternal( const int theISectionTo,
405 const int theISectionFrom )
408 CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
409 CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
411 aSection1->myPoints.insert(aSection1->myPoints.end(),
412 aSection2->myPoints.begin(),
413 aSection2->myPoints.end());
415 res = removeSection(theISectionFrom);
420 //! Join range of sections to one section (join all sections if -1 is passed in theISectionFrom argument)
421 bool CurveCreator_Curve::join( const int theISectionTo,
422 const int theISectionFrom )
426 if ( theISectionTo != theISectionFrom ) {
429 myListDiffs.back().init(this, CurveCreator_Operation::Join, theISectionTo, theISectionFrom);
431 res = joinInternal( theISectionTo, theISectionFrom );
438 //! Get number of sections
439 int CurveCreator_Curve::getNbSections() const
441 return mySections.size();
444 //! For internal use only! Undo/Redo are not used here.
445 int CurveCreator_Curve::addSectionInternal
446 (const std::string& theName, const CurveCreator::SectionType theType,
447 const bool theIsClosed, const CurveCreator::Coordinates &thePoints)
449 CurveCreator_Section *aSection = new CurveCreator_Section;
451 std::string aName = theName;
453 aName = getUniqSectionName();
455 aSection->myName = aName;
456 aSection->myType = theType;
457 aSection->myIsClosed = theIsClosed;
458 aSection->myPoints = thePoints;
459 mySections.push_back(aSection);
461 return mySections.size()-1;
464 //=======================================================================
465 // function: addSection
466 // purpose: adds an empty section
467 //=======================================================================
468 int CurveCreator_Curve::addSection
469 (const std::string& theName, const CurveCreator::SectionType theType,
470 const bool theIsClosed)
472 int resISection = -1;
473 // Set the difference.
475 CurveCreator::Coordinates aCoords; //empty list
476 if (addEmptyDiff()) {
477 myListDiffs.back().init(this, CurveCreator_Operation::AddSection,
478 theName, aCoords, theType, theIsClosed);
481 resISection = addSectionInternal(theName, theType, theIsClosed, aCoords);
486 //=======================================================================
487 // function: addSection
488 // purpose: adds a section with the given points
489 //=======================================================================
490 int CurveCreator_Curve::addSection
491 (const std::string& theName, const CurveCreator::SectionType theType,
492 const bool theIsClosed, const CurveCreator::Coordinates &thePoints)
494 int resISection = -1;
495 // Set the difference.
497 if (addEmptyDiff()) {
498 myListDiffs.back().init(this, CurveCreator_Operation::AddSection,
499 theName, thePoints, theType, theIsClosed);
502 resISection = addSectionInternal(theName, theType, theIsClosed, thePoints);
508 //! For internal use only! Undo/Redo are not used here.
509 bool CurveCreator_Curve::removeSectionInternal( const int theISection )
511 if (theISection == -1) {
512 delete mySections.back();
513 mySections.pop_back();
515 CurveCreator::Sections::iterator anIterRm = mySections.begin() + theISection;
518 mySections.erase(anIterRm);
524 //! Removes the given sections.
525 bool CurveCreator_Curve::removeSection( const int theISection )
528 // Set the difference.
531 myListDiffs.back().init(this, CurveCreator_Operation::RemoveSection, theISection);
533 res = removeSectionInternal( theISection );
540 * Get number of points in specified section or (the total number of points
541 * in Curve if theISection is equal to -1).
543 int CurveCreator_Curve::getNbPoints( const int theISection ) const
547 if (theISection == -1) {
549 const int aNbSections = getNbSections();
551 for (; i < aNbSections; i++) {
552 aNbCoords += mySections[i]->myPoints.size();
555 if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
556 aNbCoords = mySections.at(theISection)->myPoints.size();
559 return aNbCoords/myDimension;
562 //! Get "closed" flag of the specified section
563 bool CurveCreator_Curve::isClosed( const int theISection ) const
565 return mySections.at(theISection)->myIsClosed;
568 //! For internal use only! Undo/Redo are not used here.
569 bool CurveCreator_Curve::setClosedInternal( const int theISection,
570 const bool theIsClosed )
572 if (theISection == -1) {
573 int aSize = mySections.size();
576 for (i = 0; i < aSize; i++) {
577 mySections[i]->myIsClosed = theIsClosed;
581 mySections.at(theISection)->myIsClosed = theIsClosed;
588 * Set "closed" flag of the specified section (all sections if
589 * \a theISection is -1).
591 bool CurveCreator_Curve::setClosed( const int theISection,
592 const bool theIsClosed )
595 // Set the difference.
597 if (addEmptyDiff()) {
598 myListDiffs.back().init(this, CurveCreator_Operation::SetClosed,
599 theIsClosed, theISection);
601 res = setClosedInternal( theISection, theIsClosed );
606 //! Returns specified section name
607 std::string CurveCreator_Curve::getSectionName( const int theISection ) const
609 if( ( theISection >= 0 ) && ( theISection < mySections.size() ))
610 return mySections.at(theISection)->myName;
614 //! For internal use only! Undo/Redo are not used here.
615 bool CurveCreator_Curve::setSectionNameInternal( const int theISection,
616 const std::string& theName )
619 if( ( theISection >= 0 ) && ( theISection < mySections.size() )){
620 mySections.at(theISection)->myName = theName;
626 /** Set name of the specified section */
627 bool CurveCreator_Curve::setSectionName( const int theISection,
628 const std::string& theName )
631 // Set the difference.
633 if (addEmptyDiff()) {
634 myListDiffs.back().init(this, CurveCreator_Operation::RenameSection,
635 theName, theISection);
637 res = setSectionNameInternal( theISection, theName );
642 //! Get type of the specified section
643 CurveCreator::SectionType CurveCreator_Curve::getSectionType
644 ( const int theISection ) const
646 return mySections.at(theISection)->myType;
649 //! For internal use only! Undo/Redo are not used here.
650 bool CurveCreator_Curve::setSectionTypeInternal( const int theISection,
651 const CurveCreator::SectionType theType )
653 if (theISection == -1) {
655 const int aNbSections = getNbSections();
657 for (; i < aNbSections; i++) {
658 mySections[i]->myType = theType;
662 if( mySections.at(theISection)->myType != theType ){
663 mySections.at(theISection)->myType = theType;
671 * Set type of the specified section (or all sections
672 * if \a theISection is -1).
674 bool CurveCreator_Curve::setSectionType( const int theISection,
675 const CurveCreator::SectionType theType )
679 // Set the difference.
680 if (addEmptyDiff()) {
681 myListDiffs.back().init(this, CurveCreator_Operation::SetType,
682 theType, theISection);
685 res = setSectionTypeInternal( theISection, theType );
692 /***********************************************/
693 /*** Point methods ***/
694 /***********************************************/
696 //! For internal use only! Undo/Redo are not used here.
697 bool CurveCreator_Curve::addPointsInternal( const CurveCreator::SectionsMap &theSectionsMap )
700 CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
701 CurveCreator_Section *aSection = 0;
702 for ( ; anIt != theSectionsMap.end(); anIt++ ) {
703 int anISection = anIt->first;
704 aSection = mySections.at(anISection);
706 CurveCreator::PosPointsList aSectionPoints = anIt->second;
707 CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
708 for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
709 int anIPnt = (*aPntIt)->myID;
710 CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
711 CurveCreator::Coordinates::iterator anIterPosition;
713 anIterPosition = aSection->myPoints.end();
715 anIterPosition = aSection->myPoints.begin() + toICoord(anIPnt);
716 CurveCreator::Coordinates::const_iterator aFirstPosition =
718 aSection->myPoints.insert(anIterPosition,
719 aCoords.begin(), aCoords.end());
730 * Add one point to the specified section starting from the given theIPnt index
731 * (or at the end of points if \a theIPnt is -1).
733 bool CurveCreator_Curve::addPoints( const CurveCreator::Coordinates& theCoords,
734 const int theISection,
738 CurveCreator::Coordinates aCoords = theCoords;
739 // Set the difference.
741 if (addEmptyDiff()) {
742 CurveCreator_ICurve::SectionToPointCoordsList aList;
743 aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theCoords));
744 myListDiffs.back().init(this, CurveCreator_Operation::InsertPoints,
747 CurveCreator::SectionsMap aSectionsMap;
748 CurveCreator::PosPointsList aPoints;
749 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theCoords );
750 aPoints.push_back( aPosPoint );
751 aSectionsMap[theISection] = aPoints;
753 res = addPointsInternal( aSectionsMap );
759 //! For internal use only! Undo/Redo are not used here.
760 bool CurveCreator_Curve::setPointInternal( const CurveCreator::SectionsMap &theSectionsMap )
764 CurveCreator::SectionsMap::const_iterator anIt = theSectionsMap.begin();
765 CurveCreator_Section *aSection = 0;
766 for ( ; anIt != theSectionsMap.end(); anIt++ ) {
767 int anISection = anIt->first;
768 aSection = mySections.at(anISection);
770 CurveCreator::PosPointsList aSectionPoints = anIt->second;
771 CurveCreator::PosPointsList::const_iterator aPntIt = aSectionPoints.begin();
772 for( ; aPntIt != aSectionPoints.end(); aPntIt++ ){
773 int anIPnt = (*aPntIt)->myID;
774 CurveCreator::Coordinates aCoords = (*aPntIt)->myCoords;
775 for ( int i = 0; i < myDimension; i++)
776 aSection->myPoints.at(toICoord(anIPnt) + i) = aCoords[i];
787 //! Set coordinates of specified point
788 bool CurveCreator_Curve::setPoint( const int theISection,
790 const CurveCreator::Coordinates& theNewCoords )
793 // Set the difference.
795 if (addEmptyDiff()) {
796 CurveCreator_ICurve::SectionToPointCoordsList aList;
797 aList.push_back(std::make_pair(std::make_pair(theISection, theIPnt), theNewCoords));
798 myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
801 CurveCreator::SectionsMap aSectionsMap;
802 CurveCreator::PosPointsList aPoints;
803 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( theIPnt, theNewCoords );
804 aPoints.push_back( aPosPoint );
805 aSectionsMap[theISection] = aPoints;
807 int aSize1 = getNbPoints( theISection );
808 res = setPointInternal( aSectionsMap );
809 int aSize2 = getNbPoints( theISection );
816 //! Set coordinates of specified points from different sections
817 bool CurveCreator_Curve::setSeveralPoints( const SectionToPointCoordsList &theSectionToPntCoords)
820 // Set the difference.
822 if (addEmptyDiff()) {
823 myListDiffs.back().init(this, CurveCreator_Operation::SetCoordinates,
824 theSectionToPntCoords);
826 CurveCreator::SectionsMap aSectionsMap;
827 CurveCreator::PosPointsList aPosPoints;
828 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
829 theSectionToPntCoords.begin(), aLast = theSectionToPntCoords.end();
830 int aSectionId, aPointId;
831 for ( ; anIt != aLast; anIt++ ) {
833 aSectionId = anIt->first.first;
834 aPointId = anIt->first.second;
835 CurveCreator::Coordinates aNewCoords = anIt->second;
836 CurveCreator_PosPoint* aPosPoint =
837 new CurveCreator_PosPoint( aPointId, aNewCoords );
838 if( aSectionsMap.find(aSectionId) != aSectionsMap.end() )
839 aPosPoints = aSectionsMap[aSectionId];
840 aPosPoints.push_back( aPosPoint );
841 aSectionsMap[aSectionId] = aPosPoints;
844 res = setPointInternal( aSectionsMap );
850 //! For internal use only! Undo/Redo are not used here.
851 bool CurveCreator_Curve::removePointsInternal( const SectionToPointList &thePoints )
854 std::map<int, std::list<int> > aConvPoints;
855 convert( thePoints, aConvPoints );
856 std::map<int, std::list<int> >::const_iterator anIt = aConvPoints.begin(),
857 aLast = aConvPoints.end();
858 CurveCreator_Section *aSection = 0;
859 for ( ; anIt != aLast; anIt++ ) {
860 int aSectionId = anIt->first;
861 aSection = mySections.at(aSectionId);
863 std::list<int> aSectionPoints = anIt->second;
864 aSectionPoints.sort();
865 std::list<int>::const_reverse_iterator aPntIt = aSectionPoints.rbegin();
866 for( ; aPntIt != aSectionPoints.rend(); aPntIt++ ){
867 int aPntIndx = *aPntIt;
868 CurveCreator::Coordinates::iterator aFirstPosition;
870 aFirstPosition = aSection->myPoints.end() - getDimension();
872 aFirstPosition = aSection->myPoints.begin() + toICoord(aPntIndx);
873 aSection->myPoints.erase( aFirstPosition, aFirstPosition + getDimension() );
883 //! Remove point with given id
884 bool CurveCreator_Curve::removePoint( const int theISection, const int theIPnt )
887 // Set the difference.
889 if (addEmptyDiff()) {
890 myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
891 theISection, theIPnt);
893 SectionToPointList aListOfSectionsToPoints;
894 aListOfSectionsToPoints.push_back(std::make_pair(theISection, theIPnt));
895 res = removePointsInternal( aListOfSectionsToPoints );
900 //! Remove several points from different sections with given ids
901 bool CurveCreator_Curve::removeSeveralPoints( const SectionToPointList &theSectionToPntIDs)
904 // Set the difference.
906 if (addEmptyDiff()) {
907 myListDiffs.back().init(this, CurveCreator_Operation::RemovePoints,
910 res = removePointsInternal( theSectionToPntIDs );
915 //=======================================================================
916 // function: getCoordinates
918 //=======================================================================
919 CurveCreator::Coordinates CurveCreator_Curve::getPoint( const int theISection,
920 const int theIPnt) const
922 CurveCreator_Section *aSection = mySections.at(theISection);
923 CurveCreator::Coordinates::const_iterator
924 anIter = aSection->myPoints.begin() + toICoord(theIPnt);
925 CurveCreator::Coordinates aResult(anIter, anIter + myDimension);
930 //=======================================================================
931 // function: getPoints
933 //=======================================================================
934 CurveCreator::Coordinates CurveCreator_Curve::getPoints( const int theISection ) const
936 CurveCreator::Coordinates aCoords;
937 if ( ( theISection >= 0 ) && ( theISection < mySections.size() ) )
939 aCoords = mySections.at(theISection)->myPoints;
944 void CurveCreator_Curve::constructAISObject()
947 CurveCreator_Utils::constructShape( this, aShape );
949 myAISShape = new AIS_Shape( aShape );
952 Handle(AIS_InteractiveObject) CurveCreator_Curve::getAISObject( const bool theNeedToBuild ) const
954 if ( !myAISShape && theNeedToBuild ) {
955 CurveCreator_Curve* aCurve = (CurveCreator_Curve*)this;
956 aCurve->constructAISObject();