56854365 发表于 2006-5-5 20:38:00

[求助]求助一个关于扩展数据的问题

怎么样把以前添加的扩展数据更新成新的或删除
下面是网上找到的清除扩展数据的代码,我看不懂
void delXdata()
{
    ads_name eNam;
   
    ads_point pt;
   
    int ret = acedEntSel ("\nselect an object:", eNam, pt);   
   
    if ( ret!= RTNORM)   
    {
         return;   
    }

    AcDbObjectId ObjId;

    acdbGetObjectId(ObjId, eNam);

    AcDbEntity *pEnt;
    acdbOpenAcDbEntity(pEnt, ObjId, AcDb::kForWrite);
    resbuf *xdata = pEnt->xData(NULL);
    if (xdata)
    {
         xdata->rbnext = NULL;

         pEnt->setXData(xdata);
   
         acutRelRb(xdata);
    }
   
    pEnt->close();
}

heluyuan 发表于 2006-5-16 19:10:00

         public void delXdata()
         {
            ads_name eNam;
   
            ads_point pt;
   
            int ret = acedEntSel ("\nselect an object:", eNam, pt);   //点选获得一个实体
   
            if ( ret!= RTNORM)   
            {
                   return;   
            }

            AcDbObjectId ObjId;

            acdbGetObjectId(ObjId, eNam);    //获得实体的ID

            AcDbEntity *pEnt;

            acdbOpenAcDbEntity(pEnt, ObjId, AcDb::kForWrite);//通过ObjectID打开实体来编辑

            resbuf *xdata = pEnt->xData(NULL);   //获取实体的扩展数据结果缓冲区链表

            if (xdata)    //如果该扩展数据结果缓冲区链表不为空
            {
                   xdata->rbnext = NULL;//将该结果缓冲区链表的后续指针置为空

                   pEnt->setXData(xdata); //将该实体的扩展数据设为置为空了的结果缓冲区
   
                   acutRelRb(xdata);//释放链表
            }
   
            pEnt->close();
         }

heluyuan 发表于 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, resString;
    appName = resString = '\0';

    acedGetString(NULL, "Enter application name: ",
      appName);
    acedGetString(NULL, "Enter string to be added: ",
      resString);

   
    structresbuf*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, 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, 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#中的对应方法!!

aqin 发表于 2007-7-22 01:43:00

public void AddXData()
      {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("添加扩充数据XDATA\n");
            PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象\n");
            PromptEntityResult entRes;
            entRes = ed.GetEntity(entOps);
            if (entRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("选择对象失败,退出");
                return;
            }
            ObjectId objId = entRes.ObjectId;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Entity ent = trans.GetObject(objId, OpenMode.ForWrite) as Entity ;
                ent.ColorIndex = 1;

                RegAppTable appTbl = trans.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable ;
                if (!appTbl.Has("MyAppName"))
                {
                  RegAppTableRecord appTblRcd = new RegAppTableRecord();
                  appTblRcd.Name = "MyAppName";
                  appTbl.Add(appTblRcd);
                  trans.AddNewlyCreatedDBObject(appTblRcd, true);
                }
                ResultBuffer resBuf = new ResultBuffer();//new TypedValue(1001, "MyAppName"), new TypedValue(1000, "开发部门"));

                resBuf.Add(new TypedValue(1001, "MyAppName"));//注册程序名称
                resBuf.Add(new TypedValue(1000 , " Hongxian Qin"));//姓名
                resBuf.Add(new TypedValue(1000 , " 工程部"));//部门
                resBuf.Add(new TypedValue(1040, 2000.0));//薪水
                ent.XData =resBuf;
                trans.Commit();
            }
                       
      }

aqin 发表于 2007-7-22 01:44:00

      
      public void GETXDATA()
      {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("获取扩充数据XDATA\n");
            PromptEntityOptions entOps = new PromptEntityOptions("选择要打开的对象");
            PromptEntityResult entRes = ed.GetEntity(pr);
            if (entRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("选择对象失败,退出");
                return;
            }
            Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
                ResultBuffer resBuf = ent.XData;
                if (rb != null)
                {
                  //
                  IEnumerator iter = resBuf.GetEnumerator();
                  while (iter.MoveNext())
                  {
                        TypedValue tmpVal = (TypedValue)iter.Current;
                        ed.WriteMessage(tmpVal.TypeCode.ToString() + ":");
                        ed.WriteMessage(tmpVal.Value.ToString() + "\n");
                  }
                }
            }
      }
页: [1]
查看完整版本: [求助]求助一个关于扩展数据的问题