From: Allan CORNET Date: Thu, 2 Jul 2009 14:33:17 +0000 (+0200) Subject: example to call scilab from C# X-Git-Tag: 5.2.0-beta-1~327^2~118 X-Git-Url: http://gitweb.scilab.org/?p=scilab.git;a=commitdiff_plain;h=78464de18184f07fb99898cef6b9ff8b8068a794 example to call scilab from C# --- diff --git a/scilab/CHANGES_5.2.X b/scilab/CHANGES_5.2.X index 75615d0..4673596 100644 --- a/scilab/CHANGES_5.2.X +++ b/scilab/CHANGES_5.2.X @@ -96,6 +96,11 @@ Online Help : - Sub-Chapters are now allowed (See SEP #24) - xmltochm exports help to chm format (Windows) + + call_scilab module : + ==================== + - a example to call scilab from C# + see SCI/modules/call_scilab/examples/call_scilab/dotnetsci directory char encoding bugs : diff --git a/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci.sln b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci.sln new file mode 100644 index 0000000..b70d051 --- /dev/null +++ b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetScilab", "dotnetsci\DotNetScilab.csproj", "{C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example", "example\example.csproj", "{10B5A5BF-3204-4B47-91C0-7FA614EFA94A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Debug|x64.ActiveCfg = Debug|x64 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Debug|x64.Build.0 = Debug|x64 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Debug|x86.ActiveCfg = Debug|x86 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Debug|x86.Build.0 = Debug|x86 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Release|x64.ActiveCfg = Release|x64 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Release|x64.Build.0 = Release|x64 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Release|x86.ActiveCfg = Release|x86 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF}.Release|x86.Build.0 = Release|x86 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Debug|x64.ActiveCfg = Debug|x64 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Debug|x64.Build.0 = Debug|x64 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Debug|x86.ActiveCfg = Debug|x86 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Debug|x86.Build.0 = Debug|x86 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Release|x64.ActiveCfg = Release|x64 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Release|x64.Build.0 = Release|x64 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Release|x86.ActiveCfg = Release|x86 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/DotNetScilab.csproj b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/DotNetScilab.csproj new file mode 100644 index 0000000..4b56094 --- /dev/null +++ b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/DotNetScilab.csproj @@ -0,0 +1,97 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF} + Library + Properties + DotNetScilab + DotNetScilab + v3.5 + 512 + + + true + full + false + ..\..\..\..\..\..\..\bin\ + DEBUG;TRACE + prompt + 4 + true + + + pdbonly + true + ..\..\..\..\..\..\..\bin\ + TRACE + prompt + 4 + true + + + true + ..\..\..\..\..\..\..\bin\ + DEBUG;TRACE + full + x64 + prompt + true + + + ..\..\..\..\..\..\..\bin\ + TRACE + true + pdbonly + x64 + prompt + true + + + true + ..\..\..\..\..\..\..\bin\ + DEBUG;TRACE + full + x86 + prompt + true + + + ..\..\..\..\..\..\..\bin\ + TRACE + true + pdbonly + x86 + prompt + true + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + \ No newline at end of file diff --git a/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/Properties/AssemblyInfo.cs b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..313576a --- /dev/null +++ b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("dotnetsci")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Scilab - DIGITEO")] +[assembly: AssemblyProduct("dotnetsci")] +[assembly: AssemblyCopyright("Copyright © Scilab 2009")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("ef7effe8-4440-47b7-b579-433dc94cc859")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/Scilab.cs b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/Scilab.cs new file mode 100644 index 0000000..547abce --- /dev/null +++ b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/dotnetsci/Scilab.cs @@ -0,0 +1,239 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009 - DIGITEO - Allan CORNET + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt + * + */ +//============================================================================= +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; +//============================================================================= +namespace DotNetScilab +{ + public class Scilab + { + //============================================================================= + private const string CALL_SCILAB_DLL = "call_scilab.dll"; + private const string LIBSCILAB_DLL = "libscilab.dll"; + //private bool initialized = false; + //============================================================================= + /// + /// Constructor, initialize scilab engine. + /// + public Scilab() + { + // start Scilab engine + StartScilab(null, null, null); + // Disable TCL/TK and Java graphic interfaces + DisableInteractiveMode(); + } + //============================================================================= + /// + /// Terminate Scilab engine. + /// + /// 1 if it is correctly finished + public int finish() + { + int ierr = TerminateScilab(null); + return ierr; + } + //============================================================================= + /// + /// Send a job to scilab + /// + /// command to send to scilab + /// error code operation, 0 : OK + public int sendScilabJob(string command) + { + return SendScilabJob(command); + } + //============================================================================= + /// + /// Write a named matrix of double in Scilab + /// + /// variable name + /// Number of row + /// Number of column + /// pointer on data + /// if the operation successes (0) or not ( !0 ) + public int setNamedMatrixOfDouble(string matrixName, int iRows, int iCols, double[] matrixDouble) + { + return createNamedMatrixOfDouble(matrixName, iRows, iCols, matrixDouble); + } + //============================================================================= + /// + /// Read a named matrix of double from Scilab + /// + /// variable name + /// a matrix of double from scilab. If Variable name does not exist returns null + public unsafe double[] getNamedMatrixOfDouble(string matrixName) + { + int iRows = 0; + int iCols = 0; + + double[] matrixDouble = new double[0]; + // first , we want to known dimensions iRows and iCols + readNamedMatrixOfDouble(matrixName, &iRows , &iCols, matrixDouble); + // we allocate matrixDouble array + if (iRows * iCols > 0) + { + matrixDouble = new double[iRows * iCols]; + + // get values in matrixDouble + readNamedMatrixOfDouble(matrixName, &iRows, &iCols, matrixDouble); + + return matrixDouble; + } + return null; + } + //============================================================================= + /// + /// Get dimensions of a named matrix of double in scilab + /// + /// a int array. if variable name does not exist dimensions are 0 0 + public unsafe int[] getNamedMatrixOfDoubleDimensions(string matrixName) + { + int iRows = 0; + int iCols = 0; + int[] iDim = new int[2]; + + double[] matrixDouble = new double[0]; + readNamedMatrixOfDouble(matrixName, &iRows, &iCols, matrixDouble); + + iDim[0] = iRows; + iDim[1] = iCols; + + return iDim; + } + //============================================================================= + /// + /// Write a named matrix of string in scilab + /// + /// variable name + /// Number of row + /// Number of column + /// pointer on data + /// if the operation successes (0) or not ( !0 ) + public int setNamedMatrixOfString(string matrixName, int iRows, int iCols, string[] matrixString) + { + return createNamedMatrixOfString(matrixName, iRows, iCols, matrixString); + } + //============================================================================= + /// + /// Read a named matrix of string from scilab + /// + /// variable name + /// a matrix of string from scilab. If Variable name does not exist returns null + public unsafe string[] getNamedMatrixOfString(string matrixName) + { + int iRows = 0; + int iCols = 0; + + // first , we want to known dimensions iRows and iCols + readNamedMatrixOfString(matrixName, &iRows, &iCols, null, null); + + // we allocate lengthmatrixString + int[] lengthmatrixString = new int[iRows * iCols]; + // we get length of strings + readNamedMatrixOfString(matrixName, &iRows, &iCols, lengthmatrixString, null); + + // we allocate each string + string[] matrixString = new string[iRows * iCols]; + for (int i = 0; i < iRows * iCols; i++) + { + matrixString[i] = new string(' ',lengthmatrixString[i]); + } + // we get strings from scilab + readNamedMatrixOfString(matrixName, &iRows, &iCols, lengthmatrixString, matrixString); + + return matrixString; + } + //============================================================================= + /// + /// Get dimensions of a named matrix of string in scilab + /// + /// a int array. if variable name does not exist dimensions are 0 0 + public unsafe int[] getNamedMatrixOfStringDimensions(string matrixName) + { + int iRows = 0; + int iCols = 0; + int[] iDim = new int[2]; + + readNamedMatrixOfString(matrixName, &iRows, &iCols, null, null); + + iDim[0] = iRows; + iDim[1] = iCols; + + return iDim; + } + //============================================================================= + /// + /// import SendScilabJob from C (see CallScilab.h) + /// + [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)] + private static extern int SendScilabJob([In]String job); + //============================================================================= + /// + /// import StartScilab from C (see CallScilab.h) + /// + [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)] + private static extern int StartScilab([In] String SCIpath, + [In] String ScilabStartup, + [In] Int32[] Stacksize); + //============================================================================= + /// + /// import TerminateScilab from C (see CallScilab.h) + /// + [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)] + private static extern int TerminateScilab([In] String ScilabQuit); + //============================================================================= + /// + /// import DisableInteractiveMode from C (see CallScilab.h) + /// + [DllImport(CALL_SCILAB_DLL, CharSet = CharSet.Ansi)] + private static extern void DisableInteractiveMode(); + //============================================================================= + /// + /// import createNamedMatrixOfString from C (see api_string.h) + /// + [DllImport(LIBSCILAB_DLL, CharSet = CharSet.Ansi)] + private static extern int createNamedMatrixOfString([In] String _pstName, + [In] int _iRows, [In] int _iCols, + [In] String[] _pstStrings); + //============================================================================= + /// + /// import createNamedMatrixOfDouble from C (see api_double.h) + /// + [DllImport(LIBSCILAB_DLL, CharSet = CharSet.Ansi)] + private static extern int createNamedMatrixOfDouble([In] String _pstName, + [In] int _iRows, [In] int _iCols, + [In] double[] _pdblReal); + //============================================================================= + /// + /// import readNamedMatrixOfString from C (see api_string.h) + /// + [DllImport(LIBSCILAB_DLL, CharSet = CharSet.Ansi)] + private unsafe static extern int readNamedMatrixOfString([In] String _pstName, + [Out] Int32* _piRows, [Out] Int32* _piCols, + [In, Out] int[] _piLength, + [In, Out] String[] _pstStrings); + //============================================================================= + /// + /// import readNamedMatrixOfDouble from C (see api_double.h) + /// + [DllImport(LIBSCILAB_DLL, CharSet = CharSet.Ansi)] + private unsafe static extern int readNamedMatrixOfDouble([In] String _pstName, + [Out] Int32* _piRows, [Out] Int32* _piCols, + [In, Out] Double[] _pdblReal); + //============================================================================= + } +} +//============================================================================= diff --git a/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/Properties/AssemblyInfo.cs b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..61c6d33 --- /dev/null +++ b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("example")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Scilab - DIGITEO")] +[assembly: AssemblyProduct("example")] +[assembly: AssemblyCopyright("Copyright © Scilab 2009")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("17e29de2-0c66-420c-931c-662ab2883fb8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/example.cs b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/example.cs new file mode 100644 index 0000000..8f775b2 --- /dev/null +++ b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/example.cs @@ -0,0 +1,127 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2009 - DIGITEO - Allan CORNET + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt + * + */ +//============================================================================= +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using DotNetScilab; +//============================================================================= +namespace example +{ + class example + { + /* + * A small example to call scilab from C# + * read & write matrix of double and matrix of string + */ + static void example_readwriteMatrixOfDouble(Scilab _objScilab) + { + //============================================================================= + // Send a command to scilab + // Here , we want to display SCI variable + _objScilab.sendScilabJob("disp(\'SCI = \');"); + _objScilab.sendScilabJob("disp(SCI);"); + //============================================================================= + double[] A = new double[] { 1, 2, 3, 4, 5, 6 }; + int mA = 2, nA = 3; + + // Write a matrix of double named in scilab + _objScilab.setNamedMatrixOfDouble("A", mA, nA, A); + + // display matrix of double by scilab + _objScilab.sendScilabJob("disp(\'A =\');"); + _objScilab.sendScilabJob("disp(A);"); + //============================================================================= + _objScilab.sendScilabJob("B = A + 1;"); + + // get dimensions of a named matrix of double + int[] DimB = _objScilab.getNamedMatrixOfDoubleDimensions("B"); + + // get named matrix of double + double[] B = _objScilab.getNamedMatrixOfDouble("B"); + + // display matrix of double from C# + Console.WriteLine(""); + Console.WriteLine("(C#) B ="); + for (int i = 0; i < DimB[0]; i++) + { + for (int j = 0; j < DimB[1]; j++) + { + Console.Write(B[j * DimB[0] + i] + " "); + } + + Console.WriteLine(""); + } + + // display matrix of double by scilab + _objScilab.sendScilabJob("disp(\'B =\');"); + _objScilab.sendScilabJob("disp(B);"); + //============================================================================= + } + + static void example_readwriteMatrixOfString(Scilab _objScilab) + { + //============================================================================= + string[] strA = new string[] { "Scilab", "The", "open", + "source", "for", "numerical", + "computation" , ":", ")"}; + int mstrA = 3, nstrA = 3; + + // Write a matrix of string named in scilab + _objScilab.setNamedMatrixOfString("string_A", mstrA, nstrA, strA); + + // display matrix of string by scilab + _objScilab.sendScilabJob("disp(\'string_A =\');"); + _objScilab.sendScilabJob("disp(string_A);"); + //============================================================================= + _objScilab.sendScilabJob("string_B = convstr(string_A,\'u\');"); + + // get dimensions of a named matrix of string + int[] DimstrB = _objScilab.getNamedMatrixOfStringDimensions("string_B"); + + // get named matrix of string + string[] strB = _objScilab.getNamedMatrixOfString("string_B"); + + Console.WriteLine(""); + Console.WriteLine("(C#) strB ="); + for (int i = 0; i < DimstrB[0]; i++) + { + for (int j = 0; j < DimstrB[1]; j++) + { + Console.Write(strB[j * DimstrB[0] + i] + " "); + } + + Console.WriteLine(""); + } + + // display matrix of string by scilab + _objScilab.sendScilabJob("disp(\'string_B =\');"); + _objScilab.sendScilabJob("disp(string_B);"); + //============================================================================= + } + //============================================================================= + static void Main(string[] args) + { + // start scilab engine + Scilab m_oSCilab = new Scilab(); + + example_readwriteMatrixOfDouble(m_oSCilab); + example_readwriteMatrixOfString(m_oSCilab); + + // close scilab engine + m_oSCilab.finish(); + } + //============================================================================= + } +} +//============================================================================= diff --git a/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/example.csproj b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/example.csproj new file mode 100644 index 0000000..01ef8b8 --- /dev/null +++ b/scilab/modules/call_scilab/examples/call_scilab/c#/dotnetsci/example/example.csproj @@ -0,0 +1,103 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {10B5A5BF-3204-4B47-91C0-7FA614EFA94A} + Exe + Properties + example + example + v3.5 + 512 + + + true + full + false + ..\..\..\..\..\..\..\bin\ + DEBUG;TRACE + prompt + 4 + true + + + pdbonly + true + ..\..\..\..\..\..\..\bin\ + TRACE + prompt + 4 + true + + + true + ..\..\..\..\..\..\..\bin\ + DEBUG;TRACE + full + x64 + prompt + true + + + ..\..\..\..\..\..\..\bin\ + TRACE + true + pdbonly + x64 + prompt + true + + + true + ..\..\..\..\..\..\..\bin\ + DEBUG;TRACE + full + x86 + prompt + true + + + ..\..\..\..\..\..\..\bin\ + TRACE + true + pdbonly + x86 + prompt + true + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + + {C3F0C7DC-82AD-405B-990A-BF2C5B7F46FF} + DotNetScilab + + + + + \ No newline at end of file diff --git a/scilab/modules/call_scilab/examples/readme.txt b/scilab/modules/call_scilab/examples/readme.txt index 9449453..ec6f1d3 100644 --- a/scilab/modules/call_scilab/examples/readme.txt +++ b/scilab/modules/call_scilab/examples/readme.txt @@ -1,4 +1,4 @@ -Examples about the Scilab <=> C/C++/Fortran languages +Examples about the Scilab <=> C/C++/C#/Fortran languages call_scilab/c : How to call scilab from C @@ -9,6 +9,8 @@ How to call scilab from C++ call_scilab/fortran : How to call scilab from fortran +call_scilab/C# : +How to call scilab from # -INRIA 2007 +DIGITEO - 2008 A.C