Scripting in MechworksPDM

<< Click to Display Table of Contents >>

Navigation:  Programming Mechworks PDM > Scripting in MechworksPDM >

Scripting in MechworksPDM

Support for Python in MechworksPDM

Starting from R25sp0.1, Python is a supported language for scripting.

The extension .py is recognized in the LST_DIR folder and scripts will be shown in the Tools → Utilit menu.
Similarly, in the right mouse button menu on an assembly, part, drawing or project it is possible to add commands creating .py scripts in the LST\(A|P|D|0|G) folder (respectively).

.SPT and .LST files can be Python scripts too: the first line must be a .PY directive (similar to the .VBSCRIPT directive).
Examples of Python scripts are available in the LST_DIR\demo\demo_py folder.

Prerequisites

1.Python must be already installed: suggested version is 3.12.x
Installing a later version, it is necessary to add in the dbwconn.par the related version using the new parameter: SCRIPT_PYTHON_VERSION

2.PythonNET package must be already installed: via PIP install usually like

py -m pip install pythonnet

 

Debugging Python scripts in Visual Studio Code

Python scripts can be debugged in Visual Studio Code using breakpoints. Open the script folder, install the Python and Python Debugger extensions, and use Python: Select Interpreter to select the Python environment where pythonnet is installed.

When a script is started from Visual Studio Code, provide the environment variables normally supplied by MechworksPDM. Create a .vscode\launch.json file in the script folder, for example:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug MechworksPDM Python script",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"DBWorksApiNetDll": "C:\\Program Files (x86)\\Common Files\\Mechworks\\Shared\\MwPDMApi.dll",
"DBWorksApplicationName": "DBWorks Standalone"
}
}
]
}

DBWorksApiNetDll must contain the full path to the installed .NET API assembly, MwPDMApi.dll. Adjust the path if your installation uses a different folder. Despite the environment variable name, the DLL is named MwPDMApi.dll.

DBWorksApplicationName must match the application you want to connect to. Use "DBWorks Standalone" for the standalone application, or "DBWorks" for the SOLIDWORKS integration. The standalone application requires the complete name, including the space and "Standalone".

Start the corresponding MechworksPDM application before debugging. In the script initialization, pass the application name to StartConnection(); do not call it without the required application-name argument.

Open the .py file, set a breakpoint, select the debug configuration above, and press F5. The env settings apply to this debug session; no permanent Windows environment variables are required.

If import clr fails, check that pythonnet is installed in the selected Python environment. If StartConnection() fails, check DBWorksApplicationName and make sure that the corresponding application is running.