乐筑天下

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

[编程交流] getting block attributes - VBA

[复制链接]

48

主题

1073

帖子

1043

银币

后起之秀

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

铜币
238
发表于 2022-7-6 14:40:08 | 显示全部楼层 |阅读模式
I have a title block with several attributes. I know how to get all the attribute values using VBA but I was hoping to jut determine the value of one of them. It is something I think I should know how to do, but if I did then I've forgotten.
 
I have something like....
  1. myAttibutes = myBlock.GetAttributesfor i = 0 to ubound(myAttributes)debug.print myAttributes(i).TagString;myAttributes(i).TextStringnext i
 
Can anybody help me determine the text value of the attibute TagString = "Title1" without stepping through them all?
回复

使用道具 举报

0

主题

252

帖子

290

银币

限制会员

铜币
-8
发表于 2022-7-6 14:48:58 | 显示全部楼层
I dont think you can do it unless you know the index value of the tag in the block.  I have code to step through them if you want it.
回复

使用道具 举报

0

主题

252

帖子

290

银币

限制会员

铜币
-8
发表于 2022-7-6 15:01:39 | 显示全部楼层
I use this code to update my attributes, but you can tweak it to read the value of an attribute
  1.     For Each objBlkRef In objSelSet       If objBlkRef.HasAttributes Then           varAtts = objBlkRef.GetAttributes           For intAElems = LBound(varAtts) To UBound(varAtts)               Set objAttRef = varAtts(intAElems)               If objAttRef.TagString = strTagName Then                   strOrigTagValue = objAttRef.TextString                   If Len(strNewTagValue) > 0 Then                       objAttRef.TextString = strNewTagValue                       Exit For                   End If               End If           Next intAElems       End If   Next objBlkRef
回复

使用道具 举报

48

主题

1073

帖子

1043

银币

后起之秀

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

铜币
238
发表于 2022-7-6 15:10:20 | 显示全部楼层
Thanks,
I am looking for 4 out of 15 so its not a big problem to step through, which is what I have done but I thought I had seen a way to home in on one once you knew the TagString. Oh well, I'll stick with what I've got.
回复

使用道具 举报

0

主题

252

帖子

290

银币

限制会员

铜币
-8
发表于 2022-7-6 15:21:57 | 显示全部楼层
There might be a way to go directly to it, I can look if you want
回复

使用道具 举报

48

主题

1073

帖子

1043

银币

后起之秀

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

铜币
238
发表于 2022-7-6 15:23:56 | 显示全部楼层
I'm not overly fussed now as what I have works well enough - however if do you find out......
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-6 15:33:11 | 显示全部楼层
Hi guys not sure but have a look at my last post it searches all blocks and only picks out 1 to update I use the attrib value (x) not tagstring in the block the attrib(x) is always unique atributes are attribs(0) attribs(1) etc easier than tag strings
 
PS missing bit of code is a function that lets the user pick text or block in the drawing as the key to find the one block with attrib(0) = key
 
here is also a title block update routine may be usefull
 
  1. Public Sub issued_for_construction()' This Updates the Issued for construction and sets rev 0
  2.  
  3. Dim SS As AcadSelectionSet
  4. Dim Count As Integer
  5. Dim FilterDXFCode(1) As Integer
  6. Dim FilterDXFVal(1) As Variant
  7. Dim attribs As Variant
  8. Dim BLOCK_NAME As String
  9. On Error Resume Next
  10. FilterDXFCode(0) = 0
  11. FilterDXFVal(0) = "INSERT"
  12. FilterDXFCode(1) = 2
  13. FilterDXFVal(1) = "DA1DRTXT"
  14. BLOCK_NAME = "DA1DRTXT"
  15. Set SS = ThisDrawing.SelectionSets.Add("issued")
  16. SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
  17.  
  18. For Cntr = 0 To SS.Count - 1
  19.    attribs = SS.Item(Cntr).GetAttributes
  20.  
  21.  
  22.         attribs(0).TextString = "ISSUED FOR CONSTRUCTION"
  23.         attribs(3).TextString = "0"
  24.  
  25.         attribs(0).Update
  26.         attribs(3).Update
  27.  
  28. Next Cntr
  29. ThisDrawing.SelectionSets.Item("issued").Delete
  30. 'DO AGAIN FOR REVTABLE
  31. 'DATE
  32. 'Dim MyDate
  33. 'MyDate = Date
  34. Call DashDate
  35. FilterDXFCode(1) = 2
  36. FilterDXFVal(1) = "REVTABLE"
  37. BLOCK_NAME = "REVTABLE"
  38. Set SS = ThisDrawing.SelectionSets.Add("revs")
  39. SS.Select acSelectionSetAll, , , FilterDXFCode, FilterDXFVal
  40.  
  41. For Cntr = 0 To SS.Count - 1
  42.    attribs = SS.Item(Cntr).GetAttributes
  43.  
  44.  
  45.         attribs(0).TextString = "0"
  46.         attribs(1).TextString = DashDate
  47.         attribs(2).TextString = "ISSUED FOR CONSTRUCTION"
  48.  
  49.  
  50.  
  51.         attribs(0).Update
  52.         attribs(1).Update
  53.         attribs(2).Update
  54.  
  55. Next Cntr
  56. ThisDrawing.SelectionSets.Item("revs").Delete
  57. MsgBox "Drawing now changed to Issued for Construction"
  58. End Sub[code]
