Convert DgnModelRefP to DgnModelCR

Some functions in the Microstation CONNECT API require an argument of type DgnModelCR. I usually have access to the DgnModelRefP of a model. How do I convert this to DgnModelCR?

Here is my code: 

// find sheet named boundaries
DgnModelRefP activeModelRefP = mdlModelRef_getActive();
DgnModelCR activeModelCR = *activeModelRefP->AsDgnModelCP();

NamedBoundaryCollectionPtr collection = NamedBoundaryCollection::Create(activeModelCR);
boundaryCount = collection->GetCount();
for (NamedBoundaryCollection::const_iterator iter = collection->begin(); iter != collection->end(); ++iter)
{
	WString name = (*iter)->GetName();
	ElementHandle elemHandle = (*iter)->GetGraphicalElement();
	MSElementCP elemCP = elemHandle.GetElementCP();
}

It compiles with no errors but I get a 0 for boundaryCount during runtime even though the model contains a drawing boundary. I think the activeModelCR is incorrectly defined.

Any suggestions?

  • Hi Benzi,

    quick first thought: AsDgnModel returns constant pointer, whereas you assign it to DgnModel constant reference. Are you sure casting (dereferencing) from pointer to reference is done automatically and correctly?

    With regards,

      Jan

  • I'm not sure the casting is done correctly and that's why I suspect that line of code. I also tried using this instead:

    DgnModelRefP activeModelRefP = mdlModelRef_getActive();
    DgnModelCR activeModelCR = *activeModelRefP->GetDgnModelP();

    with similar results.

    When I use this:

    DgnModelRefP activeModelRefP = mdlModelRef_getActive();
    DgnModelCR activeModelCR = activeModelRefP->GetDgnModelP();

    I get a compiler "no suitable constructor to convert" error.

  • I think the activeModelCR is incorrectly defined

    This article discusses the typedefs in the MicroStationAPI. 

    DgnModelCR is an alias of typedef of DgnModel generated by a macro in DgnPlatform.h.  Once you dispense with the noise, you will see something like this:

    typedef DgnModel& DgnModelR;
    typedef DgnModel const& DgnModelCR;

    NamedBoundaryCollection::Create takes a DgnModel by const reference.  What that tells you is the the function will not attempt to modify the DgnModel.

    I'm not sure the casting is done correctly

    You're declaring a variable of type const reference to the type you receive from a function. If the compiler is satisified then look to  NamedBoundaryCollection::Create().  Unfortunately there is no example for named boundaries.

     
    Regards, Jon Summers
    LA Solutions

  • I seem to have confused Named Boundary with Drawing Boundary. The following code actually runs as expected:

    // find named boundaries
    DgnModelRefP activeModelRefP = mdlModelRef_getActive();
    DgnModelCR activeModelCR = *activeModelRefP->GetDgnModelP();
    
    NamedBoundaryCollectionPtr collection = NamedBoundaryCollection::Create(activeModelCR);
    boundaryCount = collection->GetCount();
    
    for (NamedBoundaryCollection::const_iterator iter = collection->begin(); iter != collection->end(); ++iter)
    {
    	WString name = (*iter)->GetName();
    	ElementHandle elemHandle = (*iter)->GetGraphicalElement();
    	MSElementCP elemCP = elemHandle.GetElementCP();
    }

    I was trying to access the Drawing Boundaries contained in a sheet model which requires different coding.

    Thanks for helping me figure that out.