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