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_ICurve.cxx
21 // Author: Sergey KHROMOV
23 #include "CurveCreator_ICurve.hxx"
24 #include "CurveCreator_Section.hxx"
25 #include "CurveCreator_Listener.hxx"
29 //=======================================================================
30 // function: Constructor
32 //=======================================================================
33 CurveCreator_ICurve::CurveCreator_ICurve
34 (const CurveCreator::Dimension theDimension)
36 myDimension (theDimension),
41 //=======================================================================
42 // function: Destructor
44 //=======================================================================
45 CurveCreator_ICurve::~CurveCreator_ICurve()
47 // Delete all allocated data.
51 //=======================================================================
54 //=======================================================================
55 bool CurveCreator_ICurve::isLocked() const
60 //=======================================================================
61 // function: getDimension
63 //=======================================================================
64 CurveCreator::Dimension CurveCreator_ICurve::getDimension() const
69 //=======================================================================
70 // function: getNbPoints
72 //=======================================================================
73 int CurveCreator_ICurve::getNbPoints(const int theISection) const
77 if (theISection == -1) {
79 const int aNbSections = getNbSections();
81 for (; i < aNbSections; i++) {
82 aNbCoords += mySections[i]->myPoints.size();
85 aNbCoords = mySections.at(theISection)->myPoints.size();
88 return aNbCoords/myDimension;
91 //=======================================================================
92 // function: getNbSections
94 //=======================================================================
95 int CurveCreator_ICurve::getNbSections() const
97 return mySections.size();
100 //=======================================================================
101 // function: getCoordinates
103 //=======================================================================
104 CurveCreator::Coordinates CurveCreator_ICurve::getCoordinates
105 (const int theISection, const int theIPnt) const
107 CurveCreator_Section *aSection = mySections.at(theISection);
108 CurveCreator::Coordinates::const_iterator
109 anIter = aSection->myPoints.begin() + toICoord(theIPnt);
110 CurveCreator::Coordinates aResult(anIter, anIter + myDimension);
115 //=======================================================================
118 //=======================================================================
119 CurveCreator::Type CurveCreator_ICurve::getType(const int theISection) const
121 return mySections.at(theISection)->myType;
124 //=======================================================================
125 // function: getPoints
127 //=======================================================================
128 const CurveCreator::Coordinates &CurveCreator_ICurve::getPoints
129 (const int theISection) const
131 return mySections.at(theISection)->myPoints;
134 //=======================================================================
135 // function: isClosed
137 //=======================================================================
138 bool CurveCreator_ICurve::isClosed(const int theISection) const
140 return mySections.at(theISection)->myIsClosed;
143 std::string CurveCreator_ICurve::getSectionName(const int theISection) const
145 return mySections.at(theISection)->myName;
148 //=======================================================================
151 //=======================================================================
152 void CurveCreator_ICurve::setType
153 (const CurveCreator::Type theType, const int theISection)
155 if (theISection == -1) {
157 const int aNbSections = getNbSections();
159 for (; i < aNbSections; i++) {
160 mySections[i]->myType = theType;
163 myListener->curveChanged();
165 if( mySections.at(theISection)->myType != theType ){
166 mySections.at(theISection)->myType = theType;
168 myListener->sectionTypeChanged(theISection);
173 //=======================================================================
174 // function: addSection
176 //=======================================================================
177 void CurveCreator_ICurve::addSection
178 (const std::string& theName,
179 const CurveCreator::Type theType,
180 const bool theIsClosed,
181 const CurveCreator::Coordinates &thePoints)
183 CurveCreator_Section *aSection = new CurveCreator_Section;
185 std::string aName = theName;
187 aName = getUnicSectionName();
189 aSection->myName = aName;
190 aSection->myType = theType;
191 aSection->myIsClosed = theIsClosed;
192 aSection->myPoints = thePoints;
193 mySections.push_back(aSection);
195 myListener->sectionAdded( -1 );
198 //=======================================================================
199 // function: removeSection
201 //=======================================================================
202 void CurveCreator_ICurve::removeSection(const int theISection)
204 if (theISection == -1) {
205 delete mySections.back();
206 mySections.pop_back();
208 Sections::iterator anIterRm = mySections.begin() + theISection;
211 mySections.erase(anIterRm);
214 myListener->sectionRemoved(theISection);
217 //=======================================================================
218 // function: insertPoints
220 //=======================================================================
221 void CurveCreator_ICurve::insertPoints
222 (const CurveCreator::Coordinates &thePoints,
223 const int theISection,
227 // Add points to the end of section points.
228 addPoints(thePoints, theISection);
230 CurveCreator_Section *aSection =
231 (theISection == -1 ? mySections.back() : mySections.at(theISection));
233 aSection->myPoints.insert(aSection->myPoints.begin() + toICoord(theIPnt),
234 thePoints.begin(), thePoints.end());
236 myListener->pointInserted( theISection, theIPnt );
240 void CurveCreator_ICurve::movePoint(const int theISection, const int theIPointFrom, const int theNewIndex)
242 CurveCreator::Coordinates aCoords = getCoordinates(theISection, theIPointFrom );
243 insertPoints(aCoords, theISection, theNewIndex+1);
244 int aRemPntIndx = theIPointFrom;
245 if( theNewIndex < theIPointFrom )
247 removePoints(theISection, aRemPntIndx, 1 );
250 //=======================================================================
251 // function: removePoints
253 //=======================================================================
254 void CurveCreator_ICurve::removePoints(const int theISection,
256 const int theNbPoints)
258 CurveCreator_Section *aSection = mySections.at(theISection);
259 CurveCreator::Coordinates::iterator anIterBegin =
260 aSection->myPoints.begin() + toICoord(theIPnt);
261 CurveCreator::Coordinates::iterator anIterEnd =
263 aSection->myPoints.end() : anIterBegin + toICoord(theNbPoints));
265 aSection->myPoints.erase(anIterBegin, anIterEnd);
267 myListener->pointRemoved(theISection, theIPnt, theNbPoints );
270 //=======================================================================
273 //=======================================================================
274 void CurveCreator_ICurve::clear()
276 // Delete all allocated data.
278 const int aNbSections = getNbSections();
280 for (; i < aNbSections; i++) {
281 delete mySections[i];
286 myListener->curveChanged();
289 //=======================================================================
290 // function: setCoordinates
292 //=======================================================================
293 void CurveCreator_ICurve::setCoordinates
294 (const CurveCreator::Coordinates &theCoords,
295 const int theISection,
298 if (theCoords.size() == myDimension) {
299 CurveCreator_Section *aSection = mySections.at(theISection);
302 for (i = 0; i < myDimension; i++) {
303 aSection->myPoints.at(toICoord(theIPnt) + i) = theCoords[i];
307 myListener->pointChanged( theISection, theIPnt );
311 //=======================================================================
312 // function: setClosed
314 //=======================================================================
315 void CurveCreator_ICurve::setClosed(const bool theIsClosed,
316 const int theISection)
318 if (theISection == -1) {
319 int aSize = mySections.size();
322 for (i = 0; i < aSize; i++) {
323 mySections[i]->myIsClosed = theIsClosed;
325 myListener->sectionClosed( theISection, theIsClosed );
329 mySections.at(theISection)->myIsClosed = theIsClosed;
331 myListener->sectionClosed( theISection, theIsClosed );
336 /** Set name of the specified section.
338 void CurveCreator_ICurve::setName( const std::string& theName, const int theISection )
340 if( ( theISection >= 0 ) && ( theISection < mySections.size() )){
341 mySections.at(theISection)->myName = theName;
345 //=======================================================================
346 // function: moveSection
348 //=======================================================================
349 void CurveCreator_ICurve::moveSection(const int theISection,
350 const int theNewIndex)
352 if (theISection != theNewIndex) {
353 CurveCreator_Section *aSection = mySections.at(theISection);
356 Sections::iterator anIter = mySections.begin() + theISection;
358 mySections.erase(anIter);
361 anIter = mySections.begin() + theNewIndex;
362 mySections.insert(anIter, aSection);
366 //=======================================================================
369 //=======================================================================
370 void CurveCreator_ICurve::join(const int theISectionTo,
371 const int theISectionFrom)
373 if (theISectionTo != theISectionFrom) {
374 CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
375 CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
377 aSection1->myPoints.insert(aSection1->myPoints.end(),
378 aSection2->myPoints.begin(), aSection2->myPoints.end());
380 removeSection(theISectionFrom);
382 myListener->curveChanged();
386 //=======================================================================
389 //=======================================================================
390 void CurveCreator_ICurve::join()
392 const int aSize = mySections.size();
395 CurveCreator_Section *aSection1 = mySections[0];
398 for (i = 1; i < aSize; i++) {
399 CurveCreator_Section *aSection2 = mySections[i];
401 aSection1->myPoints.insert(aSection1->myPoints.end(),
402 aSection2->myPoints.begin(), aSection2->myPoints.end());
406 // Just erace section pointers as they were deleted before.
407 mySections.erase(mySections.begin() + 1, mySections.end());
409 myListener->curveChanged();
413 //=======================================================================
414 // function: toICoord
416 //=======================================================================
417 int CurveCreator_ICurve::toICoord(const int theIPnt) const
419 return theIPnt*myDimension;
422 //=======================================================================
423 // function: getUnicSectionName
424 // purpose: return unic section name
425 //=======================================================================
426 std::string CurveCreator_ICurve::getUnicSectionName()
428 for( int i = 0 ; i < 1000000 ; i++ ){
430 sprintf( aBuffer, "Section_%d", i+1 );
431 std::string aName(aBuffer);
433 for( j = 0 ; j < mySections.size() ; j++ ){
434 if( mySections[j]->myName == aName )
437 if( j == mySections.size() )
443 //=======================================================================
444 // function: setListener
445 // purpose: set curve changes listener
446 //=======================================================================
447 void CurveCreator_ICurve::setListener( CurveCreator_Listener* theListener )
449 myListener = theListener;
452 //=======================================================================
453 // function: setListener
454 // purpose: set curve changes listener
455 //=======================================================================
456 void CurveCreator_ICurve::removeListener()