基本使用

  1. 新建类库项目 PdmsAddin
  2. 修改类库 AssemblyInfo.cs 文件,添加如下代码
1
[assembly: PMLNetCallable()]
  1. 新建类 PMLNetMain ,添加如下代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[PMLNetCallable()]
public class PMLNetMain
{
[PMLNetCallable()]
public PMLNetMain()
{

}

[PMLNetCallable()]
public void Assign(PMLNetMain that)
{

}

[PMLNetCallable()]
public void Start()
{
System.Windows.MessageBox.Show("Hello World!");
}
}
  1. 生成dll文件,并复制到 Pdms 根目录
  2. 新建 LoadMac.pmlmac 文件,内容如下
1
2
3
4
5
6
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()
!main.Start()
  1. 启动Pdms,将LoadMac.pmlmac文件拖入Pdms的CommandWindow可运行这段pml脚本

参数传递

实型

1
2
3
4
5
6
7
8
9
10
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Passing arguments of type Real
!a = 5
!b = 7
!sum = !main.Sum(!a,!b)
1
2
3
4
5
[PMLNetCallable()]
public double Sum(double a,double b)
{
return a + b;
}

数组

1
2
3
4
5
6
7
8
9
10
11
12
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Passing Array
!arraySrc[1] = 1
!arraySrc[2] = 2
!arraySrc[3] = 3
!sum2 = !main.SumArray(!arraySrc)
q var !sum2
1
2
3
4
5
6
7
8
9
10
[PMLNetCallable()]
public double SumArray(Hashtable items)
{
double sum = 0;
foreach(DictionaryEntry entry in items)
{
sum += (double)entry.Value;
}
return sum;
}

返回数组

1
2
3
4
5
6
7
8
9
10
11
12
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Passing Array and return Array
!arraySrc[1] = 1
!arraySrc[2] = 2
!arraySrc[3] = 3
!arrayRes = !main.IncArray(!arraySrc)
q var !arrayRes
1
2
3
4
5
6
7
8
9
10
11
12
[PMLNetCallable()]
public double IncArray(Hashtable items)
{
Hashtable result = new Hashtable();
int i = items.Count;
foreach(DictionaryEntry entry in items)
{
result[i] = (double)entry.Value + 1;
i--;
}
return result;
}

引用传递

1
2
3
4
5
6
7
8
9
10
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Passing by ref
!d = 2
!main.RefTest(!d)
q var !d
1
2
3
4
5
[PMLNetCallable()]
public void RefTest(ref double d)
{
d++;
}

非引用传递

1
2
3
4
5
6
7
8
9
10
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Passing by value
!dd = 2
!main.NotRefTest(!dd)
q var !dd
1
2
3
4
5
[PMLNetCallable()]
public void NotRefTest(double dd)
{
dd++;
}

字符串

1
2
3
4
5
6
7
8
9
10
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Passing string
!srcString = 'Hello World'
!success = !main.IsStringContains(!srcString)
q var !success
1
2
3
4
5
[PMLNetCallable()]
public bool IsStringContains(string src)
{
return src.Contains("Hello");
}

读取.NET属性

1
2
3
4
5
6
7
8
9
10
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Net properties
$P Get
!netProp = !main.NetProp()
q var !netProp
1
2
3
4
5
6
7
8
9
10
11
12
13
[PMLNetCallable()]
public class PMLNetMain
{
private double netProp = 3.14d;

[PMLNetCallable()]
public double NetProp
{
get => netProp;
set => netProp = value;
}

}

设置.NET属性

1
2
3
4
5
6
7
8
9
10
11
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P Net properties
$P Set
!main.NetProp(5.20)
!netProp = !main.NetProp()
q var !netProp
1
2
3
4
5
6
7
8
9
10
11
12
13
[PMLNetCallable()]
public class PMLNetMain
{
private double netProp = 3.14d;

[PMLNetCallable()]
public double NetProp
{
get => netProp;
set => netProp = value;
}

}

对象

  在Models目录下新建ParamObject类文件,在pmlmac文件中需要导入命名空间using NameSpace 'PdmsAddin.Models'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
namespace PdmsAddin.Models
{
[PMLNetCallable()]
public class ParamObject
{
public double NotPmlNetProp{get;set;}

[PMLNetCallable()]
public ParamObject()
{
NotPmlNetProp = 15d;
}

[PMLNetCallable()]
public void Assign(ParamObject that)
{

}
}
}
1
2
3
4
5
6
7
8
9
10
11
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
using NameSpace 'PdmsAddin.Models'
!main = object PMLNetMain()

