找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 931|回复: 0

[VBA程序]:用VBA是否有办法在插入属性块时候,不让使用默认属性值,而提示用户输入?

[复制链接]

已领礼包: 145个

财富等级: 日进斗金

发表于 2002-10-10 22:51:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  1. [FONT=courier new]
  2. VBA block insertion not requesting attributes  
  3. ID    14792  
  4. Applies to:    AutoCAD 2000
  5. AutoCAD 2000I
  6. AutoCAD 2002

  7. This document is part of    Attribute   Block Reference   COM-ActiveX Interfaces   VBA     


  8. Question
  9. If a block has attribute definitions, AutoCAD inserts all attributes with its
  10. default values and does not ask for the attribute values. Is there a function
  11. that asks for the attributes?
  12. Answer
  13. There is no ready-to-use function that asks for the attributes. Therefore, you
  14. will need to write your own function.

  15. After inserting the attribute you can use the AcadBlockReference.GetAttributes
  16. method to get all attributes. Then you can iterate the attributes and ask for
  17. the values. If you want to use the original prompt string, you have to get the
  18. block definition object and to look for the right attribute in it. There you
  19. can get the prompt string.

  20. The following function inserts the block 'test' and iterates the attributes.
  21. The 'UserForm1' object is a user form which contains:

  22. - An "OK" button which has to be pressed after entering the value for the current
  23. attribute and which hides the form
  24. - Three text boxes:
  25. TextBox1 : receives the attribute name (locked)
  26. TextBox2 : receives the attribute prompt string (locked)
  27. TextBox3 : there you can enter the value of the attribute


  28. Sub test()

  29.    Dim insPt As Variant
  30.    Dim insert As Object
  31.    Dim attribs As Variant
  32.    Dim attrib As Object
  33.    
  34.    ' Activate AutoCAD
  35.    AppActivate ThisDrawing.Application.Caption
  36.    
  37.    ' Get insertion point for insert entity
  38.    insPt = ThisDrawing.Utility.GetPoint(, "Select insert point: ")

  39.    ' Insert it
  40.    Set insert = ThisDrawing.ModelSpace.InsertBlock(insPt, "TEST", 1#, 1#, 1#, 0#)
  41.    
  42.    ' Ask for the attributes
  43.    attribs = insert.GetAttributes
  44.    For i = LBound(attribs) To UBound(attribs)
  45.       Set attrib = attribs(i)
  46.       UserForm1.TextBox1.Value = attrib.TagString
  47.       UserForm1.TextBox2.Value = GetPromptString("TEST", attrib)
  48.       UserForm1.TextBox3.Value = attrib.TextString
  49.       UserForm1.Show
  50.       If UserForm1.TextBox3.Value <> "" Then
  51.          attrib.TextString = UserForm1.TextBox3.Value
  52.       End If
  53.    Next i

  54. End Sub

  55. The called function 'GetPromptString' finds the original prompt string of the
  56. attribute:


  57. Public Function GetPromptString(blockName, attrib) As String

  58.    '
  59.    ' This function extracts the prompt string
  60.    ' of an attribute definition (AcadAttribute).
  61.    ' The needed parameters are the block name
  62.    ' and the attribute (AcadAttributeReference).
  63.    '

  64.    Dim blocks As Object
  65.    Dim block As Object
  66.    Dim count As Integer
  67.    Dim i As Integer
  68.    Dim ent As Object
  69.    
  70.    ' Get the block definition object of block 'blockName'
  71.    Set block = ThisDrawing.blocks.Item(blockName)
  72.    
  73.    ' Find the attribute (specified by 'attrib')
  74.    count = block.count
  75.    For i = 0 To count - 1
  76.       Set ent = block.Item(i)
  77.       If ent.EntityType = acAttribute Then
  78.          If ent.TagString = attrib.TagString Then
  79.             GetPromptString = ent.PromptString
  80.             Exit Function
  81.          End If
  82.       End If
  83.    Next i
  84.    
  85.    GetPromptString = ""

  86. End Function

  87. [/FONT]

下面附件是完整的工程代码
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-5 13:22 , Processed in 0.231199 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表