dbsymutl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. //
  12. // DESCRIPTION:
  13. //
  14. // Namespace AcDbSymbolUtilities contains various utilities for
  15. // working with symbol names, symbol records, and symbol tables.
  16. // The main access to the utility functions is through
  17. // "acdbSymUtil()", which you dereference to call the member
  18. // functions of AcDbSymbolUtilities::Services.
  19. #ifndef AD_DBSYMUTL_H
  20. #define AD_DBSYMUTL_H 1
  21. #include <assert.h>
  22. #include <stddef.h>
  23. #include "dbsymtb.h"
  24. #pragma pack (push, 8)
  25. #define ACDBSYMUTIL_SERVICES_CURRENT_VERSION 106
  26. namespace AcDbSymbolUtilities
  27. {
  28. enum CompatibilityMode {
  29. kExtendedNames = true,
  30. kPreExtendedNames = false
  31. };
  32. enum NameCaseMode {
  33. kPreserveCase = true,
  34. kForceToUpper = false
  35. };
  36. enum NewNameMode {
  37. kAsNewName = true,
  38. kAsExistingName = false
  39. };
  40. enum VerticalBarMode {
  41. kAllowVerticalBar = true,
  42. kNoVerticalBar = false
  43. };
  44. class Services
  45. {
  46. public:
  47. enum { kCurrentVersion = ACDBSYMUTIL_SERVICES_CURRENT_VERSION };
  48. virtual int version() const = 0;
  49. // --------- Pre-defined symbols ---------
  50. virtual bool isBlockLayoutName (const ACHAR * name) const = 0;
  51. virtual bool isBlockModelSpaceName (const ACHAR * name) const = 0;
  52. virtual bool isBlockPaperSpaceName (const ACHAR * name) const = 0;
  53. virtual bool isLayerDefpointsName (const ACHAR * name) const = 0;
  54. virtual bool isLayerZeroName (const ACHAR * name) const = 0;
  55. virtual bool isLinetypeByBlockName (const ACHAR * name) const = 0;
  56. virtual bool isLinetypeByLayerName (const ACHAR * name) const = 0;
  57. virtual bool isLinetypeContinuousName (const ACHAR * name) const = 0;
  58. virtual bool isRegAppAcadName (const ACHAR * name) const = 0;
  59. virtual bool isTextStyleStandardName (const ACHAR * name) const = 0;
  60. virtual bool isViewportActiveName (const ACHAR * name) const = 0;
  61. virtual AcDbObjectId blockModelSpaceId (AcDbDatabase * pDb) const = 0;
  62. virtual AcDbObjectId blockPaperSpaceId (AcDbDatabase * pDb) const = 0;
  63. virtual AcDbObjectId layerDefpointsId (AcDbDatabase * pDb) const = 0;
  64. virtual AcDbObjectId layerZeroId (AcDbDatabase * pDb) const = 0;
  65. virtual AcDbObjectId linetypeByBlockId (AcDbDatabase * pDb) const = 0;
  66. virtual AcDbObjectId linetypeByLayerId (AcDbDatabase * pDb) const = 0;
  67. virtual AcDbObjectId linetypeContinuousId (AcDbDatabase * pDb) const = 0;
  68. virtual AcDbObjectId regAppAcadId (AcDbDatabase * pDb) const = 0;
  69. virtual AcDbObjectId textStyleStandardId (AcDbDatabase * pDb) const = 0;
  70. virtual const ACHAR * blockModelSpaceName () const = 0;
  71. virtual const ACHAR * blockPaperSpaceName () const = 0;
  72. virtual const ACHAR * layerDefpointsName () const = 0;
  73. virtual const ACHAR * layerZeroName () const = 0;
  74. virtual const ACHAR * linetypeByBlockName () const = 0;
  75. virtual const ACHAR * linetypeByLayerName () const = 0;
  76. virtual const ACHAR * linetypeContinuousName () const = 0;
  77. virtual const ACHAR * regAppAcadName () const = 0;
  78. virtual const ACHAR * textStyleStandardName () const = 0;
  79. virtual const ACHAR * viewportActiveName () const = 0;
  80. // --------- Symbol names ---------
  81. virtual int compareSymbolName(
  82. const ACHAR * thisName,
  83. const ACHAR * otherName) const = 0;
  84. virtual bool hasVerticalBar(
  85. const ACHAR * name) const = 0;
  86. virtual Acad::ErrorStatus makeDependentName(
  87. ACHAR *& pNewName,
  88. const ACHAR * dwgName,
  89. const ACHAR * symbolName) const = 0;
  90. virtual Acad::ErrorStatus repairPreExtendedSymbolName(
  91. ACHAR *& pNewName,
  92. const ACHAR * oldName,
  93. bool allowVerticalBar) const = 0;
  94. virtual Acad::ErrorStatus repairSymbolName(
  95. ACHAR *& pNewName,
  96. const ACHAR * oldName,
  97. bool allowVerticalBar) const = 0;
  98. virtual bool splitDependentName(
  99. size_t & numLeftBytes,
  100. const ACHAR *& pRight,
  101. const ACHAR * symbolName) const = 0;
  102. virtual Acad::ErrorStatus validatePreExtendedSymbolName(
  103. const ACHAR * name,
  104. bool allowVerticalBar) const = 0;
  105. virtual Acad::ErrorStatus validateSymbolName(
  106. const ACHAR * name,
  107. bool allowVerticalBar) const = 0;
  108. // --------- Compatibility ---------
  109. virtual bool compatibilityMode(
  110. AcDbDatabase * pDb) const = 0;
  111. virtual Acad::ErrorStatus getBlockNameFromInsertPathName(
  112. ACHAR *& pBlockName,
  113. const ACHAR * pathName) const = 0;
  114. virtual Acad::ErrorStatus getInsertPathNameFromBlockName(
  115. ACHAR *& pPathName,
  116. const ACHAR * blockName) const = 0;
  117. virtual bool getMaxSymbolNameLength(
  118. size_t & maxLength,
  119. size_t & maxSize,
  120. bool isNewName,
  121. bool compatibilityMode) const = 0;
  122. virtual Acad::ErrorStatus getPathNameFromSymbolName(
  123. ACHAR *& pPathName,
  124. const ACHAR * symbolName,
  125. const ACHAR * extensions) const = 0;
  126. virtual Acad::ErrorStatus getSymbolNameFromPathName(
  127. ACHAR *& pSymbolName,
  128. const ACHAR * pathName,
  129. const ACHAR * extensions) const = 0;
  130. virtual Acad::ErrorStatus preValidateSymbolName(
  131. ACHAR *& pSymbolName,
  132. bool preserveCase) const = 0;
  133. virtual Acad::ErrorStatus validateCompatibleSymbolName(
  134. const ACHAR * name,
  135. bool isNewName,
  136. bool allowVerticalBar,
  137. bool compatibilityMode) const = 0;
  138. }; // end Services
  139. inline Acad::ErrorStatus
  140. getSymbolName(
  141. ACHAR *&pName,
  142. AcDbObjectId objId)
  143. {
  144. AcDbSymbolTableRecord *pRec = NULL;
  145. Acad::ErrorStatus es = acdbOpenObject(pRec, objId, AcDb::kForRead);
  146. if (es == Acad::eOk) {
  147. es = pRec->getName(pName);
  148. Acad::ErrorStatus closeStatus = pRec->close();
  149. UNREFERENCED_PARAMETER(closeStatus);
  150. assert(closeStatus == Acad::eOk);
  151. }
  152. return es;
  153. }
  154. // For use by AcDbSymbolUtilities only!
  155. #define ACDBSYMUTIL_SERVICESNAME_WITH_VERSION_1(n,v) n ## v
  156. #define ACDBSYMUTIL_SERVICESNAME_WITH_VERSION(n,v) \
  157. ACDBSYMUTIL_SERVICESNAME_WITH_VERSION_1(n,v)
  158. #define ACDBSYMUTIL_SERVICES_NAME \
  159. ACDBSYMUTIL_SERVICESNAME_WITH_VERSION( \
  160. servicesPtr, \
  161. ACDBSYMUTIL_SERVICES_CURRENT_VERSION)
  162. extern const Services * ACDBSYMUTIL_SERVICES_NAME();
  163. // --------- Inline definitions ---------
  164. inline const Services *
  165. servicesPtr()
  166. {
  167. const Services * pSymUtil = ACDBSYMUTIL_SERVICES_NAME();
  168. assert(pSymUtil != NULL);
  169. assert(pSymUtil->version() == Services::kCurrentVersion);
  170. return pSymUtil;
  171. }
  172. // The get<TABLE>Id() functions retrieve the AcDbObjectId for a symbol
  173. // record given the name of the symbol (parameter "name") and a pointer
  174. // to the database (parameter pDb) that contains the specified table.
  175. // If the function succeeds, it returns the AcDbObjectId in parameter "rId"
  176. // and returns Acad::eOk.
  177. //
  178. // We provide a get<TABLE>Id() function for all the basic symbol tables
  179. // except for the viewport table, which may have duplicate records.
  180. // The following inlined functions are available.
  181. //
  182. // getBlockId
  183. // getDimStyleId
  184. // getLayerId
  185. // getLinetypeId
  186. // getRegAppId
  187. // getTextStyleId
  188. // getUCSId
  189. // getViewId
  190. //
  191. // These functions open up the table and look for the specified record.
  192. // If you need to make successive calls for a particular table, you should
  193. // open the table yourself instead of invcurring the overhead of
  194. // repeatedly opening and closing the tables using these functions.
  195. #define DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(T_TABLE) \
  196. inline Acad::ErrorStatus \
  197. get ## T_TABLE ## Id( \
  198. AcDbObjectId &objId, \
  199. const ACHAR *name, \
  200. AcDbDatabase *pDb) \
  201. { \
  202. if (pDb == NULL) \
  203. return Acad::eNullObjectPointer; \
  204. AcDb ## T_TABLE ## Table *pTable = NULL; \
  205. Acad::ErrorStatus es = pDb->getSymbolTable(pTable, AcDb::kForRead); \
  206. if (es == Acad::eOk) { \
  207. es = pTable->getAt(name, objId); \
  208. Acad::ErrorStatus closeStatus = pTable->close(); \
  209. UNREFERENCED_PARAMETER(closeStatus); \
  210. assert(closeStatus == Acad::eOk); \
  211. } \
  212. return es; \
  213. }
  214. DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(Block)
  215. DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(DimStyle)
  216. DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(Layer)
  217. DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(Linetype)
  218. DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(RegApp)
  219. inline Acad::ErrorStatus
  220. getTextStyleId(
  221. AcDbObjectId &objId,
  222. const ACHAR *name,
  223. AcDbDatabase *pDb)
  224. {
  225. if (pDb == NULL)
  226. return Acad::eNullObjectPointer;
  227. AcDbTextStyleTable *pTable = NULL;
  228. Acad::ErrorStatus es = pDb->getSymbolTable(pTable, AcDb::kForRead);
  229. if (es == Acad::eOk) {
  230. AcDbTextStyleTableRecord *pRec = NULL;
  231. es = pTable->getAt(name, pRec, AcDb::kForRead);
  232. if (es == Acad::eOk) {
  233. if (pRec->isShapeFile())
  234. es = Acad::eKeyNotFound;
  235. else
  236. objId = pRec->objectId();
  237. Acad::ErrorStatus closeStatus = pRec->close();
  238. UNREFERENCED_PARAMETER(closeStatus);
  239. assert(closeStatus == Acad::eOk);
  240. }
  241. Acad::ErrorStatus closeStatus = pTable->close();
  242. UNREFERENCED_PARAMETER(closeStatus);
  243. assert(closeStatus == Acad::eOk);
  244. }
  245. return es;
  246. }
  247. DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(UCS)
  248. DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION(View)
  249. #undef DBSYMUTL_MAKE_GETSYMBOLID_FUNCTION
  250. // The has<TABLE>() functions return a boolean value indicating
  251. // whether a symbol record with the given name (parameter "name") or
  252. // AcDbObjectId (parameter objId) in a particular database's symbol table
  253. // exists. Parameter pDb points to the database to use for accessing
  254. // the symbol table. The functions return Adesk::kTrue if it finds
  255. // the record. Otherwise, it returns Adesk::kFalse.
  256. //
  257. // We provide a has<TABLE>() function for all the basic symbol tables.
  258. // The functions overload on the type of the first parameter. If it is
  259. // an AcDbObjectId, we look for the AcDbObjectId in the table. If it is a
  260. // string, we look for the specified name in the table.
  261. // The following inlined functions are available.
  262. //
  263. // hasBlock
  264. // hasDimStyle
  265. // hasLayer
  266. // hasLinetype
  267. // hasRegApp
  268. // hasTextStyle
  269. // hasUCS
  270. // hasView
  271. // hasViewport
  272. //
  273. // These functions open up the table and looks for the specified record.
  274. // IF you need to make successive calls for a particular table, you should
  275. // open the table yourself instead of invcurring the overhead of
  276. // repeatedly opening and closing the tables using these functions.
  277. #define DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(T_TABLE) \
  278. inline bool \
  279. has ## T_TABLE( \
  280. AcDbObjectId objId, \
  281. AcDbDatabase *pDb) \
  282. { \
  283. if (pDb == NULL) \
  284. return false; \
  285. bool hasSymbol = false; \
  286. AcDb ## T_TABLE ## Table *pTable = NULL; \
  287. Acad::ErrorStatus es = pDb->getSymbolTable(pTable, AcDb::kForRead); \
  288. if (es == Acad::eOk) { \
  289. hasSymbol = !!pTable->has(objId); \
  290. Acad::ErrorStatus closeStatus = pTable->close(); \
  291. UNREFERENCED_PARAMETER(closeStatus); \
  292. assert(closeStatus == Acad::eOk); \
  293. } \
  294. return hasSymbol; \
  295. }
  296. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(Block)
  297. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(DimStyle)
  298. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(Layer)
  299. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(Linetype)
  300. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(RegApp)
  301. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(TextStyle)
  302. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(UCS)
  303. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(View)
  304. DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION(Viewport)
  305. #undef DBSYMUTL_MAKE_HASSYMBOLID_FUNCTION
  306. #define DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(T_TABLE) \
  307. inline bool \
  308. has ## T_TABLE( \
  309. const ACHAR *name, \
  310. AcDbDatabase *pDb) \
  311. { \
  312. if (pDb == NULL) \
  313. return false; \
  314. bool hasSymbol = false; \
  315. AcDb ## T_TABLE ## Table *pTable = NULL; \
  316. Acad::ErrorStatus es = pDb->getSymbolTable(pTable, AcDb::kForRead); \
  317. if (es == Acad::eOk) { \
  318. hasSymbol = !!pTable->has(name); \
  319. Acad::ErrorStatus closeStatus = pTable->close(); \
  320. UNREFERENCED_PARAMETER(closeStatus); \
  321. assert(closeStatus == Acad::eOk); \
  322. } \
  323. return hasSymbol; \
  324. }
  325. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(Block)
  326. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(DimStyle)
  327. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(Layer)
  328. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(Linetype)
  329. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(RegApp)
  330. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(TextStyle)
  331. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(UCS)
  332. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(View)
  333. DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION(Viewport)
  334. #undef DBSYMUTL_MAKE_HASSYMBOLNAME_FUNCTION
  335. } // end AcDbSymboUtilities
  336. namespace AcDbSymUtil = AcDbSymbolUtilities;
  337. Acad::ErrorStatus getTableStyleNameFromDbById(const AcDbObjectId& objId,
  338. ACHAR*& pName);
  339. typedef AcDbSymbolUtilities::Services AcDbSymUtilServices;
  340. inline const AcDbSymUtilServices *
  341. acdbSymUtil()
  342. {
  343. return AcDbSymbolUtilities::servicesPtr();
  344. }
  345. #pragma pack (pop)
  346. #endif // !AD_DBSYMUTL_H