乐筑天下

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

[编程交流] 获取文本的坐标

[复制链接]

8

主题

21

帖子

13

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-6 22:09:55 | 显示全部楼层 |阅读模式
你好
可以获取分配给集合的任何文本对象的坐标吗?
首先,我将selectionset中的一些文本添加到collection中。然后我想根据它们的坐标X和Y对这些集合成员进行排序。但是我无法得到集合成员的坐标。这是我的收藏:
 
 
对于oSset中的每个oObj
如果oObj的类型为AcadText,则
 
Set oText=oObj
 
如果InStr(1,oText.TextString,“Km=”)>0,则
 
collection_km。添加文字
 
如果结束
如果结束
下一个oObj
 
 
我尝试了这个,但它给出了一个错误
collection_km。项目(i)。插入点(0)
谢谢你的建议
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 22:20:43 | 显示全部楼层
也许可以尝试:
 
 
设置oText=collection\u km。项目(i)
双精度dblX
dblX=文字。插入点(0)
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:29:59 | 显示全部楼层
从我的代码中尝试,略作编辑
  1. Public Sub TestCollectTextPoints()
  2. Dim ent As AcadEntity
  3. Dim objSSet As AcadSelectionSet
  4. Dim setObj As AcadSelectionSet
  5. Dim oText As AcadText
  6. Dim oMText As AcadMText
  7. Dim intFilterType(0 To 0) As Integer
  8. Dim varFilterData(0 To 0) As Variant
  9. Dim dxfCode, dxfValue
  10. intFilterType(0) = 0: varFilterData(0) = "TEXT,MTEXT" 'to select texts and mtexts
  11.   ' Creates an empty selection set.
  12.       Dim setColl As AcadSelectionSets
  13.     With ThisDrawing
  14.          Set setColl = .SelectionSets
  15.          For Each setObj In setColl
  16.               If setObj.Name = "mySelSet" Then
  17.                    .SelectionSets.item("mySelSet").Delete
  18.                    Exit For
  19.               End If
  20.          Next
  21.          Set objSSet = .SelectionSets.Add("mySelSet")
  22.     End With
  23. objSSet.SelectOnScreen intFilterType, varFilterData
  24. If objSSet.Count = 0 Then
  25. Exit Sub
  26. End If
  27. Dim txtColl As New Collection
  28. Dim n As Long
  29. n = 0
  30. Dim textArr(0 To 3) As Variant
  31. For Each ent In objSSet
  32. If TypeOf ent Is AcadText Then
  33. Set oText = ent
  34. 'store text record in array
  35. textArr(0) = oText.handle
  36. textArr(1) = oText.InsertionPoint(0)
  37. textArr(2) = oText.InsertionPoint(1)
  38. textArr(3) = oText.TextString
  39. txtColl.Add textArr
  40. End If
  41. If TypeOf ent Is AcadMText Then
  42. Set oMText = ent
  43. ' 'store mtextrecord in array
  44. textArr(0) = oMText.handle
  45. textArr(1) = oMText.InsertionPoint(0)
  46. textArr(2) = oMText.InsertionPoint(1)
  47. textArr(3) = oMText.TextString
  48. txtColl.Add textArr
  49. End If
  50. Next ent
  51. ' write collection to comma delimited file,
  52. ' You can use .csv extension instead of .txt
  53. Call ahha("C:\Test\MyTextCollection.txt", txtColl)
  54. 'release collection at the end
  55. Set txtColl = Nothing
  56. End Sub
回复

使用道具 举报

8

主题

21

帖子

