乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 196|回复: 12

Excel VBA以选择多段线并将区域带回Excel

[复制链接]

238

主题

769

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1721
发表于 2014-3-6 13:26:22 | 显示全部楼层 |阅读模式
有没有一个excel vba,如果我按下一个按钮,它将进入autocad会话,我将选择一条折线,然后将值报告回我开始的excel电子表格?(面积,z等。)?

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2014-3-6 14:02:37 | 显示全部楼层
选项明确
  1. Sub PickLwPolyAndGetData()
  2.    
  3.     Dim MyCell As Range
  4.     Dim ACAD As AcadApplication
  5.     Dim LWPoly As AcadLWPolyline
  6.     Dim ThisDrawing As AcadDocument
  7.     Dim Pt1 As Variant
  8.     Dim LWArea As Double, LWZ As Double
  9.     ' Autocad Session handling
  10.     On Error Resume Next
  11.     Set ACAD = GetObject(, "AutoCAD.Application")
  12.     On Error GoTo 0
  13.     If ACAD Is Nothing Then
  14.         Set ACAD = New AcadApplication
  15.         ACAD.Visible = True
  16.     End If
  17.     Set ThisDrawing = ACAD.ActiveDocument
  18.          
  19.    
  20.     ' select LwPolyline
  21.     On Error Resume Next
  22.     Do
  23.         Err.Clear
  24.         ThisDrawing.Utility.GetEntity LWPoly, Pt1, "Select a Polyline:"
  25.     Loop While Err
  26.     On Error GoTo 0
  27.    
  28.    
  29.     'get LWPoly data
  30.     With LWPoly
  31.         LWArea = .Area
  32.         LWZ = .Elevation
  33.     End With
  34.    
  35.     ' write LWPoly data on worksheet
  36.     Set MyCell = ActiveCell
  37.     With MyCell
  38.         .Offset(0, 0).Value = "Area:"
  39.         .Offset(0, 1).Value = LWArea
  40.         .Offset(1, 0) = "Z:"
  41.         .Offset(1, 0) = LWZ
  42.     End With
  43.    
  44.     Set ThisDrawing = Nothing
  45.     Set ACAD = Nothing
  46.    
  47. End Sub

再见
回复

使用道具 举报

238

主题

769

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1721
发表于 2014-3-6 14:07:02 | 显示全部楼层
这样很酷。我可以让用户一次选择一组多段线吗?
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2014-3-6 15:57:26 | 显示全部楼层
如果你不介意的话,我可以举个例子吗?你所做的是一块巨大的垫脚石
我们有一些多段线,其中包含需要放入电子表格的区域
回复

使用道具 举报

238

主题

769

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1721
发表于 2014-3-6 16:00:09 | 显示全部楼层
试试这个
  1. Sub PickLwPolysAndGetData()
  2.    
  3. 'for Excel sheet managing purposes
  4. Dim MySht As Worksheet
  5. Dim MyCell As Range
  6. 'for Autocad application managing purposes
  7. Dim ACAD As AcadApplication
  8. Dim ThisDrawing As AcadDocument
  9. Dim LWPoly As AcadLWPolyline
  10. ' for selection set purposes
  11. Dim ssetObj As AcadSelectionSet
  12. Dim gpCode(0) As Integer
  13. Dim dataValue(0) As Variant
  14. 'for general variables managing purposes
  15. Dim iRow As Long
  16. Dim LWArea As Double, LWZ As Double
  17. ' Autocad Session handling
  18.     On Error Resume Next
  19.     Set ACAD = GetObject(, "AutoCAD.Application")
  20.     On Error GoTo 0
  21.     If ACAD Is Nothing Then
  22.         Set ACAD = New AcadApplication
  23.         ACAD.Visible = True
  24.     End If
  25.     Set ThisDrawing = ACAD.ActiveDocument
  26.       
  27. ' selecting LwPolylines on screen by selelection set filtering method
  28.     ' managing potential selection set exsistence
  29.     On Error Resume Next
  30.     Set ssetObj = ThisDrawing.SelectionSets.Item("LWPolySSET")
  31.     If Err Then Set ssetObj = ThisDrawing.SelectionSets.Add("LWPolySSET")
  32.     On Error GoTo 0
  33.     ssetObj.Clear
  34.    
  35.     'setting filtering critera
  36.     gpCode(0) = 0
  37.     dataValue(0) = "LWPOLYLINE"
  38.    
  39.     'selecting LWPolylines
  40.     ssetObj.SelectOnScreen gpCode, dataValue
  41. ' processing LWPolylines
  42.     If ssetObj.Count > 0 Then
  43.    
  44.         ' writing sheet headings
  45.         Set MySht = ActiveSheet
  46.         Set MyCell = MySht.Cells(1, 1)
  47.         With MyCell
  48.             .Offset(0, 0).Value = "LWPoly nr"
  49.             .Offset(0, 1).Value = "Area"
  50.             .Offset(0, 2) = "Z"
  51.         End With
  52.         
  53.         'clearing previous written data
  54.         iRow = MySht.Cells(MySht.Rows.Count, 1).End(xlUp).Row
  55.         If iRow > 1 Then MyCell.Offset(1, 0).Resize(iRow - 1, 3).Clear
  56.         
  57.         'retrieving LWPolys data and writing them on worksheet
  58.         iRow = 1
  59.         For Each LWPoly In ssetObj
  60.             'retrieving LWPoly data
  61.             With LWPoly
  62.                 LWArea = .Area
  63.                 LWZ = .Elevation
  64.             End With
  65.             
  66.             ' writing LWPoly data
  67.             With MyCell
  68.                 .Offset(iRow, 0).Value = "LWPoly nr." & iRow
  69.                 .Offset(iRow, 1).Value = LWArea
  70.                 .Offset(iRow, 2) = LWZ
  71.             End With
  72.             iRow = iRow + 1
  73.         Next LWPoly
  74.         
  75.     End If
  76. ' cleaning up before ending
  77.     ssetObj.Delete
  78.     Set ssetObj = Nothing
  79.     Set ThisDrawing = Nothing
  80.     Set ACAD = Nothing
  81. End Sub

