MicroStation users are familiar with several idioms for choosing one or more DGN elements — picking elements individually or manipulating many elements in bulk using a fence or selection set. This article shows how to handle a selection set programmatically using Python. The example code, which you can download, analyses the elements in a selection set and computes their centroid.
![]() |
Use the
SelectionSetManager
with selection sets.
There is only ever one selection set active, and SelectionSetManager is a
singleton class.
Get the SelectionSetManager like this …
from MSPyDgnView import *
mgr = SelectionSetManager.GetManager()
# Test whether there is a current selection set
if mgr.IsActive():
print(f"Selection set is active. It contains {mgr.NumSelected()} elements")
else:
print("No selection set")
You can get each member of a selection set using indexing, but a more convenient way is to ask for an ElementAgenda …
def check_selection_set (self)->int:
'''
If a selection set is active, gets its contents and build
an element agenda of points.
Return: Number of entries in the new agenda
'''
mgr = SelectionSetManager.GetManager()
mgr.BuildAgenda(self._agenda)
return self._agenda.GetCount()
An ElementAgenda
is a collection of elements, with one or two methods to handle that collection.
In this example we enumerate the agenda to build a list of DPoint3d.
Each point is obtained from the element's snap origin, provided by its
DisplayHandler.
Every graphic DGN element has a DisplayHandler from which we obtain the element's
snap origin …
def agenda_to_point_list(self)->int:
'''
For each element in our agenda, get its preferred origin or snap point,
and add to our list.
We're geting a DisplayHandler, which is applicable to all graphic elements, so
your selection set is not restricted to particular element types.
'''
count = self._agenda.GetCount()
for i in range(count):
eh: ElementHandle = self._agenda.GetEntry(i)
handler: DisplayHandler = eh.GetHandler(MissingHandlerPermissions.eMISSING_HANDLER_PERMISSION_None)
snap = DPoint3d()
handler.GetSnapOrigin(eh, snap)
self._points.append(snap)
return len(self._points)
ICurvePrimitive
is a class for handling geometric concepts such as lines, arcs and closed regions.
An ICurvePrimitive is not a DGN element, although you can convert it to one.
We use it here to handle our list of points and analyse them to calculate a centroid …
def calculate_centroid(self)->bool:
'''
Create a CurvePrimitive from our list of points then calculate its centroid.
'''
status = False
# Must have at least three points to be able to find the centroid
if 2 < len(self._points):
curve = ICurvePrimitive.CreateLineString(self._points)
status, length, centroid = curve.WireCentroid() # Returns tuple: (bool, float, DPoint3d)
if status:
self._centroid = centroid
else:
msg = "Need more points to calculate centroid"
print(msg)
MessageCenter.ShowErrorMessage(msg, msg, False)
return status
Method ICurvePrimitive.WireCentroid() is
Pythonic
— it's return type is a
tuple
containing three objects of type bool, float and DPoint3d.
The first tells you if the calculation succeeded, the second is the length of the primitive (which we don't use here),
and the third is the centroid of the points that form the ICurvePrimitive.
Bentley Systems staffer YongAn Fu commented on this article on the MicroStation Programming Forum. He suggested a mathematically more satisfactory way to calculate the object centroids, using a system of weighting by each element's area.
Thanks, YongAn Fu!
The above methods are part of the CalculateCentroid class.
You'll find its full definition in file create_centroid_from_selection.py,
which is included in the ZIP file that you can download.
Download la_solutions_centroid_from_select_set.zip.
The ZIP file contains the main module la_solutions_centroid_from_select_set.py and
supporting Python files in folder SelectionSet.
├── la_solutions_centroid_from_select_set.py ├── SelectionSet │ ├── create_centroid_from_selection.py │ ├── create_text_element.py
Post questions about MicroStation programming to the MicroStation Programming Forum.