明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4117|回复: 3

按实体对象提取数据到Excel

[复制链接]
发表于 2007-12-28 11:11:00 | 显示全部楼层 |阅读模式
  1. Sub ReadEntityData()
  2.   Dim Obj As AcadEntity
  3.   Dim sel As AcadSelectionSet
  4.   Dim seldata(0) As Variant, selcode(0) As Integer
  5.   Dim gpdata As Variant, gpcode As Variant
  6.   Dim pt(0 To 2) As Double, pt1(0 To 2) As Double
  7.     With ThisDrawing
  8.         ret = .Utility.GetPoint(, "指定左上角:")
  9.         SetRet ret, pt
  10.         ret = .Utility.GetCorner(pt, "指定对角点:")
  11.         SetRet ret, pt1
  12.         Set sel = .SelectionSets.Add("ss")
  13.         selcode(0) = 0: gpcode = selcode
  14.         seldata(0) = "Line": gpdata = seldata
  15.         sel.Select acSelectionSetCrossing, pt, pt1, gpcode, gpdata
  16.         Debug.Print "Line ", sel.Count
  17.         .SelectionSets.Item("ss").Clear
  18.         'Set sel = .SelectionSets.Add("ss")
  19.         selcode(0) = 0: gpcode = selcode
  20.         seldata(0) = "Dimension": gpdata = seldata
  21.         sel.Select acSelectionSetCrossing, pt, pt1, gpcode, gpdata
  22.         Debug.Print "Dimension ", sel.Count
  23.         
  24.         .SelectionSets.Item("ss").Delete
  25.     End With
  26.    
  27. End Sub
  28. Private Sub SetRet(ret As Variant, pt() As Double)
  29.     pt(0) = ret(0)
  30.     pt(1) = ret(1)
  31.     pt(2) = ret(2)
  32. End Sub
  33. Function xlApp() As Object
  34. '  Dim xlApp As Object    ' This Line ,Not set Excel , run Excel
  35.      'Dim xlsheet As Object
  36.      
  37.      ' 发生错误时跳到下一个语句继续执行
  38.      On Error Resume Next
  39.      ' 连接Excel应用程序
  40.      Set xlApp = GetObject(, "Excel.Application")
  41.      
  42.      If Err.Number <> 0 Then
  43.          Set xlApp = CreateObject("Excel.Application")
  44.          xlApp.Visible = True
  45.          xlApp.Workbooks.Add
  46.      End If
  47.      ' 返回当前活动的工作表
  48. End Function
  49. Sub ObjectToExcel()
  50.   Set ArcXlsheet = xlApp.Sheets(1)
  51.   ArcXlsheet.Name = "Arc"
  52.   Set CircleXlSheet = xlApp.Sheets(2)
  53.   CircleXlSheet.Name = "Circle"
  54.   Set PolylineXlSheet = xlApp.Sheets(3)
  55.   PolylineXlSheet.Name = "Polyline"
  56.   Set LineXlsheet = xlApp.Sheets.Add
  57.   LineXlsheet.Name = "Line"
  58.   Set Mtextxlsheet = xlApp.Sheets.Add
  59.   Mtextxlsheet.Name = "MText"
  60.   Set TextXlSheet = xlApp.Sheets.Add
  61.   TextXlSheet.Name = "Text"
  62.   Set DimensionXlSheet = xlApp.Sheets.Add
  63.   DimensionXlSheet.Name = "Dimension"
  64. End Sub
  65. Sub ls()
  66.   Dim Obj As AcadEntity
  67.   Dim sel As AcadSelectionSet, kk As AcadSelectionSet
  68.   Dim seldata(0) As Variant, selcode(0) As Integer
  69.   Dim gpdata As Variant, gpcode As Variant
  70.   Dim pt(0 To 2) As Double, pt1(0 To 2) As Double
  71.   
  72.     With ThisDrawing
  73.       'If Not IsNull(ThisDrawing.SelectionSets.Item("sss")) Then
  74.         'ThisDrawing.SelectionSets.Item("sss").Delete
  75.         Set sel = .SelectionSets.Add("sss")
  76.       'End If
  77.         '' Line
  78.         selcode(0) = 0: gpcode = selcode
  79.         seldata(0) = "Line": gpdata = seldata
  80.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  81.         Set kk = ReadLineAttribute(sel)
  82.         Debug.Print "Line ", sel.Count
  83.         '''
  84.         sel.Clear
  85.         seldata(0) = "Dimension": gpdata = seldata
  86.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  87.         Debug.Print "Dimension ", sel.Count
  88.         ''
  89.         sel.Clear
  90.         seldata(0) = "LWPOLYLINE": gpdata = seldata
  91.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  92.         Debug.Print "LWPOLYLINE ", sel.Count
  93.         ''
  94.         sel.Clear
  95.         seldata(0) = "POLYLINE": gpdata = seldata
  96.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  97.         Debug.Print "POLYLINE ", sel.Count
  98.         '''
  99.         sel.Clear
  100.         seldata(0) = "Text": gpdata = seldata
  101.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  102.         Debug.Print "Text ", sel.Count
  103.         ''
  104.         sel.Clear
  105.         seldata(0) = "MText": gpdata = seldata
  106.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  107.         Debug.Print "MText ", sel.Count
  108.         '' Arc Data
  109.         sel.Clear
  110.         seldata(0) = "Arc": gpdata = seldata
  111.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  112.         Set kk = ReadArcAttribute(sel)
  113.         ''
  114.         sel.Clear
  115.         seldata(0) = "Circle": gpdata = seldata
  116.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  117.         Debug.Print "Circle ", sel.Count
  118.         ''
  119.         sel.Clear
  120.         seldata(0) = "Ellipse": gpdata = seldata
  121.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  122.         Debug.Print "Ellipse ", sel.Count
  123.         ''
  124.         sel.Clear
  125.         seldata(0) = "HATCH": gpdata = seldata
  126.         sel.Select acSelectionSetAll, , , gpcode, gpdata
  127.         Debug.Print "Hatch ", sel.Count
  128.         
  129.         .SelectionSets.Item("sss").Delete
  130.     End With
  131. End Sub
  132. ' Function Line
  133. Function ReadLineAttribute(InputSel As AcadSelectionSet) As AcadSelectionSet
  134.   Dim ll As AcadLine
  135.   Set LineXlsheet = xlApp.Sheets("Line")
  136.   
  137.   ii = 1
  138.     With ll
  139.       For jj = 0 To 2
  140.         LineXlsheet.Cells(ii, jj + 1).Value = "StartPoint(" & jj & ")"
  141.         LineXlsheet.Cells(ii, jj + 4).Value = "EndPoint(" & jj & ")"
  142.         LineXlsheet.Cells(ii, jj + 7).Value = "Delta(" & jj & ")"
  143.       Next jj
  144.       LineXlsheet.Cells(ii, 10).Value = "Length"
  145.       LineXlsheet.Cells(ii, 11).Value = "Layer"
  146.       LineXlsheet.Cells(ii, 12).Value = "Linetype"
  147.       LineXlsheet.Cells(ii, 13).Value = "LinetypeScale"
  148.       LineXlsheet.Cells(ii, 14).Value = "Lineweight"
  149.       LineXlsheet.Cells(ii, 15).Value = "Color"
  150.     End With
  151.   
  152.   
  153.   ii = 2
  154.   For Each ll In InputSel
  155.     With ll
  156.       For jj = 0 To 2
  157.         LineXlsheet.Cells(ii, jj + 1).Value = Round(.StartPoint(jj), 2)
  158.         LineXlsheet.Cells(ii, jj + 4).Value = Round(.EndPoint(jj), 2)
  159.         LineXlsheet.Cells(ii, jj + 7).Value = Round(.Delta(jj), 2)
  160.       Next jj
  161.       LineXlsheet.Cells(ii, 10).Value = .Length
  162.       LineXlsheet.Cells(ii, 11).Value = .Layer
  163.       LineXlsheet.Cells(ii, 12).Value = .Linetype
  164.       LineXlsheet.Cells(ii, 13).Value = .LinetypeScale
  165.       LineXlsheet.Cells(ii, 14).Value = .Lineweight
  166.       LineXlsheet.Cells(ii, 15).Value = .color
  167.     End With
  168.     ii = ii + 1
  169.   Next ll
  170. End Function
  171. '''Function Arc
  172. Function ReadArcAttribute(InputSel As AcadSelectionSet) As AcadSelectionSet
  173.   Dim Aa As AcadArc
  174.   Set ArcXlsheet = xlApp.Sheets("Arc")
  175.   
  176.   ii = 1
  177.     With Aa
  178.       For jj = 0 To 2
  179.         ArcXlsheet.Cells(ii, jj + 1).Value = "Center(" & jj & ")"
  180.         ArcXlsheet.Cells(ii, jj + 4).Value = "StartPoint(" & jj & ")"
  181.         ArcXlsheet.Cells(ii, jj + 7).Value = "EndPoint(" & jj & ")"
  182.         
  183.       Next jj
  184.       ArcXlsheet.Cells(ii, 10).Value = "StartAngle"
  185.       ArcXlsheet.Cells(ii, 11).Value = "EndAngle"
  186.       ArcXlsheet.Cells(ii, 12).Value = "TotalAngle"
  187.       ArcXlsheet.Cells(ii, 13).Value = "Radius"
  188.       ArcXlsheet.Cells(ii, 14).Value = "ArcLength"
  189.       ArcXlsheet.Cells(ii, 15).Value = "Area"
  190.       ArcXlsheet.Cells(ii, 16).Value = "Layer"
  191.       ArcXlsheet.Cells(ii, 17).Value = "Linetype"
  192.       ArcXlsheet.Cells(ii, 18).Value = "LinetypeScale"
  193.       ArcXlsheet.Cells(ii, 19).Value = "Lineweight"
  194.       ArcXlsheet.Cells(ii, 20).Value = "color"
  195.     End With
  196.   
  197.   
  198.   ii = 2
  199.   For Each Aa In InputSel
  200.     With Aa
  201.       For jj = 0 To 2
  202.         ArcXlsheet.Cells(ii, jj + 7).Value = Round(.Center(jj), 2)
  203.         ArcXlsheet.Cells(ii, jj + 1).Value = Round(.StartPoint(jj), 2)
  204.         ArcXlsheet.Cells(ii, jj + 4).Value = Round(.EndPoint(jj), 2)
  205.         
  206.       Next jj
  207.       
  208.       ArcXlsheet.Cells(ii, 10).Value = .StartAngle
  209.       ArcXlsheet.Cells(ii, 11).Value = .EndAngle
  210.       ArcXlsheet.Cells(ii, 12).Value = .TotalAngle
  211.       ArcXlsheet.Cells(ii, 13).Value = .Radius
  212.       ArcXlsheet.Cells(ii, 14).Value = .ArcLength
  213.       ArcXlsheet.Cells(ii, 15).Value = .Area
  214.       ArcXlsheet.Cells(ii, 16).Value = .Layer
  215.       ArcXlsheet.Cells(ii, 17).Value = .Linetype
  216.       ArcXlsheet.Cells(ii, 18).Value = .LinetypeScale
  217.       ArcXlsheet.Cells(ii, 19).Value = .Lineweight
  218.       ArcXlsheet.Cells(ii, 20).Value = .color
  219.     End With
  220.     ii = ii + 1
  221.   Next
  222. End Function
发表于 2009-3-6 20:47:00 | 显示全部楼层
顶,再加点注释就更好了
发表于 2009-3-6 21:02:00 | 显示全部楼层

为什么只写了,ReadArcAttribute和ReadLineAttribute

其它得呢,我想要个ReadTextAttribute,不知楼主可否提供,多谢

发表于 2009-3-7 10:22:00 | 显示全部楼层

我没有用过vb程序

请问这个程序怎么用,能不能提供一个具体的加载方法

谢谢!

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 04:41 , Processed in 0.187033 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表