8
C# goi maple package _ Đỗ Đình Thủ 2013 TẠO PACKAGE TRONG MAPLE, GỌI TRONG C# Bước 1: Tạo package trong Maple........................................................................................................ 2 Bước 2: Sử dụng trong C#.............................................3

C# Goi Maple Package

Embed Size (px)

Citation preview

Page 1: C# Goi Maple Package

C# goi maple package _ Đỗ Đình Thủ 2013

TẠO PACKAGE TRONG MAPLE, GỌI TRONG C#

Bước 1: Tạo package trong Maple................................................................................................................2

Bước 2: Sử dụng trong C#.............................................................................................................................3

Page 2: C# Goi Maple Package

C# goi maple package _ Đỗ Đình Thủ 2013

Bước 1: Tạo package trong MapleVí dụ muốn tạo package TEST trong đó có “hàm” Tong(a,b,c), trả về tổng a,b,c.Package tạo ra được lưu trong thư mục nào đó, vd : D:/TAM. Ta soạn file TEST.mw bình thường sau đó dùng lệnh lưu lại, nó sẽ thành file TEST.m lưu trong D:/TAM/TEST.m, copy file này vào thư mục Lib của Maple là ok. Chú ý nếu bạn cài Maple 12 và Maple 16 thì copy nó vào Maple 16 tương ứng.

file : TEST.mw> TEST := table():> TEST[init] := proc() end: > TEST[Tong] := proc (a,b,c) local tong; tong:= a + b + c; printf("Tong cua %A , %A va %A la %A :",a,b,c, tong);end proc:#gõ Enter#gõ Enter sau lệnh save#Tạo thư mục D:/TAM trước> save(TEST,"D:/TAM/TEST.m");> restart; #gõ Enter

Copy : file TEST.m trong D:/TAM vào …Maple 16/Lib.Xong rồi đó, test thử>

Tong cua 2 , 3 va 4 la 9 :

--Ngon lành, xong bước 1.

Page 3: C# Goi Maple Package

C# goi maple package _ Đỗ Đình Thủ 2013

Bước 2: Sử dụng trong C#Tạo Project, ConnectToMaple

ConnecToMaple sẽ sử dụng package ‘TEST’ ta mới tạo, khi nhập 2,3,4 nó sẽ tính ra tổng (hi vọng ra 9).Thiết kế giao diện tương tự như hình :

- txtInput : nhập tham số vào- txtOutput : ra kết quả- tinh : Tính kết quả

Các lớp :- MapleEngine.cs : cái này dùng link với Maple

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.ComponentModel;

namespace ConnectToMaple //chú ý đổi tên namespace trùng tên project nếu muốn dùng lại{ static class MapleEngine { public delegate void TextCallBack(IntPtr data, int tag, IntPtr output); public delegate void ErrorCallBack(IntPtr data, IntPtr offset, IntPtr msg); public delegate void StatusCallBack(IntPtr data, IntPtr used, IntPtr alloc, double time); public delegate IntPtr ReadLineCallBack(IntPtr data, IntPtr debug); public delegate long RedirectCallBack(IntPtr data, IntPtr name, IntPtr mode); public delegate IntPtr StreamCallBack(IntPtr data, IntPtr stream, int nargs, IntPtr args); public delegate long QueryInterrupt(IntPtr data); public delegate IntPtr CallBackCallBack(IntPtr data, IntPtr output);

public struct MapleCallbacks { public TextCallBack textCallBack; public ErrorCallBack errorCallBack; public StatusCallBack statusCallBack; public ReadLineCallBack readlineCallBack; public RedirectCallBack redirectCallBack; public StreamCallBack streamCallBack; public QueryInterrupt queryInterrupt; public CallBackCallBack callbackCallBack; }

[DllImport(@"maplec.dll")] public static extern IntPtr StartMaple(int argc, String[] argv, ref MapleCallbacks cb, IntPtr data, IntPtr info, byte[] err); [DllImport(@"maplec.dll")] public static extern IntPtr EvalMapleStatement(IntPtr kv, byte[] statement); [DllImport(@"maplec.dll")] public static extern IntPtr IsMapleStop(IntPtr kv, IntPtr obj); [DllImport(@"maplec.dll")] public static extern void StopMaple(IntPtr kv); } }

Page 4: C# Goi Maple Package

C# goi maple package _ Đỗ Đình Thủ 2013

