Salome HOME
9934610e31e3e0b62d99c3fba8c0475802cf53cd
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ICurvesOperations.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifdef WNT
24 // E.A. : On windows with python 2.6, there is a conflict
25 // E.A. : between pymath.h and Standard_math.h which define
26 // E.A. : some same symbols : acosh, asinh, ...
27 #include <Standard_math.hxx>
28 #include <pymath.h>
29 #endif
30
31 #include <Python.h>
32 #include <structmember.h>
33
34 #ifdef HAVE_FINITE
35 #undef HAVE_FINITE
36 #endif
37
38 #include "GEOMImpl_ICurvesOperations.hxx"
39 #include "GEOMImpl_Types.hxx"
40
41 #include "GEOM_Function.hxx"
42 #include "GEOM_PythonDump.hxx"
43
44 #include "GEOMImpl_PolylineDriver.hxx"
45 #include "GEOMImpl_CircleDriver.hxx"
46 #include "GEOMImpl_SplineDriver.hxx"
47 #include "GEOMImpl_EllipseDriver.hxx"
48 #include "GEOMImpl_ArcDriver.hxx"
49 #include "GEOMImpl_SketcherDriver.hxx"
50 #include "GEOMImpl_3DSketcherDriver.hxx"
51
52 #include "GEOMImpl_IPolyline.hxx"
53 #include "GEOMImpl_ICircle.hxx"
54 #include "GEOMImpl_ISpline.hxx"
55 #include "GEOMImpl_IEllipse.hxx"
56 #include "GEOMImpl_IArc.hxx"
57 #include "GEOMImpl_ISketcher.hxx"
58 #include "GEOMImpl_I3DSketcher.hxx"
59 #include "GEOMImpl_ICurveParametric.hxx"
60
61 #include <Basics_OCCTVersion.hxx>
62
63 #include "utilities.h"
64
65 #include <TDF_Tool.hxx>
66 #include <TColStd_HArray1OfReal.hxx>
67
68 #include <Standard_Failure.hxx>
69 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
70
71
72 /* ==================================
73  * ===========  PYTHON ==============
74  * ==================================*/
75
76 typedef struct {
77   PyObject_HEAD
78   int softspace;
79   std::string *out;
80   } PyStdOut;
81
82 static void
83 PyStdOut_dealloc(PyStdOut *self)
84 {
85   PyObject_Del(self);
86 }
87
88 static PyObject *
89 PyStdOut_write(PyStdOut *self, PyObject *args)
90 {
91   char *c;
92   int l;
93   if (!PyArg_ParseTuple(args, "t#:write",&c, &l))
94     return NULL;
95
96   //std::cerr << c ;
97   *(self->out)=*(self->out)+c;
98
99   Py_INCREF(Py_None);
100   return Py_None;
101 }
102
103 static PyMethodDef PyStdOut_methods[] = {
104   {"write",  (PyCFunction)PyStdOut_write,  METH_VARARGS,
105     PyDoc_STR("write(string) -> None")},
106   {NULL,    NULL}   /* sentinel */
107 };
108
109 static PyMemberDef PyStdOut_memberlist[] = {
110   {(char*)"softspace", T_INT,  offsetof(PyStdOut, softspace), 0,
111    (char*)"flag indicating that a space needs to be printed; used by print"},
112   {NULL} /* Sentinel */
113 };
114
115 static PyTypeObject PyStdOut_Type = {
116   /* The ob_type field must be initialized in the module init function
117    * to be portable to Windows without using C++. */
118   PyObject_HEAD_INIT(NULL)
119   0,                            /*ob_size*/
120   "PyOut",                      /*tp_name*/
121   sizeof(PyStdOut),             /*tp_basicsize*/
122   0,                            /*tp_itemsize*/
123   /* methods */
124   (destructor)PyStdOut_dealloc, /*tp_dealloc*/
125   0,                            /*tp_print*/
126   0,                            /*tp_getattr*/
127   0,                            /*tp_setattr*/
128   0,                            /*tp_compare*/
129   0,                            /*tp_repr*/
130   0,                            /*tp_as_number*/
131   0,                            /*tp_as_sequence*/
132   0,                            /*tp_as_mapping*/
133   0,                            /*tp_hash*/
134   0,                            /*tp_call*/
135   0,                            /*tp_str*/
136   PyObject_GenericGetAttr,      /*tp_getattro*/
137   /* softspace is writable:  we must supply tp_setattro */
138   PyObject_GenericSetAttr,      /* tp_setattro */
139   0,                            /*tp_as_buffer*/
140   Py_TPFLAGS_DEFAULT,           /*tp_flags*/
141   0,                            /*tp_doc*/
142   0,                            /*tp_traverse*/
143   0,                            /*tp_clear*/
144   0,                            /*tp_richcompare*/
145   0,                            /*tp_weaklistoffset*/
146   0,                            /*tp_iter*/
147   0,                            /*tp_iternext*/
148   PyStdOut_methods,             /*tp_methods*/
149   PyStdOut_memberlist,          /*tp_members*/
150   0,                            /*tp_getset*/
151   0,                            /*tp_base*/
152   0,                            /*tp_dict*/
153   0,                            /*tp_descr_get*/
154   0,                            /*tp_descr_set*/
155   0,                            /*tp_dictoffset*/
156   0,                            /*tp_init*/
157   0,                            /*tp_alloc*/
158   0,                            /*tp_new*/
159   0,                            /*tp_free*/
160   0,                            /*tp_is_gc*/
161 };
162
163 PyObject * newPyStdOut( std::string& out )
164 {
165   PyStdOut *self;
166   self = PyObject_New(PyStdOut, &PyStdOut_Type);
167   if (self == NULL)
168     return NULL;
169   self->softspace = 0;
170   self->out=&out;
171   return (PyObject*)self;
172 }
173
174
175 ////////////////////////END PYTHON///////////////////////////
176 //=============================================================================
177 /*!
178  *   constructor:
179  */
180 //=============================================================================
181 GEOMImpl_ICurvesOperations::GEOMImpl_ICurvesOperations (GEOM_Engine* theEngine, int theDocID)
182 : GEOM_IOperations(theEngine, theDocID)
183 {
184   MESSAGE("GEOMImpl_ICurvesOperations::GEOMImpl_ICurvesOperations");
185 }
186
187 //=============================================================================
188 /*!
189  *  destructor
190  */
191 //=============================================================================
192 GEOMImpl_ICurvesOperations::~GEOMImpl_ICurvesOperations()
193 {
194   MESSAGE("GEOMImpl_ICurvesOperations::~GEOMImpl_ICurvesOperations");
195 }
196
197
198 //=============================================================================
199 /*!
200  *  MakeCircleThreePnt
201  */
202 //=============================================================================
203 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCircleThreePnt (Handle(GEOM_Object) thePnt1,
204                                                                     Handle(GEOM_Object) thePnt2,
205                                                                     Handle(GEOM_Object) thePnt3)
206 {
207   SetErrorCode(KO);
208
209   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
210
211   //Add a new Circle object
212   Handle(GEOM_Object) aCircle = GetEngine()->AddObject(GetDocID(), GEOM_CIRCLE);
213
214   //Add a new Circle function for creation a circle relatively to three points
215   Handle(GEOM_Function) aFunction =
216     aCircle->AddFunction(GEOMImpl_CircleDriver::GetID(), CIRCLE_THREE_PNT);
217   if (aFunction.IsNull()) return NULL;
218
219   //Check if the function is set correctly
220   if (aFunction->GetDriverGUID() != GEOMImpl_CircleDriver::GetID()) return NULL;
221
222   GEOMImpl_ICircle aCI (aFunction);
223
224   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
225   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
226   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
227
228   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
229
230   aCI.SetPoint1(aRefPnt1);
231   aCI.SetPoint2(aRefPnt2);
232   aCI.SetPoint3(aRefPnt3);
233
234   //Compute the Circle value
235   try {
236 #if OCC_VERSION_LARGE > 0x06010000
237     OCC_CATCH_SIGNALS;
238 #endif
239     if (!GetSolver()->ComputeFunction(aFunction)) {
240       SetErrorCode("Circle driver failed");
241       return NULL;
242     }
243   }
244   catch (Standard_Failure) {
245     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
246     SetErrorCode(aFail->GetMessageString());
247     return NULL;
248   }
249
250   //Make a Python command
251   GEOM::TPythonDump(aFunction) << aCircle << " = geompy.MakeCircleThreePnt("
252     << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << ")";
253
254   SetErrorCode(OK);
255   return aCircle;
256 }
257
258 //=============================================================================
259 /*!
260  *  MakeCircleCenter2Pnt
261  */
262 //=============================================================================
263 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCircleCenter2Pnt (Handle(GEOM_Object) thePnt1,
264                                                                       Handle(GEOM_Object) thePnt2,
265                                                                       Handle(GEOM_Object) thePnt3)
266 {
267   SetErrorCode(KO);
268
269   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
270
271   //Add a new Circle object
272   Handle(GEOM_Object) aCircle = GetEngine()->AddObject(GetDocID(), GEOM_CIRCLE);
273
274   //Add a new Circle function for creation a circle relatively to center and 2 points
275   Handle(GEOM_Function) aFunction =
276     aCircle->AddFunction(GEOMImpl_CircleDriver::GetID(), CIRCLE_CENTER_TWO_PNT);
277   if (aFunction.IsNull()) return NULL;
278
279   //Check if the function is set correctly
280   if (aFunction->GetDriverGUID() != GEOMImpl_CircleDriver::GetID()) return NULL;
281
282   GEOMImpl_ICircle aCI (aFunction);
283
284   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
285   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
286   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
287
288   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
289
290   aCI.SetPoint1(aRefPnt1);
291   aCI.SetPoint2(aRefPnt2);
292   aCI.SetPoint3(aRefPnt3);
293
294   //Compute the Circle value
295   try {
296 #if OCC_VERSION_LARGE > 0x06010000
297     OCC_CATCH_SIGNALS;
298 #endif
299     if (!GetSolver()->ComputeFunction(aFunction)) {
300       SetErrorCode("Circle driver failed");
301       return NULL;
302     }
303   }
304   catch (Standard_Failure) {
305     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
306     SetErrorCode(aFail->GetMessageString());
307     return NULL;
308   }
309
310   //Make a Python command
311   GEOM::TPythonDump(aFunction) << aCircle << " = geompy.MakeCircleCenter2Pnt("
312     << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << ")";
313
314   SetErrorCode(OK);
315   return aCircle;
316 }
317
318 //=============================================================================
319 /*!
320  *  MakeCirclePntVecR
321  */
322 //=============================================================================
323 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCirclePntVecR
324        (Handle(GEOM_Object) thePnt, Handle(GEOM_Object) theVec, double theR)
325 {
326   SetErrorCode(KO);
327
328   // Not set thePnt means origin of global CS,
329   // Not set theVec means Z axis of global CS
330   //if (thePnt.IsNull() || theVec.IsNull()) return NULL;
331
332   //Add a new Circle object
333   Handle(GEOM_Object) aCircle = GetEngine()->AddObject(GetDocID(), GEOM_CIRCLE);
334
335   //Add a new Circle function for creation a circle relatively to point and vector
336   Handle(GEOM_Function) aFunction =
337     aCircle->AddFunction(GEOMImpl_CircleDriver::GetID(), CIRCLE_PNT_VEC_R);
338   if (aFunction.IsNull()) return NULL;
339
340   //Check if the function is set correctly
341   if (aFunction->GetDriverGUID() != GEOMImpl_CircleDriver::GetID()) return NULL;
342
343   GEOMImpl_ICircle aCI (aFunction);
344
345   if (!thePnt.IsNull()) {
346     Handle(GEOM_Function) aRefPnt = thePnt->GetLastFunction();
347     if (aRefPnt.IsNull()) return NULL;
348     aCI.SetCenter(aRefPnt);
349   }
350
351   if (!theVec.IsNull()) {
352     Handle(GEOM_Function) aRefVec = theVec->GetLastFunction();
353     if (aRefVec.IsNull()) return NULL;
354     aCI.SetVector(aRefVec);
355   }
356
357   aCI.SetRadius(theR);
358
359   //Compute the Circle value
360   try {
361 #if OCC_VERSION_LARGE > 0x06010000
362     OCC_CATCH_SIGNALS;
363 #endif
364     if (!GetSolver()->ComputeFunction(aFunction)) {
365       SetErrorCode("Circle driver failed");
366       return NULL;
367     }
368   }
369   catch (Standard_Failure) {
370     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
371     SetErrorCode(aFail->GetMessageString());
372     return NULL;
373   }
374
375   //Make a Python command
376   GEOM::TPythonDump(aFunction) << aCircle << " = geompy.MakeCircle("
377     << thePnt << ", " << theVec << ", " << theR << ")";
378
379   SetErrorCode(OK);
380   return aCircle;
381 }
382
383 //=============================================================================
384 /*!
385  *  MakeEllipse
386  */
387 //=============================================================================
388 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeEllipse
389                        (Handle(GEOM_Object) thePnt, Handle(GEOM_Object) theVec,
390                         double theRMajor, double theRMinor,
391                         Handle(GEOM_Object) theVecMaj)
392 {
393   SetErrorCode(KO);
394
395   // Not set thePnt means origin of global CS,
396   // Not set theVec means Z axis of global CS
397   // Not set theVecMaj means X axis of global CS
398   //if (thePnt.IsNull() || theVec.IsNull()) return NULL;
399
400   //Add a new Ellipse object
401   Handle(GEOM_Object) anEll = GetEngine()->AddObject(GetDocID(), GEOM_ELLIPSE);
402
403   //Add a new Ellipse function
404   Handle(GEOM_Function) aFunction =
405     anEll->AddFunction(GEOMImpl_EllipseDriver::GetID(), ELLIPSE_PNT_VEC_RR);
406   if (aFunction.IsNull()) return NULL;
407
408   //Check if the function is set correctly
409   if (aFunction->GetDriverGUID() != GEOMImpl_EllipseDriver::GetID()) return NULL;
410
411   GEOMImpl_IEllipse aCI (aFunction);
412
413   if (!thePnt.IsNull()) {
414     Handle(GEOM_Function) aRefPnt = thePnt->GetLastFunction();
415     if (aRefPnt.IsNull()) return NULL;
416     aCI.SetCenter(aRefPnt);
417   }
418
419   if (!theVec.IsNull()) {
420     Handle(GEOM_Function) aRefVec = theVec->GetLastFunction();
421     if (aRefVec.IsNull()) return NULL;
422     aCI.SetVector(aRefVec);
423   }
424
425   aCI.SetRMajor(theRMajor);
426   aCI.SetRMinor(theRMinor);
427
428   if (!theVecMaj.IsNull()) {
429     Handle(GEOM_Function) aRefVecMaj = theVecMaj->GetLastFunction();
430     if (aRefVecMaj.IsNull()) return NULL;
431     aCI.SetVectorMajor(aRefVecMaj);
432   }
433
434   //Compute the Ellipse value
435   try {
436 #if OCC_VERSION_LARGE > 0x06010000
437     OCC_CATCH_SIGNALS;
438 #endif
439     if (!GetSolver()->ComputeFunction(aFunction)) {
440       SetErrorCode("Ellipse driver failed");
441       return NULL;
442     }
443   }
444   catch (Standard_Failure) {
445     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
446     SetErrorCode(aFail->GetMessageString());
447     return NULL;
448   }
449
450   //Make a Python command
451   if (!theVecMaj.IsNull()) {
452     GEOM::TPythonDump(aFunction) << anEll << " = geompy.MakeEllipse("
453                                  << thePnt << ", " << theVec << ", " << theRMajor << ", " << theRMinor
454                                  << ", " << theVecMaj << ")";
455   }
456   else {
457     GEOM::TPythonDump(aFunction) << anEll << " = geompy.MakeEllipse("
458                                  << thePnt << ", " << theVec << ", " << theRMajor << ", " << theRMinor << ")";
459   }
460
461   SetErrorCode(OK);
462   return anEll;
463 }
464
465 //=============================================================================
466 /*!
467  *  MakeArc
468  */
469 //=============================================================================
470 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeArc (Handle(GEOM_Object) thePnt1,
471                                                          Handle(GEOM_Object) thePnt2,
472                                                          Handle(GEOM_Object) thePnt3)
473 {
474   SetErrorCode(KO);
475
476   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
477
478   //Add a new Circle Arc object
479   Handle(GEOM_Object) anArc = GetEngine()->AddObject(GetDocID(), GEOM_CIRC_ARC);
480
481   //Add a new Circle Arc function
482   Handle(GEOM_Function) aFunction =
483       anArc->AddFunction(GEOMImpl_ArcDriver::GetID(), CIRC_ARC_THREE_PNT);
484
485   if (aFunction.IsNull()) return NULL;
486
487   //Check if the function is set correctly
488   if (aFunction->GetDriverGUID() != GEOMImpl_ArcDriver::GetID()) return NULL;
489   GEOMImpl_IArc aCI (aFunction);
490
491   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
492   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
493   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
494
495   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
496
497   aCI.SetPoint1(aRefPnt1);
498   aCI.SetPoint2(aRefPnt2);
499   aCI.SetPoint3(aRefPnt3);
500
501   //Compute the Arc value
502   try {
503 #if OCC_VERSION_LARGE > 0x06010000
504     OCC_CATCH_SIGNALS;
505 #endif
506     if (!GetSolver()->ComputeFunction(aFunction)) {
507       SetErrorCode("Arc driver failed");
508       return NULL;
509     }
510   }
511   catch (Standard_Failure) {
512     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
513     SetErrorCode(aFail->GetMessageString());
514     return NULL;
515   }
516
517   //Make a Python command
518   GEOM::TPythonDump(aFunction) << anArc << " = geompy.MakeArc("
519     << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << ")";
520
521   SetErrorCode(OK);
522   return anArc;
523 }
524
525 //=============================================================================
526 /*!
527  *  MakeArcCenter
528  */
529 //=============================================================================
530 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeArcCenter (Handle(GEOM_Object) thePnt1,
531                                                                Handle(GEOM_Object) thePnt2,
532                                                                Handle(GEOM_Object) thePnt3,
533                                                                bool                theSense)
534 {
535   SetErrorCode(KO);
536   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
537
538   //Add a new Circle Arc object
539   Handle(GEOM_Object) anArc = GetEngine()->AddObject(GetDocID(), GEOM_CIRC_ARC);
540
541   //Add a new Circle Arc function
542   Handle(GEOM_Function) aFunction =
543       anArc->AddFunction(GEOMImpl_ArcDriver::GetID(), CIRC_ARC_CENTER);
544   if (aFunction.IsNull()) return NULL;
545
546   //Check if the function is set correctly
547   if (aFunction->GetDriverGUID() != GEOMImpl_ArcDriver::GetID()) return NULL;
548
549   GEOMImpl_IArc aCI (aFunction);
550
551   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
552   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
553   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
554
555   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
556
557   aCI.SetPoint1(aRefPnt1);
558   aCI.SetPoint2(aRefPnt2);
559   aCI.SetPoint3(aRefPnt3);
560   aCI.SetSense(theSense);
561
562   //Compute the Arc value
563   try {
564 #if OCC_VERSION_LARGE > 0x06010000
565     OCC_CATCH_SIGNALS;
566 #endif
567     if (!GetSolver()->ComputeFunction(aFunction)) {
568   SetErrorCode("Arc driver failed");
569   return NULL;
570     }
571   }
572   catch (Standard_Failure) {
573     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
574     SetErrorCode(aFail->GetMessageString());
575     return NULL;
576   }
577   //Make a Python command
578   GEOM::TPythonDump(aFunction) << anArc << " = geompy.MakeArcCenter("
579       << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << "," << theSense << ")";
580
581   SetErrorCode(OK);
582   return anArc;
583 }
584
585 //=============================================================================
586 /*!
587  *  MakeArcOfEllipse
588  */
589 //=============================================================================
590 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeArcOfEllipse (Handle(GEOM_Object) thePnt1,
591                                                                   Handle(GEOM_Object) thePnt2,
592                                                                   Handle(GEOM_Object) thePnt3)
593 {
594   SetErrorCode(KO);
595
596   if (thePnt1.IsNull() || thePnt2.IsNull() || thePnt3.IsNull()) return NULL;
597
598   //Add a new Circle Arc object
599   Handle(GEOM_Object) anArc = GetEngine()->AddObject(GetDocID(), GEOM_ELLIPSE_ARC);
600
601   //Add a new Circle Arc function
602   Handle(GEOM_Function) aFunction =
603       anArc->AddFunction(GEOMImpl_ArcDriver::GetID(), ELLIPSE_ARC_CENTER_TWO_PNT);
604
605   if (aFunction.IsNull()) return NULL;
606
607   //Check if the function is set correctly
608   if (aFunction->GetDriverGUID() != GEOMImpl_ArcDriver::GetID()) return NULL;
609   GEOMImpl_IArc aCI (aFunction);
610
611   Handle(GEOM_Function) aRefPnt1 = thePnt1->GetLastFunction();
612   Handle(GEOM_Function) aRefPnt2 = thePnt2->GetLastFunction();
613   Handle(GEOM_Function) aRefPnt3 = thePnt3->GetLastFunction();
614
615   if (aRefPnt1.IsNull() || aRefPnt2.IsNull() || aRefPnt3.IsNull()) return NULL;
616
617   aCI.SetPoint1(aRefPnt1);
618   aCI.SetPoint2(aRefPnt2);
619   aCI.SetPoint3(aRefPnt3);
620
621   //Compute the Arc value
622   try {
623 #if OCC_VERSION_LARGE > 0x06010000
624     OCC_CATCH_SIGNALS;
625 #endif
626     if (!GetSolver()->ComputeFunction(aFunction)) {
627       SetErrorCode("Arc driver failed");
628       return NULL;
629     }
630   }
631   catch (Standard_Failure) {
632     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
633     SetErrorCode(aFail->GetMessageString());
634     return NULL;
635   }
636
637   //Make a Python command
638   GEOM::TPythonDump(aFunction) << anArc << " = geompy.MakeArcOfEllipse("
639     << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << ")";
640
641   SetErrorCode(OK);
642   return anArc;
643 }
644
645 //=============================================================================
646 /*!
647  *  MakePolyline
648  */
649 //=============================================================================
650 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakePolyline (std::list<Handle(GEOM_Object)> thePoints,
651                                                               bool theIsClosed)
652 {
653   SetErrorCode(KO);
654
655   //Add a new Polyline object
656   Handle(GEOM_Object) aPolyline = GetEngine()->AddObject(GetDocID(), GEOM_POLYLINE);
657
658   //Add a new Polyline function for creation a polyline relatively to points set
659   Handle(GEOM_Function) aFunction =
660     aPolyline->AddFunction(GEOMImpl_PolylineDriver::GetID(), POLYLINE_POINTS);
661   if (aFunction.IsNull()) return NULL;
662
663   //Check if the function is set correctly
664   if (aFunction->GetDriverGUID() != GEOMImpl_PolylineDriver::GetID()) return NULL;
665
666   GEOMImpl_IPolyline aCI (aFunction);
667
668   int aLen = thePoints.size();
669   aCI.SetLength(aLen);
670   aCI.SetConstructorType(POINT_CONSTRUCTOR);
671
672   int ind = 1;
673   std::list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
674   for (; it != thePoints.end(); it++, ind++) {
675     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
676     if (aRefPnt.IsNull()) {
677       SetErrorCode("NULL point for Polyline");
678       return NULL;
679     }
680     aCI.SetPoint(ind, aRefPnt);
681   }
682
683   aCI.SetIsClosed(theIsClosed);
684
685   //Compute the Polyline value
686   try {
687 #if OCC_VERSION_LARGE > 0x06010000
688     OCC_CATCH_SIGNALS;
689 #endif
690     if (!GetSolver()->ComputeFunction(aFunction)) {
691       SetErrorCode("Polyline driver failed");
692       return NULL;
693     }
694   }
695   catch (Standard_Failure) {
696     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
697     SetErrorCode(aFail->GetMessageString());
698     return NULL;
699   }
700
701   //Make a Python command
702   GEOM::TPythonDump pd (aFunction);
703   pd << aPolyline << " = geompy.MakePolyline([";
704
705   it = thePoints.begin();
706   pd << (*it++);
707   while (it != thePoints.end()) {
708     pd << ", " << (*it++);
709   }
710   pd << "], " << theIsClosed << ")";
711
712   SetErrorCode(OK);
713   return aPolyline;
714 }
715
716 //=============================================================================
717 /*!
718  *  MakeSplineBezier
719  */
720 //=============================================================================
721 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineBezier
722                                           (std::list<Handle(GEOM_Object)> thePoints,
723                                            bool theIsClosed)
724 {
725   SetErrorCode(KO);
726
727   //Add a new Spline object
728   Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
729
730   //Add a new Spline function for creation a bezier curve relatively to points set
731   Handle(GEOM_Function) aFunction =
732     aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_BEZIER);
733   if (aFunction.IsNull()) return NULL;
734
735   //Check if the function is set correctly
736   if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
737
738   GEOMImpl_ISpline aCI (aFunction);
739
740   aCI.SetConstructorType(POINT_CONSTRUCTOR);
741
742   Handle(TColStd_HSequenceOfTransient) aPoints = new TColStd_HSequenceOfTransient;
743   std::list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
744   for (; it != thePoints.end(); it++) {
745     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
746     if (aRefPnt.IsNull()) {
747       SetErrorCode("NULL point for Besier curve");
748       return NULL;
749     }
750     aPoints->Append(aRefPnt);
751   }
752   aCI.SetPoints(aPoints);
753
754   aCI.SetIsClosed(theIsClosed);
755
756   //Compute the Spline value
757   try {
758 #if OCC_VERSION_LARGE > 0x06010000
759     OCC_CATCH_SIGNALS;
760 #endif
761     if (!GetSolver()->ComputeFunction(aFunction)) {
762       SetErrorCode("Spline driver failed");
763       return NULL;
764     }
765   }
766   catch (Standard_Failure) {
767     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
768     SetErrorCode(aFail->GetMessageString());
769     return NULL;
770   }
771
772   //Make a Python command
773   GEOM::TPythonDump pd (aFunction);
774   pd << aSpline << " = geompy.MakeBezier([";
775
776   it = thePoints.begin();
777   pd << (*it++);
778   while (it != thePoints.end()) {
779     pd << ", " << (*it++);
780   }
781   pd << "], " << theIsClosed << ")";
782
783   SetErrorCode(OK);
784   return aSpline;
785 }
786
787 //=============================================================================
788 /*!
789  *  MakeSplineInterpolation
790  */
791 //=============================================================================
792 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineInterpolation
793                                           (std::list<Handle(GEOM_Object)> thePoints,
794                                            bool theIsClosed,
795                                            bool theDoReordering)
796 {
797   SetErrorCode(KO);
798
799   //Add a new Spline object
800   Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
801
802   //Add a new Spline function for interpolation type
803   Handle(GEOM_Function) aFunction =
804     aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOLATION);
805   if (aFunction.IsNull()) return NULL;
806
807   //Check if the function is set correctly
808   if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
809
810   GEOMImpl_ISpline aCI (aFunction);
811
812   aCI.SetConstructorType(POINT_CONSTRUCTOR);
813
814   Handle(TColStd_HSequenceOfTransient) aPoints = new TColStd_HSequenceOfTransient;
815   std::list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
816   for (; it != thePoints.end(); it++) {
817     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
818     if (aRefPnt.IsNull()) {
819       return NULL;
820     }
821     aPoints->Append(aRefPnt);
822   }
823   aCI.SetPoints(aPoints);
824
825   aCI.SetIsClosed(theIsClosed);
826   aCI.SetDoReordering(theDoReordering);
827
828   //Compute the Spline value
829   try {
830 #if OCC_VERSION_LARGE > 0x06010000
831     OCC_CATCH_SIGNALS;
832 #endif
833     if (!GetSolver()->ComputeFunction(aFunction)) {
834       SetErrorCode("Spline driver failed");
835       return NULL;
836     }
837   }
838   catch (Standard_Failure) {
839     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
840     SetErrorCode(aFail->GetMessageString());
841     return NULL;
842   }
843
844   //Make a Python command
845   GEOM::TPythonDump pd (aFunction);
846   pd << aSpline << " = geompy.MakeInterpol([";
847
848   it = thePoints.begin();
849   pd << (*it++);
850   while (it != thePoints.end()) {
851     pd << ", " << (*it++);
852   }
853   pd << "], " << theIsClosed << ", " << theDoReordering << ")";
854
855   SetErrorCode(OK);
856   return aSpline;
857 }
858
859
860 //=============================================================================
861 /*!
862  *  MakeSplineInterpolWithTangents
863  */
864 //=============================================================================
865 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSplineInterpolWithTangents
866                                           (std::list<Handle(GEOM_Object)> thePoints,
867                                            Handle(GEOM_Object) theFirstVec,
868                                            Handle(GEOM_Object) theLastVec)
869 {
870   SetErrorCode(KO);
871
872   //Add a new Spline object
873   Handle(GEOM_Object) aSpline = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
874
875   //Add a new Spline function for interpolation type
876   Handle(GEOM_Function) aFunction =
877     aSpline->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOL_TANGENTS);
878   if (aFunction.IsNull()) return NULL;
879
880   //Check if the function is set correctly
881   if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
882
883   GEOMImpl_ISpline aCI (aFunction);
884
885   aCI.SetConstructorType(POINT_CONSTRUCTOR);
886
887   Handle(TColStd_HSequenceOfTransient) aPoints = new TColStd_HSequenceOfTransient;
888   std::list<Handle(GEOM_Object)>::iterator it = thePoints.begin();
889   for (; it != thePoints.end(); it++) {
890     Handle(GEOM_Function) aRefPnt = (*it)->GetLastFunction();
891     if (aRefPnt.IsNull()) {
892       SetErrorCode("NULL point for Interpolation");
893       return NULL;
894     }
895     aPoints->Append(aRefPnt);
896   }
897   aCI.SetPoints(aPoints);
898
899   Handle(GEOM_Function) aVec1 = theFirstVec->GetLastFunction();
900   Handle(GEOM_Function) aVec2 = theLastVec->GetLastFunction();
901
902   if (aVec1.IsNull() || aVec2.IsNull()) return NULL;
903
904   aCI.SetFirstVector(aVec1);
905   aCI.SetLastVector(aVec2);
906
907   //Compute the Spline value
908   try {
909 #if OCC_VERSION_LARGE > 0x06010000
910     OCC_CATCH_SIGNALS;
911 #endif
912     if (!GetSolver()->ComputeFunction(aFunction)) {
913       SetErrorCode("Spline driver failed");
914       return NULL;
915     }
916   }
917   catch (Standard_Failure) {
918     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
919     SetErrorCode(aFail->GetMessageString());
920     return NULL;
921   }
922
923   //Make a Python command
924   GEOM::TPythonDump pd (aFunction);
925   pd << aSpline << " = geompy.MakeInterpolWithTangents([";
926
927   it = thePoints.begin();
928   pd << (*it++);
929   while (it != thePoints.end()) {
930     pd << ", " << (*it++);
931   }
932   pd << "], " << theFirstVec << ", " << theLastVec << ")";
933
934   SetErrorCode(OK);
935   return aSpline;
936 }
937
938 //=============================================================================
939 /*!
940  *  MakeCurveParametric
941  */
942 //=============================================================================
943 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeCurveParametric
944              (const char* thexExpr, const char* theyExpr, const char* thezExpr,
945               double theParamMin, double theParamMax, double theParamStep,
946               CurveType theCurveType,
947               int theParamNbStep, bool theNewMethod)
948 {
949   TCollection_AsciiString aPyScript;
950   aPyScript +="from math import *                                          \n";
951   aPyScript +="def X(t):                                                   \n";
952   aPyScript +="    return ";
953   aPyScript += thexExpr;
954   aPyScript += "\n";
955   aPyScript +="def Y(t):                                                   \n";
956   aPyScript +="    return ";
957   aPyScript += theyExpr;
958   aPyScript += "\n";
959
960   aPyScript +="def Z(t):                                                   \n";
961   aPyScript +="    return ";
962   aPyScript += thezExpr;
963   aPyScript += "\n";
964
965   if (theNewMethod)
966   {
967     aPyScript +="def coordCalculator(tmin, tmax, nstep):                     \n";
968     aPyScript +="   coords = []                                              \n";
969     aPyScript +="   tstep  = (tmax - tmin) / nstep                           \n";
970     aPyScript +="   n = 0                                                    \n";
971     aPyScript +="   while n <= nstep :                                       \n";
972     aPyScript +="      t = tmin + n*tstep                                    \n";
973     aPyScript +="      coords.append([X(t), Y(t), Z(t)])                     \n";
974     aPyScript +="      n = n+1                                               \n";
975     aPyScript +="   return coords                                            \n";
976   }
977   else
978   {
979     aPyScript +="def coordCalculator(tmin, tmax, tstep):                      \n";
980     aPyScript +="   coords = []                                              \n";
981     aPyScript +="   while tmin <= tmax :                                     \n";
982     aPyScript +="      coords.append([X(tmin), Y(tmin), Z(tmin)])            \n";
983     aPyScript +="      tmin = tmin + tstep                                   \n";
984     aPyScript +="   return coords                                            \n";
985   }
986
987   SetErrorCode(KO);
988
989   if(theParamMin >= theParamMax) {
990     SetErrorCode("The minimum value of the parameter must be less than maximum value !!!");
991     return NULL;
992   }
993
994   if(!theNewMethod && theParamStep <= 0.0) {
995     SetErrorCode("Value of the step must be positive !!!");
996     return NULL;
997   }
998   else if(theNewMethod && theParamNbStep < 0) {
999     SetErrorCode("The number of steps must be positive !!!");
1000     return NULL;
1001   }
1002
1003   /* Initialize the Python interpreter */
1004   if (! Py_IsInitialized()) {
1005     SetErrorCode("Python interpreter is not initialized !!! ");
1006     return NULL;
1007   }
1008
1009   PyGILState_STATE gstate;
1010   gstate = PyGILState_Ensure();
1011
1012   PyObject* main_mod = PyImport_AddModule("__main__");
1013   PyObject* main_dict = PyModule_GetDict(main_mod);
1014
1015   PyObject* obj = PyRun_String(aPyScript.ToCString(), Py_file_input, main_dict, NULL);
1016
1017   if (obj == NULL) {
1018     SetErrorCode("Error during executing of python script !!!");
1019     PyErr_Print();
1020     PyGILState_Release(gstate);
1021     return NULL;
1022   } else {
1023     Py_DECREF(obj);
1024   }
1025
1026   PyObject * func = NULL;
1027   func = PyObject_GetAttrString(main_mod, "coordCalculator");
1028
1029   if (func == NULL){
1030     SetErrorCode("Can't get function from python module !!!");
1031     PyGILState_Release(gstate);
1032     return NULL;
1033   }
1034
1035   PyObject* coords;
1036   if (theNewMethod)
1037     coords = PyObject_CallFunction(func,(char*)"(d, d, i)", theParamMin, theParamMax, theParamNbStep );
1038   else
1039     coords = PyObject_CallFunction(func,(char*)"(d, d, d)", theParamMin, theParamMax, theParamStep );
1040
1041   PyObject* new_stderr = NULL;
1042
1043   if (coords == NULL){
1044     fflush(stderr);
1045     std::string err_description="";
1046     new_stderr = newPyStdOut(err_description);
1047     PySys_SetObject((char*)"stderr", new_stderr);
1048     PyErr_Print();
1049     PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
1050     Py_DECREF(new_stderr);
1051     MESSAGE("Can't evaluate coordCalculator()" << " error is " << err_description);
1052     SetErrorCode("Can't evaluate the expressions, please check them !!!");
1053     PyGILState_Release(gstate);
1054     return NULL;
1055   }
1056
1057   int lsize = PyList_Size( coords );
1058
1059   if(lsize <= 0) {
1060     SetErrorCode("Empty list of the points, please check input parameters !!!");
1061     return NULL;
1062   }
1063
1064   Handle(TColStd_HArray1OfReal) aCoordsArray = new TColStd_HArray1OfReal (1, lsize * 3);
1065
1066   int k=1;
1067   for ( Py_ssize_t i = 0; i < lsize; ++i ) {
1068     PyObject* coord = PyList_GetItem( coords, i );
1069     if (coord != NULL) {
1070       for ( Py_ssize_t j = 0; j < PyList_Size(coord); ++j) {
1071         PyObject* item = PyList_GetItem(coord, j);
1072         aCoordsArray->SetValue(k, PyFloat_AsDouble(item));
1073         k++;
1074       }
1075     }
1076   }
1077
1078   Py_DECREF(coords);
1079
1080   PyGILState_Release(gstate);
1081
1082   Handle(GEOM_Object) aCurve;
1083   Handle(GEOM_Function) aFunction;
1084   TCollection_AsciiString aCurveType;
1085
1086   switch(theCurveType) {
1087   case Polyline: {
1088     //Add a new Polyline object
1089     aCurve = GetEngine()->AddObject(GetDocID(), GEOM_POLYLINE);
1090
1091     //Add a new Polyline function for creation a polyline relatively to points set
1092     aFunction = aCurve->AddFunction(GEOMImpl_PolylineDriver::GetID(), POLYLINE_POINTS);
1093     if (aFunction.IsNull()) return NULL;
1094
1095     //Check if the function is set correctly
1096     if (aFunction->GetDriverGUID() != GEOMImpl_PolylineDriver::GetID()) return NULL;
1097
1098     GEOMImpl_IPolyline aCI (aFunction);
1099
1100     aCI.SetLength(lsize);
1101     aCI.SetConstructorType(COORD_CONSTRUCTOR);
1102     aCI.SetIsClosed(false);
1103     aCI.SetCoordinates(aCoordsArray);
1104     aCurveType = "GEOM.Polyline";
1105     break;
1106   }
1107   case Bezier: {
1108     //Add a new Spline object
1109     aCurve = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
1110     //Add a new Spline function for creation a bezier curve relatively to points set
1111     aFunction =
1112       aCurve->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_BEZIER);
1113     if (aFunction.IsNull()) return NULL;
1114
1115     //Check if the function is set correctly
1116     if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
1117
1118     GEOMImpl_ISpline aCI (aFunction);
1119
1120     aCI.SetConstructorType(COORD_CONSTRUCTOR);
1121     aCI.SetIsClosed(false);
1122     aCI.SetCoordinates(aCoordsArray);
1123     aCurveType = "GEOM.Bezier";
1124     break;
1125   }
1126   case Interpolation: {
1127     //Add a new Spline object
1128     aCurve = GetEngine()->AddObject(GetDocID(), GEOM_SPLINE);
1129
1130     //Add a new Spline function for creation a bezier curve relatively to points set
1131     aFunction = aCurve->AddFunction(GEOMImpl_SplineDriver::GetID(), SPLINE_INTERPOLATION);
1132     if (aFunction.IsNull()) return NULL;
1133
1134     //Check if the function is set correctly
1135     if (aFunction->GetDriverGUID() != GEOMImpl_SplineDriver::GetID()) return NULL;
1136
1137     GEOMImpl_ISpline aCI (aFunction);
1138     aCI.SetConstructorType(COORD_CONSTRUCTOR);
1139     aCI.SetIsClosed(false);
1140     aCI.SetDoReordering(false);
1141     aCI.SetCoordinates(aCoordsArray);
1142     aCurveType = "GEOM.Interpolation";
1143     break;
1144   }
1145   }
1146
1147   GEOMImpl_ICurveParametric aIP(aFunction);
1148   aIP.SetExprX      (thexExpr);
1149   aIP.SetExprY      (theyExpr);
1150   aIP.SetExprZ      (thezExpr);
1151   aIP.SetParamMin   (theParamMin);
1152   aIP.SetParamMax   (theParamMax);
1153   if ( theNewMethod )
1154     aIP.SetParamNbStep(theParamNbStep);
1155   else
1156     aIP.SetParamStep  (theParamStep);
1157
1158   //Compute the Curve value
1159   try {
1160 #if OCC_VERSION_LARGE > 0x06010000
1161     OCC_CATCH_SIGNALS;
1162 #endif
1163     if (!GetSolver()->ComputeFunction(aFunction)) {
1164       SetErrorCode("Curve driver failed !!!");
1165       return NULL;
1166     }
1167   }
1168   catch (Standard_Failure) {
1169     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1170     SetErrorCode(aFail->GetMessageString());
1171     return NULL;
1172   }
1173
1174   //Make a Python command
1175   GEOM::TPythonDump pd (aFunction);
1176   pd << aCurve << " = geompy.MakeCurveParametric(";
1177   pd << "\"" << thexExpr << "\", ";
1178   pd << "\"" << theyExpr << "\", ";
1179   pd << "\"" << thezExpr << "\", ";
1180
1181   pd << theParamMin <<", ";
1182   pd << theParamMax <<", ";
1183   if (theNewMethod)
1184     pd << theParamNbStep <<", ";
1185   else
1186     pd << theParamStep <<", ";
1187   pd << aCurveType.ToCString() <<", ";
1188   pd << theNewMethod <<")";
1189
1190   SetErrorCode(OK);
1191   return aCurve;
1192 }
1193
1194 //=============================================================================
1195 /*!
1196  *  MakeSketcher
1197  */
1198 //=============================================================================
1199 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSketcher (const char* theCommand,
1200                                                               std::list<double> theWorkingPlane)
1201 {
1202   SetErrorCode(KO);
1203
1204   if (!theCommand || strcmp(theCommand, "") == 0) return NULL;
1205
1206   //Add a new Sketcher object
1207   Handle(GEOM_Object) aSketcher = GetEngine()->AddObject(GetDocID(), GEOM_SKETCHER);
1208
1209   //Add a new Sketcher function
1210   Handle(GEOM_Function) aFunction =
1211     aSketcher->AddFunction(GEOMImpl_SketcherDriver::GetID(), SKETCHER_NINE_DOUBLS);
1212   if (aFunction.IsNull()) return NULL;
1213
1214   //Check if the function is set correctly
1215   if (aFunction->GetDriverGUID() != GEOMImpl_SketcherDriver::GetID()) return NULL;
1216
1217   GEOMImpl_ISketcher aCI (aFunction);
1218
1219   TCollection_AsciiString aCommand((char*) theCommand);
1220   aCI.SetCommand(aCommand);
1221
1222   int ind = 1;
1223   std::list<double>::iterator it = theWorkingPlane.begin();
1224   for (; it != theWorkingPlane.end(); it++, ind++)
1225     aCI.SetWorkingPlane(ind, *it);
1226
1227   //Compute the Sketcher value
1228   try {
1229 #if OCC_VERSION_LARGE > 0x06010000
1230     OCC_CATCH_SIGNALS;
1231 #endif
1232     if (!GetSolver()->ComputeFunction(aFunction)) {
1233       SetErrorCode("Sketcher driver failed");
1234       return NULL;
1235     }
1236   }
1237   catch (Standard_Failure) {
1238     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1239     SetErrorCode(aFail->GetMessageString());
1240     return NULL;
1241   }
1242
1243   //Make a Python command
1244   GEOM::TPythonDump pd (aFunction);
1245   pd << aSketcher << " = geompy.MakeSketcher(\"" << aCommand.ToCString() << "\", [";
1246
1247   it = theWorkingPlane.begin();
1248   pd << (*it++);
1249   while (it != theWorkingPlane.end()) {
1250     pd << ", " << (*it++);
1251   }
1252   pd << "])";
1253
1254   SetErrorCode(OK);
1255   return aSketcher;
1256 }
1257
1258 //=============================================================================
1259 /*!
1260  *  MakeSketcherOnPlane
1261  */
1262 //=============================================================================
1263 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::MakeSketcherOnPlane
1264                                (const char* theCommand,
1265                                 Handle(GEOM_Object)            theWorkingPlane)
1266 {
1267   SetErrorCode(KO);
1268
1269   if (!theCommand || strcmp(theCommand, "") == 0) return NULL;
1270
1271   //Add a new Sketcher object
1272   Handle(GEOM_Object) aSketcher = GetEngine()->AddObject(GetDocID(), GEOM_SKETCHER);
1273
1274   //Add a new Sketcher function
1275   Handle(GEOM_Function) aFunction =
1276     aSketcher->AddFunction(GEOMImpl_SketcherDriver::GetID(), SKETCHER_PLANE);
1277   if (aFunction.IsNull()) return NULL;
1278
1279   //Check if the function is set correctly
1280   if (aFunction->GetDriverGUID() != GEOMImpl_SketcherDriver::GetID()) return NULL;
1281
1282   GEOMImpl_ISketcher aCI (aFunction);
1283
1284   TCollection_AsciiString aCommand((char*) theCommand);
1285   aCI.SetCommand(aCommand);
1286
1287   Handle(GEOM_Function) aRefPlane = theWorkingPlane->GetLastFunction();
1288   if (aRefPlane.IsNull()) return NULL;
1289   aCI.SetWorkingPlane( aRefPlane );
1290
1291   //Compute the Sketcher value
1292   try {
1293 #if OCC_VERSION_LARGE > 0x06010000
1294     OCC_CATCH_SIGNALS;
1295 #endif
1296     if (!GetSolver()->ComputeFunction(aFunction)) {
1297       SetErrorCode("Sketcher driver failed");
1298       return NULL;
1299     }
1300   }
1301   catch (Standard_Failure) {
1302     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1303     SetErrorCode(aFail->GetMessageString());
1304     return NULL;
1305   }
1306
1307   //Make a Python command
1308   GEOM::TPythonDump (aFunction) << aSketcher << " = geompy.MakeSketcherOnPlane(\""
1309     << aCommand.ToCString() << "\", " << theWorkingPlane << " )";
1310
1311   SetErrorCode(OK);
1312   return aSketcher;
1313 }
1314
1315 //=============================================================================
1316 /*!
1317  *  Make3DSketcherCommand
1318  */
1319 //=============================================================================
1320 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::Make3DSketcherCommand (const char* theCommand)
1321 {
1322   SetErrorCode(KO);
1323
1324   if (!theCommand || strcmp(theCommand, "") == 0) return NULL;
1325
1326   //Add a new Sketcher object
1327   Handle(GEOM_Object) aSketcher = GetEngine()->AddObject(GetDocID(), GEOM_3DSKETCHER);
1328
1329   //Add a new Sketcher function
1330   Handle(GEOM_Function) aFunction =
1331     aSketcher->AddFunction(GEOMImpl_3DSketcherDriver::GetID(), SKETCHER3D_COMMAND);
1332   if (aFunction.IsNull()) return NULL;
1333
1334   //Check if the function is set correctly
1335   if (aFunction->GetDriverGUID() != GEOMImpl_3DSketcherDriver::GetID()) return NULL;
1336
1337   GEOMImpl_I3DSketcher aCI (aFunction);
1338
1339   TCollection_AsciiString aCommand ((char*) theCommand);
1340   aCI.SetCommand(aCommand);
1341
1342   //Compute the 3D Sketcher value
1343   try {
1344 #if OCC_VERSION_LARGE > 0x06010000
1345     OCC_CATCH_SIGNALS;
1346 #endif
1347     if (!GetSolver()->ComputeFunction(aFunction)) {
1348       SetErrorCode("3D Sketcher driver failed");
1349       return NULL;
1350     }
1351   }
1352   catch (Standard_Failure) {
1353     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1354     SetErrorCode(aFail->GetMessageString());
1355     return NULL;
1356   }
1357
1358   //Make a Python command
1359   GEOM::TPythonDump pd (aFunction);
1360   pd << aSketcher << " = geompy.Make3DSketcherCommand(\"" << aCommand.ToCString() << "\")";
1361
1362   SetErrorCode(OK);
1363   return aSketcher;
1364 }
1365
1366 //=============================================================================
1367 /*!
1368  *  Make3DSketcher
1369  */
1370 //=============================================================================
1371 Handle(GEOM_Object) GEOMImpl_ICurvesOperations::Make3DSketcher (std::list<double> theCoordinates)
1372 {
1373   SetErrorCode(KO);
1374
1375   //Add a new Sketcher object
1376   Handle(GEOM_Object) a3DSketcher = GetEngine()->AddObject(GetDocID(), GEOM_3DSKETCHER);
1377
1378   //Add a new Sketcher function
1379   Handle(GEOM_Function) aFunction =
1380     a3DSketcher->AddFunction(GEOMImpl_3DSketcherDriver::GetID(), SKETCHER3D_COORDS);
1381   if (aFunction.IsNull()) return NULL;
1382
1383   //Check if the function is set correctly
1384   if (aFunction->GetDriverGUID() != GEOMImpl_3DSketcherDriver::GetID()) return NULL;
1385
1386   GEOMImpl_I3DSketcher aCI (aFunction);
1387
1388   int nbOfCoords = 0;
1389   std::list<double>::iterator it = theCoordinates.begin();
1390   for (; it != theCoordinates.end(); it++)
1391     nbOfCoords++;
1392
1393   Handle(TColStd_HArray1OfReal) aCoordsArray = new TColStd_HArray1OfReal (1, nbOfCoords);
1394
1395   it = theCoordinates.begin();
1396   int ind = 1;
1397   for (; it != theCoordinates.end(); it++, ind++)
1398     aCoordsArray->SetValue(ind, *it);
1399
1400   aCI.SetCoordinates(aCoordsArray);
1401
1402   //Compute the Sketcher value
1403   try {
1404 #if OCC_VERSION_LARGE > 0x06010000
1405     OCC_CATCH_SIGNALS;
1406 #endif
1407     if (!GetSolver()->ComputeFunction(aFunction)) {
1408       SetErrorCode("3D Sketcher driver failed");
1409       return NULL;
1410     }
1411   }
1412   catch (Standard_Failure) {
1413     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1414     SetErrorCode(aFail->GetMessageString());
1415     return NULL;
1416   }
1417
1418   //Make a Python command
1419   GEOM::TPythonDump pd (aFunction);
1420   pd << a3DSketcher << " = geompy.Make3DSketcher([";
1421
1422   it = theCoordinates.begin();
1423   pd << (*it++);
1424   while (it != theCoordinates.end()) {
1425     pd << ", " << (*it++);
1426   }
1427   pd << "])";
1428
1429   SetErrorCode(OK);
1430   return a3DSketcher;
1431 }