dbDictUtil.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef AD_DBDICTUTIL_H
  2. #define AD_DBDICTUTIL_H 1
  3. //
  4. //////////////////////////////////////////////////////////////////////////////
  5. //
  6. // Copyright 2015 Autodesk, Inc. All rights reserved.
  7. //
  8. // Use of this software is subject to the terms of the Autodesk license
  9. // agreement provided at the time of installation or download, or which
  10. // otherwise accompanies this software in either electronic or hard copy form.
  11. //
  12. //////////////////////////////////////////////////////////////////////////////
  13. //
  14. // DESCRIPTION:
  15. //
  16. // The following are some dictionary utility functions similar
  17. // to those defined for symbol tables in dbsymutl.h
  18. // NOTE: These utils have the same usage guidelines as the symbol
  19. // table versions. That is; These functions are very handy for
  20. // quick one-time access to the name or id of a dictionary entry.
  21. // On the other hand; if you need to make multiple accesses to
  22. // a given dictionary, then avoid the overhead of these functions
  23. // and open the dictionary directly.
  24. //
  25. // -----------------------
  26. // Get the ID of an item
  27. // -----------------------
  28. // General purpose:
  29. // Acad::ErrorStatus
  30. // AcDbDictUtil::dictionaryGetAt(AcDbObjectId& id, const ACHAR* name, AcDbObjectId ownerDictId);
  31. //
  32. // Specific:
  33. // Acad::ErrorStatus
  34. // AcDbDictUtil::getGroupId(AcDbObjectId& id, const ACHAR* name, AcDbDatabase* pDb);
  35. // also...
  36. // getLayoutId
  37. // getPlotSettingsId
  38. // getPlotStyleNameId
  39. // getMaterialId
  40. // getColorId
  41. // getTableStyleId
  42. //
  43. // -----------------------
  44. // Get the NAME/KEY of an item:
  45. // -----------------------
  46. // General purpose:
  47. // Acad::ErrorStatus
  48. // AcDbDictUtil::dictionaryNameAt(ACHAR*& pName, AcDbObjectId itemId, AcDbObjectId ownerDictId);
  49. // or...
  50. // Acad::ErrorStatus
  51. // AcDbDictUtil::dictionaryNameAt(ACHAR*& pName, AcDbObjectId itemId);
  52. //
  53. // Specific:
  54. // Acad::ErrorStatus
  55. // AcDbDictUtil::getGroupName(ACHAR*& name, AcDbObjectId id);
  56. // also...
  57. // getLayoutName
  58. // getPlotSettingsName
  59. // getPlotStyleNameName
  60. // getMaterialName
  61. // getColorName
  62. // getTableStyleName
  63. //
  64. // NOTE: The "get name" functions allocate the returned string.
  65. // The calling application is responsible for deallocating the memory
  66. // used by the returned string.
  67. //
  68. // -----------------------
  69. // Check for existence
  70. // -----------------------
  71. //
  72. // bool hasGroup(const ACHAR* name, AcDbDatabase* pDb);
  73. // also...
  74. // hasLayout
  75. // hasPlotSettings
  76. // hasPlotStyleName
  77. // hasMaterial
  78. // hasColor
  79. // hasTableStyle
  80. //
  81. #include <assert.h>
  82. #include <stddef.h>
  83. #include "dbdict.h"
  84. #include "AcString.h"
  85. namespace AcDbDictUtil
  86. {
  87. inline Acad::ErrorStatus
  88. dictionaryNameAt(ACHAR*& pName, AcDbObjectId itemId, AcDbObjectId ownerDictId)
  89. {
  90. assert(!itemId.isNull() && !ownerDictId.isNull());
  91. AcDbDictionary* pDict;
  92. Acad::ErrorStatus es = acdbOpenObject(pDict, ownerDictId, AcDb::kForRead);
  93. if (es == Acad::eOk) {
  94. es = pDict->nameAt(itemId, pName);
  95. pDict->close();
  96. }
  97. return es;
  98. }
  99. inline Acad::ErrorStatus
  100. dictionaryNameAt(AcString& name, AcDbObjectId itemId, AcDbObjectId ownerDictId)
  101. {
  102. assert(!itemId.isNull() && !ownerDictId.isNull());
  103. AcDbDictionary* pDict;
  104. Acad::ErrorStatus es = acdbOpenObject(pDict, ownerDictId, AcDb::kForRead);
  105. if (es == Acad::eOk) {
  106. es = pDict->nameAt(itemId, name);
  107. pDict->close();
  108. }
  109. return es;
  110. }
  111. //Note: If you already know the owner of itemId, then call the overloaded
  112. // version above to avoid an extra call to acdbOpenObject.
  113. inline Acad::ErrorStatus
  114. dictionaryNameAt(ACHAR*& pName, AcDbObjectId itemId)
  115. {
  116. assert(!itemId.isNull());
  117. AcDbObject* pObject;
  118. Acad::ErrorStatus es = acdbOpenObject(pObject, itemId, AcDb::kForRead);
  119. if (es != Acad::eOk)
  120. return es;
  121. AcDbObjectId dictId = pObject->ownerId(); //get the owner id
  122. es = pObject->close();
  123. assert(es == Acad::eOk);
  124. return dictionaryNameAt(pName, itemId, dictId);
  125. }
  126. //Note: If you already know the owner of itemId, then call the overloaded
  127. // version above to avoid an extra call to acdbOpenObject.
  128. inline Acad::ErrorStatus
  129. dictionaryNameAt(AcString& name, AcDbObjectId itemId)
  130. {
  131. assert(!itemId.isNull());
  132. AcDbObject* pObject;
  133. Acad::ErrorStatus es = acdbOpenObject(pObject, itemId, AcDb::kForRead);
  134. if (es != Acad::eOk)
  135. return es;
  136. AcDbObjectId dictId = pObject->ownerId(); //get the owner id
  137. es = pObject->close();
  138. assert(es == Acad::eOk);
  139. return dictionaryNameAt(name, itemId, dictId);
  140. }
  141. // Given a dictionary and a key name, retrieve the id for that entry.
  142. inline Acad::ErrorStatus
  143. dictionaryGetAt(AcDbObjectId& id, const ACHAR* name, AcDbObjectId ownerDictId)
  144. {
  145. assert(!ownerDictId.isNull());
  146. AcDbDictionary* pDict;
  147. Acad::ErrorStatus es = acdbOpenObject(pDict, ownerDictId, AcDb::kForRead);
  148. assert(es == Acad::eOk);
  149. if (es == Acad::eOk) {
  150. es = pDict->getAt(name, id);
  151. pDict->close();
  152. }
  153. return es;
  154. }
  155. #define DBDICTUTIL_MAKE_DICTIONARY_UTILS(LOWERNAME, UPPERNAME) \
  156. inline Acad::ErrorStatus \
  157. get##UPPERNAME##Id(AcDbObjectId& id, const ACHAR* name, AcDbDatabase* pDb) \
  158. { \
  159. assert(pDb != NULL); \
  160. return (pDb != NULL) \
  161. ? dictionaryGetAt(id, name, pDb->LOWERNAME##DictionaryId()) \
  162. : Acad::eInvalidInput; \
  163. } \
  164. inline Acad::ErrorStatus \
  165. get##UPPERNAME##Name(ACHAR*& name, AcDbObjectId itemId) \
  166. { \
  167. AcDbDatabase* pDb = itemId.database(); \
  168. return (pDb != NULL) \
  169. ? dictionaryNameAt(name, itemId, pDb->LOWERNAME##DictionaryId()) \
  170. : Acad::eInvalidInput; \
  171. } \
  172. inline Acad::ErrorStatus \
  173. get##UPPERNAME##Name(AcString& name, AcDbObjectId itemId) \
  174. { \
  175. AcDbDatabase* pDb = itemId.database(); \
  176. return (pDb != NULL) \
  177. ? dictionaryNameAt(name, itemId, pDb->LOWERNAME##DictionaryId()) \
  178. : Acad::eInvalidInput; \
  179. } \
  180. inline bool \
  181. has##UPPERNAME(const ACHAR* name, AcDbDatabase* pDb) \
  182. { \
  183. AcDbObjectId id; \
  184. return (get##UPPERNAME##Id(id, name, pDb) == Acad::eOk); \
  185. }
  186. DBDICTUTIL_MAKE_DICTIONARY_UTILS( mLStyle, MLStyle)
  187. DBDICTUTIL_MAKE_DICTIONARY_UTILS( group, Group)
  188. DBDICTUTIL_MAKE_DICTIONARY_UTILS( layout, Layout)
  189. DBDICTUTIL_MAKE_DICTIONARY_UTILS( plotSettings, PlotSettings)
  190. DBDICTUTIL_MAKE_DICTIONARY_UTILS( plotStyleName, PlotStyleName)
  191. DBDICTUTIL_MAKE_DICTIONARY_UTILS( material, Material)
  192. DBDICTUTIL_MAKE_DICTIONARY_UTILS( color, Color)
  193. DBDICTUTIL_MAKE_DICTIONARY_UTILS( tableStyle, TableStyle)
  194. DBDICTUTIL_MAKE_DICTIONARY_UTILS( visualStyle, VisualStyle)
  195. #undef DBDICTUTIL_MAKE_DICTIONARY_UTILS
  196. } // end AcDbDictUtil namespace
  197. #endif // !AD_DBDICTUTIL_H