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