MicroStation's Python API is a thin wrapper of the MicroStation C++ API. Understanding the core concepts of working with DGN Files and MicroStation from the C++ API directly relates for use with the Python API.
pybind11 is a lightweight, header-only library that is utilized to create Python bindings of the MicroStation C++ API.
Python developer documentation is located here MicroStation Python API
Python Samples are delivered under following directory.
C:\ProgramData\Bentley\PowerplatformPython\Examples\
The source code for the MicroStation Python bindings is open-source and available on GitHub | MicroStation Python. This means you can directly contribute by making changes and create a pull request.
Python Manager dialog is located on the Utilities tab of the Drawing Ribbon.
The quickest way to start with using Python in MicroStation is to use the Python Manager dialog on the Utilities tab of the Drawing Ribbon.
Click the Create Python Project icon in the toolbar. Select a preferred directory to store your Python project/files. Enter the file name and click the Save button.
Click the Open a Python project from disc icon in the toolbar. Then load the sample python file from the delivered directory.
Select the loaded Python project from the Python Manager. Click the Run Python File icon in the toolbar.
In the Keyin window, type: PYTHON LOAD <file name>
For example:
PYTHON LOAD DrawLine.py
No. MicroStation Python scripts require libraries delivered only with MicroStation; therefore, scripts need to be run from the MicroStation Python Manager or Keyin's within MicroStation.
Editor menu provides User to select preferred IDE to use. Select the loaded Python project from the Python Manager and choose a preferred IDE from the available list.
Note: Installed IDEs on the machine are automatically added to the IDE list.
Recording a Macro and promoting it to Python is also a quick way to get started with Python in MicroStation. This is outlined in more detail in the Recording Macros documentation section. The process of recording a Macro will capture points in the view, mouse buttons and key-ins to execute functionality in MicroStation.
Not yet. We are currently working on exposing every feature in MicroStation, so please be patient.
If an Example script has been created, it has been exposed. If the MicroStation Python API document has it listed as an API reference, it has been exposed.
MicroStation Python, currently does not support:
If you have questions or problems, the best way to seek help is via the Bentley Communities website. Start with a search to find the latest documentation and/or existing answers. If unable to find information to resolve the issue, consider posting programming questions to the MicroStation Programming Forum, or for questions related to the Bentley Developer Network Program to the Bentley Developer Network Forum.
MicroStation Delivers version 3.12 of Python. The MicroStation python libraries are also built and tested against this version.
Whilst it is possible to use a different version of Python with MicroStation, provided it is the same major version, it's not recommended. If you choose to go this route a minimum number of packages are required to support the ability of the MicroStation Python API to function correctly.
These are:
Debugpy, et-xmlfile, matplotlib, NumPy, openpyxl, pandas, PyQt5, pytest, pywin32
IronPython is a Python implementation written in C# targeting Microsoft's.NET framework. It uses .NET Virtual Machine i.e. Common Language Runtime. IronPython can use the .NET Framework and Python libraries, and other, .NET languages can use Python code very efficiently.
References:
In the installation directory key-in "Python --version" or "Python -V" to display the delivered version number
C:\ProgramData\Bentley\PowerPlatformPython\python>Python -V
Python 3.12.3
Under installation directory, key-in "python -m pip list".
C:\ProgramData\Bentley\PowerPlatformPython\python>python -m pip list
Under installation directory, key-in "python -m pip install geojson".
C:\ProgramData\Bentley\PowerPlatformPython\python>python -m pip install geojson
Note: Ensure that you do not disrupt existing or delivered libraries.
Under installation directory, key-in "python -m pip show pandas".
C:\ProgramData\Bentley\PowerPlatformPython\python>python -m pip show pandas
Under installation directory, key-in "where python".
C:\ProgramData\Bentley\PowerPlatformPython\python>where python
Note: Where is a Windows command
. PYI IntelliSense files required by VS Code editor are delivered under:
C:\ProgramData\Bentley\PowerPlatformPython\Examples\Microstation\Intellisense\
For instructions on setting up IntelliSense in VS Code, refer to the MicroStation Python API documentation titled Using VS Code IntelliSense with MicroStation Python.
Create a Windows Environment Variable PYTHONPATH and set the variable value to root of your MicroStation application directory:
C:\Program Files\Bentley\MicroStation 2024\MicroStation\
Restart VS Code.
Refer to the wiki, MicroStation Python: VS Code IntelliSense.
Note: The .PYD files are located in the C:\Program Files\Bentley\MicroStation 2024\MicroStation\ directory.
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs