乐筑天下

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

动态块和有效名称...

[复制链接]

11

主题

40

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2014-2-16 00:00:54 | 显示全部楼层 |阅读模式
AUGI论坛上有一个电子表格工具,它将打开一个图形文件夹,并从图形中提取块,并用属性填充excel工作表。 如果你没有动态块,效果很好。
秘诀与块的“有效名称”有关,但我很难理解如何获取块的有效名称。
以下是代码摘录:
  1. For Each MyBlock In MyDbx.Blocks
  2.                 If MyBlock.IsDynamicBlock Then
  3.                   If UCase(MyBlock.EffectiveName) = UCase(Range("BlkName").Value) Then
  4.                         BlkExist = True
  5.                         Exit For
  6.                     End If
  7.                 End If
  8.                 If UCase(MyBlock.Name) = UCase(Range("BlkName").Value) Then
  9.                     BlkExist = True
  10.                     Exit For
  11.                 End If
  12. Next MyBlock

这是错误号和描述:
任何帮助将不胜感激。

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

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

使用道具 举报

4

主题

219

帖子

4

银币

后起之秀

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

铜币
238
发表于 2014-2-19 10:10:42 | 显示全部楼层
没有看到更多相关的代码,我不能说我理解你显示的代码的确切目的。
但是,错误消息是预期的:
“对于每个...”循环遍历图形的块定义,以查看图形中是否存在块定义。
因此,MyBlock 声明为 AcadBlock,而不是 AcadBlockreference,因此,MyBlock.EffectiveName 是错误的,并引发了异常。
块定义,无论是否是动态块,总是有一个名称,而块引用,如果是动态块引用,可以有一个“异常块”名称,因此,需要一个只读的“EffectiveName”来指示它从哪个动态块定义派生出来。
很明显,您在处理动态块时获得的工具存在错误。
回复

使用道具 举报

11

主题

40

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2014-2-19 21:44:46 | 显示全部楼层
没错,这个工具对动态块有问题。
我非常感谢对有效名称的解释。
我正在尝试修改现有代码,但尚未成功,但正在取得进展(见下文)。
刚买了杰里·温特·VB.net的书,可能会尝试VB.net的方法。
下面是原始代码(它适用于非动态块)。
  1. Sub GetBlockInfo()
  2.     Dim DwgCnt As Integer
  3.     Dim DwgName As String
  4.     Dim StrPath As String
  5.     Dim BlkExist As Boolean
  6.     Dim intType(1) As Integer
  7.     Dim varData(1) As Variant
  8.     Dim BlkFound As Boolean
  9.     Dim AttTitles As Boolean
  10.     Dim ChkSht As Worksheet, DwgLstSht As Worksheet
  11.    
  12.     Dim MyDbx As AxDbDocument
  13.     Dim MyLayouts As AcadLayouts
  14.     Dim MyLayout As Variant
  15.     Dim MyEnt As AcadEntity
  16.     Dim MyBlock As AcadBlock
  17.     Dim MyBlockR As AcadBlockReference
  18.     Dim MyAtt As AcadAttributeReference
  19.     Dim AttCt1, AttCt2 As Integer
  20.     Dim Atts As Variant
  21.     Dim MyBlkCount As Integer
  22.    
  23.     Set DwgLstSht = Sheets("DrawingList")
  24.     Set ChkSht = Sheets("CheckList")
  25.    
  26.     GetFileNames
  27.    
  28.     ' Set up error control
  29.     On Error GoTo Error_Control
  30.    
  31.     Init ' initialize global variables
  32.    
  33.     ' Get the Current Path
  34.     StrPath = ThisWorkbook.Path
  35.     If (Right(StrPath, 1)  "") Then
  36.         StrPath = StrPath & ""
  37.     End If
  38.    
  39.     ' Unprotect sheet for drawing modifications
  40.     DwgLstSht.Unprotect
  41.     ChkSht.Unprotect
  42.     ' Replace the Layout header since it was deleted when the attributes where cleared
  43.     DwgLstSht.Cells(ROWOFF - 1, 2) = "Layout"
  44.    
  45.     ' Get the first drawing in the list and store in DwgName
  46.     DwgCnt = 0
  47.     AttTitles = False ' Set the Attribute Titles Flag to False (no titles yet)
  48.     DwgName = DwgLstSht.Cells(DwgCnt + ROWOFF, 1)
  49.    
  50.     ' Call display status function
  51.     temp = Status_Bar(True, "Activating ObjectDBX...")
  52.     Set MyDbx = GetInterfaceObject("ObjectDBX.AxDbDocument.18")
  53.    
  54.     While DwgName  ""
  55.         ' Call Display Status Function
  56.         temp = Status_Bar(True, "Opening drawing " & DwgName)
  57.         
  58.         ' Open a drawing in ObjectDbx
  59.         Set MyDbx = dbxOpen(StrPath, DwgName)
  60.         
  61.         ' If there are no errors and there is a file open
  62.         If Err.Number = 0 And MyDbx.Name  "" Then
  63.             
  64.             ' Determine if the chosen block is present in the drawing
  65.             For Each MyBlock In MyDbx.Blocks
  66.                 If UCase(MyBlock.Name) = UCase(Range("BlkName").Value) Then
  67.                     BlkExist = True
  68.                     Exit For
  69.                 End If
  70.             Next MyBlock
  71.    
  72.             ' If the block was found , then proceed
  73.             If BlkExist Then
  74.                 BlkFound = False
  75.                
  76.                 MyBlkCount = 0
  77.                 ' Iterate through all Layouts
  78.                 Set MyLayouts = MyDbx.Layouts
  79.                 For Each MyLayout In MyDbx.Layouts
  80.                   ' Avoid Modelspace if the Check Modelspace checkbox is NOT checked
  81.                   If Sheets("DrawingList").Model_Check.Value Or MyLayout.Name  "Model" Then
  82.                     ' Call display status function
  83.                     temp = Status_Bar(True, "Searching " & DwgName & " Layout " & MyLayout.Name & " " & Range("BlkName").Value)
  84.                     
  85.                     ' Loop through each entity in the layout.block group
  86.                     For Each MyEnt In MyLayout.Block
  87.                         ' Check if the current Entity is a Block Reference
  88.                         If TypeOf MyEnt Is AcadBlockReference Then
  89.                             ' Check that the block name matches what we are looking for
  90.                             If UCase(MyEnt.Name) = UCase(Range("BlkName").Value) Then
  91.                                 ' Store the current Entity as a Block Reference
  92.                                 Set MyBlockR = MyEnt
  93.                                 ' Make sure that the Block Reference has attributes
  94.                                 If MyBlockR.HasAttributes Then
  95.                                     ' If we have already found a block in the current drawing, add another row
  96.                                     If MyBlkCount > 0 Then
  97.                                         DwgLstSht.Cells(DwgCnt + ROWOFF + 1, 1).EntireRow.Insert
  98.                                         DwgCnt = DwgCnt + 1
  99.                                         DwgLstSht.Cells(DwgCnt + ROWOFF, 1) = DwgName
  100.                                         ChkSht.Cells(DwgCnt + ROWOFF, 1) = DwgName
  101.                                     End If
  102.                                     DwgLstSht.Cells(DwgCnt + ROWOFF, 2) = MyLayout.Name
  103.                                     ChkSht.Cells(DwgCnt + ROWOFF, 2) = MyLayout.Name
  104.                                     
  105.                                     ' Store all attributes in the matrix Atts
  106.                                     Atts = MyEnt.GetAttributes
  107.                                     
  108.                                     ' Step through each attribute in Atts
  109.                                     For AttCt1 = LBound(Atts) To UBound(Atts)
  110.                                         ' Get the next attribute
  111.                                         Set MyAtt = Atts(AttCt1)
  112.                                        
  113.                                         ' Call Display Status Function
  114.                                         temp = Status_Bar(True, "Accessing " & DwgName & " attributes: " & MyAtt.TagString & ".")
  115.                                        
  116.                                         ' Write the attribute information to DrawingList and CheckList sheets
  117.                                         DwgLstSht.Cells(DwgCnt + ROWOFF, AttCt1 + COLOFF).NumberFormat = "@" 'C. White 27/09/11 added to ensure attribute is listed as a string in Excel
  118.                                         DwgLstSht.Cells(DwgCnt + ROWOFF, AttCt1 + COLOFF) = MyAtt.TextString
  119.                                         ChkSht.Cells(DwgCnt + ROWOFF, AttCt1 + COLOFF) = MyAtt.TextString
  120.                                        
  121.                                         ' If this is the first drawing, store the attribute tags in the header row
  122.                                         If AttTitles = False Then
  123.                                             DwgLstSht.Cells(ROWOFF - 1, AttCt1 + COLOFF) = MyAtt.TagString
  124.                                             ChkSht.Cells(ROWOFF - 1, AttCt1 + COLOFF) = MyAtt.TagString
  125.                                             If AttCt1 = UBound(Atts) Then
  126.                                                 AttTitles = True
  127.                                             End If
  128.                                         End If
  129.                                     Next AttCt1
  130.                                     MyBlkCount = MyBlkCount + 1
  131.                                 End If
  132.                                 Exit For
  133.                             End If
  134.                         End If
  135.                     Next
  136.                  End If
  137.                 Next
  138.             End If
  139.             ' Clean the mydbx variable
  140.             Set MyDbx = Nothing
  141.         ElseIf Err.Number = 0 And MyDbx.Name = "" Then
  142.             ' Turn off the Status bar
  143.             temp = Status_Bar(False)
  144.             Exit Sub
  145.         End If
  146.         
  147.         ' Clean up Variables
  148.         Set MyBlock = Nothing
  149.         Set MyBlockR = Nothing
  150.         Set MyAtt = Nothing
  151.         
  152.         DwgCnt = DwgCnt + 1
  153.         DwgName = Cells(DwgCnt + ROWOFF, 1)
  154.     Wend
  155. Error_Control:
  156.     If Err.Number  0 Then
  157.         Set MyBlock = Nothing
  158.         Set MyAtt = Nothing
  159.         If Err.Number = 13 Then
  160.             MsgBox "Failed to initiate ObjectDbx, probably due to another version of Autocad...", vbCritical, "ObjectDBX Fail"
  161.         Else
  162.             MsgBox "Error #" & Err.Number & vbCr & Err.Description
  163.         End If
  164.     End If
  165.    
  166.    
  167.     If Not MyDbx Is Nothing Then Set MyDbx = Nothing
  168.     ' Adjust column widths to match the data in the cells
  169.     DwgLstSht.Activate
  170.     DwgLstSht.Columns.AutoFit
  171.     DwgLstSht.Cells(4, 2) = Now
  172.     DwgLstSht.Range("A6").Select
  173.     ' Protect sheet
  174.     DwgLstSht.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
  175.     ChkSht.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
  176.    
  177.     ' Turn off the Status bar
  178.     temp = Status_Bar(False)
  179. End Sub

谢谢你的鼓励。
M
回复

使用道具 举报

11

主题

40

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2014-2-20 07:16:35 | 显示全部楼层
在取得进展的同时,它能够找到并提取动态块及其属性,只需对原始VBA代码进行一些修改
下一步是提取图形中块实例的动态特性和句柄
M.
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-4-20 18:27 , Processed in 1.660153 second(s), 61 queries .

© 2020-2025 乐筑天下

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