<?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: Refine Block Definition with New Block in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458138#M55597</link>
    <description>&lt;P&gt;I didn't try out your code, and I do see one problem, where you'll need to check if an attribute exists before...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see you noticed that already.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm guessing that your c:\temp\test.dwg has a block defined in it named test.&amp;nbsp; That is what I would expect to find if I got an eSelfReference exception.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Open your test.dwg and run the insert command.&amp;nbsp; See if there is a "test" block in the list, and if so, purge it, and save, then try your code again.&lt;/P&gt;</description>
    <pubDate>Tue, 15 May 2012 15:57:21 GMT</pubDate>
    <dc:creator>chiefbraincloud</dc:creator>
    <dc:date>2012-05-15T15:57:21Z</dc:date>
    <item>
      <title>Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456710#M55590</link>
      <description>&lt;P&gt;How do I redefine a block...&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to insert the new block definition in the place of the old one and overwrite it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The new block has more attributes than the older instance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What a good method of going about that?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2012 19:18:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456710#M55590</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-05-14T19:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456824#M55591</link>
      <description>&lt;P&gt;Not sure about if this helps, this one is&amp;nbsp; similar on your task&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;   &amp;lt;CommandMethod("BlockReplaceTest", "breplace", CommandFlags.Session Or CommandFlags.Modal Or CommandFlags.UsePickSet Or CommandFlags.Redraw)&amp;gt; _
        Public Sub TestBlockReplaceByName()
            ' objects initializing
            Dim doc As Document = acApp.DocumentManager.MdiActiveDocument

            Dim ed As Editor = doc.Editor

            Dim db As Database = doc.Database

            Try

                Using doc.LockDocument()

                    Using tr As Transaction = db.TransactionManager.StartTransaction()

                        Dim psto As New PromptStringOptions(vbLf &amp;amp; "Enter a replacement block name: ")

                        psto.AllowSpaces = True

                        psto.DefaultValue = "MyBlock"  'old block

                        Dim stres As PromptResult

                        stres = ed.GetString(psto)

                        If stres.Status &amp;lt;&amp;gt; PromptStatus.OK Then
                            Return
                        End If

                        Dim oldblock As String = stres.StringResult

                        ed.WriteMessage(vbLf &amp;amp; "Text Entered" &amp;amp; vbTab &amp;amp; "{0}", oldblock)

                        psto = New PromptStringOptions(vbLf &amp;amp; "Enter a block name to be replaced: ")

                        psto.AllowSpaces = True

                        psto.DefaultValue = "NewBlock"    'new block

                        stres = ed.GetString(psto)

                        If stres.Status &amp;lt;&amp;gt; PromptStatus.OK Then

                            Return

                        End If

                        Dim newblock As String = stres.StringResult

                        Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)

                        If Not bt.Has(newblock) Then
                            Return
                        End If

                        Dim newblkId As ObjectId = bt(newblock)

                        acApp.SetSystemVariable("nomutt", 1)

                        Dim tvs As TypedValue() = {New TypedValue(0, "insert"), New TypedValue(2, oldblock)}

                        Dim filt As New SelectionFilter(tvs)

                        Dim pso As New PromptSelectionOptions()

                        pso.MessageForRemoval = "You must select the blocks only"

                        pso.MessageForAdding = vbLf &amp;amp; "Select replacement blocks: "

                        AddHandler ed.SelectionAdded, AddressOf ed_SelectionAdded

                        Dim res As PromptSelectionResult = ed.GetSelection(pso, filt)

                        If res.Status &amp;lt;&amp;gt; PromptStatus.OK Then
                            Return
                        End If

                        Dim btr As BlockTableRecord = TryCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)

                        Dim sset As SelectionSet = res.Value

                        For Each obj As SelectedObject In sset

                            Dim ent As Entity = TryCast(DirectCast(obj.ObjectId.GetObject(OpenMode.ForRead), Entity), Entity)

                            Dim oldblk As BlockReference = TryCast(ent, BlockReference)

                            Dim ip As Point3d = oldblk.Position

                            Dim scl As Scale3d = oldblk.ScaleFactors

                            Dim rot As Double = oldblk.Rotation

                            Dim newblk As New BlockReference(ip, newblkId)

                            newblk.SetPropertiesFrom(ent)

                            newblk.Rotation = rot

                            newblk.ScaleFactors = scl

                            btr.AppendEntity(newblk)

                            tr.AddNewlyCreatedDBObject(newblk, True)

                            ApplyAttributes(db, tr, newblk)

                            oldblk.UpgradeOpen()

                            oldblk.Erase()

                            oldblk.Dispose()

                        Next

                        tr.Commit()

                    End Using

                End Using

            Catch ex As System.Exception

                ed.WriteMessage(ex.Message &amp;amp; vbLf &amp;amp; ex.StackTrace)

            Finally

                acApp.SetSystemVariable("nomutt", 0)

                RemoveHandler ed.SelectionAdded, AddressOf ed_SelectionAdded

            End Try

        End Sub


        Private Sub ed_SelectionAdded(sender As Object, e As SelectionAddedEventArgs)
            ' you may want to add some action here
            DirectCast(sender, Editor).WriteMessage(vbLf &amp;amp; vbTab &amp;amp; "{0} blocks to selection added", e.AddedObjects.Count)
        End Sub


        Public Sub ApplyAttributes(db As Database, tr As Transaction, bref As BlockReference)

            Dim btrec As BlockTableRecord = TryCast(tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)

            If btrec.HasAttributeDefinitions Then

                Dim atcoll As Autodesk.AutoCAD.DatabaseServices.AttributeCollection = bref.AttributeCollection

                For Each subid As ObjectId In btrec

                    Dim ent As Entity = DirectCast(subid.GetObject(OpenMode.ForRead), Entity)

                    Dim attDef As AttributeDefinition = TryCast(ent, AttributeDefinition)

                    If attDef IsNot Nothing Then

                        Dim attRef As New AttributeReference()

                        attRef.SetDatabaseDefaults()
                        'optional
                        attRef.SetAttributeFromBlock(attDef, bref.BlockTransform)

                        attRef.Position = attDef.Position.TransformBy(bref.BlockTransform)

                        attRef.Justify = attDef.Justify
                        'must be added all other properties for right position, eg. alignment modes etc
                        attRef.Tag = attDef.Tag

                        attRef.AdjustAlignment(db)

                        atcoll.AppendAttribute(attRef)

                        tr.AddNewlyCreatedDBObject(attRef, True)

                    End If

                Next

            End If

        End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800000" face="arial,helvetica,sans-serif"&gt;~'J'~&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2012 20:20:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456824#M55591</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2012-05-14T20:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456834#M55592</link>
      <description>&lt;P&gt;That post and comments can be good start for you: &lt;A href="http://adndevblog.typepad.com/autocad/2012/05/redefining-a-block.html" target="_blank"&gt;http://adndevblog.typepad.com/autocad/2012/05/redefining-a-block.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2012 20:22:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456834#M55592</guid>
      <dc:creator>Alexander.Rivilis</dc:creator>
      <dc:date>2012-05-14T20:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456972#M55593</link>
      <description>&lt;P&gt;The post link Alexander provided is&amp;nbsp;a good start, but does not address the issue of adding or subtracting attributes.&amp;nbsp; After using the method Stephen Prestons code, you would also need to open each BlockReference (could be in the same loop where he calls RecordGraphicsModified)&amp;nbsp;and add or remove AttributeReferences from it based on the AttributeDefinitions found in the BlockTableRecord.&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2012 22:06:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3456972#M55593</guid>
      <dc:creator>chiefbraincloud</dc:creator>
      <dc:date>2012-05-14T22:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3457016#M55594</link>
      <description>&lt;P&gt;Thanks Chiefbraincloud, and everyone else... this will definetly get me started in the process.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just trying to get an idea of the basic workflow.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The add attribute reference... okay I remember that. Used that one before somewhere.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2012 22:46:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3457016#M55594</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-05-14T22:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458112#M55595</link>
      <description>&lt;P&gt;So I'm getting the error eSelfReference....&lt;/P&gt;&lt;P&gt;I already have a different definintion of the block Test in my drawing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm wanting to do is overwrite this version in the current drawing with the one c:\temp\test.dwg as my new blockdefinition.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;  &amp;lt;Autodesk.AutoCAD.Runtime.CommandMethod("ReplaceBlock")&amp;gt; _
    Public Sub ReplaceBlock()

        Dim myDwg As Document = Application.DocumentManager.MdiActiveDocument
        Dim myDB As Database = myDwg.Database
        Dim blockName As String = "TEST"

        Editor.WriteMessage("Replacing Block" &amp;amp; Environment.NewLine)

        Dim BlkDB As Database = New Database(False, True)
        BlkDB.ReadDwgFile("C:\\Temp\\TEST.dwg", System.IO.FileShare.Read, True, "")

        Using myTrans As Transaction = myDB.TransactionManager.StartTransaction()

            Dim blockTable As BlockTable = myTrans.GetObject(myDB.BlockTableId, OpenMode.ForRead, False, True)

            Dim myBlockDefId As ObjectId = myDB.Insert(blockName, BlkDB, True)

            If myBlockDefId &amp;lt;&amp;gt; ObjectId.Null Then

                Dim myBlockDef As BlockTableRecord = myTrans.GetObject(myBlockDefId, OpenMode.ForRead, False, True)

                Dim myBlockRefIds As ObjectIdCollection = myBlockDef.GetBlockReferenceIds(False, True)

                For Each id As ObjectId In myBlockRefIds
                    Dim myBlockRef As BlockReference = myTrans.GetObject(id, OpenMode.ForWrite, False, True)
                    myBlockRef.RecordGraphicsModified(True)


                    'Append Attribute References to the BlockReference
                    Dim myAttColl As DatabaseServices.AttributeCollection
                    Dim myEntID As ObjectId
                    Dim myEnt As DatabaseServices.Entity

                    myAttColl = myBlockRef.AttributeCollection
                    For Each myEntID In myBlockDef
                        myEnt = myEntID.GetObject(OpenMode.ForWrite)
                        If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
                            Dim myAttDef As DatabaseServices.AttributeDefinition = _
                           CType(myEnt, AttributeDefinition)
                            Dim myAttRef As New DatabaseServices.AttributeReference
                            myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
                            myAttColl.AppendAttribute(myAttRef)
                            myTrans.AddNewlyCreatedDBObject(myAttRef, True)
                        End If
                    Next

                Next

            End If

            myTrans.Commit()
        End Using

        BlkDB.Dispose()

    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 May 2012 15:39:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458112#M55595</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-05-15T15:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458124#M55596</link>
      <description>&lt;P&gt;The other problem is it's Adding Duplicate Attribute Definitions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        'Append Attribute References to the BlockReference
                    Dim myAttColl As DatabaseServices.AttributeCollection
                    Dim myEntID As ObjectId
                    Dim myEnt As DatabaseServices.Entity

                    myAttColl = myBlockRef.AttributeCollection
                    For Each myEntID In myBlockDef
                        myEnt = myEntID.GetObject(OpenMode.ForWrite)
                        If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then

                            Dim myAttDef As DatabaseServices.AttributeDefinition = CType(myEnt, AttributeDefinition)
                            Dim myAttRef As New DatabaseServices.AttributeReference

                            myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
                            myAttColl.AppendAttribute(myAttRef)

                            myTrans.AddNewlyCreatedDBObject(myAttRef, True)
                        End &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hmm... I need to test if the attribute already exists, and either overwrite it, or delete the old one, and re-create it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 May 2012 15:51:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458124#M55596</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-05-15T15:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458138#M55597</link>
      <description>&lt;P&gt;I didn't try out your code, and I do see one problem, where you'll need to check if an attribute exists before...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I see you noticed that already.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm guessing that your c:\temp\test.dwg has a block defined in it named test.&amp;nbsp; That is what I would expect to find if I got an eSelfReference exception.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Open your test.dwg and run the insert command.&amp;nbsp; See if there is a "test" block in the list, and if so, purge it, and save, then try your code again.&lt;/P&gt;</description>
      <pubDate>Tue, 15 May 2012 15:57:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458138#M55597</guid>
      <dc:creator>chiefbraincloud</dc:creator>
      <dc:date>2012-05-15T15:57:21Z</dc:date>
    </item>
    <item>
      <title>Re: Refine Block Definition with New Block</title>
      <link>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458282#M55598</link>
      <description>&lt;P&gt;Yah. I purged the block out of the drawing. Now that part is working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just trying to figure out the part if an attribute exists already.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the help!&lt;/P&gt;&lt;P&gt;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://forums.autodesk.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 May 2012 17:08:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/refine-block-definition-with-new-block/m-p/3458282#M55598</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-05-15T17:08:50Z</dc:date>
    </item>
  </channel>
</rss>

