- 积分
- 5966
- 明经币
- 个
- 注册时间
- 2003-1-7
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 作者 于 2004-9-11 15:15:05 编辑
首先要指明的一点是,虽然.NET开发降低了ARX编程的门槛,但是并不意味着开发者可以在不了解AutoCAD内部实现机理的情况下进行ARX开发。所以,在使用.NET开发ARX应用程序之前,建议大家去看一下ARX版块中关于符号表、块表、符号表记录、块表记录、对象Id、和图形数据库的有关知识,在这里我就不具体讨论这些ARX的概念,直接对应ARX之VC版的帮助系统,开始写一些相应的代码片段。 本章将介绍几个基本的数据库操作。1 创建实体创建本节的函数之前,在类文件的开头引入下面的名称空间:- Imports Autodesk.AutoCAD.ApplicationServicesImports Autodesk.AutoCAD.DatabaseServicesImports Autodesk.AutoCAD.RuntimeImports Autodesk.AutoCAD.GeometryImports Autodesk.AutoCAD
复制代码 (1)创建直线的函数- <CommandMethod("Zff", "CreateLine", CommandFlags.Modal)> _ Public Shared Sub MyCommand() Dim startPt As New Point3d(4.0, 2.0, 0.0) Dim endPt As New Point3d(10.0, 7.0, 0.0) Dim line As Line Dim bt As BlockTable Dim btr As BlockTableRecord Try Dim db As Database = HostApplicationServices.WorkingDatabase Dim objId As ObjectId = db.BlockTableId ' 打开块表,注意:ObjectId.Open()取代了以前的acdbOpenObject函数 bt = objId.Open(OpenMode.ForRead) objId = bt.Item(btr.ModelSpace) btr = objId.Open(OpenMode.ForWrite) ' 创建直线 line = New Line(startPt, endPt) ' 将直线添加到块表记录中 btr.AppendEntity(line) Catch ' 这里不需要捕获错误 Finally ' 无论是否产生错误,Finally的语句都将被调用 ' 这里没有处理事务,因此手工关闭实体 line.Close() btr.Close() bt.Close() End Try End Sub
(2)创建圆的函数- <CommandMethod("Zff", "CreateCircle", CommandFlags.Modal)> _ Public Shared Sub CreateCircle() Dim ptCenter As New Point3d(9.0, 3.0, 0.0) Dim normal As New Vector3d(0.0, 0.0, 1.0) Dim objCircle As Circle Dim bt As BlockTable Dim btr As BlockTableRecord Try Dim db As Database = HostApplicationServices.WorkingDatabase Dim objId As ObjectId = db.BlockTableId bt = objId.Open(OpenMode.ForRead) objId = bt.Item(btr.ModelSpace) btr = objId.Open(OpenMode.ForWrite) objCircle = New Circle(ptCenter, normal, 2.0) btr.AppendEntity(objCircle) Catch Finally objCircle.Close() btr.Close() bt.Close() End Try End Sub
2 创建一个新图层- <CommandMethod("Zff", "CreateLayer", CommandFlags.Modal)> _ Public Shared Sub CreateLayer() Dim lt As LayerTable Dim ltr As LayerTableRecord Try Dim db As Database = HostApplicationServices.WorkingDatabase Dim objId As ObjectId = db.LayerTableId lt = objId.Open(OpenMode.ForWrite) ltr = New LayerTableRecord() ltr.Name = "ASDK_MYLAYER" lt.Add(ltr) Catch Finally ltr.Close() lt.Close() End Try End Sub
3 打开和关闭ObjectARX对象在VC++开发的ObjectARX应用程序中,使用下面的函数来打开一个对象:- acdbOpenObject(pObject, objId, AcDb::kForWrite);
复制代码 而在.NET开发的ObjectARX应用程序中,根据一个对象的Id获得其控制权,则使用ObjectId类的Open方法。值得注意的是,图形数据库中的对象在打开之后,必须在不用的时候关闭,否则就会引发异常。这在VC开发的ARX程序中也是经常遇到的一个错误。- Public Shared Function ChangeColor(ByVal entId As ObjectId, ByVal newColor As Colors.Color) _ As Autodesk.AutoCAD.Runtime.ErrorStatus Dim ent As Entity ent = entId.Open(OpenMode.ForWrite, True) ent.Color = newColor ent.Close() Return ErrorStatus.OK End Function
4 向组字典中添加一个组用AutoCAD操作者的观点来看,也就是使用GROUP命令在图形中创建了一个组。AutoCAD内部保存组的机理就是将组的名称和其对象写入到一个类似字典的数据类型中去。- Public Function CreateGroup(ByVal objIds As ObjectIdCollection, ByVal groupName As String) Dim objGroup As Group = New Group(groupName, True) Dim id As ObjectId Dim dbDic As DBDictionary For Each id In objIds objGroup.Append(id) Next Try Dim db As Database = HostApplicationServices.WorkingDatabase Dim dicId As ObjectId = db.GroupDictionaryId dbDic = dicId.Open(OpenMode.ForWrite) dbDic.SetAt(groupName, dbDic) Catch Finally dbDic.Close() objGroup.Close() End Try End Function
|
|