using namespace System::Reflection;

#include <Mstn\MdlApi\mselemen.fdf>
#include <vcclr.h>
#include "SampleMixed.h"


namespace SampleMixed
{

[assembly:AssemblyDelaySignAttribute(false)];

	ElementDerive::ElementDerive(ElementHandleP eehp)
	{
		InitializeFromElementHandle(*eehp);
	}

	DgnNetEle::Element^ ElementOperation::ConvertToDgnNetEle(InteropMstn::Element^ ele)
	{
		ElementDerive^ rtnEle = nullptr;
		if ((long)ele->MdlElementRef() != 0)
		{
			EditElementHandle eeh((ElementRefP)(void*)ele->MdlElementRef(), (DgnModelRefP)(void*)ele->ModelReference->MdlModelRefP());
			rtnEle = gcnew ElementDerive(&eeh);
			return rtnEle;
		}
		MSElementDescrP elmdscrP = (MSElementDescrP)(void*)ele->MdlElementDescrP(false);
		EditElementHandle eeh(elmdscrP, true, false, (DgnModelRefP)(void*)ele->ModelReference->MdlModelRefP());
		rtnEle = gcnew ElementDerive(&eeh);
		return rtnEle;
	}

	InteropMstn::Element^ ElementOperation::ConvertToInteropEle(DgnNetEle::Element^ ele)
	{
		cli::pin_ptr<unsigned char> pb = &(ele->ElementHandle[0]);
		ElementHandleP ehp = reinterpret_cast<ElementHandleP>(pb);
		InteropMstn::Application^ msApp = Bentley::MstnPlatformNET::InteropServices::Utilities::ComApp;
		MSElementDescrP elmdscrp = const_cast<MSElementDescrP>(ehp->GetElementDescrCP());
		long long* lp = reinterpret_cast<long long*>(&*elmdscrp);
		long long elmDscrp = *reinterpret_cast<long long*>(&lp);
		InteropMstn::Element^ rtnEle = msApp->MdlCreateElementFromElementDescrP(elmDscrp);
		return rtnEle;
	}
}