<?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: Command to erase blocks containing a tag. in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/command-to-erase-blocks-containing-a-tag/m-p/12680895#M4671</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;If I do not misunderstand what you're trying to achieve, this should work (sorry it's C#).&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;private static List&amp;lt;string&amp;gt; EraseBlockWithTag (string tag)
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;
    var blockNames = new List&amp;lt;string&amp;gt;();
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        foreach (ObjectId btrId in blockTable)
        {
            var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
            if (btr.IsLayout || 
                btr.IsDependent || 
                btr.IsFromExternalReference || 
                btr.IsAnonymous || 
                !btr.HasAttributeDefinitions)
                continue;

            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass.Name == "AcDbAttributeDefinition")
                {
                    var attrib = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
                    if (attrib.Tag.Equals(tag, StringComparison.OrdinalIgnoreCase))
                    {
                        // Try to erase all references to this block definition and purge the block definiton
                        try
                        {
                            tr.GetObject(btrId, OpenMode.ForWrite);
                            foreach (ObjectId brId in btr.GetBlockReferenceIds(true, true))
                            {
                                tr.GetObject(brId, OpenMode.ForWrite).Erase();
                            }
                            btr.Erase();
                            blockNames.Add(btr.Name);
                        }
                        catch(System.Exception ex)
                        {
                            ed.WriteMessage($"\nUnable to erase '{btr.Name}': {ex.Message}");
                        }
                        break;
                    }
                }
            }
        }
        tr.Commit();
    }
    return blockNames;
}&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 02 Apr 2024 10:07:47 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2024-04-02T10:07:47Z</dc:date>
    <item>
      <title>Command to erase blocks containing a tag.</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-to-erase-blocks-containing-a-tag/m-p/12680622#M4670</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm trying to write a command to delete blocks which contain a certain attribute tag. However it is deleting more than just the blocks. Its erasing most of the text as well. Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    Public Sub ErBlWithTag()
        Dim acDoc As Document = Core.Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        Dim ed As Editor = acDoc.Editor
        Try
            Dim pStrOpts As New PromptStringOptions(vbLf &amp;amp; "Enter the TAG name to erase blocks with this tag: ") With {
                .AllowSpaces = False
            }
            Dim pStrRes As PromptResult = ed.GetString(pStrOpts)
            If pStrRes.Status = PromptStatus.OK Then
                Dim strBlockWithTagNameToDelete As String = pStrRes.StringResult
                Dim acTrans As Transaction = acCurDb.TransactionManager.StartTransaction
                Using acTrans
                    Dim bt As BlockTable = CType(acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite), BlockTable)
                    Dim strBlockNames As String = ""
                    Dim ListofBTR As New List(Of BlockTableRecord)
                    For Each btrId As ObjectId In bt
                        Dim btr As BlockTableRecord = CType(acTrans.GetObject(btrId, OpenMode.ForWrite), BlockTableRecord)
                        If Not btr.IsFromExternalReference And Not btr.IsLayout And Not btr.IsAnonymous Then
                            Dim blockContainsTag As Boolean = False
                            For Each id As ObjectId In btr
                                Dim obj As DBObject = acTrans.GetObject(id, OpenMode.ForWrite)
                                If TypeOf obj Is AttributeDefinition Then
                                    Dim attDef As AttributeDefinition = TryCast(obj, AttributeDefinition)
                                    ' Retrieve attribute definition tag name
                                    Dim attTagName As String = attDef.Tag
                                    clsRockAUTOMessaging.RockAUTO_Prompt("Searching Block '" &amp;amp; btr.Name &amp;amp; "' Attribute Tag Name '" &amp;amp; attTagName &amp;amp; "'")
                                    'Console.WriteLine("Attribute Tag Name: " &amp;amp; attTagName)
                                    If attTagName.ToUpper.Equals(strBlockWithTagNameToDelete.ToUpper) Then
                                        'If clsString.RockCADSupport_TestStringWithSplitAndLike(",", attTagName, strBlockWithTagNameToDelete) Then
                                        blockContainsTag = True
                                        clsRockAUTOMessaging.RockAUTO_Prompt_NoCrLf("...Found...")
                                        Exit For
                                    End If
                                End If
                            Next
                            If blockContainsTag Then
                                ListofBTR.Add(btr)
                                If strBlockNames.Equals("") Then
                                    strBlockNames = btr.Name
                                Else
                                    strBlockNames &amp;amp;= "," &amp;amp; btr.Name
                                End If
                                'btr.UpgradeOpen()
                                'btr.Erase(True)
                            End If
                        End If
                    Next
                    For Each objBlockTableRecord As BlockTableRecord In ListofBTR
                        'objBlockTableRecord.UpgradeOpen()
                        objBlockTableRecord.Erase(True)
                    Next

                    acTrans.Commit()
                    Core.Application.DocumentManager.MdiActiveDocument.Editor.Regen()
                    ed.WriteMessage(vbLf &amp;amp; "Blocks with name '{0}' deleted successfully.", strBlockNames)
                End Using
            End If
        Catch ex As Exception
            ed.WriteMessage(ex.ToString())
        End Try
    End Sub&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 02 Apr 2024 08:05:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-to-erase-blocks-containing-a-tag/m-p/12680622#M4670</guid>
      <dc:creator>david_rock</dc:creator>
      <dc:date>2024-04-02T08:05:45Z</dc:date>
    </item>
    <item>
      <title>Re: Command to erase blocks containing a tag.</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-to-erase-blocks-containing-a-tag/m-p/12680895#M4671</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;If I do not misunderstand what you're trying to achieve, this should work (sorry it's C#).&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;private static List&amp;lt;string&amp;gt; EraseBlockWithTag (string tag)
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;
    var blockNames = new List&amp;lt;string&amp;gt;();
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        foreach (ObjectId btrId in blockTable)
        {
            var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
            if (btr.IsLayout || 
                btr.IsDependent || 
                btr.IsFromExternalReference || 
                btr.IsAnonymous || 
                !btr.HasAttributeDefinitions)
                continue;

            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass.Name == "AcDbAttributeDefinition")
                {
                    var attrib = (AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
                    if (attrib.Tag.Equals(tag, StringComparison.OrdinalIgnoreCase))
                    {
                        // Try to erase all references to this block definition and purge the block definiton
                        try
                        {
                            tr.GetObject(btrId, OpenMode.ForWrite);
                            foreach (ObjectId brId in btr.GetBlockReferenceIds(true, true))
                            {
                                tr.GetObject(brId, OpenMode.ForWrite).Erase();
                            }
                            btr.Erase();
                            blockNames.Add(btr.Name);
                        }
                        catch(System.Exception ex)
                        {
                            ed.WriteMessage($"\nUnable to erase '{btr.Name}': {ex.Message}");
                        }
                        break;
                    }
                }
            }
        }
        tr.Commit();
    }
    return blockNames;
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 02 Apr 2024 10:07:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-to-erase-blocks-containing-a-tag/m-p/12680895#M4671</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2024-04-02T10:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: Command to erase blocks containing a tag.</title>
      <link>https://forums.autodesk.com/t5/net-forum/command-to-erase-blocks-containing-a-tag/m-p/12681099#M4672</link>
      <description>&lt;P&gt;Perfect thank you so much!&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2024 11:50:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/command-to-erase-blocks-containing-a-tag/m-p/12681099#M4672</guid>
      <dc:creator>david_rock</dc:creator>
      <dc:date>2024-04-02T11:50:12Z</dc:date>
    </item>
  </channel>
</rss>

