Salome HOME
PAL12608: Add possibility to check geometry of a shape.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ICurvesOperations.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include <Standard_Stream.hxx>
21
22 #include <GEOMImpl_ICurvesOperations.hxx>
23
24 #include <GEOM_Function.hxx>
25 #include <GEOM_PythonDump.hxx>
26
27 #include <GEOMImpl_Types.hxx>
28
29 #include <GEOMImpl_PolylineDriver.hxx>
30 #include <GEOMImpl_CircleDriver.hxx>
31 #include <GEOMImpl_SplineDriver.hxx>
32 #include <GEOMImpl_EllipseDriver.hxx>
33 #include <GEOMImpl_ArcDriver.hxx>
34 #include <GEOMImpl_SketcherDriver.hxx>
35
36 #include <GEOMImpl_IPolyline.hxx>
37 #include <GEOMImpl_ICircle.hxx>
38 #include <GEOMImpl_ISpline.hxx>
39 #include <GEOMImpl_IEllipse.hxx>
40 #include <GEOMImpl_IArc.hxx>
41 #include <GEOMImpl_ISketcher.hxx>
42
43 #include "utilities.h"
44
45 #include <TDF_Tool.hxx>
46
47 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
48
49 //=============================================================================
50 /*!
51  *   constructor:
52  */
53 //=============================================================================
54 GEOMImpl_ICurvesOperations::GEOMImpl_ICurvesOperations (GEOM_Engine* theEngine, int theDocID)
55 : GEOM_IOperations(theEngine, theDocID)
56 {
57   MESSAGE("GEOMImpl_ICurvesOperations::GEOMImpl_ICurvesOperations");
58 }
59
60 //=============================================================================
61 /*!
62  *  destructor
63  */
64 //=============================================================================
65 GEOMImpl_ICurvesOperations::~GEOMImpl_ICurvesOperations()
66 {
67   MESSAGE("GEOMImpl_ICurvesOperations::~GEOMImpl_ICurvesOperations");
68 }
69
70
71 //=============================================================================
72 /*!
73  *  MakePolyline
74  */
75 //=============================================================================
76 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakePolyline (list<Handle(GEOM_Object)> thePoints)
77 {
78   SetErrorCode(KO);
79
80   //Add a new Polyline object
81   Handle(GEOM_Object) aPolyline = GetEngine()->AddObject(GetDocID(), GEOM_POLYLINE);
82
83   //Add a new Polyline function for creation a polyline relatively to points set
84   Handle(GEOM_Function) aFunction =
85     aPolyline->AddFunction(GEOMImpl_PolylineDriver::GetID(), POLYLINE_POINTS);
86   if (aFunction.IsNull()) return NULL;
87
88   //Check if the function is set correctly
89   if (aFunction->GetDriverGUID() != GEOMImpl_PolylineDriver::GetID()) return NULL;
90
91   GEOMImpl_IPolyline aCI (aFunction);
92
93   int aLen = thePoints.size();
94   aCI.SetLength(aLen);
95
96   int ind = 1;
97   list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
98   for (; it != thePoints.end(); it++, ind++) {
99     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
100     if (aRefPnt.IsNull()) {
101       SetErrorCode("NULL point for Polyline");
102       return NULL;
103     }
104     aCI.SetPoint(ind, aRefPnt);
105   }
106
107   //Compute the Polyline value
108   try {
109     if (!GetSolver()->ComputeFunction(aFunction)) {
110       SetErrorCode("Polyline driver failed");
111       return NULL;
112     }
113   }
114   catch (Standard_Failure) {
115     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
116     SetErrorCode(aFail->GetMessageString());
117     return NULL;
118   }
119
120   //Make a Python command
121   GEOM::TPythonDump pd (aFunction);
122   pd << aPolyline << " = geompy.MakePolyline([";
123
124   it = thePoints.begin();
125   pd << (*it++);
126   while (it != thePoints.end()) {
127     pd << ", " << (*it++);
128   }
129   pd << "])";
130
131   SetErrorCode(OK);
132   return aPolyline;
133 }
134
135 //=============================================================================
136 /*!
137  *  MakeCircleThreePnt
138  */
139 //=============================================================================
140 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCircleThreePnt (Handle(GEOM_Object) thePnt1,
141                                                                     Handle(GEOM_Object) thePnt2,
142                                                                     Handle(GEOM_Object) thePnt3)
143 {
144   SetErrorCode(KO);
145
146   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
147
148   //Add a new Circle object
149   Handle(GEOM_Object) aCircle = GetEngine()->AddObject(GetDocID(), GEOM_CIRCLE);
150
151   //Add a new Circle function for creation a circle relatively to three points
152   Handle(GEOM_Function) aFunction =
153     aCircle->AddFunction(GEOMImpl_CircleDriver::GetID(), CIRCLE_THREE_PNT);
154   if (aFunction.IsNull()) return NULL;
155
156   //Check if the function is set correctly
157   if (aFunction->GetDriverGUID() != GEOMImpl_CircleDriver::GetID()) return NULL;
158
159   GEOMImpl_ICircle aCI (aFunction);
160
161   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
162   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
163   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
164
165   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
166
167   aCI.SetPoint1(aRefPnt1);
168   aCI.SetPoint2(aRefPnt2);
169   aCI.SetPoint3(aRefPnt3);
170
171   //Compute the Circle value
172   try {
173     if (!GetSolver()->ComputeFunction(aFunction)) {
174       SetErrorCode("Circle driver failed");
175       return NULL;
176     }
177   }
178   catch (Standard_Failure) {
179     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
180     SetErrorCode(aFail->GetMessageString());
181     return NULL;
182   }
183
184   //Make a Python command
185   GEOM::TPythonDump(aFunction) << aCircle << " = geompy.MakeCircleThreePnt("
186     << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << ")";
187
188   SetErrorCode(OK);
189   return aCircle;
190 }
191
192 //=============================================================================
193 /*!
194  *  MakeCirclePntVecR
195  */
196 //=============================================================================
197 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCirclePntVecR
198        (Handle(GEOM_Object) thePnt, Handle(GEOM_Object) theVec, double theR)
199 {
200   SetErrorCode(KO);
201
202   if (thePnt.IsNull() || theVec.IsNull()) return NULL;
203
204   //Add a new Circle object
205   Handle(GEOM_Object) aCircle = GetEngine()->AddObject(GetDocID(), GEOM_CIRCLE);
206
207   //Add a new Circle function for creation a circle relatively to point and vector
208   Handle(GEOM_Function) aFunction =
209     aCircle->AddFunction(GEOMImpl_CircleDriver::GetID(), CIRCLE_PNT_VEC_R);
210   if (aFunction.IsNull()) return NULL;
211
212   //Check if the function is set correctly
213   if (aFunction->GetDriverGUID() != GEOMImpl_CircleDriver::GetID()) return NULL;
214
215   GEOMImpl_ICircle aCI (aFunction);
216
217   Handle(GEOM_Function) aRefPnt = thePnt->GetLastFunction();
218   Handle(GEOM_Function) aRefVec = theVec->GetLastFunction();
219
220   if (aRefPnt.IsNull() || aRefVec.IsNull()) return NULL;
221
222   aCI.SetCenter(aRefPnt);
223   aCI.SetVector(aRefVec);
224   aCI.SetRadius(theR);
225
226   //Compute the Circle value
227   try {
228     if (!GetSolver()->ComputeFunction(aFunction)) {
229       SetErrorCode("Circle driver failed");
230       return NULL;
231     }
232   }
233   catch (Standard_Failure) {
234     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
235     SetErrorCode(aFail->GetMessageString());
236     return NULL;
237   }
238
239   //Make a Python command
240   GEOM::TPythonDump(aFunction) << aCircle << " = geompy.MakeCircle("
241     << thePnt << ", " << theVec << ", " << theR << ")";
242
243   SetErrorCode(OK);
244   return aCircle;
245 }
246
247 //=============================================================================
248 /*!
249  *  MakeEllipse
250  */
251 //=============================================================================
252 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeEllipse
253                        (Handle(GEOM_Object) thePnt, Handle(GEOM_Object) theVec,
254                         double theRMajor, double theRMinor)
255 {
256   SetErrorCode(KO);
257
258   if (thePnt.IsNull() || theVec.IsNull()) return NULL;
259
260   //Add a new Ellipse object
261   Handle(GEOM_Object) anEll = GetEngine()->AddObject(GetDocID(), GEOM_ELLIPSE);
262
263   //Add a new Ellipse function
264   Handle(GEOM_Function) aFunction =
265     anEll->AddFunction(GEOMImpl_EllipseDriver::GetID(), ELLIPSE_PNT_VEC_RR);
266   if (aFunction.IsNull()) return NULL;
267
268   //Check if the function is set correctly
269   if (aFunction->GetDriverGUID() != GEOMImpl_EllipseDriver::GetID()) return NULL;
270
271   GEOMImpl_IEllipse aCI (aFunction);
272
273   Handle(GEOM_Function) aRefPnt = thePnt->GetLastFunction();
274   Handle(GEOM_Function) aRefVec = theVec->GetLastFunction();
275
276   if (aRefPnt.IsNull() || aRefVec.IsNull()) return NULL;
277
278   aCI.SetCenter(aRefPnt);
279   aCI.SetVector(aRefVec);
280   aCI.SetRMajor(theRMajor);
281   aCI.SetRMinor(theRMinor);
282
283   //Compute the Ellipse value
284   try {
285     if (!GetSolver()->ComputeFunction(aFunction)) {
286       SetErrorCode("Ellipse driver failed");
287       return NULL;
288     }
289   }
290   catch (Standard_Failure) {
291     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
292     SetErrorCode(aFail->GetMessageString());
293     return NULL;
294   }
295
296   //Make a Python command
297   GEOM::TPythonDump(aFunction) << anEll << " = geompy.MakeEllipse("
298     << thePnt << ", " << theVec << ", " << theRMajor << ", " << theRMinor << ")";
299
300   SetErrorCode(OK);
301   return anEll;
302 }
303
304 //=============================================================================
305 /*!
306  *  MakeArc
307  */
308 //=============================================================================
309 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeArc (Handle(GEOM_Object) thePnt1,
310                                                          Handle(GEOM_Object) thePnt2,
311                                                          Handle(GEOM_Object) thePnt3)
312 {
313   SetErrorCode(KO);
314
315   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
316
317   //Add a new Circle Arc object
318   Handle(GEOM_Object) anArc = GetEngine()->AddObject(GetDocID(), GEOM_CIRC_ARC);
319
320   //Add a new Circle Arc function
321   Handle(GEOM_Function) aFunction =
322     anArc->AddFunction(GEOMImpl_ArcDriver::GetID(), CIRC_ARC_THREE_PNT);
323   if (aFunction.IsNull()) return NULL;
324
325   //Check if the function is set correctly
326   if (aFunction->GetDriverGUID() != GEOMImpl_ArcDriver::GetID()) return NULL;
327
328   GEOMImpl_IArc aCI (aFunction);
329
330   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
331   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
332   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
333
334   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
335
336   aCI.SetPoint1(aRefPnt1);
337   aCI.SetPoint2(aRefPnt2);
338   aCI.SetPoint3(aRefPnt3);
339
340   //Compute the Arc value
341   try {
342     if (!GetSolver()->ComputeFunction(aFunction)) {
343       SetErrorCode("Arc driver failed");
344       return NULL;
345     }
346   }
347   catch (Standard_Failure) {
348     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
349     SetErrorCode(aFail->GetMessageString());
350     return NULL;
351   }
352
353   //Make a Python command
354   GEOM::TPythonDump(aFunction) << anArc << " = geompy.MakeArc("
355     << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << ")";
356
357   SetErrorCode(OK);
358   return anArc;
359 }
360
361 //=============================================================================
362 /*!
363  *  MakeSplineBezier
364  */
365 //=============================================================================
366 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineBezier
367                                           (list<Handle(GEOM_Object)> thePoints)
368 {
369   SetErrorCode(KO);
370
371   //Add a new Spline object
372   Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
373
374   //Add a new Spline function for creation a bezier curve relatively to points set
375   Handle(GEOM_Function) aFunction =
376     aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_BEZIER);
377   if (aFunction.IsNull()) return NULL;
378
379   //Check if the function is set correctly
380   if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
381
382   GEOMImpl_ISpline aCI (aFunction);
383
384   int aLen = thePoints.size();
385   aCI.SetLength(aLen);
386
387   int ind = 1;
388   list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
389   for (; it != thePoints.end(); it++, ind++) {
390     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
391
392     if (aRefPnt.IsNull()) return NULL;
393
394     aCI.SetPoint(ind, aRefPnt);
395   }
396
397   //Compute the Spline value
398   try {
399     if (!GetSolver()->ComputeFunction(aFunction)) {
400       SetErrorCode("Spline driver failed");
401       return NULL;
402     }
403   }
404   catch (Standard_Failure) {
405     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
406     SetErrorCode(aFail->GetMessageString());
407     return NULL;
408   }
409
410   //Make a Python command
411   GEOM::TPythonDump pd (aFunction);
412   pd << aSpline << " = geompy.MakeBezier([";
413
414   it = thePoints.begin();
415   pd << (*it++);
416   while (it != thePoints.end()) {
417     pd << ", " << (*it++);
418   }
419   pd << "])";
420
421   SetErrorCode(OK);
422   return aSpline;
423 }
424
425 //=============================================================================
426 /*!
427  *  MakeSplineInterpolation
428  */
429 //=============================================================================
430 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineInterpolation
431                                           (list<Handle(GEOM_Object)> thePoints)
432 {
433   SetErrorCode(KO);
434
435   //Add a new Spline object
436   Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
437
438   //Add a new Spline function for creation a bezier curve relatively to points set
439   Handle(GEOM_Function) aFunction =
440     aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOLATION);
441   if (aFunction.IsNull()) return NULL;
442
443   //Check if the function is set correctly
444   if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
445
446   GEOMImpl_ISpline aCI (aFunction);
447
448   int aLen = thePoints.size();
449   aCI.SetLength(aLen);
450
451   int ind = 1;
452   list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
453   for (; it != thePoints.end(); it++, ind++) {
454     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
455
456     if (aRefPnt.IsNull()) return NULL;
457
458     aCI.SetPoint(ind, aRefPnt);
459   }
460
461   //Compute the Spline value
462   try {
463     if (!GetSolver()->ComputeFunction(aFunction)) {
464       SetErrorCode("Spline driver failed");
465       return NULL;
466     }
467   }
468   catch (Standard_Failure) {
469     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
470     SetErrorCode(aFail->GetMessageString());
471     return NULL;
472   }
473
474   //Make a Python command
475   GEOM::TPythonDump pd (aFunction);
476   pd << aSpline << " = geompy.MakeInterpol([";
477
478   it = thePoints.begin();
479   pd << (*it++);
480   while (it != thePoints.end()) {
481     pd << ", " << (*it++);
482   }
483   pd << "])";
484
485   SetErrorCode(OK);
486   return aSpline;
487 }
488
489 //=============================================================================
490 /*!
491  *  MakeSketcher
492  */
493 //=============================================================================
494 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSketcher
495                                (const TCollection_AsciiString& theCommand,
496                                 list<double>                   theWorkingPlane)
497 {
498   SetErrorCode(KO);
499
500   if (theCommand.IsEmpty()) return NULL;
501
502   //Add a new Sketcher object
503   Handle(GEOM_Object) aSketcher = GetEngine()->AddObject(GetDocID(), GEOM_SKETCHER);
504
505   //Add a new Sketcher function
506   Handle(GEOM_Function) aFunction =
507     aSketcher->AddFunction(GEOMImpl_SketcherDriver::GetID(), SKETCHER_NINE_DOUBLS);
508   if (aFunction.IsNull()) return NULL;
509
510   //Check if the function is set correctly
511   if (aFunction->GetDriverGUID() != GEOMImpl_SketcherDriver::GetID()) return NULL;
512
513   GEOMImpl_ISketcher aCI (aFunction);
514
515   aCI.SetCommand(theCommand);
516
517   int ind = 1;
518   list<double>::iterator it = theWorkingPlane.begin();
519   for (; it != theWorkingPlane.end(); it++, ind++)
520     aCI.SetWorkingPlane(ind, *it);
521
522   //Compute the Sketcher value
523   try {
524     if (!GetSolver()->ComputeFunction(aFunction)) {
525       SetErrorCode("Sketcher driver failed");
526       return NULL;
527     }
528   }
529   catch (Standard_Failure) {
530     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
531     SetErrorCode(aFail->GetMessageString());
532     return NULL;
533   }
534
535   //Make a Python command
536   GEOM::TPythonDump pd (aFunction);
537   pd << aSketcher << " = geompy.MakeSketcher(\"" << theCommand.ToCString() << "\", [";
538
539   it = theWorkingPlane.begin();
540   pd << (*it++);
541   while (it != theWorkingPlane.end()) {
542     pd << ", " << (*it++);
543   }
544   pd << "])";
545
546   SetErrorCode(OK);
547   return aSketcher;
548 }
549
550 //=============================================================================
551 /*!
552  *  MakeSketcherOnPlane
553  */
554 //=============================================================================
555 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSketcherOnPlane
556                                (const TCollection_AsciiString& theCommand,
557                                 Handle(GEOM_Object)            theWorkingPlane)
558 {
559   SetErrorCode(KO);
560
561   if (theCommand.IsEmpty()) return NULL;
562
563   //Add a new Sketcher object
564   Handle(GEOM_Object) aSketcher = GetEngine()->AddObject(GetDocID(), GEOM_SKETCHER);
565
566   //Add a new Sketcher function
567   Handle(GEOM_Function) aFunction =
568     aSketcher->AddFunction(GEOMImpl_SketcherDriver::GetID(), SKETCHER_PLANE);
569   if (aFunction.IsNull()) return NULL;
570
571   //Check if the function is set correctly
572   if (aFunction->GetDriverGUID() != GEOMImpl_SketcherDriver::GetID()) return NULL;
573
574   GEOMImpl_ISketcher aCI (aFunction);
575   aCI.SetCommand(theCommand);
576
577   Handle(GEOM_Function) aRefPlane = theWorkingPlane->GetLastFunction();
578   if (aRefPlane.IsNull()) return NULL;
579   aCI.SetWorkingPlane( aRefPlane );
580
581   //Compute the Sketcher value
582   try {
583     if (!GetSolver()->ComputeFunction(aFunction)) {
584       SetErrorCode("Sketcher driver failed");
585       return NULL;
586     }
587   }
588   catch (Standard_Failure) {
589     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
590     SetErrorCode(aFail->GetMessageString());
591     return NULL;
592   }
593
594   //Make a Python command
595   GEOM::TPythonDump (aFunction) << aSketcher << " = geompy.MakeSketcherOnPlane(\""
596     << theCommand.ToCString() << "\", " << theWorkingPlane << " )";
597
598   SetErrorCode(OK);
599   return aSketcher;
600 }