<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Not able to copy attributes of one block to other using vb.net code in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/not-able-to-copy-attributes-of-one-block-to-other-using-vb-net/m-p/5852280#M38392</link>
    <description>&lt;P&gt;From your code, I do not see where you do the "copying" attribute (value) from an existing blockReference to the newly inserted BlockRefernce. All your code does is just create all the attributes defined in a BlockTableRecord and uses the default attribute values (i.e. AttributeDefinition.TextString). Obviously, for first 2 tags, the default value is empty and the the third attribute has default value of "*400"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to "copy" the attribute values from another Blockreference, you need to obtain them first, which you did not do. Here is how you do it (pseudo code):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Dim dic As Dictionary(of String, String)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;''user select a Block reference, and then get its attribute value&lt;/P&gt;
&lt;P&gt;Using (tran As Transaction=....&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Dim bref As BlockRefernce = tran.GetObject(....)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &lt;FONT color="#FF0000"&gt;For Each id As ObjectId In bref.AttributeCollection&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim att As AttribueReference=tran.GetObject(....)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;''Store attribute value in the Dictionary&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If Not dic.ContainsKey(att.Tag.ToUpper()) Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dic.Add(att.Tag.ToUpper(),att.TextString)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; Next&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;End Using&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'' Then go ahead to insert a new BlockReference based on the same BlcokTableRecord&lt;/P&gt;
&lt;P&gt;''When creating attribute reference in this new BlcokReference&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If Not attrDef.Constant Then&lt;BR /&gt;&amp;nbsp; Using attRef As New AttributeReference&lt;BR /&gt;&amp;nbsp; &amp;nbsp; attRef.SetAttributeFromBlock(attrDef, acBlkRef.BlockTransform)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; attRef.Position = attRef.Position.TransformBy(acBlkRef.BlockTransform)&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &lt;FONT color="#FF0000"&gt;''attRef.TextString = attrDef.TextString&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; if dic.ContainsKey(attrDef.Tag.ToUpper()) Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; attrRef.TextString=dic(attrDef.Tag.ToUpper())&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; Else&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; attrRef.TextString=attrDef.TextString&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; acBlkRef.AttributeCollection.AppendAttribute(attRef)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; trans.AddNewlyCreatedDBObject(attRef, True)&lt;BR /&gt; End Using&lt;BR /&gt; End If&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;HTH&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 08 Oct 2015 14:03:18 GMT</pubDate>
    <dc:creator>norman.yuan</dc:creator>
    <dc:date>2015-10-08T14:03:18Z</dc:date>
    <item>
      <title>Not able to copy attributes of one block to other using vb.net code</title>
      <link>https://forums.autodesk.com/t5/net-forum/not-able-to-copy-attributes-of-one-block-to-other-using-vb-net/m-p/5851466#M38391</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a complete newbie for AutoCad and AutoCad.NET. I am stuck with a problem. I am asking the user to select a block reference which has three attribures namely LINEUP, BAY and HL. Then I am trying to create a copy of it. But the attributes LINEUP and BAY (Values of these) are not being copied. Can You please help me out with this issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Original Block With Attributes :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/193139iDBC41111B8A37DBD/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="OriginalBlockWithAttributes.png" title="OriginalBlockWithAttributes.png" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;New Block Without Values of the Attributes :&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/193140i8BF32AC35A718D36/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="NewBlockWithoutAttributes.png" title="NewBlockWithoutAttributes.png" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the VB.NET Code :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;CommandMethod("WithAttr2")&amp;gt; _
        Public Sub WithAttr2()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim database As Database = ed.Document.Database
            Dim blockName As String
            Dim blockRef1 As BlockReference
            Dim options As PromptEntityOptions = New PromptEntityOptions("\nSelect block reference")
            options.SetRejectMessage("\nSelect only block reference")
            options.AddAllowedClass(GetType(BlockReference), False)

            Dim acSSPrompt As PromptEntityResult = ed.GetEntity(options)

            Using tx As Transaction = database.TransactionManager.StartTransaction()

                Dim blockRef As BlockReference = TryCast(tx.GetObject(acSSPrompt.ObjectId, OpenMode.ForRead), BlockReference)

                Dim block As BlockTableRecord = Nothing
                If (blockRef.IsDynamicBlock) Then

                    'get the real dynamic block name.
                    block = TryCast(tx.GetObject(blockRef.DynamicBlockTableRecord, OpenMode.ForRead), BlockTableRecord)

                Else

                    block = TryCast(tx.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
                End If


                If (block &amp;lt;&amp;gt; Nothing) Then
                    Dim sfilter As New SelectionFilter(New TypedValue() {New TypedValue(CInt(DxfCode.Start), "INSERT"), New TypedValue(2, block.Name)})
                    Dim res As PromptSelectionResult = ed.SelectAll(sfilter)
                    If res.Status = PromptStatus.OK Then
                        'display result
                        MsgBox("---&amp;gt;  Selected " &amp;amp; res.Value.Count &amp;amp; " objects", res.Value.Count)
                        Dim Objectset As SelectionSet = res.Value
                        For Each id As ObjectId In Objectset.GetObjectIds()
                            blockRef1 = TryCast(tx.GetObject(id, OpenMode.ForRead), BlockReference)
                            MsgBox(blockRef1.Name)
                            blockName = blockRef1.Name
                            'Dim pt As New Point3d(0, 0, 0)
                            'ACADBlockMgr.InsertBlockWithAtt(blockRef1.Name, pt)
                        Next
                    End If
                End If
                tx.Commit()
            End Using
            Using trans As Transaction = database.TransactionManager.StartTransaction

                Dim blkTable As BlockTable = TryCast(database.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
                Dim blkRecId As ObjectId = blkTable(blockName)
                MsgBox(blkRecId.ToString())
                If blkRecId &amp;lt;&amp;gt; ObjectId.Null Then
                    Dim blkTableRecord As BlockTableRecord = trans.GetObject(blkRecId, OpenMode.ForRead)
                    Using acBlkRef As New BlockReference(New Point3d(2, 2, 0), blkTableRecord.Id)
                        Dim currentSpaceBlkTableRec As BlockTableRecord = trans.GetObject(database.CurrentSpaceId, OpenMode.ForWrite)
                        currentSpaceBlkTableRec.AppendEntity(acBlkRef)
                        trans.AddNewlyCreatedDBObject(acBlkRef, True)
                        'Check For Attributes
                        If blkTableRecord.HasAttributeDefinitions Then
                            'Add attributes from Block Table Records
                            For Each objId As ObjectId In blkTableRecord
                                Dim dbObj As DBObject = trans.GetObject(objId, OpenMode.ForRead)
                                If TypeOf dbObj Is AttributeDefinition Then
                                    Dim attrDef As AttributeDefinition = dbObj
                                    MsgBox(attrDef.Tag &amp;amp; " " &amp;amp; attrDef.TextString)
                                    If Not attrDef.Constant Then
                                        Using attRef As New AttributeReference
                                            attRef.SetAttributeFromBlock(attrDef, acBlkRef.BlockTransform)
                                            attRef.Position = attRef.Position.TransformBy(acBlkRef.BlockTransform)
                                            attRef.TextString = attrDef.TextString
                                            acBlkRef.AttributeCollection.AppendAttribute(attRef)
                                            trans.AddNewlyCreatedDBObject(attRef, True)
                                        End Using
                                    End If
                                End If
                            Next
                        End If
                    End Using
                End If
                trans.Commit()
            End Using
        End Sub&lt;/PRE&gt;&lt;P&gt;What am I doing wrong here. Please let me know.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in Advance,&lt;/P&gt;&lt;P&gt;Abhilash D K&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 05:00:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/not-able-to-copy-attributes-of-one-block-to-other-using-vb-net/m-p/5851466#M38391</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-10-08T05:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Not able to copy attributes of one block to other using vb.net code</title>
      <link>https://forums.autodesk.com/t5/net-forum/not-able-to-copy-attributes-of-one-block-to-other-using-vb-net/m-p/5852280#M38392</link>
      <description>&lt;P&gt;From your code, I do not see where you do the "copying" attribute (value) from an existing blockReference to the newly inserted BlockRefernce. All your code does is just create all the attributes defined in a BlockTableRecord and uses the default attribute values (i.e. AttributeDefinition.TextString). Obviously, for first 2 tags, the default value is empty and the the third attribute has default value of "*400"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to "copy" the attribute values from another Blockreference, you need to obtain them first, which you did not do. Here is how you do it (pseudo code):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Dim dic As Dictionary(of String, String)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;''user select a Block reference, and then get its attribute value&lt;/P&gt;
&lt;P&gt;Using (tran As Transaction=....&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Dim bref As BlockRefernce = tran.GetObject(....)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &lt;FONT color="#FF0000"&gt;For Each id As ObjectId In bref.AttributeCollection&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Dim att As AttribueReference=tran.GetObject(....)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;''Store attribute value in the Dictionary&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If Not dic.ContainsKey(att.Tag.ToUpper()) Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dic.Add(att.Tag.ToUpper(),att.TextString)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; Next&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;End Using&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'' Then go ahead to insert a new BlockReference based on the same BlcokTableRecord&lt;/P&gt;
&lt;P&gt;''When creating attribute reference in this new BlcokReference&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If Not attrDef.Constant Then&lt;BR /&gt;&amp;nbsp; Using attRef As New AttributeReference&lt;BR /&gt;&amp;nbsp; &amp;nbsp; attRef.SetAttributeFromBlock(attrDef, acBlkRef.BlockTransform)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; attRef.Position = attRef.Position.TransformBy(acBlkRef.BlockTransform)&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &lt;FONT color="#FF0000"&gt;''attRef.TextString = attrDef.TextString&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; if dic.ContainsKey(attrDef.Tag.ToUpper()) Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; attrRef.TextString=dic(attrDef.Tag.ToUpper())&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; Else&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; attrRef.TextString=attrDef.TextString&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; acBlkRef.AttributeCollection.AppendAttribute(attRef)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; trans.AddNewlyCreatedDBObject(attRef, True)&lt;BR /&gt; End Using&lt;BR /&gt; End If&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;HTH&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 14:03:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/not-able-to-copy-attributes-of-one-block-to-other-using-vb-net/m-p/5852280#M38392</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2015-10-08T14:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: Not able to copy attributes of one block to other using vb.net code</title>
      <link>https://forums.autodesk.com/t5/net-forum/not-able-to-copy-attributes-of-one-block-to-other-using-vb-net/m-p/5853727#M38393</link>
      <description>&lt;P&gt;Hi Norman,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the reply. I did not know that. Will try to depict your pseudo code into VB.NET and get back to you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With Regards,&lt;/P&gt;&lt;P&gt;Abhilash D K&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2015 04:47:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/not-able-to-copy-attributes-of-one-block-to-other-using-vb-net/m-p/5853727#M38393</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-10-09T04:47:28Z</dc:date>
    </item>
  </channel>
</rss>

