AutoCAD Managed .NET API允许您使用使用. NET 4.0 引入的动态语言运行时 (DLR)。

使用DLR可以直接访问对象, 而无需:

  • 打开一个对象进行读取或写入, 然后在完成后关闭该对象。
  • 利用事务提交所做的更改。

在使用DLR时获得对象的ObjectId后, 可以直接访问对象的属性和方法。获得ObjectId后, 可以将ObjectId分配给数据类型的变量:

  • Object in VB.NET
  • dynamic in C#

获取ObjectId因对象保存到数据库的方式而异。对于存储在表或字典中的对象, 可以通过以下方式访问其ObjectId:

  • 使用ObjectId的 item 方法访问集合中的元素。
  • 创建对表或字典的ObjectId的引用并将其分配给变量, 然后访问数组的元素。

下面的示例代码显示了用于访问存储在与DLR的表或字典中的对象的两个选项:

C#
// Item method
dynamic acCurDb = HostApplicationServices.WorkingDatabase;
dynamic acMSpace = acCurDb.BlockTableId.Item(BlockTableRecord.ModelSpace); // Reference an element directly from a collection
dynamic acCurDb = HostApplicationServices.WorkingDatabase;
dynamic acBlkTbl = acCurDb.BlockTableId;
dynamic acMSpace = acBlkTbl[BlockTableRecord.ModelSpace];

重要提示: 在使用DLR和C#时, 您需要引用Microsoft.CSharp库。

使用GetEnumerator方法

在将 GetEnumerator 方法与DLR一起使用时, 需要在使用完枚举器对象后显式释放该对象。下面的示例演示如何在完成枚举器时对其进行处置。

C#
dynamic acCurDb = HostApplicationServices.WorkingDatabase;
var acLtypeTbl = acCurDb.LinetypeTableId;
var acTblEnum = acLtypeTbl.GetEnumerator();
...
acTblEnum.Dispose();

使用LINQ查询

您可以利用LINQ查询在使用DLR的图形中查询表或字典的内容。下面的示例演示如何使用 LINQ查询来查询哪些图层在当前图形中具有分配给它们的某些状态。

C#
[CommandMethod("LINQ")]
public static void LINQExample()
{
dynamic db = HostApplicationServices.WorkingDatabase;
dynamic doc = Application.DocumentManager.MdiActiveDocument; var layers = db.LayerTableId;
for (int i = ; i < ; i++)
{
var newrec = layers.Add(new LayerTableRecord());
newrec.Name = "Layer" + i.ToString();
if (i == )
newrec.IsFrozen = true;
if (i == )
newrec.IsOff = true;
} var OffLayers = from l in (IEnumerable<dynamic>)layers
where l.IsOff
select l; doc.Editor.WriteMessage("\nLayers Turned Off:"); foreach (dynamic rec in OffLayers)
doc.Editor.WriteMessage("\n - " + rec.Name); var frozenOrOffNames = from l in (IEnumerable<dynamic>)layers
where l.IsFrozen == true || l.IsOff == true
select l; doc.Editor.WriteMessage("\nLayers Frozen or Turned Off:"); foreach (dynamic rec in frozenOrOffNames)
doc.Editor.WriteMessage("\n - " + rec.Name);
}

示例代码

此页上的示例代码使用以下名称空间:

Autodesk.AutoCAD.Runtime
Autodesk.AutoCAD.ApplicationServices
Autodesk.AutoCAD.DatabaseServices
Autodesk.AutoCAD.Colors
Autodesk.AutoCAD.Geometry

下面的示例代码演示使用和不适用DLR如何将直线添加到当前空间。

[CommandMethod("ADDLINE")]
public static void AddLine()
{
// Get the current database
Database acCurDb = HostApplicationServices.WorkingDatabase; // Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord; // Create a line that starts at 5,5 and ends at 12,3
using (Line acLine = new Line(new Point3d(, , ),
new Point3d(, , )))
{
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
} // Save the new object to the database
acTrans.Commit();
}
}

C# with Dynamic Language Runtime (DLR)

[CommandMethod("ADDLINE")]
public static void AddLine()
{
// Get the current database
dynamic acCurDb = HostApplicationServices.WorkingDatabase; // Create a dynamic reference to model or paper space
dynamic acSpace = acCurDb.CurrentSpaceId; // Create a line that starts at 5,5 and ends at 12,3
dynamic acLine = new Line(new Point3d(, , ),
new Point3d(, , )); // Add the new object to the current space
acSpace.AppendEntity(acLine);
}

