这段程序的目的是把一个图中所有圆的编号同圆心坐标一同写入到EXCLE中(其中圆编号就是在圆边上用MTEXT注明的一个编号。
Sub ctoe() Dim rownum As Integer Dim Found As Boolean Dim MyObject As AcadEntity
Dim MyObject1 As AcadEntity
rownum = 2 Found = False For Each MyObject In ThisDrawing.ModelSpace '在模型空间中遍历所有的图元 If StrComp(MyObject.EntityName, "acdbcircle", 1) = 0 Then '这一句是判断对象是否是圆
If rownum = 2 Then '若是圆对象 Dim Excel As Excel.Application Dim ExcelWorkbook As Object Dim ExcelSheet As Object Set Excel = New Excel.Application '启动EXCEL Set ExcelWorkbook = Excel.Workbooks.Add Set ExcelSheet = Excel.ActiveSheet 'Excel.Visible = True '显示EXCEL Dim pt '(0 To 2) '定义数组变量,存储圆心坐标 Dim radius '圆半径 For Each MyObject1 In ThisDrawing.ModelSpace /在模型空间中遍历所有的图元
If StrComp(MyObject1.EntityName, "acdbMTEXT", 1) = 0 Then '这一句是判断对象是否是MTEXT If rownum = 2 Then '若是MTEXT对象 Dim pt_text '(0 To 2) '定义数组变量,存储MTEXT坐标
pt = MyObject.Center pt_text = MyObject1.InsertionPoint Dim Distance As Double '计算距离
Dim x As Double Dim y As Double Dim z As Double x = pt(0) - pt_text(0) y = pt(1) - pt_text(1) z = pt(2) - pt_text(2) Distance = Sqr((Sqr((x ^ 2) + (y ^ 2)) ^ 2) + (z ^ 2)) radius = MyObject.radius If Distance <= 4 * radius Then '如果距离小于四倍圆半径则该文本就是圆的编号
pt = MyObject.Center ExcelSheet.Cells(rownum, 1) = MyObject1.TextString '圆的编号
(rownum, 2) = pt(0) '圆心坐标X值 ExcelSheet.Cells(rownum, 3) = pt(1) '圆心坐标Y值 ExcelSheet.Cells(rownum, 4) = pt(2) '圆心坐标Z值 rownum = rownum + 1 Found = True '将标记设成 True。 End If '结束IF Next MyObject1 '遍历下一个文本对象
Next MyObject '遍历下一个对象
If Found = True Then ExcelSheet.Cells(1, 1) = "编号" ExcelSheet.Cells(1, 2) = "X" ExcelSheet.Cells(1, 3) = "Y" ExcelSheet.Cells(1, 4) = "Z" MsgBox "圆心坐标输出完毕,请检阅!" Excel.Visible = True '显示EXCEL Set ExcelSheet = Nothing Set ExcelWorkbook = Nothing Set Excel = Nothing Else MsgBox "在当前模型空间中未找到圆对象!" End If
End Sub
本程序根据前人程序修改而成,大家看看这程序的问题出在哪,思路有无问题,万望高人多指点,俺是初学VBA!
|