回复

使用道具 举报

48

主题

1073

帖子

1043

银币

后起之秀

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

铜币
238
发表于 2022-7-6 15:46:04 | 显示全部楼层
I know that Attribute(3) will work but this routine has to work on 15 year's worth of borders of varing sizes and revisions. All have Attribute("Title1") but they won't all have Attribute(3) = "Title1", some will be Attrbute(0), some Attribute(20). I had hoped I could go directly to "Title1" as I thought I had seen it done before but it works stepping through so I'm happy.
 
For sake of completeness here is my code. Very quick & dirty and could easilly be improved but it works so I expect it will stay.
  1. Option ExplicitPrivate Sub cmdDown_Click()Dim myString As StringSelect Case Label5.CaptionCase Is = "1"myString = lblTag1.CaptionlblTag1.Caption = lblTag2.CaptionlblTag2.Caption = myStringLabel5.Caption = "2"lblTag2.SpecialEffect = fmSpecialEffectEtchedlblTag1.SpecialEffect = fmSpecialEffectSunkenCase Is = "2"myString = lblTag2.CaptionlblTag2.Caption = lblTag3.CaptionlblTag3.Caption = myStringLabel5.Caption = "3"lblTag3.SpecialEffect = fmSpecialEffectEtchedlblTag2.SpecialEffect = fmSpecialEffectSunkenCase Is = "3"myString = lblTag3.CaptionlblTag3.Caption = lblTag4.CaptionlblTag4.Caption = myStringLabel5.Caption = "4"lblTag4.SpecialEffect = fmSpecialEffectEtchedlblTag3.SpecialEffect = fmSpecialEffectSunkenEnd SelectEnd SubPrivate Sub cmdExit_Click()Unload MeEnd SubPrivate Sub cmdPopulate_Click()Dim myObject As ObjectDim mySelectionSet As AcadSelectionSetDim myAttributes As VariantSet mySelectionSet = ThisDrawing.SelectionSets.Add("Fred")Dim gpCode(0 To 1) As IntegerDim dataValue(0 To 1) As VariantDim groupCode As VariantDim dataCode As VariantgpCode(0) = 2 'namegpCode(1) = 0 'itemgroupCode = gpCodedataValue(0) = "STLA*" 'layer namedataValue(1) = "INSERT" 'item typedataCode = dataValuemySelectionSet.Select acSelectionSetAll, , , groupCode, dataCodeSet myObject = mySelectionSet.Item(0)myAttributes = myObject.GetAttributesIf lblTag1.Caption  "" Then   myAttributes(Label1).TextString = lblTag1.Caption   Else   myAttributes(Label1).TextString = ""   End IfIf lblTag2.Caption  "" Then   myAttributes(Label2).TextString = lblTag2.Caption   Else   myAttributes(Label2).TextString = ""   End IfIf lblTag3.Caption  "" Then   myAttributes(Label3).TextString = lblTag3.Caption   Else   myAttributes(Label3).TextString = ""   End IfIf lblTag4.Caption  "" Then   myAttributes(Label4).TextString = lblTag4.Caption   Else   myAttributes(Label4).TextString = ""   End IfThisDrawing.SelectionSets.Item("Fred").DeleteUnload MeEnd SubPrivate Sub cmdUp_Click()Dim myString As StringSelect Case Label5.CaptionCase Is = "2"myString = lblTag2.CaptionlblTag2.Caption = lblTag1.CaptionlblTag1.Caption = myStringLabel5.Caption = "1"lblTag1.SpecialEffect = fmSpecialEffectEtchedlblTag2.SpecialEffect = fmSpecialEffectSunkenCase Is = "3"myString = lblTag3.CaptionlblTag3.Caption = lblTag2.CaptionlblTag2.Caption = myStringLabel5.Caption = "2"lblTag2.SpecialEffect = fmSpecialEffectEtchedlblTag3.SpecialEffect = fmSpecialEffectSunkenCase Is = "4"myString = lblTag4.CaptionlblTag4.Caption = lblTag3.CaptionlblTag3.Caption = myStringLabel5.Caption = "3"lblTag3.SpecialEffect = fmSpecialEffectEtchedlblTag4.SpecialEffect = fmSpecialEffectSunkenEnd SelectEnd SubPrivate Sub lblTag1_Click()Label5.Caption = "1"lblTag1.SpecialEffect = fmSpecialEffectEtchedlblTag2.SpecialEffect = fmSpecialEffectSunkenlblTag3.SpecialEffect = fmSpecialEffectSunkenlblTag4.SpecialEffect = fmSpecialEffectSunkenEnd SubPrivate Sub lblTag2_Click()Label5.Caption = "2"lblTag2.SpecialEffect = fmSpecialEffectEtchedlblTag1.SpecialEffect = fmSpecialEffectSunkenlblTag3.SpecialEffect = fmSpecialEffectSunkenlblTag4.SpecialEffect = fmSpecialEffectSunkenEnd SubPrivate Sub lblTag3_Click()Label5.Caption = "3"lblTag3.SpecialEffect = fmSpecialEffectEtchedlblTag1.SpecialEffect = fmSpecialEffectSunkenlblTag2.SpecialEffect = fmSpecialEffectSunkenlblTag4.SpecialEffect = fmSpecialEffectSunkenEnd SubPrivate Sub lblTag4_Click()Label5.Caption = "4"lblTag4.SpecialEffect = fmSpecialEffectEtchedlblTag1.SpecialEffect = fmSpecialEffectSunkenlblTag2.SpecialEffect = fmSpecialEffectSunkenlblTag3.SpecialEffect = fmSpecialEffectSunkenEnd SubPrivate Sub UserForm_Initialize()Dim i As IntegerDim myObject As ObjectDim mySelectionSet As AcadSelectionSetDim myAttributes As VariantSet mySelectionSet = ThisDrawing.SelectionSets.Add("Fred")Dim gpCode(0 To 1) As IntegerDim dataValue(0 To 1) As VariantDim groupCode As VariantDim dataCode As VariantgpCode(0) = 2 'namegpCode(1) = 0 'itemgroupCode = gpCodedataValue(0) = "STLA*" 'layer namedataValue(1) = "INSERT" 'item typedataCode = dataValuemySelectionSet.Select acSelectionSetAll, , , groupCode, dataCodeIf mySelectionSet.Count = 1 Then'a block has been selectedSet myObject = mySelectionSet.Item(0)myAttributes = myObject.GetAttributesFor i = 0 To UBound(myAttributes) - 1If myAttributes(i).TagString = "TITLE1" Then   lblTag1 = myAttributes(i).TextString   Label1 = i   End IfIf myAttributes(i).TagString = "TITLE2" Then   lblTag2 = myAttributes(i).TextString   Label2 = i   End IfIf myAttributes(i).TagString = "TITLE3" Then   lblTag3 = myAttributes(i).TextString   Label3 = i   End IfIf myAttributes(i).TagString = "TITLE4" Then   lblTag4 = myAttributes(i).TextString   Label4 = i   End IfNext iElseMsgBox "This ONLY works with 1 Drawing Border"End IfThisDrawing.SelectionSets.Item("Fred").DeleteEnd Sub
It basically gets the 4 "title" attributes from one of our borders and displays them on a form. You can then shuffle the order of the attributes and write them back to the title block.
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-5 00:32 , Processed in 0.368662 second(s), 68 queries .

© 2020-2025 乐筑天下

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