Python

MicroStation Element Ranges

Each and every graphical element in a MicroStation DGN file has a range. An element's range is a bounding cube (in 3D) or bounding rectangle (in 2D). The element graphics fits entirely within the range. A MicroStation user normally does not have to deal with ranges. You can see an element's range using the SET RANGE key-in.

Range Uses

A range is stored as two 64-bit integer triplets (XYZ low and XYZ high). This contrasts with MicroStation master units, with which a MicroStation user is familiar. Master units are stored as units-of-resolution (UORs) in double-precision floating-point numbers (C++ double). The reasons for using integers for element range include …

Scanning

MicroStation often scans a DGN model, looking for those elements that fit within a specified range. Because scanning compares two ranges using integers, the arithmetic is fast.

Views and Scanning

For example, MicroStation may want to find those elements that are contained in a particular view. When user issues a 'fit view' command, MicroStation needs to know those elements whose ranges will be used to calculate the new view dimensions.

Fence and Scanning

Another example is a fence. User want to perform some operation on those elements that are bounded by a fence. MicroStation scans the DGN model using the fence range to find elements that are contained by the fence.

Scanning is a Coarse Filter

Because an element range is a bounding block (or shape) the result of a scan is by nature inexact. A scan is a quick test that tells you what elements are interesting. However, 'interesting' is a loose term — it lets you find candidate elements fast. You may want subsequently to implement a more precise analysis of those candidates.

Custom Scanning

You may want to implement some custom scanning. For example, you might want to test whether one shape is inside another: a scan lets you establish quickly that topology.

Python API

MicroStation Python doesn't have a scanning API. Other MicroStation development languages (even MicroStationVBA!) do have a scanning API. So, if you want to find those elements in, say, a view or fence, then you'll have to roll your own.

scan_range class

I'm here to help. I've written a scan_range class that inherits from MicroStation Python ScanRange. I've added several constructors and comparison methods …

Constructors include …

Methods include …

Examples using scan_range

Some examples test the constructors, to confirm your suspicions about ranges. Others test the comparison methods, to shows the relationship between one range and another.

Fence

We often want to find the contents of a fence. One way to do that is to find those elements whose range falls within the fence range. I wrote a fence helper class fence_helper to help.

For the purpose of this article, my primary reason for writing the helper is to get the range of a rectangular fence. fence_helper.get_fence_points() performs that task, whether the fence is defined by points or uses a DGN element.

Python FenceManager

If you work a lot with MicroStation fences, then familiarise yourself with Python's FenceManager.

Download la_solutions_find_elements_in_range.zip

Unpack the ZIP file and copy the Python files into a folder that MicroStation knows about.

Python Manager

Start MicroStation and place some elements. You'll need to create some rectangles, some big, some small, to test this code. Put one small rectangle inside a larger rectangle to test the topology algorithm.

You'll need to place a fence to execute some code successfully.

Use MicroStation's Python Manager to find and execute the script.


Questions

Post questions about MicroStation Python programming to the MicroStation Programming Forum.