13

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-6 22:30:04 | 显示全部楼层
谢谢你的关注。我根据你的代码编辑代码,但不能使用成员的坐标。
当我试图在“If…Else If”循环中比较它们时,它再次给出了一个错误。
你能举个例子解释一下吗?
例如:
我的集合中有三个textArr数组。
这些textArr数组中的每一个都有三个这样的属性(textArr(0)=oText。插入点(0),textArr(1)=oText。插入点(1),textArr(2)=oText。文本字符串)
是否可以根据这些textarr在循环中的插入点对集合中的这些textarr进行比较和重新排序?
谢谢
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:41:17 | 显示全部楼层
habakay,我认为使用2个十进制数组就足够了
不是收藏,再一次,这是我的老歌,对不起,
没时间解释好
  1. Option Explicit
  2. Sub TestTextSort()
  3.     Dim ss As AcadSelectionSet
  4.     Dim ftype(0) As Integer
  5.     Dim fdata(0) As Variant
  6.     Dim ent As AcadEntity
  7.     Dim hndl As String
  8.     Dim xcoord As Double
  9.     Dim ycoord As Double
  10.     Dim i As Integer
  11.     Dim j As Integer
  12.     Dim tmp As Variant
  13.     Dim otext As AcadText
  14.     Dim txtstring As String
  15.     ftype(0) = 0:
  16.     fdata(0) = "TEXT":
  17.          With ThisDrawing.SelectionSets
  18.               While .Count > 0
  19.                    .Item(0).Delete
  20.               Wend
  21.          End With
  22.     With ThisDrawing.SelectionSets
  23.          Set ss = .Add("SortText")
  24.          End With
  25. ss.SelectOnScreen ftype, fdata
  26. If ss.Count = 0 Then
  27. MsgBox "Nothing selected"
  28. Exit Sub
  29. Else
  30. ' collect text handles and Y-coordinates of block for sorting them
  31. ' below by Y-coordinate from top to bottom:
  32. ReDim txtdata(0 To ss.Count - 1, 0 To 3)
  33. i = 0
  34. For Each ent In ss
  35. Set otext = ent
  36. hndl = otext.Handle
  37. xcoord = otext.InsertionPoint(0)
  38. ycoord = otext.InsertionPoint(1)
  39. txtstring = otext.TextString
  40. txtdata(i, 0) = ycoord: txtdata(i, 1) = ycoord: txtdata(i, 2) = txtstring: txtdata(i, 3) = hndl
  41. i = i + 1
  42. Next ent
  43. 'sort blocks by X
  44.   txtdata = CoolSort(txtdata, 1) ' by Y would be txtdata = CoolSort(txtdata, 2)
  45.   ' iterate through array and return text reference object
  46.   ' check if sorting algorithm is right
  47.   For i = 0 To UBound(txtdata, 1)
  48.   Set otext = ThisDrawing.HandleToObject(txtdata(i, 3))
  49.   Debug.Print otext.TextString
  50.   Next i
  51. End If
  52. End Sub
  53. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  54. ' written by Fatty T.O.H. () 2006 * all rights removed '
  55. ' SourceArr - two dimensional array '
  56. ' iPos - "column" number (starting from 1) '
  57. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  58. Public Function CoolSort(SourceArr As Variant, iPos As Integer) As Variant
  59. Dim Check As Boolean
  60. ReDim tmpArr(UBound(SourceArr, 2)) As Variant
  61. Dim iCount As Integer
  62. Dim jCount As Integer
  63. Dim nCount As Integer
  64. iPos = iPos - 1
  65. Check = False
  66. Do Until Check
  67. Check = True
  68. For iCount = LBound(SourceArr, 1) To UBound(SourceArr, 1) - 1
  69. If SourceArr(iCount, iPos) < SourceArr(iCount + 1, iPos) Then
  70. For jCount = LBound(SourceArr, 2) To UBound(SourceArr, 2)
  71. tmpArr(jCount) = SourceArr(iCount, jCount)
  72. SourceArr(iCount, jCount) = SourceArr(iCount + 1, jCount)
  73. SourceArr(iCount + 1, jCount) = tmpArr(jCount)
  74. Check = False
  75. Next
  76. End If
  77. Next
  78. Loop
  79. CoolSort = SourceArr
  80. End Function
回复

使用道具 举报

8

主题

21

帖子

13

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-6 22:44:30 | 显示全部楼层
谢谢你的关注。
回复

使用道具 举报

8

主题

