cheng5276 发表于 2014-9-1 17:12:00

请教 关于设置扩展数据 奇特现象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk..Runtime;
using Autodesk.AutoCAD.EditorInput;
using System.Drawing;
using Autodesk.AutoCAD.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using Document = Autodesk.AutoCAD.ApplicationServices.Document;
using Autodesk.AutoCAD.LayerManager;
using Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices.Filters;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Windows.ToolPalette;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.ApplicationServices;
namespace WPFClassLibrary
{
    public class Class1
    {
      static void AddRegAppTableRecord(string regAppName)
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Transaction trans = doc.TransactionManager.StartTransaction();
            RegAppTable rat = (RegAppTable)trans.GetObject(db.RegAppTableId, OpenMode.ForRead, false);
            if (!rat.Has(regAppName))
            {
                rat.UpgradeOpen();
                RegAppTableRecord ratr = new RegAppTableRecord();
                ratr.Name = regAppName;
                rat.Add(ratr);
                trans.AddNewlyCreatedDBObject(ratr, true);
            }
            trans.Commit();
            trans.Dispose();
      }
static public DBObject Selectobj(string word)
{
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\n请选择实体:");
            PromptEntityResult per = ed.GetEntity(peo);
            DBObject obj=null;
            if (per.Status == PromptStatus.OK)
            {
                Transaction trans = doc.TransactionManager.StartTransaction();
                obj = trans.GetObject(per.ObjectId, OpenMode.ForWrite);
                trans.Commit();
                trans.Dispose();
            }
   return obj;
}

static public void SXDa()
{
   SetXData(Selectobj("请选择对象aa:"));
}
static public void SetXData(DBObject obj)
{
         AddRegAppTableRecord("cheng5276");
         ResultBuffer rb = new ResultBuffer();
         rb.Add(new TypedValue(1001, "cheng5276"));
         rb.Add(new TypedValue(1000, "only ascii under AutoCAD 2006"));
         rb.Add(new TypedValue(1000, "max length is 255"));
         obj.XData = rb;
         rb.Dispose();
   }
         
      static public void SetXData2()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptEntityOptions peo = new PromptEntityOptions("\n请选择实体:");
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                Transaction trans = doc.TransactionManager.StartTransaction();
                DBObject obj = trans.GetObject(per.ObjectId, OpenMode.ForWrite);
                AddRegAppTableRecord("Test");
                ResultBuffer rb = new ResultBuffer();
                rb.Add(new TypedValue(1001, "Test"));
                rb.Add(new TypedValue(1000, "only ascii under AutoCAD 2006"));
                rb.Add(new TypedValue(1000, "max length is 255"));
                obj.XData = rb;
                rb.Dispose();
                trans.Commit();
                trans.Dispose();
            }
      }
    }
}
http://bbs.mjtd.com/forum.php?mod=attachment&aid=ODQ2NDZ8YjY1MGE5NTB8MTY1OTA0MTIyN3w3MzI2OTE4fDExMTI3Ng%3D%3D&noupdate=yes&nothumb=yes
请教,当我采用GXD2命令选择对象并设置器扩展数据,程序很顺利
但是当我将GXD2拆分成两个函数(分别为选择对象和设置扩展数据时),见SXD的命令及函数,程序就会弹出致命错误了,小弟初学C# CAD 编程,恭请大侠们指点迷津,拜谢了!

雪山飞狐_lzh 发表于 2014-9-1 17:37:00

Selectobj事务提交后自动将相关的对象销毁了
这里你应该返回objectid 在主程序或者setxdata里开事务

cheng5276 发表于 2014-9-3 09:49:00

谢谢老大的指点,小弟继续研究
页: [1]
查看完整版本: 请教 关于设置扩展数据 奇特现象