$P Passing PmlNet object
!paramObject = object ParamObject()
!notPmlNetProp = !main.ParamObjectTest(!paramObject)
q var !notPmlNetProp
1
2
3
4
5
[PMLNetCallable()]
public double ParamObjectTest(ParamObject paramObject)
{
return paramObject.NotPmlNetProp;
}

函数

函数重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P OverMethod1
!strResA = !main.OverMethod()
q var !strResA

$P OverMethod2
!strResB = !main.OverMethod(5.20)
q var !strResB

$P OverMethod3
!strResC = !main.OverMethod(5.20,13.14)
q var !strResC

$P OverMethod4
!strResD = !main.OverMethod('HelloWorld')
q var !strResD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[PMLNetCallable()]
public string OverMethod()
{
return "0";
}

[PMLNetCallable()]
public string OverMethod(double a)
{
return "0" + " " + a;
}

[PMLNetCallable()]
public string OverMethod(double a,double b)
{
return "0" + " " + a + " " + b;
}

[PMLNetCallable()]
public string OverMethod(string a)
{
return "0" + " " + a;
}

ToString函数

1
2
!res = !main.string()
q var !res
1
2
3
4
public override string ToString()
{
return "Some Text";
}

构造函数重载

1
2
3
4
!main = object PMLNetMain(5)

$P Constructor overloading
q var !main.Number()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[PMLNetCallable()]
public double Number{get;set;}

[PMLNetCallable()]
public PMLNetMain(double number)
{
// 在执行完有参构造后,还是会执行无参的构造,因此number初始化参数会被覆盖,需要再Assign函数中重新初始化
Number = number;
}

[PMLNetCallable()]
public PMLNetMain()
{

}

[PMLNetCallable()]
public void Assign(PMLNetMain that)
{
this.Number = that.Number;
}

事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

$P pmlobject
!pmlobj = object netcallback()

$P subscribe to net event
!handle = !main.addeventhandler('PmlNetEvent', !pmlobj, 'callback')

$P raise net event
$P
!main.RaisePmlNetEvent()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[PMLNetCallable()]
public class PMLNetMain
{
[PMLNetCallable()]
public event PMLNetDelegate.PMLNetEventHandler PmlNetEvent;

[PMLNetCallable()]
public PMLNetMain()
{

}

[PMLNetCallable()]
public void Assign(PMLNetMain that)
{

}

[PMLNetCallable()]
public void RaisePmlNetEvent()
{
ArrayList args = new ArrayList();
args.Add(string.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Now));
if(PmlNetEvent != null)
{
PmlNetEvent(args);
}
}

}

  在PMDS根目录下找到PMLLIB目录,新建pmlnet/objects/netcallback.pmlobj文件,内容如下:

1
2
3
4
5
6
7
define object netcallback
endobject

define method .callback(!array is ARRAY)
!args = 'Current date:' + !array[0]
$P $!args
endmethod

添加netcallback.pmlobj文件后,需要在PDMS的CommandWindow窗口执行如下指令,刷新缓存,再执行LoadMac.pmlmac文件

  1. pml index
  2. pml rehash all

异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!main = object PMLNetMain()

!main.ThrowNetException()
handle(1000,0)
$P Net Exception
endhandle

!main.ThrowPmlNetException()
handle(1000,1)
$P PMLNET Exception
endhandle

!a = 5
!b = 0
!res = !main.CalcMethod(!a,!b)
q var !res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[PMLNetCallable()]
public class PMLNetMain
{
[PMLNetCallable()]
public PMLNetMain()
{

}

[PMLNetCallable()]
public void Assign(PMLNetMain that)
{

}

[PMLNetCallable()]
public void ThrowNetException()
{
throw new Exception();
}

[PMLNetCallable()]
public void ThrowPmlNetException()
{
throw new PMLNetException(1000,1,"Exception from Net");
}

[PMLNetCallable()]
public double CalcMethod(double a,double b)
{
// For this sample only
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
int res = 0;

try
{
res = (int)a / (int)b;
}
catch(Exception ex)
{
throw new PMLNetException(1000,1,ex.Message);
}
return res;
}

}

对象拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import 'PdmsAddin'
handle (1000,0)
endhandle
using NameSpace 'PdmsAddin'
!a = object PMLNetMain()

!a.TestVar(1)

!avar = !a.TestVar()
$P $!avar

!b = !a

!bvar = !b.TestVar()
$P $!bvar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[PMLNetCallable()]
public class PMLNetMain
{
[PMLNetCallable()]
public PMLNetMain()
{

}

[PMLNetCallable()]
public void Assign(PMLNetMain that)
{
// PML中对象拷贝,需要.NET手动处理每个属性的状态复制
this.TestVar = that.TestVar;
}
}