MicroStation Python: FAQs


About MicroStation Python

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.

 

Where can I find the MicroStation Python API documentation?

Python developer documentation is located here MicroStation Python API

 

Where can I find the delivered MicroStation Python examples? 

Python Samples are delivered under following directory.

C:\ProgramData\Bentley\PowerplatformPython\Examples\

 

How can I contribute to and modify the MicroStation Python bindings? 

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.

 

Where is the Python Manager user interface located?

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.

 

How can I create a Python project?

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.

 

How can I load a Python Project?

Click the Open a Python project from disc icon in the toolbar. Then load the sample python file from the delivered directory.

 

How can I run/execute a Python file?

Select the loaded Python project from the Python Manager. Click the Run Python File icon in the toolbar.

 

Loading and running a Python file with one command

In the Keyin window, type: PYTHON LOAD <file name>

For example:

PYTHON LOAD DrawLine.py

 

Can I run MicroStation Python scripts outside of MicroStation? i.e. from with Visual Studio Code or another Python editor?

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.

 

How can I edit a Python file?

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.

 

How can I record a Macro?

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.

 

Can I use Python to access any feature within MicroStation?

Not yet. We are currently working on exposing every feature in MicroStation, so please be patient.

 

How do I know what features have been exposed?

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.

 

Current level of API support within MicroStation Python

MicroStation Python, currently does not support:

  1. MicroStation Dialog functionality. Access to mdlDialog_ functions, please use TkInter or PyQt as a UX framework.
  2. MicroStation Printing API's. This will be supported in a future version.
  3. MicroStation API Callbacks. Access to mdlSystem_ functions, access to callbacks will be supported in a future version.
  4. MicroStation API calls at the MstnPlatform layer. mdl... functions, a lot of this functionality is available in other areas of the API, if you have a particular need for a function, please post on the MicroStation Programming Forum website.

 

Questions/Problems with the API

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.

 

What is the delivered API version by MicroStation?

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

 

Difference between Python and IronPython implementation

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:

Python Implementations

Iron Python

 

How can I check the Python version installed by MicroStation? 

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

 

How can I list the Python libraries installed by MicroStation? 

Under installation directory, key-in "python -m pip list".

C:\ProgramData\Bentley\PowerPlatformPython\python>python -m pip list

 

How can I install additional Python libraries needed for my project? 

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.

 

How to find Python package's installation location?

Under installation directory, key-in "python -m pip show pandas".

C:\ProgramData\Bentley\PowerPlatformPython\python>python -m pip show pandas

 

How can I locate and display the paths of Python executable files in my machine

Under installation directory, key-in "where python".

C:\ProgramData\Bentley\PowerPlatformPython\python>where python

Note: Where is a Windows command

 

Where are the Python IntelliSense files located?

. 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.

 

How can I remove the yellow squiggly lines under import library statements in VS Code?

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.