Python

Introduction

Occasionally we programmers need to assemble a list of DGN model references. For example, if we want to use a fence to clip DGN elements, we must supply a list of DGN model references to the fence clip function to inform it where to look for candidate elements.

MicroStation: DgnModelRefList & ModelRefList

If your goal is to build a list of DGN model references then you're in luck. MicroStation Python provides not just one but two containers …

Notice that, although the names are similar, DgnModelRefList is part of the MSPyDgnPlatform module, while ModelRefList belongs to the MSPyMstnPlatform module.

Those classes are confused …

ModelRefList.Create()

Here's how to create a DgnModelRefList …

from MSPyDgnPlatform import *
from MSPyMstnPlatform import *
from MSPyDgnView import *

status, model_ref_list = ModelRefList.Create() # Creates a DgnModelRefList!
model_ref_list.push_back(dgn_model_ref) # Need at least one DgnModelRef in this list

Now you have a DgnModelRefList that you can use. For example, if you want to fence-clip some elements, then a model reference list is required …

from MSPyDgnPlatform import *
from MSPyMstnPlatform import *
from MSPyDgnView import *

status, model_ref_list = ModelRefList.Create() # Creates a DgnModelRefList!
model_ref_list.push_back(dgn_model_ref) # Need at least one DgnModelRef in this list
view_port = IViewManager.GetManager().GetActiveViewSet().GetSelectedViewport()
fence_mgr = FenceManager.GetManager()
fence_mgr.DefineByElement(eh, view_port)
#  Setup fence parameters and options
fence_params = FenceParams(dgn_model)
overlap_mode = fence_mgr.IsOverlapMode()
clip_mode = fence_mgr.IsClipMode()
allow_clip = FenceClipMode.eOriginal
clip_flag = FenceClipFlags.eNil
fence_mgr.InitFromActiveFence(fence_params, overlap_mode, clip_mode, allow_clip)
#  Create an ElementAgenda (list of elements) from a fence-clip operation
agenda = ElementAgenda()
fence_mgr.BuildAgenda(fence_params, agenda, model_ref_list, False, False, False)
# agenda now contains a list of elements created by the clip operation

In the above code fragment, the lines that create a DgnModelRefList are …

status, model_ref_list = ModelRefList.Create()
model_ref_list.push_back(dgn_model_ref)

Thanks go to Bentley Systems staffer YongAn Fu for finding the correct way to create a DgnModelRefList.

ModelRefList.Create() returns a tuple. The tuple contains a status value and an empty DgnModelRefList. You might have expected to find a ModelRefList, but alas not. I don't know how one would create a ModelRefList!

For the list to be useful, it should contain at least one DgnModelRef. It's reasonable to declare that DgnModelRefList is not Pythonic. The way to add a DGN model reference to a DgnModelRefList is to use its push_back method. push_back is a legacy of the C++ vector<DgnModelRef>, which is the foundation of those DGN model reference containers.

The complete code for performing a fence clip by element is provided in my response to a question on the MicroStation Programming Forum.


Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.