再见
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2014-3-7 02:04:37 | 显示全部楼层
直接在excel中通过一些求和的方式完成这项工作要简单得多。IF函数,该函数读取C列值(“Z”值)并对相应的B列值(“面积”值)求和
回复

使用道具 举报

238

主题

769

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1721
发表于 2014-3-8 15:01:10 | 显示全部楼层
井。我升级到了下一个 cad 版本!无论如何,现在当我运行我的宏时,我得到一个错误
运行时'13':
类型不匹配
调试:
  1. For Each LWPoly In ssetObj

任何帮助都会很棒!它在以前的cad版本中确实有效。
  1. Sub Import_POLYLINES()
  2.    
  3. 'for Excel sheet managing purposes
  4. Dim MySht As Worksheet
  5. Dim MyCell As range
  6. 'for Autocad application managing purposes
  7. Dim ACAD As AcadApplication
  8. Dim ThisDrawing As AcadDocument
  9. Dim LWPoly As AcadLWPolyline
  10. ' for selection set purposes
  11. Dim ssetObj As AcadSelectionSet
  12. Dim gpCode(0) As Integer
  13. Dim dataValue(0) As Variant
  14. 'for general variables managing purposes
  15. Dim iRow As Long
  16. Dim LWArea As Double, LWZ As Double
  17. ' Autocad Session handling
  18.     On Error Resume Next
  19.     Set ACAD = GetObject(, "AutoCAD.Application")
  20.     On Error GoTo 0
  21.     If ACAD Is Nothing Then
  22.         Set ACAD = New AcadApplication
  23.         ACAD.Visible = True
  24.     End If
  25.     Set ThisDrawing = ACAD.ActiveDocument
  26.       
  27. ' selecting LwPolylines on screen by selelection set filtering method
  28.     ' managing potential selection set exsistence
  29.     On Error Resume Next
  30.     Set ssetObj = ThisDrawing.SelectionSets.Item("LWPolySSET")
  31.     If Err Then Set ssetObj = ThisDrawing.SelectionSets.Add("LWPolySSET")
  32.     On Error GoTo 0
  33.     ssetObj.Clear
  34.    
  35.     'setting filtering critera
  36.     gpCode(0) = 0
  37.     dataValue(0) = "LWPOLYLINE"
  38.    
  39.     'selecting LWPolylines
  40.     ssetObj.SelectOnScreen gpCode, dataValue
  41. ' processing LWPolylines
  42.     If ssetObj.Count > 0 Then
  43.    
  44.         ' writing sheet headings
  45.         Set MySht = ActiveSheet
  46.         Set MyCell = MySht.Cells(11, 1) 'Where to Start the Excel Cell Input X, Y
  47.         With MyCell
  48.             '.Offset(0, 0).Value = "LWPoly nr"
  49.             '.Offset(0, 1).Value = "Area S.F."
  50.             '.Offset(0, 0) = "Elevation"
  51.         End With
  52.         
  53.         'clearing previous written data
  54.         iRow = MySht.Cells(MySht.Rows.Count, 1).End(xlUp).Row
  55.         If iRow > 1 Then MyCell.Offset(1, 0).Resize(iRow - 1, 2).Clear
  56.         
  57.         'retrieving LWPolys data and writing them on worksheet
  58.         iRow = 1
  59.         For Each LWPoly In ssetObj
  60.       
  61.             'retrieving LWPoly data
  62.             With LWPoly
  63.                 LWArea = .area
  64.                 LWZ = .Elevation
  65.             End With
  66.             
  67.              ' writing LWPoly data
  68.             With MyCell
  69.                 '.Offset(iRow, 0).Value = "LWPoly nr." & iRow
  70.                 .Offset(iRow, 1).Value = LWArea
  71.                 .Offset(iRow, 0) = LWZ
  72.             End With
  73.             iRow = iRow + 1
  74.        Next LWPoly
  75.         
  76.    End If
  77. ' cleaning up before ending
  78.     ssetObj.Delete
  79.     Set ssetObj = Nothing
  80.     Set ThisDrawing = Nothing
  81.     Set ACAD = Nothing
  82. End Sub

