I want to get a .NET Element from a COM Element...
using BCOM = Bentley.Interop.MicroStationDGN;
Element GetElementFromComElement (BCOM.Element oElement, bool addToModel)
BCOM.Application app = Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp;
IntPtr pointer = (IntPtr)oElement.MdlElementRef();
Element el = Element.GetFromElementRef(pointer);
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.
Jon Summers said: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.
Robert Hook said: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
Jon Summers said:What I could use is a .NET Element constructor that takes an MdlElementDescriptor, but unfortunately doesn't exist.
It seems it exists internally, in Bentley.DgnPlatform.ElementHandle constructor accept element descriptor (I guess it's wrapper around EditElementHandle constructor). But it's true it's not available publicly.
For now, it seems the only way is to write own wrapper / convertor, similar to what is described in the linked blog about C++/CLI :-(
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point