2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
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
12 //=============================================================================
14 using System.Collections.Generic;
17 using System.Runtime.InteropServices;
18 using System.Threading;
19 //=============================================================================
20 namespace DotNetScilab
22 public sealed class Scilab
24 //=============================================================================
25 public enum ScilabType {
30 sci_boolean_sparse = 6,
31 sci_matlab_sparse = 7, /* matlab sparse matrix */
41 sci_lufact_pointer = 128, /* lufact pointer */
42 sci_implicit_poly = 129,
43 sci_intrinsic_function = 130};
44 //=============================================================================
45 static Scilab instance = null;
46 static readonly object padlock = new object();
47 private Boolean withGraphics = false;
48 //=============================================================================
49 private const string CALL_SCILAB_DLL = "call_scilab.dll";
50 private const string API_SCILAB_DLL = "api_scilab.dll";
51 private const string GRAPHICS_DLL = "graphics.dll";
52 private const string OUTPUT_STREAM_DLL = "scioutput_stream.dll";
53 //=============================================================================
55 /// Constructor, initialize scilab engine.
59 // start Scilab engine
60 StartScilab(null, null, null);
61 // Disable TCL/TK and Java graphic interfaces
62 DisableInteractiveMode();
65 //=============================================================================
66 public Scilab(Boolean _bWithGraphics)
68 // start Scilab engine
69 StartScilab(null, null, null);
70 // Disable TCL/TK and Java graphic interfaces
71 if (_bWithGraphics == false)
73 DisableInteractiveMode();
81 //=============================================================================
84 /// Only one instance of Scilab can be launch
87 public static Scilab Instance
95 instance = new Scilab();
101 //=============================================================================
109 //=============================================================================
111 /// Send a job to scilab
113 /// <param name="command">command to send to scilab</param>
114 /// <returns>error code operation, 0 : OK</returns>
115 public int sendScilabJob(string command)
117 return SendScilabJob(command);
119 //=============================================================================
121 /// Write a named matrix of double in Scilab
123 /// <param name="matrixName"> variable name</param>
124 /// <param name="iRows"> Number of row</param>
125 /// <param name="iCols"> Number of column</param>
126 /// <param name="matrixDouble"> pointer on data</param>
127 /// <returns> if the operation successes (0) or not ( !0 )</returns>
128 public int setNamedMatrixOfDouble(string matrixName, int iRows, int iCols, double[] matrixDouble)
130 return createNamedMatrixOfDouble(matrixName, iRows, iCols, matrixDouble);
132 //=============================================================================
134 /// Read a named matrix of double from Scilab
136 /// <param name="matrixName"> variable name</param>
137 /// <returns>a matrix of double from scilab. If Variable name does not exist returns null</returns>
138 public unsafe double[] getNamedMatrixOfDouble(string matrixName)
140 // first , we want to known dimensions iRows and iCols
141 int[] iDims = getNamedMatrixOfDoubleDimensions(matrixName);
142 int iRows = iDims[0];
143 int iCols = iDims[1];
145 // we allocate matrixDouble array
146 if (iRows * iCols > 0)
148 double[] matrixDouble = new double[iRows * iCols];
150 // get values in matrixDouble
151 readNamedMatrixOfDouble(matrixName, &iRows, &iCols, matrixDouble);
157 //=============================================================================
159 /// Get dimensions of a named matrix of double in scilab
161 /// <returns>a int array. if variable name does not exist dimensions are 0 0 </returns>
162 public unsafe int[] getNamedMatrixOfDoubleDimensions(string matrixName)
166 int[] iDim = new int[2];
168 readNamedMatrixOfDouble(matrixName, &iRows, &iCols, null);
175 //=============================================================================
177 /// Write a named matrix of string in scilab
179 /// <param name="matrixName"> variable name</param>
180 /// <param name="iRows"> Number of row</param>
181 /// <param name="iCols"> Number of column</param>
182 /// <param name="matrixDouble"> pointer on data</param>
183 /// <returns> if the operation successes (0) or not ( !0 )</returns>
184 public int setNamedMatrixOfString(string matrixName, int iRows, int iCols, string[] matrixString)
186 return createNamedMatrixOfString(matrixName, iRows, iCols, matrixString);
188 //=============================================================================
190 /// Read a named matrix of string from scilab
192 /// <param name="matrixName"> variable name</param>
193 /// <returns>a matrix of string from scilab. If Variable name does not exist returns null</returns>
194 public unsafe string[] getNamedMatrixOfString(string matrixName)
196 // first , we want to known dimensions iRows and iCols
197 int[] iDim = getNamedMatrixOfStringDimensions(matrixName);
201 // we allocate lengthmatrixString
202 int[] lengthmatrixString = new int[iRows * iCols];
203 // we get length of strings
204 readNamedMatrixOfString(matrixName, &iRows, &iCols, lengthmatrixString, null);
206 // we allocate each string
207 string[] matrixString = new string[iRows * iCols];
208 for (int i = 0; i < iRows * iCols; i++)
210 matrixString[i] = new string(' ',lengthmatrixString[i]);
212 // we get strings from scilab
213 readNamedMatrixOfString(matrixName, &iRows, &iCols, lengthmatrixString, matrixString);
217 //=============================================================================
219 /// Get dimensions of a named matrix of string in scilab
221 /// <returns>a int array. if variable name does not exist dimensions are 0 0 </returns>
222 public unsafe int[] getNamedMatrixOfStringDimensions(string matrixName)
226 int[] iDim = new int[2];
228 readNamedMatrixOfString(matrixName, &iRows, &iCols, null, null);
235 //=============================================================================
237 /// Write a named matrix of boolean in Scilab
239 /// <param name="matrixName"> variable name</param>
240 /// <param name="iRows"> Number of row</param>
241 /// <param name="iCols"> Number of column</param>
242 /// <param name="matrixBoolean"> pointer on data</param>
243 /// <returns> if the operation successes (0) or not ( !0 )</returns>
244 public int setNamedMatrixOfBoolean(string matrixName, int iRows, int iCols, Boolean[] matrixBoolean)
246 int[] matrixInt = new int[matrixBoolean.Length];
247 for (int i = 0; i< matrixBoolean.Length; i++)
249 if (matrixBoolean[i] == true)
258 return createNamedMatrixOfBoolean(matrixName, iRows, iCols, matrixInt);
260 //=============================================================================
262 /// Read a named matrix of boolean from Scilab
264 /// <param name="matrixName"> variable name</param>
265 /// <returns>a matrix of boolean from scilab. If Variable name does not exist returns null</returns>
266 public unsafe Boolean[] getNamedMatrixOfBoolean(string matrixName)
268 // first , we want to known dimensions iRows and iCols
269 int[] iDim = getNamedMatrixOfBooleanDimensions(matrixName);
273 // we allocate matrixInt array
274 if (iRows * iCols > 0)
276 int[] matrixInt = new int[iRows * iCols];
278 // get values in matrixDouble
279 readNamedMatrixOfBoolean(matrixName, &iRows, &iCols, matrixInt);
281 Boolean[] matrixBoolean = new Boolean[iRows * iCols];
282 for (int i = 0; i < iRows * iCols; i++ )
284 if (matrixInt[i] == 1)
286 matrixBoolean[i] = true;
290 matrixBoolean[i] = false;
293 return matrixBoolean;
297 //=============================================================================
299 /// Get dimensions of a named matrix of boolean in scilab
301 /// <param name="matrixName"> variable name</param>
302 /// <returns>a int array. if variable name does not exist dimensions are 0 0 </returns>
303 public unsafe int[] getNamedMatrixOfBooleanDimensions(string matrixName)
307 int[] iDim = new int[2];
309 readNamedMatrixOfBoolean(matrixName, &iRows, &iCols, null);
316 //=============================================================================
318 /// Write a named matrix of int(32) in Scilab
320 /// <param name="matrixName"> variable name</param>
321 /// <param name="iRows"> Number of row</param>
322 /// <param name="iCols"> Number of column</param>
323 /// <param name="matrixInt"> pointer on data</param>
324 public int setNamedMatrixOfInt(string matrixName, int iRows, int iCols, int[] matrixInt)
326 return createNamedMatrixOfInteger32(matrixName, iRows, iCols, matrixInt);
328 //=============================================================================
330 /// Read a named matrix of int(32) in Scilab
332 /// <param name="matrixName"> variable name</param>
333 /// <returns>a matrix of int(32) from scilab. If Variable name does not exist returns null</returns>
334 public unsafe int[] getNamedMatrixOfInt(string matrixName)
336 // first , we want to known dimensions iRows and iCols
337 int[] iDim = getNamedMatrixOfIntDimensions(matrixName);
341 // we allocate matrixInt array
342 if (iRows * iCols > 0)
344 int[] matrixInt = new int[iRows * iCols];
346 // get values in matrixInt
347 readNamedMatrixOfInteger32(matrixName, &iRows, &iCols, matrixInt);
352 //=============================================================================
354 /// Get dimensions of a named matrix of int(32) in scilab
356 /// <param name="matrixName"> variable name</param>
357 /// <returns>a int array. if variable name does not exist dimensions are 0 0 </returns>
358 public unsafe int[] getNamedMatrixOfIntDimensions(string matrixName)
362 int[] iDim = new int[2];
364 readNamedMatrixOfInteger32(matrixName, &iRows, &iCols, null);
371 //=============================================================================
373 /// Write a named matrix of complex double in Scilab
375 /// <param name="matrixName"> variable name</param>
376 /// <param name="iRows">Number of row</param>
377 /// <param name="iCols">Number of column</param>
378 /// <param name="matrixRealPart">real part</param>
379 /// <param name="matrixImagPart">imag part</param>
380 /// <returns></returns>
381 public int setNamedMatrixOfComplexDouble(string matrixName,
382 int iRows, int iCols,
383 double[] matrixRealPart,
384 double[] matrixImagPart)
386 return createNamedComplexMatrixOfDouble(matrixName,
391 //=============================================================================
393 /// Read a named matrix of complex double in Scilab (Real part)
395 /// <param name="matrixName">variable name</param>
396 /// <returns> real part</returns>
397 public unsafe double[] getNamedMatrixOfComplexDoubleRealPart(string matrixName)
399 int[] iDim = getNamedMatrixOfComplexDoubleDimensions(matrixName);
402 double[] dRealPart = new double[iRows * iCols];
403 double[] dImagPart = new double[iRows * iCols];
405 readNamedComplexMatrixOfDouble(matrixName,
412 //=============================================================================
414 /// Read a named matrix of complex double in Scilab (Imag part)
416 /// <param name="matrixName">variable name</param>
417 /// <returns>imag part</returns>
418 public unsafe double[] getNamedMatrixOfComplexDoubleImagPart(string matrixName)
420 int[] iDim = getNamedMatrixOfComplexDoubleDimensions(matrixName);
423 double[] dRealPart = new double[iRows * iCols];
424 double[] dImagPart = new double[iRows * iCols];
426 readNamedComplexMatrixOfDouble(matrixName,
432 //=============================================================================
434 /// Get dimensions of a matrix of complex double in scilab
436 /// <param name="matrixName">variable name</param>
437 /// <returns>dimension</returns>
438 public unsafe int[] getNamedMatrixOfComplexDoubleDimensions(string matrixName)
442 int[] iDim = new int[2];
444 readNamedComplexMatrixOfDouble(matrixName, &iRows, &iCols, null,null);
451 //=============================================================================
453 /// get scilab type of named matrix
455 /// <param name="matrixName"> variable name</param>
456 /// <returns>scilab type (see enum ScilabType)</returns>
457 public unsafe int getNamedMatrixType(string matrixName)
459 int* piAdress = null;
460 getVarAddressFromName(matrixName, &piAdress);
461 return getVarType(piAdress);
463 //=============================================================================
465 /// Detect if a variable name exists in Scilab
467 /// <param name="matrixName"> variable name</param>
468 /// <returns> true if exists</returns>
469 public unsafe Boolean existNamedVariable(string matrixName)
471 int* piAdress = null;
472 int ierr = getVarAddressFromName(matrixName, &piAdress);
473 if ( (ierr == 1) && (piAdress != null)) return true;
476 //=============================================================================
478 /// Execute a scilab script .sce
480 /// <param name="scriptFilename">the path to the .sce file</param>
481 /// <returns>error code operation, 0 : OK</returns>
482 public int execScilabScript(String scriptFilename)
484 return sendScilabJob( "exec('" + scriptFilename + "');" );
486 //=============================================================================
488 /// Detect if a Scilab graphic window is opened
490 /// <returns>true or false</returns>
491 public Boolean HaveAGraph()
495 int ierr = sciHasFigures();
496 if (ierr == 1) return true;
500 //=============================================================================
502 /// do a scilab event
503 /// parser need to run to do a event
505 /// <returns>error code operation, 0 : OK</returns>
508 // do a pause (we do not want 100% CPU used)
509 // ugly but it works ...
511 // do a loop of parser
512 return sendScilabJob("");
514 //=============================================================================
516 /// Terminate Scilab engine.
518 /// <returns>1 if it is correctly finished</returns>
521 int ierr = TerminateScilab(null);
524 //=============================================================================
526 /// import SendScilabJob from C (see CallScilab.h)
528 [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)]
529 private static extern int SendScilabJob([In]String job);
530 //=============================================================================
532 /// import StartScilab from C (see CallScilab.h)
534 [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)]
535 private static extern int StartScilab([In] String SCIpath,
536 [In] String ScilabStartup,
537 [In] Int32[] Stacksize);
538 //=============================================================================
540 /// import TerminateScilab from C (see CallScilab.h)
542 [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)]
543 private static extern int TerminateScilab([In] String ScilabQuit);
544 //=============================================================================
546 /// import DisableInteractiveMode from C (see CallScilab.h)
548 [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)]
549 private static extern void DisableInteractiveMode();
550 //=============================================================================
552 /// import createNamedMatrixOfString from C (see api_string.h)
554 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
555 private static extern int createNamedMatrixOfString([In] String _pstName,
556 [In] int _iRows, [In] int _iCols,
557 [In] String[] _pstStrings);
558 //=============================================================================
560 /// import createNamedMatrixOfDouble from C (see api_double.h)
562 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
563 private static extern int createNamedMatrixOfDouble([In] String _pstName,
564 [In] int _iRows, [In] int _iCols,
565 [In] double[] _pdblReal);
566 //=============================================================================
568 /// import createNamedMatrixOfBoolean from C (see api_boolean.h)
570 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
571 private static extern int createNamedMatrixOfBoolean([In] String _pstName,
572 [In] int _iRows, [In] int _iCols,
574 //=============================================================================
576 /// import createNamedMatrixOfInteger32 from C (see api_int.h)
578 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
579 private unsafe static extern int createNamedMatrixOfInteger32([In] String _pstName,
580 [In] int _iRows, [In] int _iCols,
582 //=============================================================================
584 /// import createNamedComplexMatrixOfDouble from C (see api_double.h)
586 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
587 private unsafe static extern int createNamedComplexMatrixOfDouble([In] String _pstName,
588 [In] int _iRows, [In] int _iCols,
589 [In] double[] _pdblReal,
590 [In] double[] _pdblImg);
591 //=============================================================================
593 /// import readNamedMatrixOfString from C (see api_string.h)
595 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
596 private unsafe static extern int readNamedMatrixOfString([In] String _pstName,
597 [Out] Int32* _piRows, [Out] Int32* _piCols,
598 [In, Out] int[] _piLength,
599 [In, Out] String[] _pstStrings);
600 //=============================================================================
602 /// import readNamedMatrixOfDouble from C (see api_double.h)
604 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
605 private unsafe static extern int readNamedMatrixOfDouble([In] String _pstName,
606 [Out] Int32* _piRows, [Out] Int32* _piCols,
607 [In, Out] Double[] _pdblReal);
608 //=============================================================================
610 /// import readNamedMatrixOfBoolean from C (see api_boolean.h)
612 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
613 private unsafe static extern int readNamedMatrixOfBoolean([In] String _pstName,
614 [Out] Int32* _piRows, [Out] Int32* _piCols,
615 [In, Out] int[] _piBool);
616 //=============================================================================
618 /// import readNamedMatrixOfInteger32 from C (see api_int.h)
620 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
621 private unsafe static extern int readNamedMatrixOfInteger32([In] String _pstName,
622 [Out] Int32* _piRows, [Out] Int32* _piCols,
623 [In, Out] int[] _piData);
625 //=============================================================================
627 /// import readNamedComplexMatrixOfDouble from C (see api_double.h)
629 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
630 private unsafe static extern int readNamedComplexMatrixOfDouble([In] String _pstName,
631 [Out] Int32* _piRows, [Out] Int32* _piCols,
632 [In, Out] double[] _pdblReal,
633 [In, Out] double[] _pdblImg);
634 //=============================================================================
636 /// get Variable Adress in scilab stack from name
637 /// used for getNamedMatrixType (internal)
639 /// <param name="_pstName">variable name</param>
640 /// <param name="_piAddress"> stack address</param>
641 /// <returns>1 if ok</returns>
642 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
643 private unsafe static extern int getVarAddressFromName([In] String _pstName,
644 [Out] Int32** _piAddress);
645 //=============================================================================
647 /// get variable type with adress in scilab stack
648 /// used for getNamedMatrixType (internal)
650 /// <param name="_piAddress"> stack address</param>
651 /// <returns>scilab type, 0 fails</returns>
652 [DllImport(API_SCILAB_DLL, CharSet = CharSet.Ansi)]
653 private unsafe static extern int getVarType([In] Int32* _piAddress);
654 //=============================================================================
656 /// Detect if a Scilab graphic window is opened
658 /// <returns>0 (FALSE) or 1 (TRUE)</returns>
659 [DllImport(GRAPHICS_DLL, CharSet = CharSet.Ansi)]
660 private unsafe static extern int sciHasFigures();
661 //=============================================================================
663 /// get last error code
665 /// <returns>last error code</returns>
666 [DllImport(OUTPUT_STREAM_DLL, CharSet = CharSet.Ansi)]
667 public unsafe static extern int GetLastErrorCode();
668 //=============================================================================
671 //=============================================================================