Python

Named Groups

Named Groups provide a way for a MicroStation user to connect logically a set of MicroStation graphic elements. Named groups can include elements from the active file and those from directly attached references. Elements in a named group can be manipulated collectively: read MicroStation help for more information. The MicroStation user interface makes it easy to create a named group and add and remove elements in that group …

MicroStation: Named Groups dialog

Nested Named Groups

As well as individual named groups, you can create a hierarchy of them by adding “child” named groups to a “parent”. When you have a hierarchy, manipulating the “parent” named group will include any attached “child” named groups. You can add and drop elements from individual named groups (including those that are part of a hierarchy), as well as manipulate the elements within each named group (again, including those that are part of a hierarchy).

Programming Named Groups

A question popped up in the MicroStation Forum along the lines of "How do I copy Named Groups to another DGN file?"

My response to that question is …

  1. Read the Named Groups in the active DGN file
  2. Write the Named Group data to a termporary file
  3. Open the destination DGN file
  4. Read the Named Group data from the temporary file
  5. Persist the copied Named Groups in the destination file

I chose JSON (JavaScript Object Notation) for the temporary file format. It's simple for humans and the computer to read or write, and it's supported by Python. A more complex alternative would be XML (eXtensible Markup Language). A simpler choice might be CSV format, but CSV is a flat-file-format and can't convey the hierarchy of nested Named Groups.

Read Named Groups from DGN File

The first step is to acquire Named Group data from a DGN file. You can read more about that here.

That's where I encountered the first snag: it's not possible to copy a NamedGroup class instance. There's something wrong with the Python translation from C++ class NamedGroup to Python class NamedGroup. I wrote the Named Group Proxy class to work around that problem.

Read more about the Named Group Proxy class.

Read Named Groups from JSON File

Reading a JSON file is simple, since JSON I/O is supported by Python. Simply import json. The reader opens the JSON file and parses its conents to a list of NamedGroupProxy classes.

The code is presented in the example Read Named Groups from JSON.

Write Named Groups to JSON

You have a list of Named Group Proxy instances obtained from the active DGN file. You want to persist those to a JSON file. Read more about that in Write Named Groups to JSON.

Update Named Groups from JSON

Here's the methodology …

That calculation is simple: we convert each list to a Python set and get the difference of those sets.

Python Sets

A set is a container that has no duplicates.

We read from the JSON file to create a Python list of NamedGroupProxy, then convert that to a Python set. Then we compare that set to another set created from the current DGN file. The set.difference of those sets contains those Named Groups that don't exist in the present DGN file.

We use set logic to work out what Named Groups are missing in the DGN file …

You'll find the set logic in the example Read Named Groups from JSON.