Salome HOME
updated copyright message
[modules/kernel.git] / src / DSC / DSC_Python / calcium.i
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 %define DOCSTRING
21 "CALCIUM python wrapping : Superv_Component class
22 "
23 %enddef
24
25 %module(docstring=DOCSTRING) calcium
26
27 %feature("autodoc", "1");
28
29 %include cstring.i
30
31 %{
32 //C++ Includes 
33 #include <SALOMEconfig.h>
34 #include <Calcium.hxx>
35 #include <calcium.h>
36 #include <Superv_Component_i.hxx>
37 #include <Salome_file_i.hxx>
38 #include <omniORB4/CORBA.h>
39
40 #include <Python.h>
41 #include <omniORBpy.h>
42 omniORBpyAPI* api=0;
43 PyObject* dsc ;
44
45 %}
46
47 %init
48 %{
49   // init section
50 #ifdef WITH_NUMPY
51   import_array()
52 #endif
53
54   PyObject* omnipy = PyImport_ImportModule((char*)"_omnipy");
55   if (!omnipy)
56   {
57     PyErr_SetString(PyExc_ImportError,
58         (char*)"Cannot import _omnipy");
59     return NULL;
60   }
61   PyObject* pyapi = PyObject_GetAttrString(omnipy, (char*)"API");
62   api = (omniORBpyAPI*)PyCapsule_GetPointer(pyapi,"_omnipy.API");
63   Py_DECREF(pyapi);
64
65   PyObject* engines = PyImport_ImportModule("Engines");
66   dsc = PyObject_GetAttrString(engines, "DSC");
67 %}
68
69 %include <exception.i>
70
71
72
73 /*
74  * Most of this code is borrowed from numpy distribution
75  * The following code originally appeared in enthought/kiva/agg/src/numeric.i,
76  * author unknown.  It was translated from C++ to C by John Hunter.  Bill
77  * Spotz has modified it slightly to fix some minor bugs, add some comments
78  * and some functionality.
79  */
80
81 %{
82
83 #ifdef WITH_NUMPY
84 /* With Numpy */
85 #ifdef HAVE_ISINF
86 #undef HAVE_ISINF
87 #endif
88 #include <numpy/arrayobject.h>
89
90 typedef PyArrayObject ArrayObject;
91
92 /* Macros to extract array attributes.
93  */
94 #define is_array(a)            ((a) && PyArray_Check((PyArrayObject *)a))
95 #define array_type(a)          (int)(PyArray_TYPE(a))
96 #define array_dimensions(a)    (((PyArrayObject *)a)->nd)
97 #define array_size(a,i)        (((PyArrayObject *)a)->dimensions[i])
98 #define array_is_contiguous(a) (PyArray_ISCONTIGUOUS(a))
99
100 const char* pytype_string(PyObject*);
101 const char* typecode_string(int);
102 int type_match(int, int);
103 int require_size(PyArrayObject*, int*, int);
104 int require_dimensions_n(PyArrayObject*, int*, int);
105 int require_dimensions(PyArrayObject*, int);
106 int require_contiguous(PyArrayObject*);
107 PyArrayObject* make_contiguous(PyArrayObject*, int*, int, int);
108 PyArrayObject* obj_to_array_no_conversion(PyObject*, int);
109 PyArrayObject* obj_to_array_allow_conversion(PyObject*, int, int*);
110 PyArrayObject* obj_to_array_contiguous_allow_conversion(PyObject*, int, int*);
111 PyArrayObject* obj_to_array_contiguous_allow_conversion(PyObject*, int, int*);
112
113 /* Given a PyObject, return a string describing its type.
114  */
115 const char* pytype_string(PyObject* py_obj) {
116   if (py_obj == NULL          ) return "C NULL value";
117   if (PyCallable_Check(py_obj)) return "callable"    ;
118   if (PyString_Check(  py_obj)) return "string"      ;
119   if (PyInt_Check(     py_obj)) return "int"         ;
120   if (PyFloat_Check(   py_obj)) return "float"       ;
121   if (PyDict_Check(    py_obj)) return "dict"        ;
122   if (PyList_Check(    py_obj)) return "list"        ;
123   if (PyTuple_Check(   py_obj)) return "tuple"       ;
124   if (PyModule_Check(  py_obj)) return "module"      ;
125 #if PY_MAJOR_VERSION < 3
126   if (PyFile_Check(    py_obj)) return "file"        ;
127   if (PyInstance_Check(py_obj)) return "instance"    ;
128 #endif
129
130   return "unknown type";
131 }
132
133 /*
134 For documentation only : numpy typecodes
135
136 enum NPY_TYPECHAR { NPY_BOOLLTR = '?',
137                         NPY_BYTELTR = 'b',
138                         NPY_UBYTELTR = 'B',
139                         NPY_SHORTLTR = 'h',
140                         NPY_USHORTLTR = 'H',
141                         NPY_INTLTR = 'i',
142                         NPY_UINTLTR = 'I',
143                         NPY_LONGLTR = 'l',
144                         NPY_ULONGLTR = 'L',
145                         NPY_LONGLONGLTR = 'q',
146                         NPY_ULONGLONGLTR = 'Q',
147                         NPY_FLOATLTR = 'f',
148                         NPY_DOUBLELTR = 'd',
149                         NPY_LONGDOUBLELTR = 'g',
150                         NPY_CFLOATLTR = 'F',
151                         NPY_CDOUBLELTR = 'D',
152                         NPY_CLONGDOUBLELTR = 'G',
153                         NPY_OBJECTLTR = 'O',
154                         NPY_STRINGLTR = 'S',
155                         NPY_STRINGLTR2 = 'a',
156                         NPY_UNICODELTR = 'U',
157                         NPY_VOIDLTR = 'V',
158                         NPY_CHARLTR = 'c',
159
160                         NPY_INTPLTR = 'p',
161                         NPY_UINTPLTR = 'P',
162
163                         NPY_GENBOOLLTR ='b',
164                         NPY_SIGNEDLTR = 'i',
165                         NPY_UNSIGNEDLTR = 'u',
166                         NPY_FLOATINGLTR = 'f',
167                         NPY_COMPLEXLTR = 'c'
168 };
169 */
170
171 /* Given a Numeric typecode, return a string describing the type.
172  */
173 const char* typecode_string(int typecode) {
174   const char* type_names[] = {"bool","byte","unsigned byte","short",
175         "unsigned short","int","unsigned int","long","unsigned long",
176         "longlong","unsigned longlong",
177         "float","double","long double","complex float","complex double","complex long double",
178         "object","string","unicode","void","ntypes","notype","char","unknown"};
179   return type_names[typecode];
180 }
181
182 /* Make sure input has correct numeric type.  Allow character and byte
183  * to match.  Also allow int and long to match.
184  */
185 int type_match(int actual_type, int desired_type) {
186   return PyArray_EquivTypenums(actual_type, desired_type);
187 }
188
189 /* Given a PyObject pointer, cast it to a PyArrayObject pointer if
190  * legal.  If not, set the python error string appropriately and
191  * return NULL./
192  */
193 PyArrayObject* obj_to_array_no_conversion(PyObject* input, int typecode) {
194   PyArrayObject* ary = NULL;
195   if (is_array(input) && (typecode == PyArray_NOTYPE ||
196         PyArray_EquivTypenums(array_type(input),
197             typecode))) {
198         ary = (PyArrayObject*) input;
199     }
200     else if is_array(input) {
201       const char* desired_type = typecode_string(typecode);
202       const char* actual_type = typecode_string(array_type(input));
203       PyErr_Format(PyExc_TypeError,
204        "Array of type '%s' required.  Array of type '%s' given",
205        desired_type, actual_type);
206       ary = NULL;
207     }
208     else {
209       const char * desired_type = typecode_string(typecode);
210       const char * actual_type = pytype_string(input);
211       PyErr_Format(PyExc_TypeError,
212        "Array of type '%s' required.  A %s was given",
213        desired_type, actual_type);
214       ary = NULL;
215     }
216   return ary;
217 }
218
219 /* Convert the given PyObject to a Numeric array with the given
220  * typecode.  On Success, return a valid PyArrayObject* with the
221  * correct type.  On failure, the python error string will be set and
222  * the routine returns NULL.
223  */
224 PyArrayObject* obj_to_array_allow_conversion(PyObject* input, int typecode,
225                                              int* is_new_object)
226 {
227   PyArrayObject* ary = NULL;
228   PyObject* py_obj;
229   if (is_array(input) && (typecode == PyArray_NOTYPE || type_match(array_type(input),typecode))) {
230     ary = (PyArrayObject*) input;
231     *is_new_object = 0;
232   }
233   else {
234     py_obj = PyArray_FromObject(input, typecode, 0, 0);
235     /* If NULL, PyArray_FromObject will have set python error value.*/
236     ary = (PyArrayObject*) py_obj;
237     *is_new_object = 1;
238   }
239   return ary;
240 }
241
242 /* Given a PyArrayObject, check to see if it is contiguous.  If so,
243  * return the input pointer and flag it as not a new object.  If it is
244  * not contiguous, create a new PyArrayObject using the original data,
245  * flag it as a new object and return the pointer.
246  */
247 PyArrayObject* make_contiguous(PyArrayObject* ary, int* is_new_object,
248                                int min_dims, int max_dims)
249 {
250   PyArrayObject* result;
251   if (array_is_contiguous(ary)) {
252     result = ary;
253     *is_new_object = 0;
254   }
255   else {
256     result = (PyArrayObject*) PyArray_ContiguousFromObject((PyObject*)ary,
257                  array_type(ary),
258                  min_dims,
259                  max_dims);
260     *is_new_object = 1;
261   }
262   return result;
263 }
264
265 /* Convert a given PyObject to a contiguous PyArrayObject of the
266  * specified type.  If the input object is not a contiguous
267  * PyArrayObject, a new one will be created and the new object flag
268  * will be set.
269  */
270 PyArrayObject* obj_to_array_contiguous_allow_conversion(PyObject* input,
271                                                         int typecode,
272                                                         int* is_new_object) {
273   int is_new1 = 0;
274   int is_new2 = 0;
275   PyArrayObject* ary2;
276   PyArrayObject* ary1 = obj_to_array_allow_conversion(input, typecode,
277                   &is_new1);
278   if (ary1) {
279     ary2 = make_contiguous(ary1, &is_new2, 0, 0);
280     if ( is_new1 && is_new2) {
281       Py_DECREF(ary1);
282     }
283     ary1 = ary2;
284   }
285   *is_new_object = is_new1 || is_new2;
286   return ary1;
287 }
288
289 /* Test whether a python object is contiguous.  If array is
290  * contiguous, return 1.  Otherwise, set the python error string and
291  * return 0.
292  */
293 int require_contiguous(PyArrayObject* ary) {
294   int contiguous = 1;
295   if (!array_is_contiguous(ary)) {
296     PyErr_SetString(PyExc_TypeError, "Array must be contiguous.  A discontiguous array was given");
297     contiguous = 0;
298   }
299   return contiguous;
300 }
301
302 /* Require the given PyArrayObject to have a specified number of
303  * dimensions.  If the array has the specified number of dimensions,
304  * return 1.  Otherwise, set the python error string and return 0.
305  */
306 int require_dimensions(PyArrayObject* ary, int exact_dimensions) {
307   int success = 1;
308   if (array_dimensions(ary) != exact_dimensions) {
309     PyErr_Format(PyExc_TypeError,
310      "Array must have %d dimensions.  Given array has %d dimensions",
311      exact_dimensions, array_dimensions(ary));
312     success = 0;
313   }
314   return success;
315 }
316
317 /* Require the given PyArrayObject to have one of a list of specified
318  * number of dimensions.  If the array has one of the specified number
319  * of dimensions, return 1.  Otherwise, set the python error string
320  * and return 0.
321  */
322 int require_dimensions_n(PyArrayObject* ary, int* exact_dimensions, int n) {
323   int success = 0;
324   int i;
325   char dims_str[255] = "";
326   char s[255];
327   for (i = 0; i < n && !success; i++) {
328     if (array_dimensions(ary) == exact_dimensions[i]) {
329       success = 1;
330     }
331   }
332   if (!success) {
333     for (i = 0; i < n-1; i++) {
334       sprintf(s, "%d, ", exact_dimensions[i]);
335       strcat(dims_str,s);
336     }
337     sprintf(s, " or %d", exact_dimensions[n-1]);
338     strcat(dims_str,s);
339     PyErr_Format(PyExc_TypeError,
340      "Array must have %s dimensions.  Given array has %d dimensions",
341      dims_str, array_dimensions(ary));
342   }
343   return success;
344 }
345
346 /* Require the given PyArrayObject to have a specified shape.  If the
347  * array has the specified shape, return 1.  Otherwise, set the python
348  * error string and return 0.
349  */
350 int require_size(PyArrayObject* ary, int* size, int n) {
351   int i;
352   int success = 1;
353   int len;
354   char desired_dims[255] = "[";
355   char s[255];
356   char actual_dims[255] = "[";
357   for(i=0; i < n;i++) {
358     if (size[i] != -1 &&  size[i] != array_size(ary,i)) {
359       success = 0;
360     }
361   }
362   if (!success) {
363     for (i = 0; i < n; i++) {
364       if (size[i] == -1) {
365         sprintf(s, "*,");
366       }
367       else
368       {
369         sprintf(s, "%d,", size[i]);
370       }
371       strcat(desired_dims,s);
372     }
373     len = strlen(desired_dims);
374     desired_dims[len-1] = ']';
375     for (i = 0; i < n; i++) {
376       sprintf(s, "%d,", array_size(ary,i));
377       strcat(actual_dims,s);
378     }
379     len = strlen(actual_dims);
380     actual_dims[len-1] = ']';
381     PyErr_Format(PyExc_TypeError,
382      "Array must have shape of %s.  Given array has shape of %s",
383      desired_dims, actual_dims);
384   }
385   return success;
386 }
387
388 #else
389 /* Without Numpy */
390 typedef PyObject ArrayObject;
391
392 #endif
393 %}
394
395 %include "carrays.i" 
396
397 %array_class(int,    intArray);
398 %array_class(long,   longArray);
399 %array_class(float,  floatArray);
400 %array_class(double, doubleArray);
401
402 /* special struct to handle string arrays */
403 %inline %{
404 struct stringArray
405 {
406   stringArray(int nelements,int size=0) {
407     nelem=nelements;
408     size=size;
409     data= new char*[nelements];
410     for(int i=0;i<nelements;i++)
411     {
412       data[i]=(char *)malloc((size+1)*sizeof(char));
413       data[i][size+1]='\0';
414     }
415   }
416   ~stringArray() 
417   {
418     for(int i=0;i<nelem;i++)
419       free(data[i]);
420     delete [] data;
421   }
422   char* __getitem__(int index) {
423     return data[index];
424   }
425   void __setitem__(int index, char* value) {
426     free(data[index]);
427     data[index] = strdup(value);
428   }
429   char** data;
430   int nelem;
431   int size;
432 };
433 %}
434 /* End of special struct to handle string arrays */
435
436 /* input typemap 
437    This typemap can be used for input array objects only.
438    It accepts swig carray objects or numpy contiguous or non contiguous objects.
439    In case of non-contiguous numpy object, it is converted (new object) into a contiguous numpy object
440    This new object is deleted after the call.
441 */
442 %define TYPEMAP_IN3(type,typecode)
443 %typemap(in) type* IN_ARRAY3
444              (ArrayObject* array=NULL, int is_new_object) {
445   if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0)) == -1)
446   {
447 %#ifdef WITH_NUMPY
448     int size[1] = {-1};
449     array = obj_to_array_contiguous_allow_conversion($input, typecode, &is_new_object);
450     if (!array || !require_dimensions(array,1) || !require_size(array,size,1)) SWIG_fail;
451     $1 = (type*) array->data;
452 %#else
453     SWIG_exception(SWIG_TypeError, "type* expected");
454 %#endif
455   }
456 }
457 %typemap(freearg) type* IN_ARRAY3 {
458 %#ifdef WITH_NUMPY
459   if (is_new_object$argnum && array$argnum) 
460     {
461       Py_DECREF(array$argnum);
462     }
463 %#endif
464 }
465 %enddef
466
467 TYPEMAP_IN3(int,     PyArray_INT)
468 TYPEMAP_IN3(long,    PyArray_LONG)
469 TYPEMAP_IN3(float,   PyArray_FLOAT )
470 TYPEMAP_IN3(double,  PyArray_DOUBLE)
471
472 #undef TYPEMAP_IN3
473
474 %apply int*    IN_ARRAY3 {int    *eval};
475 %apply long*   IN_ARRAY3 {long   *eval};
476 %apply float*  IN_ARRAY3 {float  *eval};
477 %apply double* IN_ARRAY3 {double *eval};
478
479 /*  Specific typemap for complex */
480 %typemap(in) float*  ecpval
481              (ArrayObject* array=NULL, int is_new_object) {
482   if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0)) == -1)
483   {
484 %#ifdef WITH_NUMPY
485     int size[1] = {-1};
486     array = obj_to_array_contiguous_allow_conversion($input, PyArray_CFLOAT, &is_new_object);
487     if (!array || !require_dimensions(array,1) || !require_size(array,size,1)) SWIG_fail;
488     $1 = (float*) array->data;
489 %#else
490     SWIG_exception(SWIG_TypeError, "complex array expected");
491 %#endif
492   }
493 }
494 %typemap(freearg) float* ecpval {
495 %#ifdef WITH_NUMPY
496   if (is_new_object$argnum && array$argnum) 
497     {
498       Py_DECREF(array$argnum);
499     }
500 %#endif
501 }
502 /* End of  Specific typemap for complex */
503
504 /* array of strings on input */
505 %typemap(in) (char** eval,int strSize)
506          (ArrayObject* array=NULL, int is_new_object) {
507   stringArray* sarray;
508   if ((SWIG_ConvertPtr($input, (void **) &sarray, $descriptor(stringArray *),0)) == -1)
509   {
510 %#ifdef WITH_NUMPY
511     int size[1] = {-1};
512     array = obj_to_array_contiguous_allow_conversion($input, PyArray_STRING, &is_new_object);
513     if (!array || !require_dimensions(array,1) || !require_size(array,size,1)) SWIG_fail;
514     $1 = (char**) malloc(array_size(array,0)*sizeof(char*));
515     $2 = array->strides[0];
516     for(int i=0;i<array_size(array,0);i++)
517       {
518         $1[i]=(char*) malloc(sizeof(char)*(array->strides[0]+1));
519         strncpy($1[i],(char*) array->data + i* array->strides[0],array->strides[0]);
520         *($1[i]+array->strides[0])='\0';
521       }
522 %#else
523     SWIG_exception(SWIG_TypeError, "string array expected");
524 %#endif
525   }
526   else
527   {
528     $1=sarray->data;
529     $2=sarray->size;
530   }
531 }
532
533 %typemap(freearg) (char** eval,int strSize) {
534 %#ifdef WITH_NUMPY
535   if (array$argnum)
536     {
537       for(int i=0;i<array_size(array$argnum,0);i++)
538         free($1[i]);
539       free($1);
540     }
541   if (is_new_object$argnum && array$argnum) 
542     {
543       Py_DECREF(array$argnum);
544     }
545 %#endif
546 }
547 /* End of array of strings on input */
548
549 /* inplace typemaps 
550    This typemap can be used for input/output array objects.
551    It accepts swig carray objects or numpy contiguous objects.
552 */
553
554 %define TYPEMAP_INPLACE3(type,typecode)
555 %typemap(in) type* INPLACE_ARRAY3 (ArrayObject* temp=NULL) {
556   if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0)) == -1)
557   {
558 %#ifdef WITH_NUMPY
559     temp = obj_to_array_no_conversion($input,typecode);
560     if (!temp  || !require_contiguous(temp)) SWIG_fail;
561     $1 = (type*) temp->data;
562 %#else
563     temp = NULL;
564     SWIG_exception(SWIG_TypeError, "type* expected");
565 %#endif
566   }
567 }
568 %enddef
569
570 TYPEMAP_INPLACE3(int,     PyArray_INT)
571 TYPEMAP_INPLACE3(long,    PyArray_LONG)
572 TYPEMAP_INPLACE3(float,   PyArray_FLOAT )
573 TYPEMAP_INPLACE3(double,  PyArray_DOUBLE)
574
575 #undef TYPEMAP_INPLACE3
576
577 %apply int*    INPLACE_ARRAY3 {int    *lval};
578 %apply long*   INPLACE_ARRAY3 {long   *lval};
579 %apply float*  INPLACE_ARRAY3 {float  *lval};
580 %apply double* INPLACE_ARRAY3 {double *lval};
581
582 /*  typemap for complex inout */
583 %typemap(in) float* lcpval
584              (ArrayObject* temp=NULL) {
585   if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0)) == -1)
586   {
587 %#ifdef WITH_NUMPY
588     temp = obj_to_array_no_conversion($input,PyArray_CFLOAT);
589     if (!temp  || !require_contiguous(temp)) SWIG_fail;
590     $1 = (float*) temp->data;
591 %#else
592     temp = NULL;
593     SWIG_exception(SWIG_TypeError, "complex array expected");
594 %#endif
595   }
596 }
597 /*  End of typemap for complex inout */
598
599 /* typemap for array of strings on input/output */
600 %typemap(in) (char** lval,int strSize)
601             (ArrayObject* temp=NULL) {
602   stringArray* sarray;
603   if ((SWIG_ConvertPtr($input, (void **) &sarray, $descriptor(stringArray *) ,0)) == -1)
604   {
605 %#ifdef WITH_NUMPY
606     temp = obj_to_array_no_conversion($input,PyArray_STRING);
607     if (!temp  || !require_contiguous(temp)) SWIG_fail;
608     $1 = (char**) malloc(array_size(temp,0)*sizeof(char*));
609     $2 = temp->strides[0];
610     for(int i=0;i<array_size(temp,0);i++)
611       {
612         $1[i]=(char*) temp->data+i*temp->strides[0];
613         memset($1[i],0,temp->strides[0]); //numpy strings must be completed with 0 up to elsize
614       }
615 %#else
616     temp = NULL;
617     SWIG_exception(SWIG_TypeError, "string array expected");
618 %#endif
619   }
620   else
621   {
622     $1=sarray->data;
623     $2=sarray->size;
624   }
625 }
626 %typemap(freearg) (char** lval,int strSize) {
627 %#ifdef WITH_NUMPY
628   if (temp$argnum) free($1);
629 %#endif
630 }
631 /* End of typemap for array of strings on input/output */
632
633 %typemap(in) CORBA::Boolean
634 {
635   $1=(CORBA::Boolean)PyInt_AsLong($input);
636 }
637 %typemap(out) CORBA::Boolean
638 {
639   $result=PyInt_FromLong($1 ? 1 : 0);
640 }
641
642 %define CORBAPTR(type)
643 %typemap(in) type##_ptr
644 {
645   Py_BEGIN_ALLOW_THREADS
646   try
647   {
648      CORBA::Object_var obj = api->pyObjRefToCxxObjRef($input,0);
649      $1 = type##::_narrow(obj);
650   }
651   catch(...)
652   {
653      Py_BLOCK_THREADS
654      PyErr_SetString(PyExc_RuntimeError, "not a valid CORBA object ptr");
655   }
656   Py_END_ALLOW_THREADS
657 }
658 %typemap(freearg) type##_ptr {
659   CORBA::release($1);
660 }
661 %enddef
662
663 CORBAPTR(CORBA::ORB)
664 CORBAPTR(Ports::PortProperties)
665 CORBAPTR(Ports::Port)
666 CORBAPTR(Engines::Container)
667 CORBAPTR(PortableServer::POA)
668
669 %typemap(out) Ports::Port_ptr 
670 {
671   $result = api->cxxObjRefToPyObjRef($1, 1);
672   //All output Ports::Port_ptr variables are duplicated by security. Need to release them for python.
673   CORBA::release($1);
674 }
675
676 %typemap(out) Ports::PortProperties_ptr, Engines::Salome_file_ptr
677 {
678   $result = api->cxxObjRefToPyObjRef($1, 1);
679   //the _ptr is duplicated by the routine called. 
680   //Need to release it for Python because the call to cxxObjRefToPyObjRef has created another ref with a count of 1
681   CORBA::release($1);
682 }
683
684 %typemap(out) Engines::DSC::uses_port *
685 {
686    $result = PyList_New($1->length());
687    for (CORBA::ULong i=0; i < $1->length() ; i++)
688      PyList_SetItem($result,i,api->cxxObjRefToPyObjRef((*$1)[i], 1));
689    //delete the copy (created by new) of uses port sequence
690    delete $1;
691 }
692
693 /*
694  * Exception section
695  */
696 // a general exception handler
697 %exception {
698    Py_BEGIN_ALLOW_THREADS
699    try {
700       $action
701    }
702    catch(Engines::DSC::PortNotDefined& _e) {
703       Py_BLOCK_THREADS
704       PyObject* excc = PyObject_GetAttrString(dsc, "PortNotDefined");
705       PyObject* exci = PyEval_CallObject(excc, (PyObject *)NULL);
706       PyErr_SetObject(excc, exci);
707       Py_XDECREF(excc);
708       Py_XDECREF(exci);
709       return NULL;
710    }
711    catch(Engines::DSC::PortNotConnected& _e) {
712       Py_BLOCK_THREADS
713       PyObject* excc = PyObject_GetAttrString(dsc, "PortNotConnected");
714       PyObject* exci = PyEval_CallObject(excc, (PyObject *)NULL);
715       PyErr_SetObject(excc, exci);
716       Py_XDECREF(excc);
717       Py_XDECREF(exci);
718       return NULL;
719    }
720    catch(Engines::DSC::BadPortType& _e) {
721       Py_BLOCK_THREADS
722       PyObject* excc = PyObject_GetAttrString(dsc, "BadPortType");
723       PyObject* exci = PyEval_CallObject(excc, (PyObject *)NULL);
724       PyErr_SetObject(excc, exci);
725       Py_XDECREF(excc);
726       Py_XDECREF(exci);
727       return NULL;
728    }
729    catch (SALOME_Exception &e) {
730       Py_BLOCK_THREADS
731       PyErr_SetString(PyExc_RuntimeError,e.what());
732       return NULL;
733    }
734    catch (SALOME::SALOME_Exception &e) {
735       Py_BLOCK_THREADS
736       //This one should be converted into a python corba exception
737       PyErr_SetString(PyExc_RuntimeError,e.details.text);
738       return NULL;
739    }
740    catch (const CORBA::SystemException& e) {
741       Py_BLOCK_THREADS 
742       return api->handleCxxSystemException(e);
743    }
744    catch(...) {
745       Py_BLOCK_THREADS
746       PyErr_SetString(PyExc_ValueError,"Unknown exception");
747       return NULL;
748    }
749    Py_END_ALLOW_THREADS
750 }
751
752 /*
753  * End of Exception section
754  */
755 namespace Engines
756 {
757 class DSC
758 {
759   public:
760     enum Message { AddingConnection, RemovingConnection, ApplicationError };
761 };
762 }
763
764 class PySupervCompo:public Superv_Component_i
765 {
766   public:
767
768     PySupervCompo(CORBA::ORB_ptr orb,
769          PortableServer::POA_ptr poa,
770          Engines::Container_ptr contai,
771          const char *instanceName,
772          const char *interfaceName);
773
774     virtual ~PySupervCompo();
775     CORBA::Boolean init_service(const char * service_name){return true;};
776     virtual provides_port * create_provides_data_port(const char* port_fab_type)
777         throw (BadFabType);
778     virtual uses_port * create_uses_data_port(const char* port_fab_type)
779         throw (BadFabType);
780     virtual void add_port(const char * port_fab_type,
781         const char * port_type,
782         const char * port_name)
783         throw (PortAlreadyDefined, BadFabType, BadType, BadProperty);
784     template < typename SpecificPortType >
785     SpecificPortType * add_port(const char * port_fab_type,
786             const char * port_type,
787             const char * port_name)
788           throw (PortAlreadyDefined, BadFabType, BadType, BadCast, BadProperty);
789     virtual void add_port(provides_port * port,
790           const char* provides_port_name)
791           throw (PortAlreadyDefined, NilPort, BadProperty);
792     virtual void add_port(uses_port * port,
793           const char* uses_port_name)
794           throw (PortAlreadyDefined, NilPort, BadProperty);
795     template <typename SpecificPortType >
796     SpecificPortType * get_port( const char * port_name)
797           throw (PortNotDefined, PortNotConnected, BadCast, UnexpectedState);
798     virtual Ports::Port_ptr get_provides_port(const char* provides_port_name,
799               const CORBA::Boolean connection_error)
800               throw (Engines::DSC::PortNotDefined,
801                      Engines::DSC::PortNotConnected,
802                      Engines::DSC::BadPortType);
803     virtual void connect_uses_port(const char* uses_port_name,
804                          Ports::Port_ptr provides_port_ref)
805               throw (Engines::DSC::PortNotDefined,
806                      Engines::DSC::BadPortType,
807                      Engines::DSC::NilPort);
808     virtual void connect_provides_port(const char* provides_port_name)
809               throw (Engines::DSC::PortNotDefined);
810     virtual void disconnect_provides_port(const char* provides_port_name,
811               const Engines::DSC::Message message)
812               throw (Engines::DSC::PortNotDefined,
813                      Engines::DSC::PortNotConnected);
814
815     virtual void disconnect_uses_port(const char* uses_port_name,
816                   Ports::Port_ptr provides_port_ref,
817                   const Engines::DSC::Message message)
818               throw (Engines::DSC::PortNotDefined,
819                      Engines::DSC::PortNotConnected,
820                      Engines::DSC::BadPortReference);
821
822     virtual Ports::PortProperties_ptr get_port_properties(const char* port_name);
823
824 // Interface for Salome_file
825     Engines::Salome_file_ptr getInputFileToService(const char* service_name, const char* Salome_file_name);
826     void checkInputFilesToService(const char* service_name);
827     Engines::Salome_file_ptr setInputFileToService(const char* service_name, const char* Salome_file_name);
828     Engines::Salome_file_ptr getOutputFileToService(const char* service_name, const char* Salome_file_name);
829     void checkOutputFilesToService(const char* service_name);
830     Engines::Salome_file_ptr setOutputFileToService(const char* service_name, const char* Salome_file_name);
831 // End of Interface for Salome_file
832
833 // DSC interface for python components
834   virtual void add_provides_port(Ports::Port_ptr ref, const char* provides_port_name, Ports::PortProperties_ptr port_prop);
835   virtual void add_uses_port(const char* repository_id, const char* uses_port_name, Ports::PortProperties_ptr port_prop);
836   virtual Engines::DSC::uses_port * get_uses_port(const char* uses_port_name);
837   CORBA::Boolean is_connected(const char* port_name) throw (Engines::DSC::PortNotDefined);
838 // End of DSC interface for python components
839
840    static void setTimeOut();
841
842
843     %extend
844       {
845        //To get the address of the component
846         long ptr()
847         {
848           return (long)self;
849         }
850       }
851 };
852
853 %apply int *OUTPUT { int *nval };
854 %apply float *INOUT { float  *ti };
855 %apply float *INPUT { float  *tf };
856 %apply int *INOUT { int  *niter };
857 %apply double *INOUT { double  *ti };
858 %apply double *INPUT { double  *tf };
859
860 extern "C" void create_calcium_port(Superv_Component_i* compo,char* name,char* type,char *mode,char* depend);
861
862 %ignore CPMESSAGE;
863 %include "calciumP.h"
864
865 %cstring_bounded_output(char *instanceName, 1024);
866
867 int  cp_cd(Superv_Component_i *component,char *instanceName);
868
869 int cp_een(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,int    *eval);
870 int cp_edb(Superv_Component_i *component,int dep,double t,int n,char *nom,int nval,double *eval);
871 int cp_ere(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,float  *eval);
872 int cp_erd(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,float  *eval);
873 int cp_ecp(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,float  *ecpval);
874 int cp_elo(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,int    *eval);
875 int cp_ech(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,char** eval,int strSize);
876 int cp_elg(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,long   *eval);
877 int cp_eln(Superv_Component_i *component,int dep,float  t,int n,char *nom,int nval,long   *eval);
878
879
880 int cp_len(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,int    *lval);
881 int cp_ldb(Superv_Component_i *component,int dep,double *ti,double *tf,int *niter,char *nom,int nmax,int *nval,double *lval);
882 int cp_lre(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,float  *lval);
883 int cp_lrd(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,float  *lval);
884 int cp_lcp(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,float  *lcpval);
885 int cp_llo(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,int    *lval);
886 int cp_lch(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,char** lval,int strSize);
887 int cp_llg(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,long   *lval);
888 int cp_lln(Superv_Component_i *component,int dep,float  *ti,float  *tf,int *niter,char *nom,int nmax,int *nval,long   *lval);
889
890 int cp_fini(Superv_Component_i *component,char *nom, int n);
891 int cp_fint(Superv_Component_i *component,char *nom, float t);
892 int cp_effi(Superv_Component_i *component,char *nom, int n);
893 int cp_efft(Superv_Component_i *component,char *nom, float t);
894
895 int cp_fin(Superv_Component_i *component,int cp_end);
896