[CONNECT .NET] Get In-Memory Element from COM Impossible

I want to get a .NET Element from a COM Element...

using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.MstnPlatformNET;
using BCOM = Bentley.Interop.MicroStationDGN;
Element GetElementFromComElement (BCOM.Element oElement, bool addToModel)
{
  if (addToModel)
  {
    BCOM.Application app = Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp;
    app.ActiveModelReference.AddElement(oElement);
  }
  IntPtr pointer = (IntPtr)oElement.MdlElementRef();
  Element el = Element.GetFromElementRef(pointer);
  return el;
}

If I call the above function with addToModel True, it works. If I call the function with addToModel False, it returns a null.  But I don't want to write the COM element to file: I want an in-memory .NET Element.  What's going on?

I've marked this thread as solved, because a work-around exists.  The question: 'Can we obtain a .NET Element from COM?' remains unanswered.

Parents
  • If I call the above function with addToModel True, it works. If I call the function with addToModel False, it returns a null

    When an element is first manufactured, underneath the paraphernalia of C++, .NET or VBA, it exists as a C++ ElementDescriptor.  Until the element is persisted (added to a DGN model) the elementRef in that descriptor is NULL.  Once the element is added to a model, an elementRef is created and (a) stored in the model cache and (b) assigned in the ElementDescriptor.  At that point it becomes possible to use the conversion mechanism above to obtain a .NET Element.

    So, if I want to create a .NET element in memory from a COM source, using the conversion shown above won't work.  I must find some other procedure. Is there a way to construct a .NET Element from an MdlElementDescriptor, which is available from a COM element?  There is a .NET Element constructor that takes a C++ ElementHandle, but there's no way to obtain an ElementHandle from a COM Element.  What I could use is a .NET Element constructor that takes an MdlElementDescriptor, but unfortunately doesn't exist.

    Does populating the ModelRef via GetFromElementRefAndModelRef work and provide desired results?

    No, for the same reason explained above.  The elementRef doesn't exist before the element has been added to a DGN model.

     
    Regards, Jon Summers
    LA Solutions

Reply
  • If I call the above function with addToModel True, it works. If I call the function with addToModel False, it returns a null

    When an element is first manufactured, underneath the paraphernalia of C++, .NET or VBA, it exists as a C++ ElementDescriptor.  Until the element is persisted (added to a DGN model) the elementRef in that descriptor is NULL.  Once the element is added to a model, an elementRef is created and (a) stored in the model cache and (b) assigned in the ElementDescriptor.  At that point it becomes possible to use the conversion mechanism above to obtain a .NET Element.

    So, if I want to create a .NET element in memory from a COM source, using the conversion shown above won't work.  I must find some other procedure. Is there a way to construct a .NET Element from an MdlElementDescriptor, which is available from a COM element?  There is a .NET Element constructor that takes a C++ ElementHandle, but there's no way to obtain an ElementHandle from a COM Element.  What I could use is a .NET Element constructor that takes an MdlElementDescriptor, but unfortunately doesn't exist.

    Does populating the ModelRef via GetFromElementRefAndModelRef work and provide desired results?

    No, for the same reason explained above.  The elementRef doesn't exist before the element has been added to a DGN model.

     
    Regards, Jon Summers
    LA Solutions

Children