Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExternalEvaluation Scientific/DotNetScilab/dotnetsci/Scilab.cs @ 10122

Last change on this file since 10122 was 10122, checked in by mkommend, 10 years ago

#2082: Added Scilab .NET coupling to external evaluation branch.

File size: 21.7 KB
Line 
1/*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at   
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12//=============================================================================
13/* Please notice that it is only a example and not a full C# wrapper */
14//=============================================================================
15using System;
16using System.Collections.Generic;
17using System.Text;
18using System.Runtime.InteropServices;
19using System.Threading;
20//=============================================================================
21namespace DotNetScilab
22{
23    /* Scilab Types */
24    public enum ScilabType
25    {
26        sci_matrix = 1,
27        sci_poly = 2,
28        sci_boolean = 4,
29        sci_sparse = 5,
30        sci_boolean_sparse = 6,
31        sci_matlab_sparse = 7,
32        sci_ints = 8,
33        sci_handles = 9,
34        sci_strings = 10,
35        sci_u_function = 11,
36        sci_c_function = 13,
37        sci_lib = 14,
38        sci_list = 15,
39        sci_tlist = 16,
40        sci_mlist = 17,
41        sci_pointer = 128,
42        sci_implicit_poly = 129,
43        sci_intrinsic_function = 130
44    };
45
46    public sealed class Scilab
47    {
48        //=============================================================================
49        static Scilab instance = null;
50        static readonly object padlock = new object();
51        private Boolean withGraphics = false;
52        //=============================================================================
53        /// <summary>
54        /// Constructor, initialize scilab engine.
55        /// </summary>
56        public Scilab()
57        {
58            // Disable TCL/TK and Java graphic interfaces
59            Scilab_cs_wrapper.DisableInteractiveMode();
60            withGraphics = false;
61
62            // start Scilab engine configurated without java
63            Scilab_cs_wrapper.StartScilab(null, null, null);
64        }
65        //=============================================================================
66        public Scilab(Boolean _bWithGraphics)
67        {
68            // Disable TCL/TK and Java graphic interfaces
69            if (_bWithGraphics == false)
70            {
71                Scilab_cs_wrapper.DisableInteractiveMode();
72                withGraphics = false;
73            }
74            else
75            {
76                withGraphics = true;
77            }
78
79            // start Scilab engine
80            Scilab_cs_wrapper.StartScilab(null, null, null);
81        }
82        //=============================================================================
83        /// <summary>
84        /// Singleton
85        /// Only one instance of Scilab can be launch
86        /// thread safe
87        /// </summary>
88        public static Scilab Instance
89        {
90            get
91            {
92                lock (padlock)
93                {
94                    if (instance == null)
95                    {
96                        instance = new Scilab();
97                    }
98                    return instance;
99                }
100            }
101        }
102        //=============================================================================
103        /// <summary>
104        /// Destructor
105        /// </summary>
106        ~Scilab()
107        {
108            // freed by O.S
109            //Scilab_cs_wrapper.TerminateScilab(null);
110        }
111        //=============================================================================
112        /// <summary>
113        /// Send a job to scilab
114        /// </summary>
115        /// <param name="command">command to send to scilab</param>
116        /// <returns>error code operation, 0 : OK</returns>
117        public int SendScilabJob(string command)
118        {
119            return Scilab_cs_wrapper.SendScilabJob(command);
120        }
121        //=============================================================================
122        /// <summary>
123        ///  get last error code
124        /// </summary>
125        /// <returns>last error code</returns>
126        public int GetLastErrorCode()
127        {
128            return Scilab_cs_wrapper.GetLastErrorCode();
129        }
130        //=============================================================================
131        /// <summary>
132        /// Write a named matrix of double in Scilab
133        /// </summary>
134        /// <param name="matrixName"> variable name</param>
135        /// <param name="iRows"> Number of row</param>
136        /// <param name="iCols"> Number of column</param>
137        /// <param name="matrixDouble"> pointer on data</param>
138        /// <returns> if the operation successes (0) or not ( !0 )</returns>
139        public int createNamedMatrixOfDouble(string matrixName, int iRows, int iCols, double[] matrixDouble)
140        {
141            System.IntPtr ptrEmpty = new System.IntPtr();
142            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.createNamedMatrixOfDouble(ptrEmpty, matrixName, iRows, iCols, matrixDouble);
143            return SciErr.iErr;
144        }
145        //=============================================================================
146        /// <summary>
147        /// Write a named matrix of string in scilab
148        /// </summary>
149        /// <param name="matrixName"> variable name</param>
150        /// <param name="iRows"> Number of row</param>
151        /// <param name="iCols"> Number of column</param>
152        /// <param name="matrixDouble"> pointer on data</param>
153        /// <returns> if the operation successes (0) or not ( !0 )</returns>
154        public int createNamedMatrixOfString(string matrixName, int iRows, int iCols, string[] matrixString)
155        {
156            System.IntPtr ptrEmpty = new System.IntPtr();
157            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.createNamedMatrixOfString(ptrEmpty, matrixName, iRows, iCols, matrixString);
158            return SciErr.iErr;
159        }
160        //=============================================================================
161        /// <summary>
162        /// Write a named matrix of boolean in Scilab
163        /// </summary>
164        /// <param name="matrixName"> variable name</param>
165        /// <param name="iRows"> Number of row</param>
166        /// <param name="iCols"> Number of column</param>
167        /// <param name="matrixBoolean"> pointer on data</param>
168        /// <returns> if the operation successes (0) or not ( !0 )</returns>
169        public int createNamedMatrixOfBoolean(string matrixName, int iRows, int iCols, Boolean[] matrixBoolean)
170        {
171            int[] matrixInt = new int[matrixBoolean.Length];
172            for (int i = 0; i < matrixBoolean.Length; i++)
173            {
174                if (matrixBoolean[i] == true)
175                {
176                    matrixInt[i] = 1;
177                }
178                else
179                {
180                    matrixInt[i] = 0;
181                }
182            }
183            System.IntPtr ptrEmpty = new System.IntPtr();
184            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.createNamedMatrixOfBoolean(ptrEmpty, matrixName, iRows, iCols, matrixInt);
185            return SciErr.iErr;
186        }
187        //=============================================================================
188        /// <summary>
189        /// Write a named matrix of int(32) in Scilab
190        /// </summary>
191        /// <param name="matrixName"> variable name</param>
192        /// <param name="iRows"> Number of row</param>
193        /// <param name="iCols"> Number of column</param>
194        /// <param name="matrixInt"> pointer on data</param>
195        public int createNamedMatrixOfInt32(string matrixName, int iRows, int iCols, int[] matrixInt)
196        {
197            System.IntPtr ptrEmpty = new System.IntPtr();
198            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.createNamedMatrixOfInteger32(ptrEmpty, matrixName, iRows, iCols, matrixInt);
199            return SciErr.iErr;
200        }
201        //=============================================================================
202        /// <summary>
203        /// Write a named matrix of complex double in Scilab
204        /// </summary>
205        /// <param name="matrixName"> variable name</param>
206        /// <param name="iRows">Number of row</param>
207        /// <param name="iCols">Number of column</param>
208        /// <param name="matrixRealPart">real part</param>
209        /// <param name="matrixImagPart">imag part</param>
210        /// <returns></returns>
211        public int createNamedComplexMatrixOfDouble(string matrixName,
212                                                int iRows, int iCols,
213                                                double[] matrixRealPart,
214                                                double[] matrixImagPart)
215        {
216            System.IntPtr ptrEmpty = new System.IntPtr();
217            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.createNamedComplexMatrixOfDouble(ptrEmpty, matrixName,
218                                                    iRows, iCols,
219                                                    matrixRealPart,
220                                                    matrixImagPart);
221            return SciErr.iErr;
222        }
223        //=============================================================================
224        /// <summary>
225        /// Read a named matrix of double from Scilab
226        /// </summary>
227        /// <param name="matrixName"> variable name</param>
228        /// <returns>a matrix of double from scilab. If Variable name does not exist returns null</returns>
229        public unsafe double[] readNamedMatrixOfDouble(string matrixName)
230        {
231            int iRows = 0;
232            int iCols = 0;
233
234            System.IntPtr ptrEmpty = new System.IntPtr();
235            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.readNamedMatrixOfDouble(ptrEmpty, matrixName, &iRows, &iCols, null);
236
237            if (iRows * iCols > 0)
238            {
239                double[] matrixDouble = new double[iRows * iCols];
240
241                // get values in matrixDouble
242                SciErr = Scilab_cs_wrapper.readNamedMatrixOfDouble(ptrEmpty, matrixName, &iRows, &iCols, matrixDouble);
243                if (SciErr.iErr != 0) return null;
244                return matrixDouble;
245            }
246            return null;
247        }
248        //=============================================================================
249        /// <summary>
250        /// Get dimensions of a named matrix in scilab
251        /// </summary>
252        /// <returns>a int array.
253        /// if variable name does not exist dimensions are null </returns>
254        public unsafe int[] getNamedVarDimension(string matrixName)
255        {
256            int[] iDim = null;
257            int iRows = 0;
258            int iCols = 0;
259
260            System.IntPtr ptrEmpty = new System.IntPtr();
261            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.getNamedVarDimension(ptrEmpty, matrixName, &iRows, &iCols);
262            if (SciErr.iErr == 0)
263            {
264                iDim = new int[2];
265                iDim[0] = iRows;
266                iDim[1] = iCols;
267            }
268            return iDim;
269        }
270        //=============================================================================
271        /// <summary>
272        /// Read a named matrix of string from scilab
273        /// </summary>
274        /// <param name="matrixName"> variable name</param>
275        /// <returns>a matrix of string from scilab. If Variable name does not exist returns null</returns>
276        public unsafe string[] readNamedMatrixOfString(string matrixName)
277        {
278            string[] matrixString = null;
279
280            int[] iDim = getNamedVarDimension(matrixName);
281
282            if (iDim != null)
283            {
284                int iRows = iDim[0];
285                int iCols = iDim[1];
286
287                // we allocate lengthmatrixString
288                int[] lengthmatrixString = new int[iRows * iCols];
289
290                System.IntPtr ptrEmpty = new System.IntPtr();
291
292                // we get length of strings
293                Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.readNamedMatrixOfString(ptrEmpty, matrixName,
294                                        &iRows, &iCols,
295                                        lengthmatrixString, null);
296
297                // we allocate each string
298                matrixString = new string[iRows * iCols];
299                for (int i = 0; i < iRows * iCols; i++)
300                {
301                    matrixString[i] = new string(' ', lengthmatrixString[i]);
302                }
303
304                // we get strings from scilab
305                SciErr = Scilab_cs_wrapper.readNamedMatrixOfString(ptrEmpty, matrixName,
306                                                &iRows, &iCols,
307                                                lengthmatrixString,
308                                                matrixString);
309            }
310            return matrixString;
311        }
312        //=============================================================================
313        /// <summary>
314        /// Read a named matrix of boolean from Scilab
315        /// </summary>
316        /// <param name="matrixName"> variable name</param>
317        /// <returns>a matrix of boolean from scilab. If Variable name does not exist returns null</returns>
318        public unsafe Boolean[] getNamedMatrixOfBoolean(string matrixName)
319        {
320            Boolean[] matrixBoolean = null;
321            int[] iDim = getNamedVarDimension(matrixName);
322
323            if (iDim != null)
324            {
325                int iRows = iDim[0];
326                int iCols = iDim[1];
327                int[] matrixInt = new int[iRows * iCols];
328
329                System.IntPtr ptrEmpty = new System.IntPtr();
330
331                // get values in matrixDouble
332                Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.readNamedMatrixOfBoolean(ptrEmpty, matrixName,
333                                                            &iRows, &iCols,
334                                                            matrixInt);
335
336                if (matrixInt != null)
337                {
338                    matrixBoolean = new Boolean[iRows * iCols];
339                    for (int i = 0; i < iRows * iCols; i++)
340                    {
341                        if (matrixInt[i] == 1)
342                        {
343                            matrixBoolean[i] = true;
344                        }
345                        else
346                        {
347                            matrixBoolean[i] = false;
348                        }
349                    }
350                }
351            }
352            return matrixBoolean;
353        }
354        //=============================================================================
355        /// <summary>
356        /// Read a named matrix of complex double in Scilab (Real part)
357        /// </summary>
358        /// <param name="matrixName">variable name</param>
359        /// <returns> real part. If Variable name does not exist returns null</returns>
360        public unsafe double[] readNamedComplexMatrixOfDoubleRealPart(string matrixName)
361        {
362            double[] dRealPart = null;
363            int[] iDim = getNamedVarDimension(matrixName);
364            if (iDim != null)
365            {
366                int iRows = iDim[0];
367                int iCols = iDim[1];
368
369                double[] dImagPart = new double[iRows * iCols];
370                dRealPart = new double[iRows * iCols];
371
372                System.IntPtr ptrEmpty = new System.IntPtr();
373
374                Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.readNamedComplexMatrixOfDouble(ptrEmpty, matrixName,
375                                           &iRows, &iCols,
376                                           dRealPart,
377                                           dImagPart);
378            }
379            return dRealPart;
380        }
381        //=============================================================================
382        /// <summary>
383        /// Read a named matrix of complex double in Scilab (Imag part)
384        /// </summary>
385        /// <param name="matrixName">variable name</param>
386        /// <returns> img part. If Variable name does not exist returns null</returns>
387        public unsafe double[] readNamedComplexMatrixOfDoubleImgPart(string matrixName)
388        {
389            double[] dImagPart = null;
390            int[] iDim = getNamedVarDimension(matrixName);
391            if (iDim != null)
392            {
393                int iRows = iDim[0];
394                int iCols = iDim[1];
395
396                double[] dRealPart = new double[iRows * iCols];
397                dImagPart = new double[iRows * iCols];
398
399                System.IntPtr ptrEmpty = new System.IntPtr();
400
401                Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.readNamedComplexMatrixOfDouble(ptrEmpty, matrixName,
402                                           &iRows, &iCols,
403                                           dRealPart,
404                                           dImagPart);
405            }
406            return dImagPart;
407        }
408        //=============================================================================
409        /// <summary>
410        /// Read a named matrix of int(32) in Scilab
411        /// </summary>
412        /// <param name="matrixName"> variable name</param>
413        /// <returns>a matrix of int(32) from scilab. If Variable name does not exist returns null</returns>
414        public unsafe int[] readNamedMatrixOfInt32(string matrixName)
415        {
416            int[] matrixInt = null;
417            int[] iDim = getNamedVarDimension(matrixName);
418            if (iDim != null)
419            {
420                int iRows = iDim[0];
421                int iCols = iDim[1];
422
423                // we allocate matrixInt array
424                matrixInt = new int[iRows * iCols];
425
426                System.IntPtr ptrEmpty = new System.IntPtr();
427
428                // get values in matrixInt
429                Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.readNamedMatrixOfInteger32(ptrEmpty, matrixName, &iRows, &iCols, matrixInt);
430            }
431            return matrixInt;
432        }
433        //=============================================================================
434        /// <summary>
435        /// get scilab type of named matrix
436        /// </summary>
437        /// <param name="matrixName"> variable name</param>
438        /// <returns>scilab type (see enum ScilabType)</returns>
439        public unsafe int getNamedVarType(string matrixName)
440        {
441            int iType = 0;
442            System.IntPtr ptrEmpty = new System.IntPtr();
443
444            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.getNamedVarType(ptrEmpty, matrixName, &iType);
445            if (SciErr.iErr == 0) return iType;
446            return 0;
447        }
448        //=============================================================================
449        /// <summary>
450        /// Detect if a variable name exists in Scilab
451        /// </summary>
452        /// <param name="matrixName"> variable name</param>
453        /// <returns> true if exists</returns>
454        public unsafe Boolean existNamedVariable(string matrixName)
455        {
456            int* piAdress = null;
457            System.IntPtr ptrEmpty = new System.IntPtr();
458
459            Scilab_cs_wrapper.api_Err SciErr = Scilab_cs_wrapper.getVarAddressFromName(ptrEmpty, matrixName, &piAdress);
460            if (SciErr.iErr == 0) return true;
461            return false;
462        }
463        //=============================================================================
464        /// <summary>
465        /// Execute a scilab script .sce
466        /// </summary>
467        /// <param name="scriptFilename">the path to the .sce file</param>
468        /// <returns>error code operation, 0 : OK</returns>
469        public int execScilabScript(String scriptFilename)
470        {
471            return Scilab_cs_wrapper.SendScilabJob("exec('" + scriptFilename + "');");
472        }
473        //=============================================================================
474        /// <summary>
475        /// Detect if a Scilab graphic window is opened
476        /// </summary>
477        /// <returns>true or false</returns>
478        public Boolean HaveAGraph()
479        {
480            if (withGraphics)
481            {
482                int ierr = Scilab_cs_wrapper.sciHasFigures();
483                if (ierr == 1) return true;
484            }
485            return false;
486        }
487        //=============================================================================
488        /// <summary>
489        /// do a scilab event
490        /// parser need to run to do a event
491        /// </summary>
492        /// <returns>error code operation, 0 : OK</returns>
493        public int doEvent()
494        {
495            // do a pause (we do not want 100% CPU used)
496            // ugly but it works ...
497            Thread.Sleep(1);
498            // do a loop of parser
499            return SendScilabJob("");
500        }
501        //=============================================================================
502        /// <summary>
503        /// get scilab type of named matrix
504        /// </summary>
505        /// <param name="matrixName"> variable name</param>
506        /// <returns>scilab type (see enum ScilabType)</returns>
507        public int isNamedVarComplex(string matrixName)
508        {
509            System.IntPtr ptrEmpty = new System.IntPtr();
510            return Scilab_cs_wrapper.isNamedVarComplex(ptrEmpty, matrixName);
511        }
512        //=============================================================================
513    }
514}
515//=============================================================================
Note: See TracBrowser for help on using the repository browser.