找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 3978|回复: 0

[求助]:AutoCAD 2007.NET API中,在C#中如何给实体设置XData扩展属性

[复制链接]
发表于 2006-5-17 10:15:52 | 显示全部楼层 |阅读模式

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

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

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

本版积分规则

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

GMT+8, 2024-9-20 22:24 , Processed in 0.221073 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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