- Xử lý chính : Form1.csTạo link với Maple ngay sau khi tạo Formpublic Form1() { InitializeComponent();//load Maple MapleEngine.MapleCallbacks cb; byte[] err = new byte[2048]; IntPtr kv; String[] argv = new String[2]; argv[0] = "maple"; argv[1] = "-A2"; cb.textCallBack = cbText; cb.errorCallBack = cbError; cb.statusCallBack = cbStatus; cb.readlineCallBack = null; cb.redirectCallBack = null; cb.streamCallBack = null; cb.queryInterrupt = null; cb.callbackCallBack = null;

try { kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; }

if (kv.ToInt64() == 0) { MessageBox.Show("Fatal Error, could not start Maple: " + System.Text.Encoding.ASCII.GetString(err, 0, Array.IndexOf(err, (byte)0)), "Lỗi", MessageBoxButtons.OK); return; } //Load package mình vừa viết, tên là TEST try { MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes("with(TEST):")); } catch (Exception) { } }

Các hàm khác cho MapleEngineXuất kết quả public void cbText(IntPtr data, int tag, IntPtr output) {//Xuất kết quả ra ngoài txtOutput.Text = Marshal.PtrToStringAnsi(output); }

Các hàm khác, không hiểu lắm nên mình để trống public static void cbError(IntPtr data, IntPtr offset, IntPtr msg) { string s = Marshal.PtrToStringAnsi(msg); }

public static void cbStatus(IntPtr data, IntPtr used, IntPtr alloc, double time) {

}

Tới hàm tính theo input private void Tinh_Click(object sender, EventArgs e) { MapleEngine.MapleCallbacks cb; byte[] err = new byte[2048]; IntPtr kv;

String[] argv = new String[2]; argv[0] = "maple";

Page 5: C# Goi Maple Package

C# goi maple package _ Đỗ Đình Thủ 2013

argv[1] = "-A2";

cb.textCallBack = cbText; cb.errorCallBack = cbError; cb.statusCallBack = cbStatus; cb.readlineCallBack = null; cb.redirectCallBack = null; cb.streamCallBack = null; cb.queryInterrupt = null; cb.callbackCallBack = null;

try { kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); try { //sử dụng hàm Tong trong package TEST String expr = "Tong("; expr += txtInput.Text; expr += ");"; IntPtr val = MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes(expr)); } catch (Exception) { MessageBox.Show("Không thể load Maple", "Lỗi", MessageBoxButtons.OK); } } catch (Exception) { } }

Code hoàn chỉnh Form1.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;

namespace ConnectToMaple{ public partial class Form1 : Form { public Form1() { InitializeComponent();//load Maple MapleEngine.MapleCallbacks cb; byte[] err = new byte[2048]; IntPtr kv; String[] argv = new String[2]; argv[0] = "maple"; argv[1] = "-A2"; cb.textCallBack = cbText; cb.errorCallBack = cbError; cb.statusCallBack = cbStatus; cb.readlineCallBack = null; cb.redirectCallBack = null; cb.streamCallBack = null; cb.queryInterrupt = null; cb.callbackCallBack = null;

try { kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; }

if (kv.ToInt64() == 0)

Page 6: C# Goi Maple Package

C# goi maple package _ Đỗ Đình Thủ 2013

{ MessageBox.Show("Fatal Error, could not start Maple: " + System.Text.Encoding.ASCII.GetString(err, 0, Array.IndexOf(err, (byte)0)), "Lỗi", MessageBoxButtons.OK); return; } //Load package mình vừa viết, tên là TEST try { MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes("with(TEST):")); } catch (Exception) { } }

public void cbText(IntPtr data, int tag, IntPtr output) {//Xuất kết quả ra ngoài txtOutput.Text = Marshal.PtrToStringAnsi(output); }

public static void cbError(IntPtr data, IntPtr offset, IntPtr msg) { string s = Marshal.PtrToStringAnsi(msg); }

public static void cbStatus(IntPtr data, IntPtr used, IntPtr alloc, double time) {

}

private void Tinh_Click(object sender, EventArgs e) { MapleEngine.MapleCallbacks cb; byte[] err = new byte[2048]; IntPtr kv;

String[] argv = new String[2]; argv[0] = "maple"; argv[1] = "-A2";

cb.textCallBack = cbText; cb.errorCallBack = cbError; cb.statusCallBack = cbStatus; cb.readlineCallBack = null; cb.redirectCallBack = null; cb.streamCallBack = null; cb.queryInterrupt = null; cb.callbackCallBack = null;

try { kv = MapleEngine.StartMaple(2, argv, ref cb, IntPtr.Zero, IntPtr.Zero, err); try { //sử dụng hàm Tong trong package TEST String expr = "Tong("; expr += txtInput.Text; expr += ");"; IntPtr val = MapleEngine.EvalMapleStatement(kv, Encoding.ASCII.GetBytes(expr)); } catch (Exception) { MessageBox.Show("Không thể load Maple", "Lỗi", MessageBoxButtons.OK); } } catch (Exception) { } }

}}

Kết quả

Page 7: C# Goi Maple Package

C# goi maple package _ Đỗ Đình Thủ 2013

May quá đúng rồi.

@Tham khảo : http://www.mapleprimes.com/posts/38048-Using-OpenMaple-With-C