乐筑天下

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

[编程交流] VBA:行列表

[复制链接]

2

主题

7

帖子

5

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:03:30 | 显示全部楼层 |阅读模式
在VBA中是否可以创建图形中所有直线(及其起点和终点坐标)的列表?
VBA(或VB)对我来说并不新鲜,但与Autocad的结合却是。
回复

使用道具 举报

24

主题

1265

帖子

1028

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
362
发表于 2022-7-6 15:10:36 | 显示全部楼层
对 
为线实体创建过滤的选择集,然后迭代该选择集,查询所需的属性。帮助文件包含过滤选择集的示例,这里有一个很好的教程:http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=2768231&linkID=9240615
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 15:16:43 | 显示全部楼层
 
欢迎加入!
试试这个例子
 
  1. Option Explicit
  2. Sub AllLinesData()
  3.    Dim oSset As AcadSelectionSet
  4.    Dim oEnt As AcadEntity
  5.    Dim fcode(0) As Integer
  6.    Dim fData(0) As Variant
  7.    Dim dxfcode, dxfdata
  8.    Dim setName As String
  9.    Dim i As Integer
  10.    Dim n As Integer
  11.    Dim s As String
  12.    fcode(0) = 0
  13.    fData(0) = "LINE"
  14.    dxfcode = fcode
  15.    dxfdata = fData
  16.    setName = "$Lines$"
  17.    '// make sure the selection set does not exist
  18.    For i = 0 To ThisDrawing.SelectionSets.Count - 1
  19.        If ThisDrawing.SelectionSets.Item(i).Name = setName Then
  20.            '// if this named selection set is already exist then delete it
  21.            ThisDrawing.SelectionSets.Item(i).Delete
  22.            Exit For
  23.        End If
  24.    Next i
  25.    '// add new selection set with this name
  26.    Set oSset = ThisDrawing.SelectionSets.Add(setName)
  27.    oSset.Select acSelectionSetAll, , , dxfcode, dxfdata
  28.    On Error GoTo Err_Control
  29.    '// loop through all lines
  30.    ReDim lineArr(0 To oSset.Count - 1, 0 To 6) As Variant
  31.    For n = 0 To oSset.Count - 1
  32.        Dim cEnt As AcadEntity
  33.        Set cEnt = oSset.Item(n)
  34.        Dim oLine As AcadLine
  35.        Set oLine = cEnt
  36.        Dim startp As Variant
  37.        '// get the start point
  38.        startp = oLine.StartPoint
  39.        Dim endp As Variant
  40.        '// get the end point
  41.        endp = oLine.EndPoint
  42.        '// collect line handles and points into a two-dimensional array
  43.        lineArr(n, 0) = oLine.Handle
  44.        lineArr(n, 1) = startp(0)
  45.        lineArr(n, 2) = startp(1)
  46.        lineArr(n, 3) = startp(2)
  47.        lineArr(n, 4) = endp(0)
  48.        lineArr(n, 5) = endp(1)
  49.        lineArr(n, 6) = endp(2)
  50.    Next n
  51.    '// clean up memory
  52.    oSset.Delete
  53.    '// do what you want with array 'lineArr' here, e.g. write data to the text file etc.
  54.    Open "C:\AllLines.txt" For Output As #1    'Open file for output
  55.    For n = 0 To UBound(lineArr, 1)
  56.        s = ""
  57.        For i = 0 To UBound(lineArr, 2)
  58.            s = s & CStr(lineArr(n, i)) & ","
  59.        Next i
  60.        Write #1, Left(s, Len(s) - 1)    'Write comma-delimited data (cut the last comma from string)
  61.    Next n
  62.    Close #1    'Close file
  63. Err_Control:
  64.    MsgBox Err.Description
  65. End Sub

 
~'J'~
回复

使用道具 举报

2

主题

7

帖子

5

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:22:20 | 显示全部楼层
谢谢,尝试了代码,它满足了我的需要。有了这段代码,我可以完成我的程序多一点。
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 15:29:17 | 显示全部楼层
不客气
很乐意帮忙
 
~'J'~
回复

使用道具 举报

