在用代码生成的实体上直接添加扩展数据就崩溃的问题
void addXdata(AcDbObject* pObj, ACHAR* appName,ACHAR* resString,
Adesk::Boolean isReplace = Adesk::kFalse) //如该appname已有扩展数据,kFalse为继续添加,kTrue替换为新的扩展数据
{
if ( !pObj ) {
acutPrintf(_T("\nNo object to addXdata"));
return;
}
MessageBox(NULL, _T("ok1"), _T("ok1"), 0);
structresbuf*pRb, *pTemp;
//返回pObj已有的appName的扩展数据链表
pRb = pObj->xData(appName);
if (pRb != NULL) {////如果已经包含以appName命名的扩展数据
if ( isReplace == Adesk::kFalse){////对已有以appName命名的扩展数据不替换,而是重复添加
// If xdata is present, then walk to the end of the list.
//指针移到连表尾,以便添加新的扩展数据
for (pTemp = pRb; pTemp->rbnext != NULL; pTemp = pTemp->rbnext)
{ ; }
}
else {////替换已有以appName命名的扩展数据
if (pRb->rbnext != NULL){
//把指针移到appName的链表的第二元素开始
pTemp = pRb->rbnext;
//把从第二个元素开始的链表部分清空
acutRelRb(pTemp);
}
//把指针移到appName的链表第一个元素
pTemp = pRb;
}
}
else {////还没有注册以appName命名的扩展数据
// If xdata is not present, register the application
// and add appName to the first resbuf in the list.
// Notice that there is no -3 group as there is in
// AutoLISP. This is ONLY the xdata so
// the -3 xdata-start marker isn't needed.
acdbRegApp(appName);////新注册扩展数据的应用程序名appName
pRb = acutNewRb(AcDb::kDxfRegAppName);////分配一个新的结果缓冲区,指定数据类型为"应用程序名",并返回指向缓冲区指针
pTemp = pRb;
if ( pTemp != NULL){
pTemp->resval.rstring = (ACHAR *) malloc(wcslen(appName) + 1);//分配存放字符串的缓冲区
wcscpy(pTemp->resval.rstring, appName);//对该“应用程序名”数据类型,把字符串数据类型应用程序名赋值给变量
}
}
MessageBox(NULL, _T("ok2"), _T("ok2"), 0);
if ( pTemp != NULL){
// Add user-specified string to the xdata.
//
pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);////分配一个新的结果缓冲区,指定数据类型为"字符串",并返回指向缓冲区指针
pTemp = pTemp->rbnext;
pTemp->resval.rstring = (ACHAR *)malloc(wcslen(resString) + 1);//分配存放字符串的缓冲区
wcscpy(pTemp->resval.rstring, resString);//对该“字符串”数据类型,把字符串类型扩展数据值赋值复制给变量
}
// The following code shows the use of upgradeOpen()
// to change the entity from read to write.
//
pObj->upgradeOpen();
MessageBox(NULL, _T("ok3"), _T("ok3"), 0);
if (pRb != NULL){
pObj->setXData(pRb);
acutRelRb(pRb);
}
pObj->downgradeOpen();
MessageBox(NULL, _T("ok4"), _T("ok4"), 0);
}
void Test()
{ AcDbObjectPointer<AcDbLine> pline;
pline.create();
// set the properties for it
pline->setStartPoint(AcGePoint3d(10,10,0));
pline->setEndPoint(AcGePoint3d(20,30,0));
// now add it to the current space
AcDbBlockTableRecordPointer curSpace(curDoc()->database()->currentSpaceId(), AcDb::kForWrite);
// if ok
if (curSpace.openStatus() == Acad::eOk){
Acad::ErrorStatus es = curSpace->appendAcDbEntity(pline);
// check that everything was ok
if (es != Acad::eOk){
// no need for a delete as the smartpointer does this for us
return;
}
}// everything will be closed automatically for us
addXdata(pline, _T("appName"), _T("resString"), kTrue);/////这里添加扩展数据,程序就崩溃,为什么?
pline->close();
curSpace->close();
}
AcDbObject *pObj1;
if ((pObj1 = selectOneObject(AcDb::kForRead)) != NULL){
// Get the application name and string to be added to xdata.
//
ACHARappName, resString;
appName = resString = '\0';
acedGetString(NULL, _T("\nEnter application name: "), appName);
acedGetString(NULL, _T( "\nEnter string to be added: "), resString);
addXdata(pObj1, appName, resString, Adesk::kTrue);
addXdata(pObj1, appName, (LONG)32, Adesk::kFalse);
printXdata(pObj1, appName);
pObj1->close();
}
如果是选一个实体,添加扩展数据,就不会崩溃
页:
[1]