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 …
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).
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 …
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.
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.
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.
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.
Here's the methodology …
That calculation is simple: we convert each list to a Python set and get the difference of those 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 …
set.difference to find those Named Group not present in the current DGN file
You'll find the set logic in the example Read Named Groups from JSON.