Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2082: Corrected marshalling and configuration of the Scilab .NET wrapper.

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