CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ActiveX 开发指南

相关分类

GetAttributes Method (ActiveX)

2023-1-4 10:05| 发布者: admin| 查看: 575| 评论: 0|来自: AutoCAD

摘要: 获取块引用中的属性。

获取块引用中的属性。

支持的平台:仅窗口

签名

工 务 局:

RetVal = object.GetAttributes
对象

类型:块引用,比较引用,外部引用最小块

此方法适用的对象。

返回值(RetVal)

类型:变量(属性引用对象的数组)

对象数组。AttributeReference

言论

此方法返回附加到块参照的可编辑属性参照的数组。

只能返回外部引用的常量属性。若要查找外部参照或块参照的常量属性,请使用该方法。GetConstantAttributes

例子

工 务 局:

Sub Example_GetAttributes()
    ' This example creates a block. It then adds attributes to that
    ' block. The block is then inserted into the drawing to create
    ' a block reference.
    
    ' Create the block
    Dim blockObj As AcadBlock
    Dim insertionPnt(0 To 2) As Double
    insertionPnt(0) = 0#: insertionPnt(1) = 0#: insertionPnt(2) = 0#
    Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "TESTBLOCK")
    
    ' Define the attribute definition
    Dim attributeObj As AcadAttribute
    Dim height As Double
    Dim mode As Long
    Dim prompt As String
    Dim insertionPoint(0 To 2) As Double
    Dim tag As String
    Dim value As String
    height = 1#
    mode = acAttributeModeVerify
    prompt = "Attribute Prompt"
    insertionPoint(0) = 5#: insertionPoint(1) = 5#: insertionPoint(2) = 0
    tag = "Attribute_Tag"
    value = "Attribute Value"
    
    ' Create the attribute definition object in model space
    Set attributeObj = blockObj.AddAttribute(height, mode, prompt, insertionPoint, tag, value)
    
    ' Insert the block
    Dim blockRefObj As AcadBlockReference
    insertionPnt(0) = 2#: insertionPnt(1) = 2#: insertionPnt(2) = 0
    Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "TESTBLOCK", 1#, 1#, 1#, 0)
    ZoomAll
    
    ' Get the attributes for the block reference
    Dim varAttributes As Variant
    varAttributes = blockRefObj.GetAttributes
    
    ' Move the attribute tags and values into a string to be displayed in a Msgbox
    Dim strAttributes As String
    Dim I As Integer
    For I = LBound(varAttributes) To UBound(varAttributes)
        strAttributes = strAttributes & vbLf & "  Tag: " & varAttributes(I).TagString & _
                        vbLf & "  Value: " & varAttributes(I).TextString & vbLf & "    "
    Next
    MsgBox "The attributes for blockReference " & blockRefObj.Name & " are: " & strAttributes, , "GetAttributes Example"
    
    ' Change the value of the attribute
    ' Note: There is no SetAttributes. Once you have the variant array, you have the objects.
    ' Changing them changes the objects in the drawing.
    varAttributes(0).TextString = "NEW VALUE!"
    
    ' Get the attributes
    Dim newvarAttributes As Variant
    newvarAttributes = blockRefObj.GetAttributes
    
    ' Again, display the tags and values
    strAttributes = ""
    For I = LBound(varAttributes) To UBound(varAttributes)
        strAttributes = strAttributes & vbLf & "  Tag: " & varAttributes(I).TagString & _
                        vbLf & "  Value: " & varAttributes(I).TextString & vbLf & "    "
    Next
    MsgBox "The attributes for blockReference " & blockRefObj.Name & " are: " & strAttributes, , "GetAttributes Example"
    
End Sub

Visual LISP:

(vl-load-com)
(defun c:Example_GetAttributes()
    ;; This example creates a block. It then adds attributes to that
    ;; block. The block is then inserted into the drawing to create
    ;; a block reference.
    (setq acadObj (vlax-get-acad-object))
    (setq doc (vla-get-ActiveDocument acadObj))
    
    ;; Create the block
    (setq insertionPnt (vlax-3d-point 0 0 0))
    (setq blockObj (vla-Add (vla-get-Blocks doc) insertionPnt "TESTBLOCK"))
    
    ;; Define the attribute definition
    (setq attHeight 1
          attMode acAttributeModeVerify
          attPrompt "Attribute Prompt"
          attTag "Attribute_Tag"
          attValue "Attribute Value"
          insertionPoint (vlax-3d-point 5 5 0))
    
    ;; Create the attribute definition object in model space
    (setq attributeObj (vla-AddAttribute blockObj attHeight attMode attPrompt insertionPoint attTag attValue))
    
    ;; Insert the block
    (setq insertionPnt (vlax-3d-point 2 2 0))
    (setq modelSpace (vla-get-ModelSpace doc))
    (setq blockRefObj (vla-InsertBlock modelSpace insertionPnt "TESTBLOCK" 1 1 1 0))
    (vla-ZoomAll acadObj)
    
    ;; Get the attributes for the block reference
    (setq varAttributes (vlax-variant-value (vla-GetAttributes blockRefObj)))
    
    ;; Move the attribute tags and values into a string to be displayed in a Msgbox
    (setq strAttributes ""
          I 0)
    (while (>= (vlax-safearray-get-u-bound varAttributes 1) I)
        (setq strAttributes (strcat strAttributes "\n  Tag: " (vla-get-TagString (vlax-safearray-get-element varAttributes I))
                                                  "\n  Value: " (vla-get-TextString (vlax-safearray-get-element varAttributes I)) "\n"))
        (setq I (1+ I))
    )

    (alert (strcat "The attributes for blockReference " (vla-get-Name blockRefObj) " are: " strAttributes))
    
    ;; Change the value of the attribute
    ;; Note: There is no SetAttributes. Once you have the variant array, you have the objects.
    ;; Changing them changes the objects in the drawing.
    (vla-put-TextString (vlax-safearray-get-element varAttributes 0) "NEW VALUE!")
    
    ;; Get the attributes
    (setq newvarAttributes (vlax-variant-value (vla-GetAttributes blockRefObj)))
    
    ;; Again, display the tags and values
    (setq strAttributes ""
          I 0)
    (while (>= (vlax-safearray-get-u-bound varAttributes 1) I)
        (setq strAttributes (strcat strAttributes "\n  Tag: " (vla-get-TagString (vlax-safearray-get-element varAttributes I))
                                                  "\n  Value: " (vla-get-TextString (vlax-safearray-get-element varAttributes I)) "\n"))
        (setq I (1+ I))
    )
    (alert (strcat "The attributes for blockReference " (vla-get-Name blockRefObj) " are: " strAttributes))
)

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 12:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部