请教C#能取到通过 (vlax-ldata-put)的扩展数据么
假设图中有一直线,通过 (vlax-ldata-put)给他赋予了扩展数据,请教老大们,c#能直接读取和设置这个数据么?请自行脑补 NamedObjectDictionary 本帖最后由 cheng5276 于 2014-9-18 17:34 编辑
可是老大,能请您说详细点么,我采用NamedObjectDictionary方式,并未成功,通过C#设置的扩展数据,通过(vlax-ldata-get (car(entsel))"cheng5276" )还是获取不了,还得恭请老大继续指点,拜谢了!
+++++++++++++++++以下为程序源码+++++++++++++++++
using Autodesk.AutoCAD.Runtime;
//using qApp = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using Document = Autodesk.AutoCAD.ApplicationServices.Document;
//
namespace Cheng5276_CAD
{
public class Main//: IExtensionApplication
{
//添加对象扩展数据
public void AddExtDict()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions entOps = new PromptEntityOptions("选择要添加扩展数据的对象\n");
PromptEntityResult entRes = ed.GetEntity(entOps);
if (entRes.Status != PromptStatus.OK)
{
ed.WriteMessage("选择对象失败,退出");
return;
}
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBObject obj = trans.GetObject(entRes.ObjectId, OpenMode.ForWrite) as DBObject;
obj.CreateExtensionDictionary();
DBDictionary extensionDict = (DBDictionary)trans.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite, false);
// 通过Xrecord准备附加属性数据
Xrecord xRec = new Xrecord();
xRec.Data = new ResultBuffer(
new TypedValue((int)DxfCode.Text, "张三"),// 姓名
new TypedValue((int)DxfCode.Real, 1200.0),//薪水
new TypedValue((int)DxfCode.Text, "技术部"));// 部门
// 在扩展词典中添加扩展记录
extensionDict.SetAt("cheng5276", xRec);
trans.AddNewlyCreatedDBObject(xRec, true);
trans.Commit();
}
}
//取得对象扩展数据
public void GetExtDict()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions entOps = new PromptEntityOptions("选择添加了扩展数据的块\n");
PromptEntityResult entRes = ed.GetEntity(entOps);
if (entRes.Status != PromptStatus.OK)
{
ed.WriteMessage("选择对象失败,退出");
return;
}
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
DBObject obj = trans.GetObject(entRes.ObjectId, OpenMode.ForWrite) as DBObject;
DBDictionary extensionDict = (DBDictionary)trans.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite, false);
Xrecord EmpXRec = (Xrecord)trans.GetObject(extensionDict.GetAt("cheng5276"), OpenMode.ForRead);
ResultBuffer resBuf = EmpXRec.Data;
TypedValue val = resBuf.AsArray();
ed.WriteMessage("出图比例为:{0}\n", val.Value);
trans.Commit();
}
}
}
}
+++++++++++++++++++++以下图形词典也无法通过(vlax-ldata-get)来取得数据
//--------------------------------------------------------------
// 功能:在命名对象词典中添加数据
// 作者:
// 日期:2007-7-20
// 说明:
//
//------------------------------------------------------------
public void AddInNOD()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("在命名对象词典中添加数据\n");
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//获取命名对象词典(NOD)
DBDictionary NOD =trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite) as DBDictionary ;
// 声明一个新的词典
DBDictionary copyrightDict;
// 判断是否存在COPYRIGHT词典,没有则创建
try
{
// 获取COPYRIGHT词典
copyrightDict = (DBDictionary)trans.GetObject(NOD.GetAt("cheng5276-DIC"), OpenMode.ForRead);
}
catch
{
//在NOD下创建COPYRIGHT词典
copyrightDict = new DBDictionary();
NOD.SetAt("cheng5276-DIC", copyrightDict);
trans.AddNewlyCreatedDBObject(copyrightDict, true);
}
// 在copyrightDict中,获取或创建 "author" 词典
DBDictionary authorDict;
try
{
authorDict = (DBDictionary)trans.GetObject(copyrightDict.GetAt("Author"), OpenMode.ForWrite);
}
catch
{
authorDict = new DBDictionary();
//"author" doesn't exist, create one
copyrightDict.UpgradeOpen();
copyrightDict.SetAt("Author", authorDict);
trans.AddNewlyCreatedDBObject(authorDict, true);
}
// 通过Xrecord和ResultBuffer添加扩展数据
Xrecord authorRec;
try
{
authorRec = (Xrecord)trans.GetObject(authorDict.GetAt("AuthorInfo"), OpenMode.ForWrite);
}
catch
{
authorRec = new Xrecord();
authorRec.Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, "张三"));
authorDict.SetAt("AuthorInfo", authorRec);
trans.AddNewlyCreatedDBObject(authorRec, true);
}
trans.Commit();
}
}
//--------------------------------------------------------------
// 功能:获取命名对象词典中的数据
// 作者:
// 日期:2007-7-20
// 说明:
//
//------------------------------------------------------------
//(vlax-ldata-GET"cheng5276-DIC" "AuthorInfo")
public void GetInNod()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("获取命名对象词典中数据\n");
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 获取NOD
DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, false);
// 获取COPYRIGHT词典
DBDictionary copyrightDict = (DBDictionary)trans.GetObject(NOD.GetAt("cheng5276-DIC"), OpenMode.ForRead);
// 获取Author词典
DBDictionary AuthorDict = (DBDictionary)trans.GetObject(copyrightDict.GetAt("Author"), OpenMode.ForRead);
// 获取AuthorInfo扩展记录Xrecord
Xrecord authorXRec = (Xrecord)trans.GetObject(AuthorDict.GetAt("AuthorInfo"), OpenMode.ForRead);
ResultBuffer resBuf = authorXRec.Data;
TypedValue val = resBuf.AsArray();
ed.WriteMessage("该图纸由{0}设计\n", val.Value);
}
}
ldata是lisp私有的 只能通过调用lisp函数的方式获取吧 相应数据类型会是ImpDBObject,在C#可以调用Lisp的注册函数读取 相应的类型确实是ImpDBObject,
老大能进一步解释下么?【在C#可以调用Lisp的注册函数读取】 就是想办法在C#里面调用Lisp的vlax-ldata-get函数呀,因为要求Lisp注册后才能被C#调用,所以我是另外再写个Lisp函数让C#调用,里面有vlax-ldata-get可以读取相关数据
页:
[1]