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