1 // Copyright (C) 2013-2014 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, or (at your option) any later version.
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_Operation.cxx
21 // Author: Sergey KHROMOV
23 #include "CurveCreator_Operation.hxx"
24 #include "CurveCreator_Curve.hxx"
30 //=======================================================================
31 // function: Constructor
33 //=======================================================================
34 CurveCreator_Operation::CurveCreator_Operation()
35 : myType (CurveCreator_Operation::Unknown),
40 //=======================================================================
41 // function: Destructor
43 //=======================================================================
44 CurveCreator_Operation::~CurveCreator_Operation()
49 //=======================================================================
50 // function: Constructor
52 //=======================================================================
53 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType)
57 if (theType == CurveCreator_Operation::Clear ||
58 theType == CurveCreator_Operation::Join) {
67 //=======================================================================
68 // function: Constructor
70 //=======================================================================
71 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
72 const int theIntParam)
76 if (theType == CurveCreator_Operation::RemoveSection) {
77 int *pData = (int *)allocate(sizeof(int));
79 pData[0] = theIntParam;
87 //=======================================================================
88 // function: Constructor
90 //=======================================================================
91 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
92 const int theIntParam1,
93 const int theIntParam2)
97 if (theType == CurveCreator_Operation::SetType ||
98 theType == CurveCreator_Operation::SetClosed ||
99 theType == CurveCreator_Operation::MoveSection ||
100 theType == CurveCreator_Operation::Join) {
101 int *pData = (int *)allocate(2*sizeof(int));
103 pData[0] = theIntParam1;
104 pData[1] = theIntParam2;
112 //=======================================================================
113 // function: Constructor
115 //=======================================================================
116 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
117 const int theIntParam1,
118 const int theIntParam2,
119 const int theIntParam3)
123 if (theType == CurveCreator_Operation::RemovePoints) {
124 int *pData = (int *)allocate(3*sizeof(int));
126 pData[0] = theIntParam1;
127 pData[1] = theIntParam2;
128 pData[2] = theIntParam3;
136 //=======================================================================
137 // function: Constructor
139 //=======================================================================
140 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
141 const CurveCreator::Coordinates &theCoords,
142 const int theIntParam)
146 if (theType == CurveCreator_Operation::AddPoints) {
147 const int aNbCoords = theCoords.size();
149 2*sizeof(theIntParam) + aNbCoords*sizeof(CurveCreator::TypeCoord);
150 int *pIntData = (int *)allocate(aSize);
152 *pIntData++ = theIntParam;
153 *pIntData++ = aNbCoords;
155 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
158 for (; i < aNbCoords; i++) {
159 *pRealData++ = theCoords[i];
169 //=======================================================================
170 // function: Constructor
172 //=======================================================================
173 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
174 const CurveCreator::Coordinates &theCoords,
175 const int theIntParam1,
176 const int theIntParam2)
180 if (theType == CurveCreator_Operation::AddSection ||
181 theType == CurveCreator_Operation::InsertPoints ||
182 theType == CurveCreator_Operation::SetCoordinates) {
183 const int aNbCoords = theCoords.size();
185 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord);
186 int *pIntData = (int *)allocate(aSize);
188 *pIntData++ = theIntParam1;
189 *pIntData++ = theIntParam2;
190 *pIntData++ = aNbCoords;
192 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)pIntData;
195 for (; i < aNbCoords; i++) {
196 *pRealData++ = theCoords[i];
206 //=======================================================================
207 // function: Constructor
209 //=======================================================================
210 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
211 const std::string& theName,
212 const CurveCreator::Coordinates &theCoords,
213 const int theIntParam1,
214 const int theIntParam2)
217 if (theType == CurveCreator_Operation::AddSection ) {
218 const int aNbCoords = theCoords.size();
220 3*sizeof(theIntParam1) + aNbCoords*sizeof(CurveCreator::TypeCoord) + theName.length() + 1;
221 int *pIntData = (int *)allocate(aSize);
223 *pIntData++ = theIntParam1;
224 *pIntData++ = theIntParam2;
225 char* aStrPtr = (char*)pIntData;
226 if( !theName.empty() ){
227 strcpy( aStrPtr, theName.c_str() );
228 aStrPtr += theName.length();
234 pIntData = (int*)aStrPtr;
235 *pIntData++ = aNbCoords;
237 CurveCreator::TypeCoord *pRealData = (CurveCreator::TypeCoord *)aStrPtr;
240 for (; i < aNbCoords; i++) {
241 *pRealData++ = theCoords[i];
251 bool CurveCreator_Operation::init(const CurveCreator_Operation::Type theType,
252 const std::string &theName,
253 const int theIntParam1 )
255 if (theType == CurveCreator_Operation::RenameSection ) {
256 size_t aSize = sizeof(theIntParam1) + theName.length() + 1;
257 int *pIntData = (int *)allocate(aSize);
258 *pIntData = theIntParam1;
260 if( !theName.empty() ){
261 strcpy( (char*)pIntData, theName.c_str() );
264 *((char*)pIntData) = 0;
272 //=======================================================================
275 //=======================================================================
276 void CurveCreator_Operation::apply(CurveCreator_Curve *theCurve)
278 if (theCurve != NULL) {
279 int *pInt = (int *)myPData;
282 case CurveCreator_Operation::AddPoints:
284 CurveCreator::Coordinates aCoords;
286 getCoords(&pInt[1], aCoords);
287 theCurve->addPoints(aCoords, pInt[0]);
290 case CurveCreator_Operation::RemovePoints:
291 theCurve->removePoints(pInt[0], pInt[1], pInt[2]);
293 case CurveCreator_Operation::InsertPoints:
295 CurveCreator::Coordinates aCoords;
297 getCoords(&pInt[2], aCoords);
298 theCurve->insertPoints(aCoords, pInt[0], pInt[1]);
301 case CurveCreator_Operation::SetType:
303 const CurveCreator::Type aType = (CurveCreator::Type) pInt[0];
305 theCurve->setType(aType, pInt[1]);
308 case CurveCreator_Operation::Clear:
311 case CurveCreator_Operation::SetCoordinates:
313 CurveCreator::Coordinates aCoords;
315 getCoords(&pInt[2], aCoords);
316 theCurve->setCoordinates(aCoords, pInt[0], pInt[1]);
319 case CurveCreator_Operation::SetClosed:
320 theCurve->setClosed((pInt[0] != 0), pInt[1]);
322 case CurveCreator_Operation::MoveSection:
323 theCurve->moveSection(pInt[0], pInt[1]);
325 case CurveCreator_Operation::Join:
326 if (myPData == NULL) {
329 theCurve->join(pInt[0], pInt[1]);
332 case CurveCreator_Operation::AddSection:
334 const CurveCreator::Type aType = (CurveCreator::Type) pInt[0];
336 std::string aName = std::string((char*)&pInt[2]);
338 CurveCreator::Coordinates aCoords;
340 char* aPtr = ((char*)&pInt[2]);
341 aPtr += (aName.length()) + 1;
342 getCoords((int*)aPtr, aCoords);
343 theCurve->addSection(aName, aType, (pInt[1] != 0), aCoords);
346 case CurveCreator_Operation::RemoveSection:
347 theCurve->removeSection(pInt[0]);
349 case CurveCreator_Operation::RenameSection:
351 std::string aName = std::string((char*)&pInt[1]);
352 theCurve->setName(aName, pInt[0]);
361 //=======================================================================
362 // function: allocate
364 //=======================================================================
365 void *CurveCreator_Operation::allocate(const size_t theSize)
367 if (myPData != NULL) {
371 myPData = malloc(theSize);
376 //=======================================================================
379 //=======================================================================
380 void CurveCreator_Operation::clear()
382 myType = CurveCreator_Operation::Unknown;
384 if (myPData != NULL) {
390 //=======================================================================
391 // function: getCoords
393 //=======================================================================
394 void CurveCreator_Operation::getCoords
395 (int *thePInt, CurveCreator::Coordinates &theCoords) const
397 const int aNbPnts = *thePInt;
398 CurveCreator::TypeCoord *pCoord = (CurveCreator::TypeCoord *)&thePInt[1];
400 for (int i = 0; i < aNbPnts; i++) {
401 theCoords.push_back(pCoord[i]);