乐筑天下

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

[编程交流] 提取特定块

[复制链接]

18

主题

66

帖子

48

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
90
发表于 2022-7-6 22:13:05 | 显示全部楼层 |阅读模式
你好
我有一个excel宏,它将带有属性的块从打开的acad dwg中提取到excel电子表格中。
我需要您的帮助来修改此代码,以便能够做到这一点:
-在行代码中指定要提取的块的名称
-指定将提取每个块的列。
我使用AutoCAD 2011和excel 2010
下面是excel VBA代码。
谢谢你的帮助。
  1. Public acad As Object
  2. Public mspace As Object
  3. Public excel As Object
  4. Public AcadRunning As Integer
  5. Public excelSheet As Object
  6. Sub Extract()
  7.    Dim sheet As Object
  8.    Dim shapes As Object
  9.    Dim elem As Object
  10.    Dim excel As Object
  11.    Dim Max As Integer
  12.    Dim Min As Integer
  13.    Dim NoOfIndices As Integer
  14.    Dim excelSheet As Object
  15.    Dim RowNum As Integer
  16.    Dim Array1 As Variant
  17.    Dim Count As Integer
  18.    
  19.    Set excel = GetObject(, "Excel.Application")
  20.    Set excelSheet = excel.ActiveWorkbook.ActiveSheet
  21.    excelSheet.Range(Cells(1, 1), Cells(45, ).Clear
  22.    excelSheet.Range(Cells(1, 1), Cells(1, ).Font.Bold = True
  23.    excelSheet.Range(Cells(1, 1), Cells(1, ).Font.Color = 1152
  24.        Set acad = Nothing
  25.    On Error Resume Next
  26.    Set acad = GetObject(, "AutoCAD.Application")
  27.    If Err <> 0 Then
  28.        Set acad = CreateObject("AutoCAD.Application")
  29.        acad.Visible = True
  30.        MsgBox "Please open a drawing file and then restart this macro."
  31.        Exit Sub
  32.    End If
  33.    Set doc = acad.ActiveDocument
  34.    Set mspace = doc.ModelSpace
  35.    RowNum = 1
  36.    Dim Header As Boolean
  37.    Header = False
  38.    For Each elem In mspace
  39.        With elem
  40.            If StrComp(.EntityName, "AcDbBlockReference", 1) = 0 Then
  41.                If .HasAttributes Then
  42.                    Array1 = .GetAttributes
  43.                    For Count = LBound(Array1) To UBound(Array1)
  44.                        If Header = False Then
  45.                            If StrComp(Array1(Count).EntityName, "AcDbAttribute", 1) = 0 Then
  46.                                excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TagString
  47.                            End If
  48.                        End If
  49.                    Next Count
  50.                    RowNum = RowNum + 1
  51.                    For Count = LBound(Array1) To UBound(Array1)
  52.                        excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TextString
  53.                    Next Count
  54.                    Header = True
  55.                End If
  56.            End If
  57.        End With
  58.    Next elem
  59.    NumberOfAttributes = RowNum - 1
  60.    If NumberOfAttributes > 0 Then
  61.       ActiveWorkbook.ActiveSheet.Range("A1").Sort _
  62.        key1:=ActiveWorkbook.ActiveSheet.Columns("A"), _
  63.        Header:=xlGuess
  64.    Else
  65.        MsgBox "No attributes found in the current drawing."
  66.    End If
  67.    Set acad = Nothing
  68. End Sub
  69. Private Sub Auto_Close()
  70.    Set excelSheet = Nothing
  71. End Sub
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 22:26:25 | 显示全部楼层
你需要在这里更深入一点。(EntityName,“AcDbBlockReference”,1)这返回yes它是一个块,然后你会添加另一个if,is=到我的块名中,在一个循环列表中为所需的块。
 
可能选择集直接生成为一组1块对象。
  1. FilterDXFCode(0) = 0
  2. FilterDXFVal(0) = "INSERT"
  3. FilterDXFCode(1) = 2
  4. FilterDXFVal(1) = "DA1DRTXT"
  5. BLOCK_NAME = "DA1DRTXT"
  6. Set SS = ThisDrawing.SelectionSets.Add("issued")
  7. SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
  8. For Cntr = 0 To SS.Count - 1
  9.   attribs = SS.Item(Cntr).GetAttributes
  10. attribval1 = attribs(1).TextString
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2022-7-6 22:34:50 | 显示全部楼层
 
至于excel编码,它实际上似乎适合列出单个块名事件,但您最好发布一个示例,说明您在块列表方面的实际需求。
如果我理解得很好,您希望在不同的列组中列出每个不同块名的所有引用。如果是这样,每个列表结构都严格地与每个块的标记数相连接,这些标记很可能彼此不同。因此,为要处理的每个块设置和使用不同的板材可能更有效。
让我知道。
 
与此同时,我认为你至少应该做以下改变
1)
  1.     Set excelSheet = Excel.ActiveWorkbook.ActiveSheet
  2.    With excelSheet
  3.        .Range(.Cells(1, 1), .Cells(45, ).Clear
  4.        .Range(.Cells(1, 1), .Cells(1, ).Font.Bold = True
  5.        .Range(.Cells(1, 1), .Cells(1, ).Font.color = 1152
  6.    End With

使代码运行
 
2)
  1.                     For Count = LBound(Array1) To UBound(Array1)
  2.                        excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TextString
  3.                    Next Count

 
和3)
  1.     If NumberOfAttributes > 0 Then
  2.       excelSheet.Range("A1").Sort _
  3.        key1:=excelSheet.Columns("A"), _
  4.        Header:=xlGuess
  5.    Else

两者都只是为了在开始时正确设置变量
 
4)
  1.                 If .HasAttributes Then
  2.                    Array1 = .GetAttributes
  3.                    If Header = False Then
  4.                        For Count = LBound(Array1) To UBound(Array1)
  5.                            If StrComp(Array1(Count).EntityName, "AcDbAttribute", 1) = 0 Then
  6.                                excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TagString
  7.                            End If
  8.                        Next Count
  9.                        Header = True
  10.                    End If
  11.                    RowNum = RowNum + 1
  12.                    For Count = LBound(Array1) To UBound(Array1)
  13.                        excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TextString
  14.                    Next Count
  15.                End If

避免在处理标记字符串后进行无用的检查。
 
当然,上述所有内容都需要重写,以匹配Bigal建议,如果是这样,则每个blockname设置都需要一张表
回复

使用道具 举报

18

主题

66

帖子

48

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
90
发表于 2022-7-6 22:40:17 | 显示全部楼层
非常感谢您的回复。
我已经将你的建议应用到代码中,我觉得它比以前运行得更流畅了,但我仍然没有操作数据的能力。我听从了你的建议,创建了一个小的EXCEL数据示例:
1、数据提取将从BA列开始
2.BA和BB列对应于块1的属性
3.BC和BD列对应于块2的属性
4.Col Be对应于Block3的属性,依此类推。
有没有办法声明要提取哪些块/属性以及在哪里提取?
谢谢你的帮助和投入的时间。
EXCEL块列表。xlsx公司
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2022-7-6 22:46:17 | 显示全部楼层
 
当然有!明天我会花一些时间,让你有一个代码,可以让你“深入挖掘”并达到你的目标。
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2022-7-6 22:51:53 | 显示全部楼层
 
这是vba列表
 
选项ExplicitSub Extract()如果此绘图为空,则MsgBox“请打开绘图文件,然后重新启动此宏。”如果“----------------------------------------------------”Excel设置Dim Excel As Application Dim MySheet As Excel,则退出子端。工作表Dim BlckNameRng为Excel。范围,标记为Excel。范围,myCell为Excel。Range Dim iniColStr As String Dim iniRow As Long,iniCol As Long“出错时处理excel应用程序继续下一步设置excel=GetObject(,“excel.application”),如果出错,则设置excel=CreateObject(“excel.application”)'用excel处理工作簿和工作表。Visible=True Set MySheet=。ActiveWorkbook。如果出现错误,则为ActiveSheet。工作簿。添加ActiveWorkbook。工作表。添加集MySheet=。ActiveWorkbook。ActiveSheet End If On Error GoTo 0 End With“handling columns where to start write data from iniColStr=“BA””
回复

使用道具 举报

18

主题

66

帖子

48

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
90
发表于 2022-7-6 23:04:08 | 显示全部楼层
你好RICVBA
你为这段代码所做的大量工作给我留下了深刻的印象,说实话,我说不出话来。我将此代码嵌入EXCEL电子表格中,但当我尝试运行宏时,我收到一条错误消息“变量未定义”。你认为windows 7-64位与此有关吗?
非常感谢。
回复

使用道具 举报

12

主题

175

帖子

77

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
149
发表于 2022-7-6 23:06:02 | 显示全部楼层
 
 
 
Tazzz,我从Autocad“透视”编写了vba代码。i、 e:您必须在Autocad(而不是Excel)中打开VBA IDE,插入模块并将代码粘贴到其代码窗口中,然后进行调试。别忘了确保您设置了正确的参考。
 
在此之后,可能会出现32-64位问题。但我希望这不会发生,因为我无法支持你。
回复

使用道具 举报

18

主题

66

帖子

48

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
90
发表于 2022-7-6 23:15:36 | 显示全部楼层
非常感谢您的澄清。如果64位有问题,我会想办法解决。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 08:44 , Processed in 0.851261 second(s), 70 queries .

© 2020-2025 乐筑天下

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