Scripting in MechworksPDM

<< Click to Display Table of Contents >>

Navigation:  Programming Mechworks PDM >

Scripting in MechworksPDM

The scripting in MechworksPDM

MechworksPDM allows to program scripts in different languages so to best fit users' needs.

Storically vbscript has been the most popular language, also because it has been part of every MS operating system. With Windows 11 version 24H2, Microsoft announced that vbscript will be dismissed as supported language, so MechworksPDM introduced the support for other languages.

Currently supported languages are:

vbscript (.vbs) → legacy

.NET

PowerShell (.PS1)

Python (.PY)

Encoding in MechworksPDM scripting

Depending on the language, a different enconding is required, expecially if particular characters are involved.

extension

encoding

additional notes

.PS1

ANSI

If declared as UTF-8 will produce a bad return value in case of instructions like:

$app.UserInterface().MsgBox("€")

.PY

UTF-8

Python default enconding is UTF-8; you can use a different encoding but an additional directive has to be declared at the beginning of the script; in case of ANSI for example is

# -*- coding: cp1252 -*-

.MWNET

ANSI

The core works with ANSI default; based on compiled MWPDMEvents module

.LST/.SPT

ANSI

The core works with ANSI default, so also text files used to pass parameters are encoded as ANSI, and so it must be the script source

.VBS

ANSI

If declared as UTF-8 will produce a bad return value in case of instructions like:

App.UserInterface.MsgBox("€")

If no special characher are involved, the enconding is not relevant for all but the python scripts, but the good practice is to properly set encoding as described above.

.NET scripting full support

Based on MWPDMEvents compiled code, .NET code is executed with no vbs engine usage.

PowerShell and Python scripting full support

R26 is fully compatible with PowerShell and Python scripting:

PowerShell or Python can be used everywhere it was possible to use a vbs script, for example in ShortcutBar or with the .VBScript directive in a Variant Note.

PowerShell and Python now support a direct function call or parameter passing in the same way as vbs. Please note that the parameters syntax remains the same for compatibility reason: if a variant note was calling a .lst file passing parameters, now the .lst can be converted internally to a .ps1 script without the need to change the note.

Example

if the parameters in the note are defined as follows:

Script_File_Name&@Para1=val1[\r\nPara2=val2[\r\nPara3=val3[…]]]

In PowerShell the script needs to declare input parameters named Para1, Para2, Para3.

param(

   [string] $Para1,
   [string] $Para2,
   [string] $Para3
  )

In Python the parameters are passed with the standard syntax and can be parsed using the standard argparse module. The script is invoked as follows (please note that the double '-' sign is added by the PDM framework):

script.py --Para1 val1 --Para2 val2 --Para3 val3

The .async directive has been also implemented for both PowerShell and Python scripts in order to have launch-and-forget scripts, that means scripts running in parallel.

Other directives like .x64 or .include are no more necessary.

Remarks

Performance has been improved when launching scripts since the PowerShell and Python engine are started once and then remain active for every subsequent execution. This avoids the engine staring time and the api initialization time, since it is internally initialized only once. However, please note that the standard initialization instructions are still required.