<?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: Error When Deleteing Block - eNotOpenForWrite in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5175159#M43176</link>
    <description>&lt;P&gt;Nested block references would not be in model space, they would be in the containing block. If you want to erase all instances of a block, you should simply get the list of block references from the block table record, then open each instance and erase it regardless of who owns it. You're likely to get more responses if you post your followup as a new thread.&lt;/P&gt;</description>
    <pubDate>Fri, 25 Jul 2014 05:31:15 GMT</pubDate>
    <dc:creator>owenwengerd</dc:creator>
    <dc:date>2014-07-25T05:31:15Z</dc:date>
    <item>
      <title>Error When Deleteing Block - eNotOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5169657#M43173</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I try to delete a block I get this error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;---------------------------&lt;BR /&gt;AutoCAD Error Aborting&lt;BR /&gt;---------------------------&lt;BR /&gt;INTERNAL ERROR:&amp;nbsp; !dbobji.cpp@8638: eNotOpenForWrite&lt;BR /&gt;---------------------------&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;What is happenening? I have my tables opened for Write.&lt;/P&gt;&lt;P&gt;Here is the code Im using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;lt;CommandMethod("CleanHouse")&amp;gt;
        Sub CleanHouse()
            Dim OldBlocks() As String = {"MyOldBlock1","OldBlock2"}

            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            'get object id's of all block in the active drawing
            Dim objIdColl As ObjectIdCollection = BlockReferenceInModelSpace(ed)
            'lock document
            Dim docLock As DocumentLock = doc.LockDocument
            'start transaction
            Dim tx As Transaction = db.TransactionManager.StartTransaction
            'iterate object id
            For Each objId As ObjectId In objIdColl
                'get the obeject
                Dim ent As Entity = tx.GetObject(objId, OpenMode.ForWrite)
                If (TypeOf ent Is BlockReference) Then
                    'casting block reference
                    Dim br As BlockReference = CType(ent, BlockReference)
                    '~ Loop Thru the Defined Borders
                    For x = LBound(OldBlocks) To UBound(OldBlocks)
                            '~ Check if the block is in the block table
                            Dim acBlkTbl As BlockTable
                        acBlkTbl = tx.GetObject(db.BlockTableId, OpenMode.ForWrite)
                        If acBlkTbl.Has(OldBlocks(x)) Then
                            'Loop thru all the blocks 
                            For Each blk In acBlkTbl
                                ''now check the block name here
                                If (br.Name = OldBlocks(x)) Then
                                    br.Erase(True)
                                    tx.Commit()
                                End If
                            Next
                        End If
                    Next x
                End If
            Next objId
        End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jul 2014 14:12:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5169657#M43173</guid>
      <dc:creator>TTIII</dc:creator>
      <dc:date>2014-07-23T14:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: Error When Deleteing Block - eNotOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5171455#M43174</link>
      <description>&lt;P&gt;The line tx.Commit() closes all objects open in the transaction.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jul 2014 02:56:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5171455#M43174</guid>
      <dc:creator>owenwengerd</dc:creator>
      <dc:date>2014-07-24T02:56:35Z</dc:date>
    </item>
    <item>
      <title>Re: Error When Deleteing Block - eNotOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5174491#M43175</link>
      <description>&lt;P&gt;Thanks, that worked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I moved the commit before the end function and modified the IF to include an Else for tx.dispose().&lt;/P&gt;&lt;P&gt;One question though, if a block is nested why doesnt it get deleted?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tony&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jul 2014 20:29:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5174491#M43175</guid>
      <dc:creator>TTIII</dc:creator>
      <dc:date>2014-07-24T20:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: Error When Deleteing Block - eNotOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5175159#M43176</link>
      <description>&lt;P&gt;Nested block references would not be in model space, they would be in the containing block. If you want to erase all instances of a block, you should simply get the list of block references from the block table record, then open each instance and erase it regardless of who owns it. You're likely to get more responses if you post your followup as a new thread.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jul 2014 05:31:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5175159#M43176</guid>
      <dc:creator>owenwengerd</dc:creator>
      <dc:date>2014-07-25T05:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: Error When Deleteing Block - eNotOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5176169#M43177</link>
      <description>&lt;P&gt;While you got your code working by&amp;nbsp;moving Transaction.Commit()&amp;nbsp;to correct place, your code logic is rather odd.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you need outer for (...) loop to loop through a set of block references? in your code the outer for (...) loop does nothing but wasting computing power. Also, you should open BlockTable once before you loop through OldBlocks array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that your intention is to remove an array of old block definitions from drawing. Unless you are absolutely sure, before this code runs, there is no block references based on the old block definitions in drawing, your code runs ok would only based on good luck. You need to first to see if a block definition is really purge-able/delete-able before you call br.Erase(), or the code would raise exception.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is, when loop through the OldBlocks, for each block definition, you need to see if there is block reference based on this block definition&amp;nbsp;by calling GetBlockreferenceIds(). If the block definition is a dynamic block, you also need to find out if there is anonymous block is derived from this block definition, by calling GetAnonymousBlockIds().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want to try to delete a block definition and&amp;nbsp;when that if the block definition is not erase-able, you want to skip it, then you can use try...catch...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;try&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; br.Erase(true);&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;catch&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; //print on command line or messagebox, if necessary;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;//the code coninue on next block definition&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jul 2014 14:50:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/error-when-deleteing-block-enotopenforwrite/m-p/5176169#M43177</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-07-25T14:50:31Z</dc:date>
    </item>
  </channel>
</rss>

