乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 47|回复: 2

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

[复制链接]

32

主题

140

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
268
发表于 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;
}
[CommandMethod("SXD")]
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();
     }
         [CommandMethod("SXD2")]
        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();
            }
        }
    }
}

                               
登录/注册后可看大图

请教,当我采用GXD2命令选择对象并设置器扩展数据,程序很顺利
但是当我将GXD2拆分成两个函数(分别为选择对象和设置扩展数据时),见SXD的命令及函数,程序就会弹出致命错误了,小弟初学C# CAD 编程,恭请大侠们指点迷津,拜谢了!
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2014-9-1 17:37:00 | 显示全部楼层
Selectobj事务提交后自动将相关的对象销毁了
这里你应该返回objectid 在主程序或者setxdata里开事务
回复

使用道具 举报

32

主题

140

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
268
发表于 2014-9-3 09:49:00 | 显示全部楼层
谢谢老大的指点,小弟继续研究
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 21:15 , Processed in 1.012871 second(s), 58 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表