回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2014-3-10 03:53:12 | 显示全部楼层
而不是:
对于ssetObj中的每个LWPoly...
使用方式:
Dim oEnt as acadentity
对于ssetObj中的每个ent
如果oEnt的类型是AcadLWpolyline,则
设置LWPoly=ent
...
“折线的一些剩余工作
结束,如果
下一个
类似这样的事情,只是从内存中
回复

使用道具 举报

238

主题

769

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1721
发表于 2014-4-16 16:10:44 | 显示全部楼层
非常感谢。我明天会看看能不能让它工作。我会让你知道我的结果!
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2014-4-16 20:36:17 | 显示全部楼层
好吧,我试过了,但在同一个地方我仍然得到同样的错误…奇怪。你看到什么不正确的地方了吗
  1. Sub Import_POLYLINES()
  2.    
  3.    
  4. 'for Excel sheet managing purposes
  5. Dim MySht As Worksheet
  6. Dim MyCell As range
  7. 'for Autocad application managing purposes
  8. Dim ACAD As AcadApplication
  9. Dim ThisDrawing As AcadDocument
  10. Dim LWPoly As AcadLWPolyline
  11. Dim oEnt As AcadEntity
  12. ' for selection set purposes
  13. Dim ssetObj As AcadSelectionSet
  14. Dim gpCode(0) As Integer
  15. Dim dataValue(0) As Variant
  16. 'for general variables managing purposes
  17. Dim iRow As Long
  18. Dim LWArea As Double, LWZ As Double
  19. ' Autocad Session handling
  20.     On Error Resume Next
  21.     Set ACAD = GetObject(, "AutoCAD.Application")
  22.     On Error GoTo 0
  23.     If ACAD Is Nothing Then
  24.         Set ACAD = New AcadApplication
  25.         ACAD.Visible = True
  26.     End If
  27.     Set ThisDrawing = ACAD.ActiveDocument
  28.       
  29. ' selecting LwPolylines on screen by selelection set filtering method
  30.     ' managing potential selection set exsistence
  31.     On Error Resume Next
  32.     Set ssetObj = ThisDrawing.SelectionSets.Item("LWPolySSET")
  33.     If Err Then Set ssetObj = ThisDrawing.SelectionSets.Add("LWPolySSET")
  34.     On Error GoTo 0
  35.     ssetObj.Clear
  36.    
  37.     'setting filtering critera
  38.     gpCode(0) = 0
  39.     dataValue(0) = "LWPOLYLINE"
  40.    
  41.     'selecting LWPolylines
  42.     ssetObj.SelectOnScreen gpCode, dataValue
  43. ' processing LWPolylines
  44.     If ssetObj.Count > 0 Then
  45.    
  46.         ' writing sheet headings
  47.         Set MySht = ActiveSheet
  48.         Set MyCell = MySht.Cells(11, 1) 'Where to Start the Excel Cell Input X, Y
  49.         With MyCell
  50.             '.Offset(0, 0).Value = "LWPoly nr"
  51.             '.Offset(0, 1).Value = "Area S.F."
  52.             '.Offset(0, 0) = "Elevation"
  53.         End With
  54.         
  55.         'clearing previous written data
  56.         iRow = MySht.Cells(MySht.Rows.Count, 1).End(xlUp).Row
  57.         If iRow > 1 Then MyCell.Offset(1, 0).Resize(iRow - 1, 2).Clear
  58.         
  59.         'retrieving LWPolys data and writing them on worksheet
  60.         iRow = 1
  61.         For Each oEnt In ssetObj
  62.         If TypeOf oEnt Is AcadLWPolyline Then
  63.         Set LWPoly = oEnt
  64.       
  65.             'retrieving LWPoly data
  66.             With LWPoly
  67.                 LWArea = .area
  68.                 LWZ = .Elevation
  69.             End With
  70.             
  71.              ' writing LWPoly data
  72.             With MyCell
  73.                 '.Offset(iRow, 0).Value = "LWPoly nr." & iRow
  74.                 .Offset(iRow, 1).Value = LWArea
  75.                 .Offset(iRow, 0) = LWZ
  76.             End With
  77.             iRow = iRow + 1
  78.             
  79.             End If
  80.       
  81.        Next oEnt
  82.         
  83.    End If
  84. ' cleaning up before ending
  85.     ssetObj.Delete
  86.     Set ssetObj = Nothing
  87.     Set ThisDrawing = Nothing
  88.     Set ACAD = Nothing
  89. End Sub

回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-4-20 18:24 , Processed in 0.307207 second(s), 73 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表