下面的示例代码演示使用和不使用DLR如何将层添加到当前数据库中。

[CommandMethod("ADDLAYER")]
public static void AddLayer()
{
// Get the current database
Database acCurDb = HostApplicationServices.WorkingDatabase; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Returns the layer table for the current database
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable; // Check to see if MyLayer exists in the Layer table
if (acLyrTbl.Has("MyLayer") != true)
{
// Open the Layer Table for write
acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite); // Create a new layer named "MyLayer"
using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
{
acLyrTblRec.Name = "MyLayer"; // Assign the ACI color 3 to the new layer
Color acClr = Color.FromColorIndex(ColorMethod.ByAci, );
acLyrTblRec.Color = acClr; // Add the new layer table record to the layer table and the transaction
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
} // Commit the changes
acTrans.Commit();
} // Dispose of the transaction
}
}

C# with Dynamic Language Runtime (DLR)

[CommandMethod("ADDLAYER")]
public static void AddLayer()
{
// Get the current database
dynamic acCurDb = HostApplicationServices.WorkingDatabase; dynamic acLyrTbl = acCurDb.LayerTableId; // Check to see if MyLayer exists in the Layer table
if (acLyrTbl.Has("MyLayer") != true)
{
// Create a new layer named "MyLayer"
dynamic acLyrTblRec = new LayerTableRecord();
acLyrTblRec.Name = "MyLayer"; // Assign the ACI color 3 to the new layer
dynamic acClr = Color.FromColorIndex(ColorMethod.ByAci, );
acLyrTblRec.Color = acClr; // Add the new layer table record to the layer table
acLyrTbl.Add(acLyrTblRec);
}
}

下面演示了使用和不使用DLR如何逐步通过并列出当前空间中的所有对象。

[CommandMethod("LISTOBJECTS")]
public static void ListObjects()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = HostApplicationServices.WorkingDatabase; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table record Model space for write
BlockTableRecord acSpace;
acSpace = acTrans.GetObject(acCurDb.CurrentSpaceId,
OpenMode.ForRead) as BlockTableRecord; // Step through the current space
foreach (ObjectId objId in acSpace)
{
// Display the class and current layer of the object
Entity acEnt = (Entity)acTrans.GetObject(objId, OpenMode.ForRead);
acDoc.Editor.WriteMessage("\nObject Class: " + acEnt.GetRXClass().Name +
"\nCurrent Layer: " + acEnt.Layer +
"\n");
}
acTrans.Commit();
}
}

C# with Dynamic Language Runtime (DLR)

[CommandMethod("LISTOBJECTS")]
public static void ListObjects()
{
// Get the current document and database
dynamic acDoc = Application.DocumentManager.MdiActiveDocument;
dynamic acCurDb = HostApplicationServices.WorkingDatabase; // Create a dynamic reference to model or paper space
dynamic acSpace = acCurDb.CurrentSpaceId; // Step through the current space
foreach (dynamic acEnt in acSpace)
{
// Display the class and current layer of the object
acDoc.Editor.WriteMessage("\nObject Class: " + acEnt.GetRXClass().Name +
"\nCurrent Layer: " + acEnt.Layer +
"\n");
}
}

