脚本使用Python语言编写(Python 2.7),通过IronPython与sDAP交互。Python 2.7.18 documentation,IronPython .NET Integration documentation。
使用录制模式会自动将用户操作录制成脚本,然后可以运行脚本重复执行这些操作。录制脚本步骤:
使用脚本可以自动执行重复的操作。执行脚本步骤:
除了通过录制生成脚本,用户也可以手动编辑脚本。任何合法的Python脚本都可以在sDAP中执行。例如下面的脚本计算一个数的阶乘:
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
def main():
num = 5
result = factorial(num)
dap.sMsgBox("The factorial of " + str(num) + " is " + str(result))
执行上面的脚本,得到下面的结果:
也可以在其他编辑器中编辑脚本,然后在sDAP中执行。
在脚本中使用dap全局变量访问sDAP。
(为了与sDAP中的其他方法区分,脚本使用的方法都以s开头。从技术上讲,所有的公共方法和属性都可以通过脚本调用。):
说明:打开sef文件。sDAP使用异步方式加载通道,使用该方法打开文件时必须等通道加载完成后再执行其他操作,或者订阅事件FileLoadCompletedEvent执行其他操作。
IronPython示例:
def callBack(str):
dap.sSwitchActiveDocument(str)
dap.ActiveDocument.sClearSelections()
dap.ActiveDocument.sChannel(0)
dap.ActiveDocument.sOpenZoomWindow()
dap.FileLoadCompletedEvent -= callBack
def main():
dap.FileLoadCompletedEvent += callBack
dap.sOpen('D:\\TestData\\test.sef')
CPython示例:
class CallableHandler(object):
def __init__(self):
self.value = None
def __call__(self, str):
dap.sSwitchActiveDocument(str)
dap.ActiveDocument.sClearSelections()
dap.ActiveDocument.sChannel(0)
dap.ActiveDocument.sOpenZoomWindow()
def main():
handler = CallableHandler()
dap.FileLoadCompletedEvent += handler
dap.sOpen('D:\\TestData\\Test.sef')
dap.sOnFileOpened('Test.sef')
IronPython也支持这种写法,如果使用IronPython执行代码,推荐使用上面的写法。
参数:file:文件路径。
示例:dap**.**sOpen**(**'D:\\TestData\\test.sef'**)**
说明:关闭打开的文件。
参数:name:文档名(通常是文件名)。
dap.sClose('test.sef')
说明:弹出提示信息。
参数:message:弹出的信息。
dap.sMsgBox('Hello sDAP 2022.')
说明:记录日志。
说明:使用索引切换当前激活的文档。
参数:index:当前文档在所有打开文档列表中的索引。
dap.sSwitchActiveDocument(2)
说明:使用文档名称切换当前激活的文档。
参数:name:文档名。
dap.sSwitchActiveDocument('test.sef')
说明:使用文档名称选择文档。使用该方法选择文档时,选择的文档不必是当前激活的文档就可以应用所有的脚本。
参数:name:文档名。
dap.sDocument('test.sef').sClearSelections()
dap.sDocument('test.sef').sChannel(1)
dap.sDocument('test.sef').sDeleteSelectedChannels()
dap.sDocument('test.sef').sSave()
说明:选择bin。
参数:bin:Bin名称(S1、S2……)。
dap.sSelectBin('S1')
说明:清除Bin的数据。
参数:bin:Bin名称。
dap.sEmptyBin('S1')
说明:清除所有Bin的选择状态。
说明:在频域分析窗体中显示通道的数据。
参数:channels: 通道数据数组。
说明:在时域分析窗体中显示通道的数据。
参数:channels:通道数据数组。
dap的属性用于获取当前实例下的激活的文档,进行切换文档的操作。
ActiveDocument
说明:当前激活的文档。
下面的代码使用两个文件的通道进行跨文件的通道选取并求传递函数:
dap.sSwitchActiveDocument(1) #切换到打开的文档1,(序号索引从0开始,实际为从左第2个sef标签),
dap.ActiveDocument.sSetCrossFileOperation('TransFunc1DIsChecked') #激活Transfer Function 1D的功能
dap.ActiveDocument.sClearSelections() #清除选中的列表
dap.ActiveDocument.sSelectYChannels(1) # 切选中索引为1作为Y通道,
dap.sSwitchActiveDocument(0) #换到打开的文档0,(最左边的第一个sef标签),
dap.ActiveDocument.sSelectXChannels(0) # 选中索引为0作为X通道,
dap.ActiveDocument.sTransFunc(0) # 切选中索引为1作为Y通道,
dap.ActiveDocument.sChildren(0)
dap.ActiveDocument.sChildren(0)
dap.ActiveDocument.sResetCursor()
sChildren(0)
待完成
ActiveResultWindow
待完成
ActiveZoomChild
待完成
针对通道执行的方法在调用前都需要调用sChannel(int index)或sChannel(string channelName)选择通道。
说明:根据索引选择通道加入到选中列表,后续对该列表中全部通道进行处理。
参数:index:通道的索引。
返回值:Channel。对返回通道的任何修改都会更新到原通道中。
例:删除文件中的若干通道,比如删除0-3以及5通道:
dap.sSwitchActiveDocument(0)
dap.ActiveDocument.sClearSelections()
dap.ActiveDocument.sChannel(0)
dap.ActiveDocument.sChannel(1)
dap.ActiveDocument.sChannel(2)
dap.ActiveDocument.sChannel(3)
dap.ActiveDocument.sChannel(5)
dap.ActiveDocument.sDeleteSelectedChannels()
dap.ActiveDocument.sChannel(0)
dap.ActiveDocument.sResetCursor()
说明:根据通道名称选择通道。
参数:channelName:通道名称。
返回值:Channel。对返回通道的任何修改都会更新到原通道中。
dap.ActiveDocument.sChannel('Load.1')
说明:根据通道索引获取通道数据。
参数:index: 通道索引。
返回值:Channel。对返回通道的任何修改都会更新到原通道中。
channel = dap.ActiveDocument.sGetChannel(1)
说明:根据通道名称获取通道数据。
参数:channelName: 通道名称。
返回值:Channel。对返回通道的任何修改都会更新到原通道中。
channel = dap.ActiveDocument.sGetChannel('Velocity')
说明:新建一个通道实例。
说明:返回指定索引的通道数据的副本。
参数:index:通道的索引。
返回值:Channel:通道数据。该数据为指定索引的通道数据的副本,对该通道数据的修改不会影响原始通道数据。
说明:返回指定索引的通道数据的副本。
参数:channelName:通道的名称。
返回值:Channel:通道数据。该数据为指定索引的通道数据的副本,对该通道数据的修改不会影响原始通道数据。
说明:修改通道名称。
参数:index:通道的索引。channelName:新通道名称。
说明:修改通道Y轴数据的单位。
参数:index:通道的索引。unitY:新单位。
说明:修改通道的量程。
参数:index:通道的索引。scale:新量程。
说明:将通道数据保存到Bin。
参数:channel:通道数据。dataType:通道数据类型,0 频域数据,1 时域数据。
示例:读取CSV文件中的数据并保存到Bin。
# ----------------------------------------------------------
# -- Python Script File
# -- Created on Monday, August 15, 2022 5:37 PM
# ----------------------------------------------------------
import sys
import clr
import csv
import System
# -- Use tab indentation by default (8 spaces) --
# -- Beginning of user code --
# -- This is the entry point of the script, do not modify the method name --
def main():
file_csv = open('C:\\Users\\Anders.Chen\\Downloads\\FRS.csv', 'rb')
data_csv = csv.reader((line.replace('\0','') for line in file_csv), delimiter=",")
list_csv = list(data_csv)
channel = dap.ActiveDocument.sNewChannel()
# The first row is the axis name, The second row is the unit
channel.ChannelName = list_csv[0][1]
channel.NameX = list_csv[0][0]
channel.UnitX = list_csv[1][0]
channel.UnitY = list_csv[1][1]
size = len(list_csv)
dataX = System.Array.CreateInstance(float, size - 2)
dataY = System.Array.CreateInstance(float, size - 2)
# Data starts from the third row
index = 0
for row in list_csv[2:]:
dataX[index] = float(row[0])
dataY[index] = float(row[1])
index += 1
channel.DataX = dataX
channel.DataY = dataY
dap.ActiveDocument.sSaveToBin(channel)
说明:在频域分析窗体中显示通道的数据。
参数:channels: 通道数据数组。
说明:在时域分析窗体中显示通道的数据。
参数:channels:通道数据数组。
说明:移动通道,将指定的通道移动到目标索引位置的通道后面。
参数:targetPosition: 目标索引;indexes:通道索引数据。
例:将索引 8-11的通道移到索引1通道后面
dap.ActiveDocument.sMoveChannel(1,8,9,10,11)
说明:选择所有通道。
参数:无
说明:取消所有选择。
参数:无
说明:保存文件。
参数:无
说明:显示另存为对话框。
参数:ext:另存的文件格式。
说明:将选择的通道数据保存到指定的Bin。
参数:bin:Bin名称(S1、S2…)。
说明:删除选择的通道。
说明:将选择的Bin的数据添加到通道列表中。
说明:放大显示通道信息。
说明:关闭当前激活的子窗体。
说明:对所选的通道Y轴数据应用数学表达式。
参数:expression:数学表达式。使用y代替Y轴数据。
dap.ActiveDocument.sChannel(8)
dap.ActiveDocument.sApplyMathFormulas('y*5+30')
说明:对所选的通道Y轴数据按比例缩放。
参数:缩放系数。
说明:对所选的通道Y轴数据归一化Y轴数据。
说明:局部平滑。
参数:start:开始时间。end:结束时间。depth:深度。
说明:对所选的通道Y轴数据首尾平滑。
参数:start:开始时间。end:结束时间。range:类型 0首部平滑、1 尾部平滑、2 首尾平滑。
说明:对所选的通道Y轴数据移除均值。
说明:对所选的通道Y轴数据去趋势项(N阶)。
参数:order:阶数。
说明:对所选的通道Y轴数据平滑处理。
参数:points:平滑的点数。
说明:对所选的通道Y轴数据进行FFT 滤波。
参数:highPassFreq:高通频率。lowPassFreq:低通频率。bPass:带阻滤波。bNotch:陷波滤波。
说明:对当前激活的sef文件设定新开始时间。
参数:startTime:开始时间点。
说明:对当前激活的sef文件设定新结束时间。
参数:endTime:结束时间点。
说明:对当前激活的sef文件裁切数据。
参数:newStart:开始时间。newEnd:结束时间。invert:反选开始时间和结束时间。
说明:对当前激活的sef文件进行时间尺度压缩。
参数:factor:压缩系数,2表示压缩两倍,30s的时间长度将变成15s。
说明:对当前激活的sef文件重采样。
参数:newSampleRate:新采样率。
说明:二元操作选择Y轴数据,括号内以逗号隔开表示数组。
参数:indexes:通道索引。
例:求两个文件的传递函数
dap.sSwitchActiveDocument(1)
dap.sSelectTab(2)
dap.ActiveDocument.sSetCrossFileOperation('TransFunc1DIsChecked')
dap.ActiveDocument.sClearSelections()
dap.ActiveDocument.sSelectYChannels(0,1,2)
dap.sSwitchActiveDocument(0)
dap.ActiveDocument.sSelectXChannels(1,0,2)
dap.ActiveDocument.sTransFunc(0) # 0 表示1维传递函数,1表示3维传递函数
dap.ActiveDocument.sChildren(0)
dap.ActiveDocument.sResetCursor()
说明:二元操作选择X轴数据。
参数:indexes:通道索引。
说明:加减乘除运算。调用该操作前需要先调用sSelectYChannels和sSelectXChannels选择两组通道。
参数:operation:+、-、*、/。
说明:最多选择8个通道的数据应用数学表达式计算。
参数:expression:数学表达式。在表达式中使用a-h表示通道的数据。
dap.ActiveDocument.sChannel(10)
dap.ActiveDocument.sChannel(12)
dap.ActiveDocument.sAnalysisMathExpression('a*9-b+100')
说明:PSD。
参数:0 Autopower Spectrum, 1 Autopower Spectrum Linear, 2 PSD, 3 ESD。
说明:FFT。
参数:type:0 FFT,1 FFT Slice Line,2 FFT Slice Log。
说明:传函。
参数:type:0 1D,1 3D。
说明:FRS 误差。
参数:type:0 FRS Line Error,1 FRS Log Error。
说明:合并通道。通道数必须和当前打开的文件的通道数相同。
参数:file:文件路径。
说明:将文件的通道添加到当前通道列表中。
参数:file:文件路径。
说明:激活指定索引的子窗体。
参数:index:子窗体的索引。
dap.ActiveDocument.sChildren(0)
说明:设置跨文件操作类型。
参数:name:操作类型。可以设置的操作类型:XY、FrsLogError、FrsLinError、CPsd、Transf1D、Transf3D、Coherence、PsdError、Correlation、AddSub、MultiplyDivide。
dap.ActiveDocument.sSetCrossFileOperation('Transf1D')
通道数据类。
属性:
ChannelName | string | 通道名称 |
IsSelected | bool | 是否选中 |
SampleRate | double | 采样率 |
Scale | float | 量程 |
NameX | string | X轴名称 |
NameY | string | Y轴名称 |
UnitX | string | X轴数据单位 |
UnitY | string | Y轴数据单位 |
DataX | double[] | X轴数据 |
DataY | double[] | Y轴数据 |
说明:当前窗体的数据保存到Bin。
说明:微分。
说明:积分。
参数:type:y Y轴;x X轴。highPassFreq:高通频率。lowPassFreq:低通频率。offset:偏移量。
说明:应用数学表达式。
参数:expression:数学表达式。
说明:平滑。
参数:points:点数。
频域子窗体的方法
说明:比例缩放Y轴的值。
说明:归一化Y轴的值。
说明:去量纲。
说明:去均值。
说明:去趋势值。
参数:order:阶数。
说明:计算Y轴的绝对值。
说明:FFT滤波。
参数:highPassFreq:高通频率。lowPassFreq:低通频率。bPass:带阻滤波。
sDAP 2022支持三种格式的命令行参数,在系统启动时执行打开文件、显示数据、执行脚本等操作。
打开文件。
命令格式:sDAP.exe file 文件1 文件2 …
打开文件是默认操作,因此file 可以省略,直接写 sDAP.exe 文件名。sDAP 支持同时打开多个文件,文件路径之间使用空格分隔,如果文件路径中包含空格则使用双引号括起来。
示例:sDAP.exe D:\Code\TestData\test.sef D:\Code\TestData\DamperTemperature.sdat
使用参数中的数据显示图表。
命令格式:sDAP.exe data -t 数据类型 -r 采样率 -c 通道名列表 -u 单位列表 -s 量程 -x X轴数据列表 -y Y轴数据列表
参数 | 说明 |
-t或–type | 数据类型,0 时域数据,1 频域数据。 |
-r或–rate | 采样率 |
-c或–channels | 通道名列表,通道名之间使用空格分隔。 |
-u或–units | Y轴数据单位列表,单位之间使用空格分隔。 |
-s或–scales | 量程列表,量程之间使用空格分隔。 |
-x或–dataX | X轴数据临时文件列表。对于时域数据该参数可选。先将数据保存成使用逗号分隔的文本文件中,然后将文件名作为参数传到sDAP。 |
-y或–dataY | Y轴数据临时文件列表。先将数据保存成使用逗号分隔的文本文件中,然后将文件名作为参数传到sDAP。 |
参数中的通道名、单位、量程、X轴数据、Y轴数据的数量必须相等。
示例:sDAP.exe data -t 0 -c Actuator1 -u mRad -r 204.8 -s 0.01 -y C:\Users\Anders.Chen\AppData\Local\Temp\tmp6BE5.tmp
打开文件并执行脚本。
命令格式:sDAP.exe script -f 文件名 -s 脚本文件名
参数 | 说明 |
-f或–file | 数据文件路径 |
-s或–script | 脚本文件路径 |
示例:sDAP.exe script -f D:\Code\TestData\test.sef -s C:\Users\Anders.Chen\AppData\Roaming\sDAP\test.py