[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

  • if I want to create a .NET element in memory from a COM source

    Just for curiosity, in what situation you need to do this conversion?

      Jan

  • in what situation you need to do this conversion?

    Principally to solve the missing .NET create cell instance requirement.  That is, use COM CreateCellElement2() and get a .NET CellHeaderElement

    It works once the cell is added to a model.  But I want to show the cell in dynamics, and therefore need an in-memory object.

     
    Regards, Jon Summers
    LA Solutions

  • Principally to solve the missing .NET create cell instance requirement.

    I agree it's the issue not solved by Bentley for very long time (and asked many times).

    When looking into documentation, even in C++ API it's not possible to use handler to load a cell from library, but "old" mdlCell_ functions have to be used.

    But I want to show the cell in dynamics, and therefore need an in-memory object.

    It looks like such simple and valid requirement leads to complex transaction: When displayed in dynamics, the cell and all relevant styles + levels should be loaded (but not stored in active DGN yet). And when placed, also attached data (XAttributes...) have to be copied from the cell library.

    Regards,

      Jan

Reply Children