1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: CurveCreator_Curve.cxx
24 // Created: Thu Jun 20 9:54:07 2013
25 // Author: Sergey KHROMOV
29 #include <CurveCreator_Curve.hxx>
30 #include <CurveCreator_Section.hxx>
33 //=======================================================================
34 // function: Constructor
36 //=======================================================================
37 CurveCreator_Curve::CurveCreator_Curve
38 (const CurveCreator::Dimension theDimension)
40 myDimension (theDimension)
44 //=======================================================================
45 // function: Destructor
47 //=======================================================================
48 CurveCreator_Curve::~CurveCreator_Curve()
50 // Delete all allocated data.
54 //=======================================================================
57 //=======================================================================
58 bool CurveCreator_Curve::isLocked() const
63 //=======================================================================
64 // function: getDimension
66 //=======================================================================
67 CurveCreator::Dimension CurveCreator_Curve::getDimension() const
72 //=======================================================================
73 // function: getNbPoints
75 //=======================================================================
76 int CurveCreator_Curve::getNbPoints(const int theISection) const
80 if (theISection == -1) {
82 const int aNbSections = getNbSections();
84 for (; i < aNbSections; i++) {
85 aNbCoords += mySections[i]->myPoints.size();
88 aNbCoords = mySections.at(theISection)->myPoints.size();
91 return aNbCoords/myDimension;
94 //=======================================================================
95 // function: getNbSections
97 //=======================================================================
98 int CurveCreator_Curve::getNbSections() const
100 return mySections.size();
103 //=======================================================================
104 // function: getCoordinates
106 //=======================================================================
107 CurveCreator::Coordinates CurveCreator_Curve::getCoordinates
108 (const int theISection, const int theIPnt) const
110 CurveCreator_Section *aSection = mySections.at(theISection);
111 CurveCreator::Coordinates::const_iterator
112 anIter = aSection->myPoints.begin() + toICoord(theIPnt);
113 CurveCreator::Coordinates aResult(anIter, anIter + myDimension);
118 //=======================================================================
121 //=======================================================================
122 CurveCreator::Type CurveCreator_Curve::getType(const int theISection) const
124 return mySections.at(theISection)->myType;
127 //=======================================================================
128 // function: getPoints
130 //=======================================================================
131 const CurveCreator::Coordinates &CurveCreator_Curve::getPoints
132 (const int theISection) const
134 return mySections.at(theISection)->myPoints;
137 //=======================================================================
138 // function: isClosed
140 //=======================================================================
141 bool CurveCreator_Curve::isClosed(const int theISection) const
143 return mySections.at(theISection)->myIsClosed;
146 std::string CurveCreator_Curve::getSectionName(const int theISection) const
148 return mySections.at(theISection)->myName;
151 //=======================================================================
154 //=======================================================================
155 void CurveCreator_Curve::setType
156 (const CurveCreator::Type theType, const int theISection)
158 if (theISection == -1) {
160 const int aNbSections = getNbSections();
162 for (; i < aNbSections; i++) {
163 mySections[i]->myType = theType;
166 mySections.at(theISection)->myType = theType;
170 //=======================================================================
171 // function: addPoints
173 //=======================================================================
174 void CurveCreator_Curve::addPoints
175 (const CurveCreator::Coordinates &thePoints, const int theISection)
177 CurveCreator_Section *aSection =
178 (theISection == -1 ? mySections.back() : mySections.at(theISection));
180 aSection->myPoints.insert(aSection->myPoints.end(),
181 thePoints.begin(), thePoints.end());
184 //=======================================================================
185 // function: addSection
187 //=======================================================================
188 void CurveCreator_Curve::addSection
189 (const std::string& theName,
190 const CurveCreator::Type theType,
191 const bool theIsClosed,
192 const CurveCreator::Coordinates &thePoints)
194 CurveCreator_Section *aSection = new CurveCreator_Section;
196 std::string aName = theName;
198 aName = getUnicSectionName();
200 aSection->myName = aName;
201 aSection->myType = theType;
202 aSection->myIsClosed = theIsClosed;
203 aSection->myPoints = thePoints;
204 mySections.push_back(aSection);
207 //=======================================================================
208 // function: removeSection
210 //=======================================================================
211 void CurveCreator_Curve::removeSection(const int theISection)
213 if (theISection == -1) {
214 delete mySections.back();
215 mySections.pop_back();
217 Sections::iterator anIterRm = mySections.begin() + theISection;
220 mySections.erase(anIterRm);
224 //=======================================================================
225 // function: insertPoints
227 //=======================================================================
228 void CurveCreator_Curve::insertPoints
229 (const CurveCreator::Coordinates &thePoints,
230 const int theISection,
234 // Add points to the end of section points.
235 addPoints(thePoints, theISection);
237 CurveCreator_Section *aSection =
238 (theISection == -1 ? mySections.back() : mySections.at(theISection));
240 aSection->myPoints.insert(aSection->myPoints.begin() + toICoord(theIPnt),
241 thePoints.begin(), thePoints.end());
245 void CurveCreator_Curve::movePoint(const int theISection, const int theIPointFrom, const int theNewIndex)
247 CurveCreator::Coordinates aCoords = getCoordinates(theISection, theIPointFrom );
248 insertPoints(aCoords, theISection, theNewIndex+1);
249 int aRemPntIndx = theIPointFrom;
250 if( theNewIndex < theIPointFrom )
252 removePoints(theISection, aRemPntIndx, 1 );
255 //=======================================================================
256 // function: removePoints
258 //=======================================================================
259 void CurveCreator_Curve::removePoints(const int theISection,
261 const int theNbPoints)
263 CurveCreator_Section *aSection = mySections.at(theISection);
264 CurveCreator::Coordinates::iterator anIterBegin =
265 aSection->myPoints.begin() + toICoord(theIPnt);
266 CurveCreator::Coordinates::iterator anIterEnd =
268 aSection->myPoints.end() : anIterBegin + toICoord(theNbPoints));
270 aSection->myPoints.erase(anIterBegin, anIterEnd);
273 //=======================================================================
276 //=======================================================================
277 void CurveCreator_Curve::clear()
279 // Delete all allocated data.
281 const int aNbSections = getNbSections();
283 for (; i < aNbSections; i++) {
284 delete mySections[i];
290 //=======================================================================
291 // function: setCoordinates
293 //=======================================================================
294 void CurveCreator_Curve::setCoordinates
295 (const CurveCreator::Coordinates &theCoords,
296 const int theISection,
299 if (theCoords.size() == myDimension) {
300 CurveCreator_Section *aSection = mySections.at(theISection);
303 for (i = 0; i < myDimension; i++) {
304 aSection->myPoints.at(toICoord(theIPnt) + i) = theCoords[i];
309 //=======================================================================
310 // function: setClosed
312 //=======================================================================
313 void CurveCreator_Curve::setClosed(const bool theIsClosed,
314 const int theISection)
316 if (theISection == -1) {
317 int aSize = mySections.size();
320 for (i = 0; i < aSize; i++) {
321 mySections[i]->myIsClosed = theIsClosed;
324 mySections.at(theISection)->myIsClosed = theIsClosed;
328 /** Set name of the specified section.
330 void CurveCreator_Curve::setName( const std::string& theName, const int theISection )
332 if( ( theISection >= 0 ) && ( theISection < mySections.size() )){
333 mySections.at(theISection)->myName = theName;
337 //=======================================================================
338 // function: moveSection
340 //=======================================================================
341 void CurveCreator_Curve::moveSection(const int theISection,
342 const int theNewIndex)
344 if (theISection != theNewIndex) {
345 CurveCreator_Section *aSection = mySections.at(theISection);
348 Sections::iterator anIter = mySections.begin() + theISection;
350 mySections.erase(anIter);
353 anIter = mySections.begin() + theNewIndex;
354 mySections.insert(anIter, aSection);
358 //=======================================================================
361 //=======================================================================
362 void CurveCreator_Curve::join(const int theISectionTo,
363 const int theISectionFrom)
365 if (theISectionTo != theISectionFrom) {
366 CurveCreator_Section *aSection1 = mySections.at(theISectionTo);
367 CurveCreator_Section *aSection2 = mySections.at(theISectionFrom);
369 aSection1->myPoints.insert(aSection1->myPoints.end(),
370 aSection2->myPoints.begin(), aSection2->myPoints.end());
372 removeSection(theISectionFrom);
376 //=======================================================================
379 //=======================================================================
380 void CurveCreator_Curve::join()
382 const int aSize = mySections.size();
385 CurveCreator_Section *aSection1 = mySections[0];
388 for (i = 1; i < aSize; i++) {
389 CurveCreator_Section *aSection2 = mySections[i];
391 aSection1->myPoints.insert(aSection1->myPoints.end(),
392 aSection2->myPoints.begin(), aSection2->myPoints.end());
396 // Just erace section pointers as they were deleted before.
397 mySections.erase(mySections.begin() + 1, mySections.end());
401 //=======================================================================
402 // function: toICoord
404 //=======================================================================
405 int CurveCreator_Curve::toICoord(const int theIPnt) const
407 return theIPnt*myDimension;
410 //=======================================================================
411 // function: getUnicSectionName
412 // purpose: return unic section name
413 //=======================================================================
414 std::string CurveCreator_Curve::getUnicSectionName()
416 for( int i = 0 ; i < 1000000 ; i++ ){
418 sprintf( aBuffer, "Section_%d", i+1 );
419 std::string aName(aBuffer);
421 for( j = 0 ; j < mySections.size() ; j++ ){
422 if( mySections[j]->myName == aName )
425 if( j == mySections.size() )