Script - Calling a Python function from VBS

In the DIAdem Script panel, create one VBScript (VBS) and one Python script.   Save them both in the same folder.  

Python.VBS


'-------------------------------------------------------------------------------
'-- VBS script file Python.VBS
'-- Author:   Mechatronic Solutions LLC
'             Mark W Kiehl
'             www.SavvyDiademSolutions.com
'             www.MechatronicSolutionsLLC.com
'-- License:  http://www.savvydiademsolutions.com/license.php
'-- Comment: 
'-------------------------------------------------------------------------------
Option Explicit  
Call LogFileDel()

'Create internal data in the Data Portal.
Call bCreateInternalData()

'Configure references to the time and a numeric channels
Dim oChnX, oChnY
Set oChnX = Data.Root.ChannelGroups(1).Channels("Time")
Set oChnY = Data.GetChannel("[1]/Numeric")

'You cannot pass local variables to a function via ScriptStart().  
'However, the Python script Python.PY has access to global DIAdem variables. 
T1 = "AnalyzeChnData('" & oChnX.GetReference(eRefTypeNameName) & "','" & oChnY.GetReference(eRefTypeNameName) & "')"
'Call LogFileWrite("T1 = '" & T1 & "'")

B1 = False: T2 = ""
'ScriptStart() will run the script Python.PY
Call ScriptStart(CurrentScriptPath & "Python.PY")

'Look at what the Python.PY script has done..
Call LogFileWrite("ChnY property 'AnalyzeChnData' = '" & oChnY.Properties("AnalyzeChnData").Value & "'")
Call LogFileWrite("ChnY property 'Result~Statistics~ExtremeValues~Maximum' = '" & oChnY.Properties("Result~Statistics~ExtremeValues~Maximum").Value & "'")
Call LogFileWrite("T2 = '" & T2 & "'")
If B1 = True Then
  Call LogFileWrite("The execution of the Python function AnalyzeChnData() was successful.")
Else
  Call LogFileWrite("The execution of the Python function AnalyzeChnData() was NOT successful.")
End If


Function bCreateInternalData()
  bCreateInternalData = False
  Dim oGrp, oChn, oChnX, oChnDateTime, oChnY, sChn, oUsiLocal, dStartTime, oChnWf
  Dim oElementList, oElement
  Call Data.Root.Clear
  Set oGrp = Data.Root.ChannelGroups.Add("Group1")
  Set oChnX = oGrp.Channels.Add("Time",DataTypeChnFloat64)
  Call ChnLinGen(oChnX,0,500,100,"s")
  Call Data.Move(oChnX,oGrp.Channels(),1)
  'Create a Date/Time channel from oChnX.
  Set oChnDateTime = oGrp.Channels.AddChannel(oChnX)
  oChnDateTime.Name = "DateTime"
  'Define the datetime offset. 
  Set oUsiLocal = CreateTime(Year(Now), Month(Now), Day(Now), Hour(Now), Minute(Now), Second(Now), 0, 0, 20000)  
  'Call LogFileWrite("OffsetStartTime =  " & Str(oUsiLocal.GetUTC.SecondsFrom0000 + oUsiLocal.Fraction,"#dd-ttt-yyyy hh:nn:ss,ffffff"))
  'Convert the numeric channel oChnDateTime to a channel data type of time (date/time).  
  Call ChnNumericToTime(oChnDateTime, oUsiLocal.GetUTC.SecondsFrom0000 + oUsiLocal.Fraction, False)
  'Call LogFileWrite("DateTime channel first value =  " & Str(oChnDateTime.Values(1),"#dd-ttt-yyyy hh:nn:ss,ffffff"))
  'Create a numeric channel
  sChn = ChnLinGenImp("Numeric",100,0.0,1.0/33.0,"s")
  Set oChnY = Data.GetChannel(sChn)
  Call ChnValExpand(oChny)
  'Call LogFileWrite("oChnY = '" & oChnY.GetReference(eReferenceNameName) & "'")
  Set oChnY.XRelation = oChnX
  'Create a waveform channel
  Set oChnWf = oGrp.Channels.AddChannel(oChnY,,true): oChnWf.Name = "Waveform"
  Call ChnToWfChn(oChnDateTime, oChnWf, False, "WfXAbsolute")
  'Create a numeric channel from the waveform channel oChnWf
  Set oChnY = oGrp.Channels.AddChannel(oChnWf): oChnY.Name = "NumericFromWf"
  Set oElementList = WfChnToChn(oChnY, True, "WfXAbsolute")
  oElementList.Item(1).Name = "DateTimeNumericFromWf"
  bCreateInternalData = True
End Function  'bCreateInternalData()

Python.PY


# --------------------------------------------------------------------
# -- Python Script File
# -- Author:  Mechatronic Solutions LLC
#             Mark W Kiehl
#             www.SavvyDiademPySolutions.com
#             www.MechatronicSolutionsLLC.com
# -- License: http://www.savvydiademsolutions.com/license.php
# -- Comment: .
# --------------------------------------------------------------------
from DIAdem import Application as dd

if False:
    import DIAdem_CodeCompletion as dd
import sys
from platform import python_version
from pathlib import Path
import importlib
from datetime import datetime

print('Script "' + dd.CurrentScriptName + '"  Python version ' + python_version())


# --------------------------------------------------------------------

def AnalyzeChnData(sChnX, sChnY):
    oChnX = dd.Data.GetChannel(sChnX)
    oChnY = dd.Data.GetChannel(sChnY)
    iStatsSelection = dd.eStatsMinimum + dd.eStatsMaximum + dd.eStatsArithmeticMean + dd.eStatsSquareMean + dd.eStatsGeometricMean
    dd.ChnStatisticsChannelCalc(oChnY, iStatsSelection,  None,  None, False, False, False, "NameName")
    #The statistics max, min, mean, median, .. were added as properties to ChnY
    #Adding properties to a channel can be a useful way to pass results back to VBS information.
    dtNow = datetime.now()
    oChnY.Properties.Add('AnalyzeChnData', dtNow)
    #Assign a value to a DIAdem global variable
    dd.T2 = 'Fn AnalyzeChnData() executed on the channels ' + oChnX.GetReference(dd.eRefTypeNameName) + ' and ' + oChnY.GetReference(dd.eRefTypeNameName)
    dd.B1 = True


# Execute the DIAdem command passed by Python.VBS within the DIAdem global variable T1
exec(dd.T1)

 


Do you need help customizing DIAdem using Python for your specific test measurement data management needs?   Click on the button below to book a free 1 hour phone / web share consultation.