|
上面讲的概念对于AutoCAD编程是很重要的,因为容器对象经常包含不同类型的对象。你会在AutoCAD程序的开发中经常碰到这种类型转化。
请定义一个名为EmployeeCount()的函数,函数的结构如上所示,它用来统计模型空间中的块索引的数目。这个函数不会输出任何东西,但你可以使用逐步调试程序来查看整数变量的增加(每发现一个块索引对象)。
8)
接下来,为了把结果输出到命令行,我们需要使用Application.DocumentManager.MdiActiveDocument.Editor对象的服务。要使用它,请加入下面的代码:
Imports
Autodesk.AutoCAD.EditorInput
Imports
Autodesk.AutoCAD.ApplicationServices
在函数的内部:
Dim ed As Editor =
Application.DocumentManager.MdiActiveDocument.Editor
最后,在循环的后面确定找到了多少个块索引:
ed.WriteMessage("Employees
Found: {0}" + ControlChars.Lf, nEmployeeCount.ToString())
End of Lab
4:
第四章结束
下面的代码片断演示了怎样获取Employee对象的所有内容,包括ACME_DIVISION字典中的部门经理的名字。这部分要在后面的章节中使用,但因为它和本章有关,因此我们把它放在本章作介绍。如果有时间的话,请阅读一下其中的代码来看看它是怎么使用的。它可以被直接放到你的类中并可以运行。命令的名字是PRINTOUTEMPLOYEE。ListEmployee()函数接收一个ObjectId参数,它通过一个ref类型的字符串数组返回值(包含相应的雇员数据)。调用它的PrintoutEmployee()函数只是用来在命令行中输出这些数据。
''''我们需要一个遍历并显示所有雇员数据的命令。
Public Shared Sub
ListEmployee(ByVal employeeId As ObjectId, ByRef
saEmployeeList() As String)
Dim
nEmployeeDataCount As Integer
Dim db
= HostApplicationServices.WorkingDatabase
Dim
trans As Transaction = db.TransactionManager.StartTransaction()
''''开始事务处理。
Try
Dim
ent As Entity = trans.GetObject(employeeId,
OpenMode.ForRead, False) ''''打开当前对象!
If TypeOf ent Is
BlockReference Then
''''不是所有的块索引都有雇员数据,所以我们要处理错误
Dim
bHasOurDict As Boolean
= True
Dim
EmployeeXRec As Xrecord
Try
Dim
br As BlockReference = CType(ent,
BlockReference)
Dim
extDict As DBDictionary =
trans.GetObject(br.ExtensionDictionary(), OpenMode.ForRead, False)
EmployeeXRec =
trans.GetObject(extDict.GetAt("EmployeeData"), OpenMode.ForRead, False)
Catch
bHasOurDict = False ''''出现了错误……字典或扩展记录不能访问
End
Try
If
bHasOurDict Then ''''如果获得扩展字典,而又有扩展记录……
''''调整雇员列表的大小来使得它能放三个条目...
ReDim
Preserve
saEmployeeList(saEmployeeList.GetUpperBound(0) + 4)
''''加入雇员的名字
Dim
resBuf As TypedValue = EmployeeXRec.Data.AsArray(0)
saEmployeeList.SetValue(String.Format("{0}" + ControlChars.Lf,
resBuf.Value), nEmployeeDataCount)
nEmployeeDataCount += 1
''''加入雇员的薪水
resBuf =
EmployeeXRec.Data.AsArray(1)
saEmployeeList.SetValue(String.Format("{0}" + ControlChars.Lf,
resBuf.Value), nEmployeeDataCount)
nEmployeeDataCount += 1
''''加入雇员所在的部门
resBuf =
EmployeeXRec.Data.AsArray(2)
Dim
str As String =
resBuf.Value()
saEmployeeList.SetValue(String.Format("{0}" + ControlChars.Lf,
resBuf.Value), nEmployeeDataCount)
nEmployeeDataCount += 1
&n 上一页 [1] [2] [3] [4] 下一页 |