lh869 发表于 2006-1-5 18:55:00

怎样用VBA来修改属性块中的值

本帖最后由 作者 于 2006-1-6 8:03:14 编辑 <br /><br /> <P>请教各位大侠,这个问题已经捆扰我好久了,就是怎样才能利用VBA(或者是VB也行)将我这属性中的数值相乘并写入当前这个属性块中呢??拜托各位了!</P>
<P>前提是必须要用VB语言来写的哦!</P>

gaobin888 发表于 2006-1-6 15:59:00

<P>&nbsp;BlkSet为选择集,应该能解决&nbsp;&nbsp; </P>
<P>For Each elem In BlkSet<BR>&nbsp;&nbsp;&nbsp; 'MsgBox elem<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; With elem<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If StrComp(.EntityName, "AcDbBlockReference", 1) = 0 Then '如果块参照包含任何可编辑或固定(不可编辑)的属性时该属性为 TRUE 如果没有属性则为 FALSE。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If .HasAttributes Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Array1 = .GetAttributes '返回一个附着在块参照上可编辑的属性参照数组<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Array2 = .GetConstantAttributes '返回包含附着在块或外部参照的固定属性的数组<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For Count = LBound(Array1) To UBound(Array1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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p;&nbsp;&nbsp;&nbsp; Next Count<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End With<BR>&nbsp;&nbsp;&nbsp; Next elem</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>

lh869 发表于 2006-1-6 17:31:00

楼上这位大哥,我试了一下根本就不对啊!!

dragonboy 发表于 2006-1-9 23:10:00

<P>参考一下.其中有块中属性的操作</P><PRE class=Code>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 &amp; "Tag: " &amp; varAttributes(I).TagString &amp; _
                        "   Value: " &amp; varAttributes(I).textString &amp; "    "
    Next
    MsgBox "The attributes for blockReference " &amp; blockRefObj.name &amp; " are: " &amp; 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 &amp; "Tag: " &amp; varAttributes(I).TagString &amp; _
                        "   Value: " &amp; varAttributes(I).textString &amp; "    "
    Next
    MsgBox "The attributes for blockReference " &amp; blockRefObj.name &amp; " are: " &amp; strAttributes, , "GetAttributes Example"
   
End Sub</PRE>
页: [1]
查看完整版本: 怎样用VBA来修改属性块中的值