找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 789|回复: 1

[每日一码] 如何修改图形中XREF路径(包括嵌套)?

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2017-3-8 09:27:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
问题:

如何修改图形中XREF路径(包括嵌套)?
How to change the path of Xref (including the nested ones) in a drawing?

解决方案:

下面代码段演示来如何修改图形中XREFS的路径指向一个新的位置。对于嵌套的XREFS,代码递归运行改变路径。这个代码适合把XREFS文件从一个目录改变到另外的目录时候使用。

The following code snippet shows how to change the path of all the Xrefs in a given drawing, to point to a new location. The code sample runs recursively to change the paths of the Xref even in the nested Xrefs. This code is useful in situations where the Xrefed drawings are being migrated from one folder/server to another folder/server.


  1. // TODO: Here you can add your own includes / declarations
  2. #include "acedxref.h"
  3. void fTest();
  4. void fXrefRePath(const char* pDwgPath,const char* pNewLocation,long mnDepth = 1);


  5. ///////////////////////////////////////////////////////////////////////////////////////////////
  6. //Description: fGetXYPlane
  7. //1)Call this from ARX defined command
  8. ///////////////////////////////////////////////////////////////////////////////////////////////
  9. void fTest()
  10. {
  11. acutPrintf(_T("\nTrying to change the paths of XREFs..."));
  12. fXrefRePath(_T("c:\\test.dwg"),_T("c:\\newlocation\\")); //feel free to change the path as required
  13. }

  14. ///////////////////////////////////////////////////////////////////////////////////////////////
  15. //Description: fGetXYPlane
  16. //Parameters:
  17. //a)pDwgPath: Drawing name to repath the XREFs
  18. //b)pNewLocation: The new folder where the XREF are available
  19. //c)nDepth: Do not use. It specifies the nest level. It is optional and is used while running recursively
  20. ///////////////////////////////////////////////////////////////////////////////////////////////
  21. void fXrefRePath(const TCHAR* pDwgPath,const TCHAR* pNewLocation,long nDepth)
  22. {
  23. //preparing the prefix
  24. TCHAR mPrefix[255];

  25. if (1 < nDepth)
  26. {
  27.   mPrefix[nDepth] = _T('\0');
  28. }
  29. else
  30. {
  31.   mPrefix[0] = _T('\0');
  32. }

  33. AcDbBlockTable *pBT;
  34. AcDbBlockTableRecord *pBTR;
  35. AcDbBlockTableIterator *pBTIter;

  36. AcDbDatabase *pDB = new AcDbDatabase(Adesk::kFalse);
  37. Acad::ErrorStatus mEs;

  38. mEs = pDB->readDwgFile(pDwgPath);

  39. if(Acad::eOk != mEs)
  40. {
  41.   acutPrintf(_T("\n%sError: reading database: %s"),mPrefix,pDwgPath);
  42.   delete pDB;
  43.   return;
  44. }

  45. pDB->getBlockTable(pBT,AcDb::kForRead);
  46. pBT->newIterator(pBTIter);
  47. pBT->close();

  48. const TCHAR *pOldPathName;
  49. TCHAR *pNewPathName = new TCHAR[255];

  50. //path buffers
  51. TCHAR mOldDrive[_MAX_DRIVE];
  52. TCHAR mOldDir[_MAX_DIR];
  53. TCHAR mOldFname[_MAX_FNAME];
  54. TCHAR ext[_MAX_EXT];

  55. TCHAR mNewDrive[_MAX_DRIVE];
  56. TCHAR mNewDir[_MAX_DIR];
  57. TCHAR mNewFname[_MAX_FNAME];
  58. TCHAR mNewExt[_MAX_EXT];


  59. acutPrintf(_T("\n%s-%s"),mPrefix,pDwgPath);

  60. while(!pBTIter->done())
  61. {
  62.   if(Acad::eOk != pBTIter->getRecord(pBTR,AcDb::kForRead)) continue;

  63.   if (pBTR->isFromExternalReference())
  64.   {
  65.    //changing the path name
  66.    pBTR->pathName(pOldPathName);

  67.    //split the old location
  68.    _wsplitpath_s(pOldPathName, mOldDrive, mOldDir, mOldFname, ext);

  69.    //split the new location
  70.    //file name and ext not used and is not expected to be supplied
  71.    _wsplitpath_s(pNewLocation, mNewDrive, mNewDir, mNewFname, mNewExt);

  72.    _wmakepath_s(pNewPathName, MAX_PATH,mNewDrive, mNewDir, mOldFname, ext);

  73.    //break if unable to upgrade to write
  74.    if(Acad::eOk != pBTR->upgradeOpen()) break;
  75.    mEs = pBTR->setPathName(pNewPathName);

  76.    //recurse to update all the drawings that are refered too
  77.    fXrefRePath(pNewPathName,pNewLocation,nDepth+1);
  78.   }

  79.   pBTR->close();
  80.   pBTIter->step();

  81. }

  82. //save the changes
  83. if(Acad::eOk != pDB->saveAs(pDwgPath))
  84.   acutPrintf(_T("\n%sError: unable to save the changes to %s "),mPrefix,pDwgPath);

  85. //clean up
  86. delete pBTIter;
  87. delete pDB;
  88. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-22 22:07 , Processed in 0.352745 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表