关于使用动态语言运行时 (. net)的更多相关文章

  1. 《精通C#》第十六章-动态类型和动态语言运行时-第一节至第四节

    在.Net4.0中引入了一个关键字dynamic,这是一个动态类型关键字.Net中还有一个关键字是var,这是一个隐式类型,可以定义本地变量,此时var所代表的实际的数据类型有编译器在初次分配时决定, ...

  2. 日志系统实战(二)-AOP动态获取运行时数据

    介绍 这篇距上一篇已经拖3个月之久了,批评自己下. 通过上篇介绍了解如何利用mono反射代码,可以拿出编译好的静态数据.例如方法参数信息之类的. 但实际情况是往往需要的是运行时的数据,就是用户输入等外 ...

  3. C语言运行时数据结构

    段(Segment): 对象文件/可执行文件: SVr4 UNIX上被称为ELF(起初"Extensible Linker Format", 现在"Executable ...

  4. 使用Mono Cecil 动态获取运行时数据 (Atribute形式 进行注入 用于写Log) [此文报考 xxx is declared in another module and needs to be imported的解决方法]-摘自网络

    目录 一:普通写法 二:注入定义 三:Weave函数 四:参数构造 五:业务编写 六:注入调用 7.  怎么调用别的程序集的方法示例 8. [is declared in another module ...

  5. 动态链接--运行时加载dlopen

    前面我们在编译可执行文件时,如果可执行文件要依赖某个so.必须要通过-L指定so路径,并且-l指定so名字. 而且在可执行文件运行时,要先加载so的load部分到进程地址空间. 有一种方式可以在编译时 ...

  6. 在使用Qt5.8完成程序动态语言切换时遇到的问题

    因为之前了解过一些Qt国际化的东西,所以在写程序的时候需要显示给用户的字符都使用了 tr(" ")的形式,然后使用 Qt Linguist得到相应的 qm(Qt message)文 ...

  7. 类CL_ABAP_TYPEDESCR,动态取得运行时类型

    有时候我们要在程序运行的时候取得某个内表或者某个结构它的属性或者它的字段的属性,可能通过类CL_ABAP_TYPEDESCR和它的子类取得指定内表的属性.类CL_ABAP_TYPEDESCR和它的子类 ...

  8. 【C#表达式树 开篇】 Expression Tree - 动态语言

    .NET 3.5中新增的表达式树(Expression Tree)特性,第一次在.NET平台中引入了"逻辑即数据"的概念.也就是说,我们可以在代码里使用高级语言的形式编写一段逻辑, ...

  9. Cocoa 框架 For iOS(一) 框架的介绍,Objectivie-C运行时能力的解析等 (转载)

    http://blog.csdn.net/totogo2010/article/details/8081253 Cocoa框架是iOS应用程序的基础,了解Cocoa框架,对开发iOS应用有很大的帮助. ...

随机推荐

  1. github插件

    可能是迄今为止最好的GitHub代码浏览插件,基本实现浏览器变成代码阅读器,支持目录列表,交叉索引等功能: O网页链接 ​​​​ http://weibo.com/1963193953/Fdj2cFQ ...

  2. (1)spring boot起步之Hello World【从零开始学Spring Boot】

    Spring Boot交流平台 1.1 介绍 自从structs2出现上次的漏洞以后,对spring的关注度开始越来越浓. 以前spring开发需要配置一大堆的xml,后台spring加入了annot ...

  3. POJ 1944

    明天补上... 这道题的思路确实很精致.考虑到连的边肯定不会是一个环,所以至少有一个断点.于是,可以枚举这个断点.断点一确定,那么连边的走向也就确定了.用D[i]表示由i开始可以到达的最远点即可.对于 ...

  4. Hit 2255 Not Fibonacci

    今天下午刚起来眼睛就比較涨,,并且还有点恶心,唉.结果一直不在状态.并且这个题太坑了.. .. 点击此处即可传送 Hit 2255 Maybe ACMers of HIT are always fon ...

  5. SGU 531 - Bonnie and Clyde 预处理+二分

    Bonnie and Clyde Description Bonnie and Clyde are into robbing banks. This time their target is a to ...

  6. JavaScript(js)对象常用操作,JS操作JSON总结

    数据类型判断可以通过一元操作符typeof,此操作符可以判断大部分JS数据类型. 也可以通过instanceof来判断.如: var a = []; alert(typeof a); // objec ...

  7. NDK历史版本

    https://developer.android.google.cn/ndk/downloads/older_releases.html https://developer.android.goog ...

  8. bzoj3713: [PA2014]Iloczyn(乱搞)

    3713: [PA2014]Iloczyn 题目:传送门 题解: 随手一发水题x2 直接离线啊,斐波那契到了第五十个就炒鸡大了 代码: #include<cstdio> #include& ...

  9. Ubuntu16.04系统安装搜狗输入法

    前言:正常双击.deb软件包安装搜狗输入法会有bug,需要按照下面操作进行消除错误. 一.下载搜狗输入法Linux版软件包 下载地址为:http://pinyin.sogou.com/linux/ , ...

  10. HD-ACM算法专攻系列(20)——七夕节

    问题描述: AC源码: /**/ #include"iostream" #include"cmath" using namespace std; int mai ...