2

主题

7

帖子

5

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:31:16 | 显示全部楼层
我添加了代码以获得LWpolyline的开始点和结束点。这适用于具有1个顶点的LWD多段线。当我绘制具有多个顶点的LW多段线时,我看到组码“90”表示顶点的数量。我如何读取组码“90”的上下文,或者是否有其他方法来确定顶点的数量?
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 15:38:04 | 显示全部楼层
试着读读这个稍微编辑过的版本
 
选项显式sub AllPlinesData()Dim oSset As AcadSelectionSet Dim oEnt As AcadEntity Dim fcode(0)As Integer Dim fData(0)As Variant Dim dxfcode,dxfdata Dim setName As String Dim i As Integer Dim n As Integer Dim s As String fcode(0)=0 fData(0)=“LWPOLYLINE”dxfcode=fcode dxfdata=fData setName=“$Plines$”//确保此图形的i=0的选择集不存在。选择集。如果此绘图,则计数-1。选择集。项目(i)。Name=setName Then'//如果此命名选择集已存在,请将其删除ThisDrawing。选择集。项目(i)。Delete Exit For End If Next i’//添加具有此名称集的新选择集oSset=ThisDrawing。选择集。添加(setName)oSset。选择acSelectionSetAll、dxfcode、dxfdata On Error GoTo Err\u Control’//循环通过所有轻多段线ReDim plineAr(0到oSset。计数-1,0到4),作为n=0到oSset的变量。Count-1 Dim cEnt作为AcadEntity Set cEnt=oSset。Item(n)Dim oPline As AcadLWPolyline Set oPline=cEnt Dim coord As Variant’//get coordinates coord=oPline。坐标'//将多段线控制柄和点收集到二维数组plineAr(n,0)=oPline中。手柄多线性(n,1)=坐标(0)'
回复

使用道具 举报

2

主题

7

帖子

5

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:42:56 | 显示全部楼层
代码仅返回多段线的开始和结束。我将部分代码更改为:
 
[code]'//获取坐标坐标=oPline。坐标“//将多段线句柄和点收集到二维arrayxtemp=coord(0)”中
回复

使用道具 举报

29

主题

94

帖子

14

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
198
发表于 2022-7-6 15:48:28 | 显示全部楼层
puffeltje,
 
我尝试将您的代码合并到Fixo提供的示例代码中,但无法执行。你能帮忙吗。想试试你的代码。
谢谢
回复

使用道具 举报

2

主题

7

帖子

5

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-6 15:57:05 | 显示全部楼层
Oeps,我没有发布我所有的更改:哎呀:。以下是工作代码:
 
选项显式sub AllPlinesData()Dim oSset As AcadSelectionSet Dim oEnt As AcadEntity Dim fcode(0)As Integer Dim fData(0)As Variant Dim dxfcode,dxfdata Dim setName As String Dim i As Integer Dim n As Integer Dim s As String Dim xtemp Dim ytemp Dim j Dim PLindex fcode(0)=0 fData(0)=“LWPOLYLINE”dxfcode=fcode dxfdata=fData setName=“$Plines$”//确保此图形的i=0的选择集不存在。选择集。如果此绘图,则计数-1。选择集。项目(i)。Name=setName Then'//如果此命名选择集已存在,请将其删除ThisDrawing。选择集。项目(i)。Delete Exit For End If Next i’//添加具有此名称集的新选择集oSset=ThisDrawing。选择集。添加(setName)oSset。选择acSelectionSetAll、、dxfcode、dxfdata On Error GoTo Err\u Control’//循环通过所有轻多段线ReDim plinear(0到3,0到oSset。Count-1)作为变量PLindex=0,对于n=0到oSset。Count-1 Dim cEnt作为AcadEntity Set cEnt=oSset。Item(n)Dim oPline As AcadLWPolyline Set oPline=cEnt Dim coord As Variant’//get coordinates coord=oPline。坐标“//将多段线句柄和点收集到二维数组xtemp=coord(0)”中
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 21:30 , Processed in 0.325881 second(s), 72 queries .

© 2020-2025 乐筑天下

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