mgdinterop.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Copyright 2015 Autodesk, Inc. All rights reserved.
  5. //
  6. // Use of this software is subject to the terms of the Autodesk license
  7. // agreement provided at the time of installation or download, or which
  8. // otherwise accompanies this software in either electronic or hard copy form.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include <vcclr.h>
  13. #include <gcroot.h>
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // Forward Declarations
  16. //
  17. // Unmanaged types
  18. class AcGeVector2d;
  19. class AcGeVector3d;
  20. class AcGeMatrix2d;
  21. class AcGeMatrix3d;
  22. class AcGePoint2d;
  23. class AcGePoint3d;
  24. class AcGeScale2d;
  25. class AcGeScale3d;
  26. class AcGeTol;
  27. class AcDbObjectId;
  28. class AcDbExtents;
  29. // Managed types
  30. namespace Autodesk
  31. {
  32. namespace AutoCAD
  33. {
  34. namespace Runtime
  35. {
  36. #ifdef __cplusplus_cli
  37. ref class DisposableWrapper;
  38. #else
  39. public __gc __abstract class DisposableWrapper;
  40. #endif
  41. }
  42. }
  43. }
  44. #ifdef __cplusplus_cli
  45. #define AC_GCHANDLE_TO_VOIDPTR(x) ((GCHandle::operator System::IntPtr(x)).ToPointer())
  46. #define AC_VOIDPTR_TO_GCHANDLE(x) (GCHandle::operator GCHandle(System::IntPtr(x)))
  47. #define AC_NULLPTR nullptr
  48. #define AC_GCNEW gcnew
  49. #define AC_WCHAR_PINNED_GCPTR pin_ptr<const wchar_t>
  50. typedef Autodesk::AutoCAD::Runtime::DisposableWrapper^ DisposableWrapper_GcPtr;
  51. typedef System::Type^ Type_GcPtr;
  52. typedef System::String^ String_GcPtr;
  53. #else
  54. #define AC_GCHANDLE_TO_VOIDPTR(x) ((GCHandle::op_Explicit(x)).ToPointer())
  55. #define AC_VOIDPTR_TO_GCHANDLE(x) (GCHandle::op_Explicit(x))
  56. #define AC_NULLPTR 0
  57. #define AC_GCNEW new
  58. #define AC_WCHAR_PINNED_GCPTR const wchar_t __pin*
  59. typedef Autodesk::AutoCAD::Runtime::DisposableWrapper* DisposableWrapper_GcPtr;
  60. typedef System::Type* Type_GcPtr;
  61. typedef System::String* String_GcPtr;
  62. #endif
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // Classes
  65. //
  66. class AcMgObjectFactoryBase : public AcRxObject
  67. {
  68. public:
  69. ACRX_DECLARE_MEMBERS(AcMgObjectFactoryBase);
  70. virtual gcroot<DisposableWrapper_GcPtr> createRCW(AcRxObject* unmanagedPointer, bool autoDelete) = 0;
  71. virtual bool isManaged()
  72. {
  73. return false;
  74. }
  75. virtual ~AcMgObjectFactoryBase(){}
  76. virtual gcroot<Type_GcPtr> getType() = 0;
  77. };
  78. template <typename RCW, typename ImpObj>
  79. class AcMgObjectFactory : public AcMgObjectFactoryBase
  80. {
  81. public:
  82. gcroot<DisposableWrapper_GcPtr> createRCW(AcRxObject* unmanagedPointer, bool autoDelete)
  83. {
  84. gcroot<DisposableWrapper_GcPtr> temp = AC_GCNEW RCW(System::IntPtr(static_cast<ImpObj*>(unmanagedPointer)),autoDelete);
  85. return temp;
  86. }
  87. AcMgObjectFactory()
  88. {
  89. ImpObj::desc()->addX(AcMgObjectFactoryBase::desc(),this);
  90. }
  91. ~AcMgObjectFactory()
  92. {
  93. ImpObj::desc()->delX(AcMgObjectFactoryBase::desc());
  94. }
  95. virtual gcroot<Type_GcPtr> getType() ADESK_OVERRIDE
  96. {
  97. #ifdef __cplusplus_cli
  98. return RCW::typeid;
  99. #else
  100. return __typeof(RCW);
  101. #endif
  102. }
  103. };
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // Data Marshalling
  106. //
  107. class StringToWchar
  108. {
  109. typedef System::Runtime::InteropServices::GCHandle GCHandle;
  110. const wchar_t* m_ptr;
  111. void* m_pinner;
  112. public:
  113. StringToWchar(String_GcPtr str)
  114. {
  115. //pin the string
  116. m_pinner = AC_GCHANDLE_TO_VOIDPTR(
  117. GCHandle::Alloc(str,System::Runtime::InteropServices::GCHandleType::Pinned)
  118. );
  119. AC_WCHAR_PINNED_GCPTR tmp = PtrToStringChars(str);
  120. m_ptr = tmp;
  121. }
  122. ~StringToWchar()
  123. {
  124. GCHandle g = AC_VOIDPTR_TO_GCHANDLE(m_pinner);
  125. g.Free();
  126. m_pinner = 0;
  127. }
  128. operator const wchar_t*()
  129. {
  130. return m_ptr;
  131. }
  132. };
  133. inline String_GcPtr WcharToString(const wchar_t* value)
  134. {
  135. return AC_GCNEW System::String(value);
  136. }
  137. #undef AC_GCHANDLE_TO_VOIDPTR
  138. #undef AC_VOIDPTR_TO_GCHANDLE
  139. #undef AC_NULLPTR
  140. #undef AC_GCNEW
  141. //these defines make legacy clients happy
  142. #define StringToCIF StringToWchar
  143. #define CIFToString WcharToString
  144. #ifndef ACDBMGD
  145. #define GETVECTOR3D(vec3d) (*reinterpret_cast<AcGeVector3d*>(&(vec3d)))
  146. #define GETVECTOR2D(vec2d) (*reinterpret_cast<AcGeVector2d*>(&(vec2d)))
  147. #define GETMATRIX3D(mat3d) (*reinterpret_cast<AcGeMatrix3d*>(&(mat3d)))
  148. #define GETMATRIX2D(mat2d) (*reinterpret_cast<AcGeMatrix2d*>(&(mat2d)))
  149. #define GETPOINT3D(point3d) (*reinterpret_cast<AcGePoint3d*>(&(point3d)))
  150. #define GETPOINT2D(point2d) (*reinterpret_cast<AcGePoint2d*>(&(point2d)))
  151. #define GETSCALE2D(scale2d) (*reinterpret_cast<AcGeScale2d*>(&(scale2d)))
  152. #define GETSCALE3D(scale3d) (*reinterpret_cast<AcGeScale3d*>(&(scale3d)))
  153. #define GETTOL(tol) (*reinterpret_cast<AcGeTol*>(&(tol)))
  154. #define GETOBJECTID(id) (*reinterpret_cast<AcDbObjectId*>(&(id)))
  155. #define GETEXTENTS3D(ext3d) (*reinterpret_cast<AcDbExtents*>(&(ext3d)))
  156. #define GETSUBENTITYID(subentityId) (*reinterpret_cast<AcDbSubentId*>(&(subentityId)))
  157. #ifdef AC_GEVEC3D_H
  158. inline Autodesk::AutoCAD::Geometry::Vector3d ToVector3d(const AcGeVector3d& pt)
  159. {
  160. Autodesk::AutoCAD::Geometry::Vector3d ret;
  161. GETVECTOR3D(ret) = pt;
  162. return ret;
  163. }
  164. #endif
  165. #ifdef AC_GEVEC2D_H
  166. inline Autodesk::AutoCAD::Geometry::Vector2d ToVector2d(const AcGeVector2d& pt)
  167. {
  168. Autodesk::AutoCAD::Geometry::Vector2d ret;
  169. GETVECTOR2D(ret) = pt;
  170. return ret;
  171. }
  172. #endif
  173. #ifdef AC_GEMAT2D_H
  174. inline Autodesk::AutoCAD::Geometry::Matrix3d ToMatrix3d(const AcGeMatrix3d& pt)
  175. {
  176. Autodesk::AutoCAD::Geometry::Matrix3d ret;
  177. GETMATRIX3D(ret) = pt;
  178. return ret;
  179. }
  180. #endif
  181. #ifdef AC_GEMAT3D_H
  182. inline Autodesk::AutoCAD::Geometry::Matrix2d ToMatrix2d(const AcGeMatrix2d& pt)
  183. {
  184. Autodesk::AutoCAD::Geometry::Matrix2d ret;
  185. GETMATRIX2D(ret) = pt;
  186. return ret;
  187. }
  188. #endif
  189. #ifdef AC_GEPNT3D_H
  190. inline Autodesk::AutoCAD::Geometry::Point3d ToPoint3d(const AcGePoint3d& pt)
  191. {
  192. Autodesk::AutoCAD::Geometry::Point3d ret;
  193. GETPOINT3D(ret) = pt;
  194. return ret;
  195. }
  196. #endif
  197. #ifdef AC_GEPNT2D_H
  198. inline Autodesk::AutoCAD::Geometry::Point2d ToPoint2d(const AcGePoint2d& pt)
  199. {
  200. Autodesk::AutoCAD::Geometry::Point2d ret;
  201. GETPOINT2D(ret) = pt;
  202. return ret;
  203. }
  204. #endif
  205. #ifdef AC_GESCL2D_H
  206. inline Autodesk::AutoCAD::Geometry::Scale2d ToScale2d(const AcGeScale2d& pt)
  207. {
  208. Autodesk::AutoCAD::Geometry::Scale2d ret;
  209. GETSCALE2D(ret) = pt;
  210. return ret;
  211. }
  212. #endif
  213. #ifdef AC_GESCL3D_H
  214. inline Autodesk::AutoCAD::Geometry::Scale3d ToScale3d(const AcGeScale3d& pt)
  215. {
  216. Autodesk::AutoCAD::Geometry::Scale3d ret;
  217. GETSCALE3D(ret) = pt;
  218. return ret;
  219. }
  220. #endif
  221. #ifdef AC_GETOL_H
  222. inline Autodesk::AutoCAD::Geometry::Tolerance ToTolerance(const AcGeTol& pt)
  223. {
  224. Autodesk::AutoCAD::Geometry::Tolerance ret;
  225. GETTOL(ret) = pt;
  226. return ret;
  227. }
  228. #endif
  229. #ifdef _AD_DBID_H
  230. inline Autodesk::AutoCAD::DatabaseServices::ObjectId ToObjectId(const AcDbObjectId& pt)
  231. {
  232. Autodesk::AutoCAD::DatabaseServices::ObjectId ret;
  233. GETOBJECTID(ret) = pt;
  234. return ret;
  235. }
  236. #endif
  237. #ifdef AD_DBMAIN_H
  238. inline Autodesk::AutoCAD::DatabaseServices::Extents3d ToExtents3d(const AcDbExtents& pt)
  239. {
  240. Autodesk::AutoCAD::DatabaseServices::Extents3d ret;
  241. GETEXTENTS3D(ret) = pt;
  242. return ret;
  243. }
  244. #endif
  245. #endif // #ifndef ACDBMGD