|
发表于 2006-5-17 10:13:00
|
显示全部楼层
本想发贴咨询一下在AutoCAD 2007.NET API中,用C#中如何给实体设置XData扩展属性,现就此贴延续一下了,希望能把XData彻底弄清楚。
在VC60下的XData的添加方法,Autodesk公司的示例文档中有很好的代码:
void
addXdata()
{
AcDbObject* pObj = selectObject(AcDb::kForRead);
if (!pObj) {
acutPrintf("Error selecting object\n");
return;
}
// Get the application name and string to be added to
// xdata.
//
char appName[132], resString[200];
appName[0] = resString[0] = '\0';
acedGetString(NULL, "Enter application name: ",
appName);
acedGetString(NULL, "Enter string to be added: ",
resString);
struct resbuf *pRb, *pTemp;
pRb = pObj->xData(appName);
if (pRb != NULL) {
// If xdata is present, then walk to the
// end of the list.
//
for (pTemp = pRb; pTemp->rbnext != NULL;
pTemp = pTemp->rbnext)
{ ; }
} else {
// 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);
pRb = acutNewRb(AcDb::kDxfRegAppName);
pTemp = pRb;
pTemp->resval.rstring
= (char*) malloc(strlen(appName) + 1);
strcpy(pTemp->resval.rstring, appName);
}
// Add user-specified string to the xdata.
//
pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);
pTemp = pTemp->rbnext;
pTemp->resval.rstring
= (char*) malloc(strlen(resString) + 1);
strcpy(pTemp->resval.rstring, resString);
// The following code shows the use of upgradeOpen()
// to change the entity from read to write.
//
pObj->upgradeOpen();
pObj->setXData(pRb);
pObj->close();
acutRelRb(pRb);
}
问题是在AutoCAD 2007.NET API下:
网上现有的只是关于Xrecord的示例代码,如下:
public ObjectId createXrecord(string name,string salary,string division)
{
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)MyT.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
BlockTableRecord br = (BlockTableRecord)MyT.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
//Create the custom per-employee data
Xrecord xRec = new Xrecord();
//We want to add //Name//, //Salary// and //Division// information. Here is how:
xRec.Data = new ResultBuffer(
new TypedValue((int)DxfCode.Text, name),
new TypedValue((int)DxfCode.Real, salary),
new TypedValue((int)DxfCode.Text, division));
//Next, we need to add this data to the //Extension Dictionary// of the employee.
br.CreateExtensionDictionary();
DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
brExtDict.SetAt("EmployeeData", xRec); //Set our XRecord in the dictionary at //EmployeeData//.
trans.AddNewlyCreatedDBObject(xRec, true);
return xRec.ObjectId;
}
catch
{
return ObjectId.Null;
}
}
但为何添加XData扩展数据就会报错eBadDxfSequence呢?
public void addXData(Entity ent,string name,string salary,string division)
{
Database db = HostApplicationServices.WorkingDatabase;
Transaction MyT = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)MyT.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
BlockTableRecord btr = (BlockTableRecord)MyT.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
ent.XData = new ResultBuffer();
ent.XData = new ResultBuffer(
new TypedValue((int)DxfCode.Text, name),
new TypedValue((int)DxfCode.Real, salary),
new TypedValue((int)DxfCode.Text, division));
btr.AppendEntity(ent);
MyT.AddNewlyCreatedDBObject(ent, true);
}
catch
{
}
}
后来又试过如下方法:
Autodesk.AutoCAD.DatabaseServices.SymbolUtilityServices.IsRegAppAcadName("name");
pEnt.XData = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName,"name"));
pEnt.XData.Add(new TypedValue((int)DxfCode.ExtendedDataControlString, "this is a test!"));
也报错……为eRegappIdNotFound,应该是因为未注册应用程序名的原因,
求教全局函数acdbRegApp(appName)在C#中的对应方法!!
|
|