写扩展数据,其中使用upgradeOpen()函数对象的打开方式变为写方式
1 static void lzxARX_AcDbDatabaseAddXData(void) 2 { 3 //获得对象 4 AcDbObject* pObj = selectObject(AcDb::kForRead); 5 6 if (!pObj) 7 { 8 acutPrintf(L"Error selecting object\n"); 9 return;10 }11 12 //获得将要添加到扩展数据中的应用程序名和字符串13 ACHAR appName[132], resString[200];14 appName[0] = resString[0] = '\0';15 16 acedGetString(NULL, L"Enter application name:",appName);17 acedGetString(NULL,L"Enter string to be addec:", resString);18 19 struct resbuf *pRb, *pTemp;20 21 //添加扩展数据22 pRb = pObj->xData(appName);23 24 if (pRb != NULL)25 {26 //如果对象上已经有扩展数据,则到链表层27 for (pTemp = pRb; pTemp->rbnext != NULL; pTemp = pTemp->rbnext)28 {29 30 }31 }32 else33 {34 //如果扩展数据不存在,则注册该应用程序,然后将应用程序名添加到链表的首部35 acdbRegApp(appName);36 37 pRb = acutNewRb(AcDb::kDxfRegAppName);38 pTemp = pRb;39 pTemp->resval.rstring = (ACHAR *)malloc(_tcslen(appName)+1);40 _tcscpy(pTemp->resval.rstring, appName);41 }42 43 //将用户指定的数据添加到扩展数据中44 pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);45 pTemp = pTemp->rbnext;46 pTemp->resval.rstring = (ACHAR *)malloc(_tcslen(resString)+1);47 _tcscpy(pTemp->resval.rstring, resString);48 49 //下面使用upgradeOpen()函数对象的打开方式变为写方式50 pObj->upgradeOpen();51 pObj->setXData(pRb);52 pObj->close();53 acutRelRb(pRb);54 }
读取扩展数据
1 static void lzxARX_AcDbDatabasePrintXData(void) 2 { 3 //选择对象并打开该该对象 4 AcDbObject* pObj; 5 if ((pObj = selectObject(AcDb::kForRead)) == NULL) 6 { 7 return; 8 } 9 10 //获得应用程序名称11 ACHAR appName[133];12 if (acedGetString(NULL,L"\nEnter the desired Xdata application name:", appName) != RTNORM)13 {14 return;15 }16 17 //获得对应于应用程序名称的扩展数据18 struct resbuf *pRb;19 pRb = pObj->xData(appName);20 21 if (pRb!=NULL)22 {23 //如果存在数据,则打印扩展数据24 printList(pRb);25 acutRelRb(pRb);26 }27 else28 {29 //没有扩展数据30 acutPrintf(L"\nNo xdata for this sppname");31 }32 pObj->close();33 }
下面是两个自定义函数
1 //打印链表中的数据 2 static void printList(struct resbuf* pRb) 3 { 4 int rt, i; 5 ACHAR buf[133]; 6 7 //对链表中数据的类型进行判断 8 for(i = 0; pRb != NULL; i++, pRb = pRb->rbnext) 9 { 10 if (pRb->restype < 1010) 11 { 12 rt = RTSTR; 13 } 14 else if (pRb->restype < 1040) 15 { 16 rt = RT3DPOINT; 17 } 18 else if (pRb->restype < 1060) 19 { 20 rt = RTREAL; 21 } 22 else if (pRb->restype < 1071) 23 { 24 rt = RTSHORT; 25 } 26 else if (pRb->restype = 1071) 27 { 28 rt = RTLONG; 29 } 30 else 31 { 32 //未知类型 33 rt = pRb->restype; 34 } 35 36 //对不同类型数据进行处理 37 switch(rt) 38 { 39 //短整型数据 40 case RTSHORT: 41 if (pRb->restype == RTSHORT) 42 { 43 acutPrintf(L"RTSHORT : %d\n", pRb->resval.rint); 44 } 45 else 46 { 47 acutPrintf(L"(%d . %d)\n",pRb->restype,pRb->resval.rint); 48 } 49 break; 50 51 //实型数据 52 case RTREAL: 53 if (pRb->restype == RTREAL) 54 { 55 acutPrintf(L"RTREAL : %0.3f\n", pRb->resval.rreal); 56 } 57 else 58 { 59 acutPrintf(L"(%d . %0.3f)\n",pRb->restype,pRb->resval.rreal); 60 } 61 break; 62 63 //这符串型数据 64 case RTSTR: 65 if (pRb->restype == RTSTR) 66 { 67 acutPrintf(L"RTSTR : %s\n", pRb->resval.rstring); 68 } 69 else 70 { 71 acutPrintf(L"(%d . \"%s\")\n", pRb->restype, pRb->resval.rstring); 72 } 73 break; 74 75 //三维点 76 case RT3DPOINT: 77 if (pRb->restype == RT3DPOINT) 78 { 79 acutPrintf(L"RT3DPOINT : %0.3f, %0.3f, %0.3f,\n", 80 pRb->resval.rpoint[X], 81 pRb->resval.rpoint[Y], 82 pRb->resval.rpoint[Z]); 83 } 84 else 85 { 86 acutPrintf(L"(%d %0.3f, %0.3f, %0.3f,\n", 87 pRb->restype, 88 pRb->resval.rpoint[X], 89 pRb->resval.rpoint[Y], 90 pRb->resval.rpoint[Z]); 91 } 92 break; 93 94 //长整型 95 case RTLONG: 96 acutPrintf(L"RTLONG : %d\n", pRb->resval.rlong); 97 break; 98 } 99 100 //数据处理完成101 if ((i == 23) && (pRb->rbnext != NULL))102 {103 i = 0;104 acedGetString(0, L"Pressto continue...", buf);105 }106 }107 }
1 //获得对象并打开该对象的函数 2 static AcDbObject* selectObject(AcDb::OpenMode openMode) 3 { 4 //提示用户选择实体 5 int ss; 6 ads_name en; 7 ads_point pt; 8 ss = acedEntSel(L"\nSelect an Entity: ", en, pt); 9 10 AcDbObjectId eId;11 12 //实体选择错误13 if(ss != RTNORM)14 {15 acutPrintf(L"Selection error, Return Code = %d\n", ss);16 return NULL;17 }18 19 //通过ads_name获得对象ID20 Acad::ErrorStatus retStat;21 retStat = acdbGetObjectId(eId, en);22 if (retStat != Acad::eOk)23 {24 //没胡获得对象ID25 acutPrintf(L"\nacadGelObjectId failed");26 acutPrintf(L"\nen == (%lx,%lx), restStat == %d\n", en[0], en[1], eId);27 return NULL;28 }29 30 AcDbObject* obj;31 32 //打开对象33 if ((retStat = acdbOpenObject(obj, eId, openMode)) != Acad::eOk)34 {35 acutPrintf(L"acdbOpenEntiy failed: ename: (%lx,%lx), mode:%d retStat:%d", en[0], en[1], openMode, retStat);36 return NULL;37 }38 39 //返回对象指针40 return obj;41 }