21

帖子

13

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-6 22:53:22 | 显示全部楼层
 
谢谢你的代码。我使用你的代码并修复我的代码。但我仍然有错误。数组出现“类型不匹配”错误。
你能看一下吗?
这是我的密码
 
 
  1. Option Explicit
  2. Const xlFileName As String = "C:\Users\halil.abakay\Desktop\AutoCAD.xlsx" '<--change existing file name here
  3. Public Sub Text_to_Excel()
  4. Dim SS As AcadSelectionSet
  5. Dim ftype(0) As Integer
  6. Dim fdata(0) As Variant
  7. Dim obj As AcadObject
  8. Dim otext As AcadText
  9. Dim txtdata_km
  10. Dim txtdata_srbst
  11. Dim txtdata_dolgu
  12. Dim txtdata_yarma
  13. Dim counter1 As Long
  14. Dim counter2 As Long
  15. Dim counter3 As Long
  16. Dim counter4 As Long
  17. Dim counterveri As Integer
  18. Dim k As Integer
  19. Dim xcoord As Double
  20. Dim ycoord As Double
  21. Dim txtstring As String
  22. Dim xlApp As Object
  23. Dim xlBook As Workbook
  24. Dim xlSheet As Worksheet
  25. Dim lngRow As Long, lngCol As Long
  26. On Error Resume Next
  27. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28.    Set xlApp = GetObject(, "Excel.Application")
  29.    
  30.    If Err <> 0 Then
  31.        Err.Clear
  32.        Set xlApp = CreateObject("Excel.Application")
  33.    If Err <> 0 Then
  34.        MsgBox "Impossible to run Excel.", vbExclamation
  35.        End
  36.    End If
  37.    End If
  38.    xlApp.Visible = True
  39.    
  40.    Set xlBook = xlApp.Workbooks.Open(xlFileName)
  41.    
  42.    Set xlSheet = xlBook.Sheets(1)
  43.    
  44.    xlApp.ScreenUpdating = True
  45.    If xlSheet.Range("A1") = "" Then
  46.        lngRow = 1: lngCol = 1
  47.    Else
  48.        lngRow = xlSheet.Range("a65536").End(3).Offset(1, 0).Row: lngCol = 1
  49.    End If
  50. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51.    ftype(0) = 0:
  52.    fdata(0) = "TEXT":
  53.    With ThisDrawing.SelectionSets
  54.        While .Count > 0
  55.            .Item(0).Delete
  56.        Wend
  57.    End With
  58.    
  59.    With ThisDrawing.SelectionSets
  60.        Set SS = .Add("Textkubaj")
  61.    End With
  62.    SS.SelectOnScreen ftype, fdata
  63.    
  64.    
  65.    If SS.Count = 0 Then
  66.        MsgBox "Nothing selected"
  67.        Exit Sub
  68.    Else
  69.        counter1 = 0
  70.        counter2 = 0
  71.        counter3 = 0
  72.        counter4 = 0
  73.        counterveri = 0
  74.        For Each obj In SS
  75.                
  76.                Set otext = obj
  77.                
  78.                If InStr(1, otext.TextString, "Km =") > 0 Then
  79.                    counter1 = counter1 + 1
  80.                    ReDim Preserve txtdata_km(0 To 2, counter1 - 1)
  81.                    xcoord = otext.InsertionPoint(0)
  82.                    ycoord = otext.InsertionPoint(1)
  83.                    txtstring = otext.TextString
  84.                    txtdata_km(0, counter1 - 1) = xcoord: txtdata_km(1, counter1 - 1) = ycoord: txtdata_km(2, counter1 - 1) = txtstring
  85.                    counterveri = counterveri + 1
  86.                   
  87.                ElseIf InStr(1, otext.TextString, "Serbest Kazı=") > 0 Then
  88.                    counter2 = counter2 + 1
  89.                    ReDim Preserve txtdata_srbst(0 To 2, 0 To counter2 - 1)
  90.                    xcoord = otext.InsertionPoint(0)
  91.                    ycoord = otext.InsertionPoint(1)
  92.                    txtstring = otext.TextString
  93.                    txtdata_srbst(0, counter2 - 1) = xcoord: txtdata_srbst(1, counter2 - 1) = ycoord: txtdata_srbst(2, counter2 - 1) = txtstring
  94.                    counterveri = counterveri + 1
  95.                
  96.                ElseIf InStr(1, otext.TextString, "Dolgu =") > 0 Then
  97.                    counter3 = counter3 + 1
  98.                    ReDim Preserve txtdata_dolgu(0 To 2, 0 To counter3 - 1)
  99.                    xcoord = otext.InsertionPoint(0)
  100.                    ycoord = otext.InsertionPoint(1)
  101.                    txtstring = otext.TextString
  102.                    txtdata_dolgu(0, counter3 - 1) = xcoord: txtdata_dolgu(1, counter3 - 1) = ycoord: txtdata_dolgu(2, counter3 - 1) = txtstring
  103.                    counterveri = counterveri + 1
  104.                
  105.                ElseIf InStr(1, otext.TextString, "Yarma =") > 0 Then
  106.                    counter4 = counter4 + 1
  107.                    ReDim Preserve txtdata_yarma(0 To 2, 0 To counter4 - 1)
  108.                    xcoord = otext.InsertionPoint(0)
  109.                    ycoord = otext.InsertionPoint(1)
  110.                    txtstring = otext.TextString
  111.                    txtdata_yarma(0, counter4 - 1) = xcoord: txtdata_yarma(1, counter4 - 1) = ycoord: txtdata_yarma(2, counter4 - 1) = txtstring
  112.                    counterveri = counterveri + 1
  113.                
  114.                Else: GoTo devam
  115.                End If
  116.                
  117. devam:
  118.        Next obj
  119.    
  120.    End If
  121.                
  122.                
  123. For k = 0 To counter1 - 1
  124.            MsgBox txtdata_km(k, 0)
  125.            xlSheet.Cells(lngRow, lngCol + 1).Value = txtdata_km(k, 0)
  126.            xlSheet.Cells(lngRow, lngCol + 2).Value = txtdata_km(k, 1)
  127.            xlSheet.Cells(lngRow, lngCol).Value = Replace(Replace(txtdata_km(k, 2).TextString, "Km =", ""), ".", ",")
  128.            lngRow = lngRow + 1
  129. Next k
  130.    
  131. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  132. xlSheet.Columns.HorizontalAlignment = xlHAlignLeft
  133. xlSheet.Columns.AutoFit
  134. xlApp.ScreenUpdating = True
  135. xlBook.Save
  136. xlBook.Close
  137. xlApp.Quit
  138. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  139. Set xlApp = Nothing
  140. Set xlBook = Nothing
  141. Set xlSheet = Nothing
  142. counter1 = Empty
  143. counter2 = Empty
  144. counter3 = Empty
  145. counter4 = Empty
  146. counterveri = Empty
  147. k = Empty
  148. Set txtdata_km = Nothing
  149. Set txtdata_srbst = Nothing
  150. Set txtdata_dolgu = Nothing
  151. Set txtdata_yarma = Nothing
  152. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  153. MsgBox "Done"
  154. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
  155. Err_Control:
  156. If Err.Number <> 0 Then
  157.    MsgBox Err.Description
  158. End If
  159. End Sub

谢谢
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2022-7-6 23:03:20 | 显示全部楼层
在这里,你可以找到我的建议,以及一些评论(它们前面都有“
 
选项显式约束xlFileName为String=“C:\Users\halil.abakay\Desktop\AutoCAD.xlsx”
回复

使用道具 举报

8

主题

21

帖子

13

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-6 23:08:28 | 显示全部楼层
 
 
 
我非常感激。它工作完美。
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2022-7-6 23:16:30 | 显示全部楼层
 
好的
干得好!
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 11:12 , Processed in 0.462873 second(s), 72 queries .

© 2020-2025 乐筑天下

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