<?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: Inserting a Block with Attributes in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5014656#M44120</link>
    <description>&lt;P&gt;If you had stepped through your code in debugging, you would have easily found out thta the code inside the "If...End If"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If TypeOf myEnt Is... Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;End If&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is never reached, thus, no attribute reference is added to the BlockReference. Do not just run code and wonder why it is not work. Debugging teaches a lot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is with Enumerator "myBTREnum", which you declare as BlockTableEnumerator. Why you need to iterate through BlockTable for an AttributeDefinition that belongs to a BlockTablerecord? You'll never find an AttributeDefinition in BlockTable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, what you need is to interate through the BlockTableRecord your BlockReference is based on to find AttributeDefinition. The code would be like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;''btr is the BlockTableRecord&lt;/P&gt;
&lt;P&gt;bref=New Blockreference(....)&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For Each id As ObjectId in btr&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myEnt=id.GetObject(OpenMode.ForRead)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; If TypeOf myEnt Is AttributeDefinition Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim myAttDef As AttributeDefinition = myEnt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''Create attribute reference based on the attributeDefinition&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim attrRef As New Attributereference&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''''&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;/P&gt;
&lt;P&gt;Next&lt;/P&gt;</description>
    <pubDate>Tue, 06 May 2014 13:31:33 GMT</pubDate>
    <dc:creator>norman.yuan</dc:creator>
    <dc:date>2014-05-06T13:31:33Z</dc:date>
    <item>
      <title>Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013326#M44113</link>
      <description>&lt;P&gt;Hello, this is the code that I've put together by reading posts on here and elsewhere.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have drawings in a library folder like "SMD_0201.dwg".&amp;nbsp; The drawing consists of a rectangle and an invisible attribute.&lt;/P&gt;&lt;P&gt;I want to bring the appropriate block (dwg)&amp;nbsp;into my drawing and place the references at the correct location and rotation with the correct attribute text.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I use this, all seems to run fine (the rectangles show up placed perfectly and rotated), but there are no attributes.&amp;nbsp; Then when I double pick on the block, it does not have a block name, it doesn't even have a definition.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BlockName = the path and name of the block&lt;/P&gt;&lt;P&gt;basename = block name&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Public Sub InsertSMDComp(ByVal InsPt As Point3d, ByVal BlockName As String, ByVal basename As String, ByVal RefDes As String, _
        ByVal rotation As Double, ByVal scale As Double)
        Dim smdTransMan As DatabaseServices.TransactionManager
        Dim smdTrans As DatabaseServices.Transaction
        Dim myDwg As Document
        Dim myBT As BlockTable

        myDwg = Application.DocumentManager.MdiActiveDocument
        smdTransMan = myDwg.TransactionManager
        smdTrans = smdTransMan.StartTransaction
        myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
        Dim myDB As Database
        myDB = myDwg.Database
        Try
            Using db As Database = New Database(False, True)
                db.ReadDwgFile(BlockName, IO.FileShare.Read, True, "")
                Dim BlkId As ObjectId
                BlkId = myDB.Insert(BlockName, db, True)
                Dim bt As BlockTable = smdTrans.GetObject(myDB.BlockTableId, OpenMode.ForRead, True)
                Dim btr As BlockTableRecord = smdTrans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
                Dim bref As BlockReference
                bref = New BlockReference(InsPt, BlkId)
                bref.Rotation = rotation
                bref.ScaleFactors = New Scale3d(scale, scale, scale)
                btr.AppendEntity(bref)
                smdTrans.AddNewlyCreatedDBObject(bref, True)
                'Set the Attribute Value
                Dim myAttColl As DatabaseServices.AttributeCollection
                Dim myEnt As DatabaseServices.Entity
                Dim myBTREnum As BlockTableRecordEnumerator
                myAttColl = bref.AttributeCollection
                myBTREnum = btr.GetEnumerator
                While myBTREnum.MoveNext
                    myEnt = myBTREnum.Current.GetObject(OpenMode.ForWrite)
                    If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
                        Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
                        Dim myAttRef As New DatabaseServices.AttributeReference
                        myAttRef.SetAttributeFromBlock(myAttDef, bref.BlockTransform)
                        myAttRef.TextString = RefDes
                        myAttColl.AppendAttribute(myAttRef)
                        smdTrans.AddNewlyCreatedDBObject(myAttRef, True)
                    End If
                End While
                smdTrans.Commit()
            End Using
        Catch ex As Autodesk.AutoCAD.Runtime.Exception
            MsgBox(ex.ToString)
        End Try
        smdTrans.Dispose()
        smdTransMan.Dispose()
    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Help!&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Mon, 05 May 2014 21:18:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013326#M44113</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-05T21:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013386#M44114</link>
      <description>&lt;P&gt;The problem that you get a block definition without name created in the drawing, is because the block definition was inserted into this drawing with an INVALID block name. The offending code is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BlkId = myDB.Insert(BlockName, db, True)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;necause BlockName is a file name with a full folder path, which includes characters line ":","\" that are not allowed to be used in block name. When you manulally define block, AutoCAD validates the block name; while you inserts block with .NET API's Database.Insert(), if the block name contaims invalid characters, the block definition can still be inserted, the AutoCAD somehow gives the block definition an empty name, instead of raising an exception. I personally think this is an API bug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on your sub's signature, I think you meant to use "baseName" as block name in the Insert() method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you should follow AutoCAD convention to use block's file name as Block name, in general. So, you do not need to pass "baseName" to the subroutine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you can get block file name from its full path name like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dim bName As String=System.IO.Path.GetFileNameWothoutExtension(BlockName)&lt;/P&gt;
&lt;P&gt;BlkId=nyDB.Insert(bName,...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 May 2014 21:45:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013386#M44114</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-05-05T21:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013402#M44115</link>
      <description>&lt;P&gt;Hi Norman,&lt;/P&gt;&lt;P&gt;Thanks, but when I try to use you suggestion, it tells me "GetFileNameWothoutExtension is not a member of 'System.IO.Path"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Above the sub, I do have:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Imports &lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;System.IO&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, if I don't include the path, how does it know where to get the drawing?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;Thanks&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 May 2014 21:55:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013402#M44115</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-05T21:55:21Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013414#M44116</link>
      <description>&lt;P&gt;Never mind, I got it.&lt;/P&gt;&lt;P&gt;Since I already have the "basename" I used that instead.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;Try&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;Using&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt; db &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;Database&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt; = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;Database&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;(&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;False&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;, &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; db.ReadDwgFile(BlockName, IO.&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;FileShare&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;.Read, &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;, &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#a31515" size="2" face="Consolas"&gt;&lt;FONT color="#a31515" size="2" face="Consolas"&gt;&lt;FONT color="#a31515" size="2" face="Consolas"&gt;""&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;)&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000" size="2" face="Consolas"&gt;&lt;FONT color="#008000" size="2" face="Consolas"&gt;&lt;FONT color="#008000" size="2" face="Consolas"&gt;&amp;nbsp;&amp;nbsp;'Dim bName As String = System.IO.Path.GetFileNameWothoutExtension(BlockName)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&amp;nbsp;&amp;nbsp; Dim&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt; BlkId &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;ObjectId&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BlkId = myDB.Insert(basename, db, &lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 May 2014 21:59:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013414#M44116</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-05T21:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013436#M44117</link>
      <description>&lt;P&gt;Ok, was too quick on that.&amp;nbsp; The blocks come in, and they look good.&amp;nbsp; They even have names and definitions (including an attribute definition) in the block editor.&amp;nbsp; The only problem now is how to insert them and apply a text value to the attribute?&lt;/P&gt;&lt;P&gt;I thought this should work.&amp;nbsp; it includes the value for the attribute&amp;nbsp;textstring.&lt;/P&gt;&lt;PRE&gt;While myBTREnum.MoveNext
                    myEnt = myBTREnum.Current.GetObject(OpenMode.ForWrite)
                    If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
                        Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
                        Dim myAttRef As New DatabaseServices.AttributeReference
                        myAttRef.SetAttributeFromBlock(myAttDef, bref.BlockTransform)
                        myAttRef.TextString = RefDes
                        myAttColl.AppendAttribute(myAttRef)
                        smdTrans.AddNewlyCreatedDBObject(myAttRef, True)
                    End If
                End While&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 May 2014 22:06:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013436#M44117</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-05T22:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013444#M44118</link>
      <description>&lt;P&gt;Also, when I try to do a manual insert on one of these existing blocks, there is a problem.&lt;/P&gt;&lt;P&gt;When I pick a name from the list, and pick ok to insert, it pops up with "The specified file was not found.&amp;nbsp; Specify the correct filename or select a valid block name."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 May 2014 22:19:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5013444#M44118</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-05T22:19:12Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5014622#M44119</link>
      <description>&lt;P&gt;Sorry, there was a typo:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;GetFileNameWothout...() SHOULD HAVE BEEN GetFileNameWithout...()&lt;/P&gt;</description>
      <pubDate>Tue, 06 May 2014 13:17:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5014622#M44119</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-05-06T13:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5014656#M44120</link>
      <description>&lt;P&gt;If you had stepped through your code in debugging, you would have easily found out thta the code inside the "If...End If"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If TypeOf myEnt Is... Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;End If&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is never reached, thus, no attribute reference is added to the BlockReference. Do not just run code and wonder why it is not work. Debugging teaches a lot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is with Enumerator "myBTREnum", which you declare as BlockTableEnumerator. Why you need to iterate through BlockTable for an AttributeDefinition that belongs to a BlockTablerecord? You'll never find an AttributeDefinition in BlockTable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, what you need is to interate through the BlockTableRecord your BlockReference is based on to find AttributeDefinition. The code would be like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;''btr is the BlockTableRecord&lt;/P&gt;
&lt;P&gt;bref=New Blockreference(....)&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For Each id As ObjectId in btr&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myEnt=id.GetObject(OpenMode.ForRead)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; If TypeOf myEnt Is AttributeDefinition Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim myAttDef As AttributeDefinition = myEnt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''Create attribute reference based on the attributeDefinition&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim attrRef As New Attributereference&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''''&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;/P&gt;
&lt;P&gt;Next&lt;/P&gt;</description>
      <pubDate>Tue, 06 May 2014 13:31:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5014656#M44120</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-05-06T13:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5014682#M44121</link>
      <description>&lt;P&gt;Debugging, and Debugging. If you step thorugh the code, or place a break point at where InsertSMDComp() is called, you simply examine what the block name and block file path is passed to this subroutine, then you probably can tell why you get that error message.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the code passes a file name without full path, the .NET runtime would only examine the current application folder for the block file. Thta is probably why the message. You need either supply full path, or write code to search through AutoCAd supporting folders to find block file by only given file name.&lt;/P&gt;</description>
      <pubDate>Tue, 06 May 2014 13:38:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5014682#M44121</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-05-06T13:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5015558#M44122</link>
      <description>&lt;P&gt;Hi Norman, you, as always, have been very helpful.&amp;nbsp; I did run through the code many, many times.&amp;nbsp; I put in the breaks, even inside the If...End If block and it stopped there.&amp;nbsp; I had watches going as well.&amp;nbsp; That's why this was so confusing and frustrating.&lt;/P&gt;&lt;P&gt;The blocks in my library include a rectangle and a block attribute, but sometimes it sounds like I'm having to add a new attribute to the block that just got read in.&lt;/P&gt;&lt;P&gt;I had the code you mentioned above in regards to btr and bref:&lt;/P&gt;&lt;PRE&gt; Dim btr As BlockTableRecord = smdTrans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
                Dim bref As BlockReference
                bref = New BlockReference(InsPt, BlkId)
                bref.Rotation = rotation
                bref.ScaleFactors = New Scale3d(scale, scale, scale)
                btr.AppendEntity(bref)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Since bref was included in the If..End If, I thought it was using the attribute in the block.&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I look up some of these things, like enumerator, most of the help I find is really no help.&amp;nbsp; All of the writers think their audience must be full time programmers with multiple degrees.&amp;nbsp; I'm not one to just splice code together and cross my fingers hoping it will work.&amp;nbsp; I search the Internet trying to understand what it is.&lt;/P&gt;&lt;P&gt;Thanks for all the time you've already spent helping me.&amp;nbsp; I really do appreciate it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Tue, 06 May 2014 19:38:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5015558#M44122</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-06T19:38:00Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5015634#M44123</link>
      <description>&lt;P&gt;Unfortunately, the blocks being read in, still do not have usable attributes.&amp;nbsp; Also, I cannot insert any of them manually.&lt;/P&gt;</description>
      <pubDate>Tue, 06 May 2014 20:25:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5015634#M44123</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-06T20:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5017654#M44124</link>
      <description>&lt;P&gt;Hi Norman,&lt;/P&gt;&lt;P&gt;Ok, I did some additional code thinking that maybe the blocks keep getting overwritten when the same block needs to get inserted.&lt;/P&gt;&lt;P&gt;I put a break in as you suggested and found it keeps failing in the "Else" section&lt;/P&gt;&lt;P&gt;Dim &lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;bref &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;As&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt; DatabaseServices.&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;&lt;FONT color="#2b91af" size="2" face="Consolas"&gt;BlockReference&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;(InsPt, myBT(baseName))&amp;nbsp; &amp;lt;--crashes here (InsPt and basename are valid)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I know you're busy, but could you explain BlockTables and BlockTableReference, as I can't see why this wouldn't work.&lt;/P&gt;&lt;PRE&gt;Public Sub InsertSMDComp(ByVal InsPt As Point3d, ByVal BlockName As String, ByVal RefDes As String, _
        ByVal rotation As Double, ByVal scale As Double)
        Dim smdTransMan As DatabaseServices.TransactionManager
        Dim smdTrans As DatabaseServices.Transaction
        Dim myDwg As Document
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord
        Dim baseName As String = System.IO.Path.GetFileNameWithoutExtension(BlockName)

        myDwg = Application.DocumentManager.MdiActiveDocument
        smdTransMan = myDwg.TransactionManager
        smdTrans = smdTransMan.StartTransaction
        myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
        If myBT.Has(baseName) Then
            myBTR = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
            Dim myBlockRef As New DatabaseServices.BlockReference(InsPt, myBT(baseName))
            myBlockRef.Rotation = rotation
            myBlockRef.ScaleFactors = New Geometry.Scale3d(scale, scale, scale)
            myBTR.AppendEntity(myBlockRef)
            smdTrans.AddNewlyCreatedDBObject(myBlockRef, True)
            'Set the Attribute Value
            Dim myEnt As DatabaseServices.Entity
            For Each id As ObjectId In myBTR
                myEnt = id.GetObject(OpenMode.ForRead)
                If TypeOf myEnt Is AttributeDefinition Then
                    Dim myAttDef As AttributeDefinition = myEnt
                    'Create attribute reference based on the attributeDefinition
                    Dim attrRef As New AttributeReference
                    attrRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
                    attrRef.TextString = RefDes
                    myBlockRef.AttributeCollection.AppendAttribute(attrRef)
                    smdTrans.AddNewlyCreatedDBObject(attrRef, True)
                End If
            Next
            smdTrans.Commit()
        Else
            Try
                Using db As Database = New Database(False, True)
                    Dim myDB As Database = myDwg.Database
                    db.ReadDwgFile(BlockName, IO.FileShare.Read, True, "")
                    'Dim bt As BlockTable = smdTrans.GetObject(myDB.BlockTableId, OpenMode.ForRead, True)
                    Dim btr As BlockTableRecord = smdTrans.GetObject(myBT(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
                    Dim bref As New DatabaseServices.BlockReference(InsPt, myBT(baseName))
                    bref.Rotation = rotation
                    bref.ScaleFactors = New Scale3d(scale, scale, scale)
                    btr.AppendEntity(bref)
                    smdTrans.AddNewlyCreatedDBObject(bref, True)
                    'Set the Attribute Value
                    Dim myEnt As DatabaseServices.Entity
                    For Each id As ObjectId In btr
                        myEnt = id.GetObject(OpenMode.ForRead)
                        If TypeOf myEnt Is AttributeDefinition Then
                            Dim myAttDef As AttributeDefinition = myEnt
                            'Create attribute reference based on the attributeDefinition
                            Dim attrRef As New AttributeReference
                            attrRef.SetAttributeFromBlock(myAttDef, bref.BlockTransform)
                            attrRef.TextString = RefDes
                            bref.AttributeCollection.AppendAttribute(attrRef)
                            smdTrans.AddNewlyCreatedDBObject(attrRef, True)
                        End If
                    Next
                    smdTrans.Commit()
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.ToString)
            End Try
        End If

        smdTrans.Dispose()
        smdTransMan.Dispose()
    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2014 15:55:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5017654#M44124</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-07T15:55:47Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5018106#M44125</link>
      <description>&lt;P&gt;Well, the error is quite obvious to me: in the the If myBT.Has(baseName) Then...&amp;nbsp;Else...End If statement, I saw your logic is like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If The block definition in fond in the BlockTable Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ''Insert the Block&lt;/P&gt;
&lt;P&gt;Else&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; ''Create a side Database from the block file;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; ''Insert it into the drawing database-&amp;gt;&lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;Here You FORGOT TO DO IT!!!&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT color="#000000"&gt; ''Insert the block&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Since in the Else clause you did not insert the Block file into the drawing as Block definition (e.g. MyDb.Insert(baseName, db, False)), thus the error.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Your code in the Then... branch is wrong too, (had you test it with a drawing that already has the block definition in it, you would have run into an exception at &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;myBlockRef=New BlockReference(intPt, myBT)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Because, myBT is a BlockTable, not a BlockTableRecord Id (type of ObjectId). It should be&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;myBlockref=New BlockReference(intPt, myBT(baseName))&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Also, you might want to lean up your code a bit, because you have duplicated code to create Blockreference in both branches of the IF.. statement. You can do it this way (psuedo-code)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;//Get BlockTable&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;BlockTable bt=...&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;//Test if the block definition exists or not&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Dim BtrId As ObjectId=ObjectId.Null&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;If bt.Has(baseName) Then btrId=bt(baseName)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;If BtrId.IsNull Then&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Insert block file&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Using dbAs Database=new Database(False,True)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; db.ReadDwgFile(....)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BtrId = myDb.Insert(baseName, db, False)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Using&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;End If&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;//Now, your are ready to create a Blcokreference&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;myBlockRef=New Blockreference(insPt, BtrId);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;....&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 May 2014 19:25:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5018106#M44125</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-05-07T19:25:11Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5020508#M44126</link>
      <description>&lt;P&gt;Ok, the drawings are being read into my drawing as blocks (with attributes).&lt;/P&gt;&lt;P&gt;Here's the part I don't understand, I added this to check for the attribute definition:&lt;/P&gt;&lt;P&gt;myBTR.AppendEntity(bref)&lt;/P&gt;&lt;P&gt;smdTrans.AddNewlyCreatedDBObject(bref,&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000" size="2" face="Consolas"&gt;&lt;FONT color="#008000" size="2" face="Consolas"&gt;&lt;FONT color="#008000" size="2" face="Consolas"&gt;'Set the Attribute Value&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;If&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt; myBTR.HasAttributeDefinitions &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;&lt;FONT color="#0000ff" size="2" face="Consolas"&gt;Then....&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;&lt;FONT size="2" face="Consolas"&gt;Even though the&amp;nbsp;drawings (consisting of a polyline and an attribute definition)&amp;nbsp;get read into my drawing as blocks (they have the polyline and att. def. in the block),&amp;nbsp;myBTR.HasAttributeDefinitions shows no attribute definitions.&amp;nbsp; It skips the code after this if.&amp;nbsp; The watch also shows no&amp;nbsp;attribute definitions.&amp;nbsp; How can that be if&amp;nbsp;I can see them?&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 17:05:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5020508#M44126</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-08T17:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5020826#M44127</link>
      <description>&lt;P&gt;In your code, myBTR is &lt;FONT color="#800000"&gt;&lt;STRONG&gt;ModelSpace&lt;/STRONG&gt;&lt;/FONT&gt; block, &lt;STRONG&gt;NOT&lt;/STRONG&gt; the block definition that you use to create the BlockReference!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to open the block definition then loop thorugh its AttributeDefinition. Something, after you created your BlockReference, like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dim bDef As BlockTableRecord=myTran.GetObject(myBT{baseName), OpenMode.ForRead)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If (bDef.HasAttributeDefinitions) Then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;End If&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 18:46:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5020826#M44127</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-05-08T18:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5021430#M44128</link>
      <description>&lt;P&gt;Hi Norman, thank you for being patient with me.&amp;nbsp; This was quite a learning experience, and I appreciate the time you spent.&lt;/P&gt;&lt;P&gt;Here is&amp;nbsp;the updated code, cleaned up for anyone who needs it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Public Sub InsertSMDComp(ByVal InsPt As Point3d, ByVal BlockName As String, ByVal RefDes As String, _
        ByVal rotation As Double, ByVal scale As Double)
        Dim smdTransMan As DatabaseServices.TransactionManager
        Dim smdTrans As DatabaseServices.Transaction
        Dim myDwg As Document
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord
        Dim baseName As String = (System.IO.Path.GetFileNameWithoutExtension(BlockName))

        myDwg = Application.DocumentManager.MdiActiveDocument
        smdTransMan = myDwg.TransactionManager
        smdTrans = smdTransMan.StartTransaction
        myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)

        Dim BtrId As ObjectId = ObjectId.Null
        If myBT.Has(baseName) Then BtrId = myBT(baseName) 'Drawing has the block
        If BtrId.IsNull Then 'Drawing does not have the block, read the drawuing file to create the block
            Try
                Using db As Database = New Database(False, True)
                    Dim myDB As Database = myDwg.Database
                    db.ReadDwgFile(BlockName, IO.FileShare.Read, True, "") 'Read the drawing file
                    BtrId = myDB.Insert(baseName, db, False) 'Insert drawing file as block into database
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.ToString)
            End Try
        End If
        ' Create the block reference in the drawing, and add the attribute text
        myBTR = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
        Dim bref As New DatabaseServices.BlockReference(InsPt, myBT(baseName))
        bref.Rotation = rotation
        bref.ScaleFactors = New Scale3d(scale, scale, scale)
        myBTR.AppendEntity(bref)
        smdTrans.AddNewlyCreatedDBObject(bref, True)
        Dim bDef As BlockTableRecord = smdTrans.GetObject(myBT(baseName), OpenMode.ForRead)
        'Set the Attribute Value
        If bDef.HasAttributeDefinitions Then
            Dim myEnt As DatabaseServices.Entity
            For Each id As ObjectId In bDef
                myEnt = id.GetObject(OpenMode.ForRead)
                If TypeOf myEnt Is AttributeDefinition Then
                    Dim myAttDef As AttributeDefinition = myEnt
                    'Create attribute reference based on the attributeDefinition
                    Dim attrRef As New AttributeReference
                    attrRef.SetAttributeFromBlock(myAttDef, bref.BlockTransform)
                    attrRef.TextString = RefDes
                    bref.AttributeCollection.AppendAttribute(attrRef)
                    smdTrans.AddNewlyCreatedDBObject(attrRef, True)
                End If
            Next
        End If
        smdTrans.Commit()

        smdTrans.Dispose()
        smdTransMan.Dispose()
    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 May 2014 22:41:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/5021430#M44128</guid>
      <dc:creator>mgorecki</dc:creator>
      <dc:date>2014-05-08T22:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/11416843#M44129</link>
      <description>&lt;P&gt;Thanks for including the scale and rotation parameters!&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 20:24:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/11416843#M44129</guid>
      <dc:creator>brian.k.smith</dc:creator>
      <dc:date>2022-09-12T20:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/12018365#M44130</link>
      <description>&lt;P&gt;When I do this, it renames my block when it's inserted into the blocktable&amp;nbsp; and cannot find the reference.&amp;nbsp; At first I noticed I had a nested block.&amp;nbsp; So, I exploded the other block and purged it and saved it.&amp;nbsp; But it's still trying to insert the block with the nested block name instead of the block name.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 19:49:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/12018365#M44130</guid>
      <dc:creator>mark.martinezECRPT</dc:creator>
      <dc:date>2023-06-07T19:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/12018522#M44131</link>
      <description>&lt;P&gt;I found out that I had a setting set to true instead of false.&lt;/P&gt;&lt;P&gt;However, I still cannot read the attributes from my block.&amp;nbsp; Infact, when I double-click the block, I do not get the attribute list.&amp;nbsp; I get some odd window.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 21:12:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/12018522#M44131</guid>
      <dc:creator>mark.martinezECRPT</dc:creator>
      <dc:date>2023-06-07T21:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Inserting a Block with Attributes</title>
      <link>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/12018534#M44132</link>
      <description>&lt;P&gt;You really should post your question as a new thread where you can provide more details of the issues you have, including your code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 21:17:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/inserting-a-block-with-attributes/m-p/12018534#M44132</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2023-06-07T21:17:44Z</dc:date>
    </item>
  </channel>
</rss>

