I want to get a .NET Element from a COM Element...
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?
addToModel
I've marked this thread as solved, because a work-around exists. The question: 'Can we obtain a .NET Element from COM?' remains unanswered.
Hi Jon,
be aware you posted your question to a wrong forum...
Jon Summers said:What's going on?
I think you cannot expect pointer from COM is the same as pointer from NET. I do not recall any such feature described.
Without knowing and compare complete code both for COM/Interop and NET APIs, it's hard to say what is happening internally and whether it's feature or bug. Because GetFromElementRef calls internally some "convert" and "element factory" methods, I can imagine that it does find in-memory element, when not written to DGN file.
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Jan Šlegr said:you posted your question to a wrong forum
Oops! Thanks for pointing that out.
Jan Šlegr said:I think you cannot expect pointer from COM is the same as pointer from NET
It's an ElementRef pointer that is used: the code gets that pointer from COM, then uses it in .NET. As shared by Yongan.Fu in his csAddIns project.
ElementRef
IntPtr pointer = (IntPtr)oElement.MdlElementRef();
So why does that COM function exist if the pointer it obtains cannot be used anywhere?
Element.GetFromElementRef(pointer);
Why does that function exist if it can't construct a .NET Element from an ElementRef pointer?
Regards, Jon Summers LA Solutions
Jon Summers said: As shared by Yongan.Fu in his csAddIns project.
Can you share this project? I found a project with the same name, but I do not see such code used, so it's probably different one.
Jon Summers said:So why does that COM function exist if the pointer it obtains cannot be used anywhere?
How you can say that? In VBA, which is the same COM API, MdlElementRef is useful in many situations.
Jon Summers said:Why does that function exist if it can't construct a .NET Element from an ElementRef pointer?
As you wrote, it works when element is persisted in DGN file. So you contradict yourself.
The right question in my opinion is what is internal difference between in-memory-only and persisted representation, so the output is different.
BTW Did you read this blog? Even when it's about C++/CLI, it provides a good insight into casting process from one API to another (because to use C++/CLI is primarily about handling these types of conversions ;-).
Regards,
Jan Šlegr said:Can you share this project?
As long as it's OK with Yongan.Fu...
5165.csAddins.zip
Hmmm ... I still do not see any MdlElementRef or GetFromElementRef in the code. Or maybe something was lost when unzziped (my antivirus complains about some dll file)?