<?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: Editing multiple files in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751482#M27592</link>
    <description>&lt;P&gt;Here's a quick example (C# but easily convertible).&lt;/P&gt;
&lt;P&gt;I'd rather use&amp;nbsp; a return value and arguments because I do not know where 'tbl', 'strFind' and 'strReplace' are from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        public List&amp;lt;string[]&amp;gt; FindReplace(string fileName, string find, string replace)
        {
            using (Database db = new Database(false, true))
            {
                try
                {
                    db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
                }
                catch { return null; }
                var log = new List&amp;lt;string[]&amp;gt;();
                var textClass = RXObject.GetClass(typeof(DBText));
                var mtextClass = RXObject.GetClass(typeof(MText));
                var blockRefClass = RXObject.GetClass(typeof(BlockReference));
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var modelSpace = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                    foreach (ObjectId id in modelSpace)
                    {
                        if (id.ObjectClass.IsDerivedFrom(textClass))
                        {
                            var text = (DBText)tr.GetObject(id, OpenMode.ForWrite);
                            var str = text.TextString;
                            var pt = text.Position;
                            text.TextString = str.Replace(find, replace);
                            log.Add(new[] {
                                text.TextString,
                                str,
                                "DBText",
                                Path.GetFileName(fileName),
                                text.Handle.ToString(),
                                text.BlockName,
                                pt.X.ToString(),
                                pt.Y.ToString(),
                                pt.Z.ToString() });&lt;BR /&gt;                            continue;
                        }
                        if (id.ObjectClass.IsDerivedFrom(mtextClass))
                        {
                            var text = (MText)tr.GetObject(id, OpenMode.ForWrite);
                            var str = text.Contents;
                            var pt = text.Location;
                            text.Contents = str.Replace(find, replace);
                            log.Add(new[] {
                                text.Contents,
                                str,
                                "MText",
                                Path.GetFileName(fileName),
                                text.Handle.ToString(),
                                text.BlockName,
                                pt.X.ToString(),
                                pt.Y.ToString(),
                                pt.Z.ToString() });&lt;BR /&gt;                            continue;
                        }
                        if (id.ObjectClass.IsDerivedFrom(blockRefClass))
                        {
                            var blockRef = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                            foreach (ObjectId attId in blockRef.AttributeCollection)
                            {
                                var attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
                                var str = attRef.TextString;
                                var pt = attRef.Position;
                                attRef.TextString = str.Replace(find, replace);
                                log.Add(new[] {
                                attRef.TextString,
                                str,
                                "AttributeReference",
                                Path.GetFileName(fileName),
                                attRef.Handle.ToString(),
                                attRef.BlockName,
                                pt.X.ToString(),
                                pt.Y.ToString(),
                                pt.Z.ToString() });
                            }
                        }
                    }
                    tr.Commit();
                }
                db.SaveAs(fileName, DwgVersion.Current);
                return log;
            }
        }&lt;/PRE&gt;</description>
    <pubDate>Mon, 05 Feb 2018 12:43:22 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2018-02-05T12:43:22Z</dc:date>
    <item>
      <title>Editing multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7750925#M27590</link>
      <description>&lt;P&gt;I wrote a tool to perform a find and replace on the active document.&amp;nbsp; I’ve been attempting to modify it to allow me to pass the sub a file name so that I can perform the edit to multiple files.&amp;nbsp; As best I can tell the editor variable I set points to the active document.&amp;nbsp; I simply can’t figure out how to edit the passed document, and not the open one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    Private Sub test(fn As String)
        Dim db As Database = New Database(False, True),
            dwgDir As String = System.IO.Path.GetDirectoryName(fn),
            dwgName As String = System.IO.Path.GetFileName(fn),
            DocMgr As DocumentCollection = Application.DocumentManager,
            Doc As Document = DocumentCollectionExtension.Add(DocMgr, fn),
            ed As Editor = Doc.Editor
        Using db
            Try
                db.ReadDwgFile(fn, FileOpenMode.OpenForReadAndWriteNoShare, False, "")

            Catch __unusedException1__ As System.Exception

                Return
            End Try
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "TEXT")},
                    Filter As SelectionFilter = New SelectionFilter(filList),
                    Res As PromptSelectionResult = ed.SelectAll(Filter),
                    selSet As SelectionSet = Res.Value
                If selSet IsNot Nothing Then
                    Dim idArray As ObjectId() = selSet.GetObjectIds()
                    Try
                        For Each txtID As ObjectId In idArray
                            Dim txt As DBText = CType(tr.GetObject(txtID, OpenMode.ForWrite), DBText),
                                    wksp As String = txt.BlockName.ToString,
                                    hand As String = txt.Handle.ToString,
                                    val As String = txt.TextString,
                                    locX As String = txt.Position.X.ToString,
                                    locY As String = txt.Position.Y.ToString,
                                    locZ As String = txt.Position.Z.ToString
                            If hand &amp;lt;&amp;gt; Nothing And val &amp;lt;&amp;gt; Nothing And val.Contains(strFind) Then

                                txt.TextString = val.Replace(strFind, strReplace)
                                txt.RecordGraphicsModified(True)
                                tbl.Rows.Add(txt.TextString, val, "DBText", dwgName, hand, wksp, locX, locY, locZ)
                            End If
                        Next
                        tr.Commit()
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        ed.WriteMessage(("Exception: " &amp;amp; ex.Message))
                    Finally
                        tr.Dispose()
                    End Try
                End If
            End Using

            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Try
                    Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "INSERT")},
                            filter As SelectionFilter = New SelectionFilter(filList),
                                    Res As PromptSelectionResult = ed.SelectAll(filter),
                            selSet As SelectionSet = Res.Value
                    If selSet IsNot Nothing Then
                        Dim idArray As ObjectId() = selSet.GetObjectIds()
                        For Each blkId As ObjectId In idArray
                            Dim blkRef As BlockReference = CType(tr.GetObject(blkId, OpenMode.ForRead), BlockReference)
                            Dim BLK_n As String = blkRef.Name,
                                    BLK_hand As String = blkRef.Handle.ToString
                            Dim btr As BlockTableRecord = CType(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
                            btr.Dispose()
                            Dim attCol As AttributeCollection = blkRef.AttributeCollection
                            If attCol IsNot Nothing Then
                                For Each attId As ObjectId In attCol
                                    Dim att As AttributeReference = CType(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference),
                                            wksp As String = att.BlockName.ToString,
                                            hand As String = att.Handle.ToString,
                                            val As String = att.TextString,
                                            locX As String = att.Position.X.ToString,
                                            locY As String = att.Position.Y.ToString,
                                            locZ As String = att.Position.Z.ToString
                                    If hand &amp;lt;&amp;gt; Nothing And val &amp;lt;&amp;gt; Nothing And BLK_hand &amp;lt;&amp;gt; Nothing And BLK_n &amp;lt;&amp;gt; Nothing And val.Contains(strFind) = True Then
                                        att.TextString = val.Replace(strFind, strReplace)
                                        att.RecordGraphicsModified(True)
                                        tbl.Rows.Add(att.TextString, val, "Attribute", dwgName, hand, wksp, locX, locY, locZ)

                                    End If
                                Next
                            End If
                        Next
                        tr.Commit()
                    End If
                Catch ex As Autodesk.AutoCAD.Runtime.Exception
                    ed.WriteMessage(("Exception: " &amp;amp; ex.Message))
                Finally
                    tr.Dispose()
                End Try
            End Using

            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim filList As TypedValue() = New TypedValue(0) {New TypedValue(CInt(DxfCode.Start), "MTEXT")},
                    Filter As SelectionFilter = New SelectionFilter(filList),
                    Res As PromptSelectionResult = ed.SelectAll(Filter),
                    selSet As SelectionSet = Res.Value
                If selSet IsNot Nothing Then
                    Dim idArray As ObjectId() = selSet.GetObjectIds()
                    Try
                        For Each txtID As ObjectId In idArray
                            Dim mtxt As MText = CType(tr.GetObject(txtID, OpenMode.ForWrite), MText),
                                        wksp As String = mtxt.BlockName.ToString,
                                        hand As String = mtxt.Handle.ToString,
                                        val As String = mtxt.Contents.ToString,
                                        locX As String = mtxt.Location.X.ToString,
                                        locY As String = mtxt.Location.Y.ToString,
                                        locZ As String = mtxt.Location.Z.ToString
                            If hand &amp;lt;&amp;gt; Nothing And val &amp;lt;&amp;gt; Nothing And val.Contains(strFind) = True Then
                                mtxt.Contents = val.Replace(strFind, strReplace)
                                mtxt.RecordGraphicsModified(True)
                                tbl.Rows.Add(mtxt.Contents, val, "MText", dwgName, hand, wksp, locX, locY, locZ)
                            End If
                        Next
                        tr.Commit()
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        ed.WriteMessage(("Exception: " &amp;amp; ex.Message))
                    Finally
                        tr.Dispose()
                    End Try
                End If
            End Using
        End Using



    End Sub&lt;/PRE&gt;&lt;P&gt;I attempted to approach it differently&amp;nbsp;hoping to resolve the issue, but this code failed&amp;nbsp;too.&amp;nbsp; It goes through the files but won't&amp;nbsp;update any of them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    Private Sub test(fn As String)
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument,
            ed As Editor = doc.Editor,
            db As Database = New Database(False, True),
            dwgDir As String = System.IO.Path.GetDirectoryName(fn),
            dwgName As String = System.IO.Path.GetFileName(fn)

        Using db
            Try
                db.ReadDwgFile(fn, System.IO.FileShare.Read, False, "")
            Catch __unusedException1__ As System.Exception
                ed.WriteMessage(vbLf &amp;amp; "Unable to read drawing file.")
                Return
            End Try

            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Using tr
                Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
                Dim btr As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead), BlockTableRecord)
                For Each objId As ObjectId In btr
                    Dim objtyp As String = objId.GetType().ToString,
                            objent As Entity = CType(tr.GetObject(objId, OpenMode.ForWrite), Entity),
                            OBJDXF As String = objent.GetRXClass().DxfName
                    Select Case OBJDXF
                        Case "TEXT"
                            Dim txt As DBText = CType(tr.GetObject(objId, OpenMode.ForWrite), DBText),
                                    wksp As String = txt.BlockName.ToString,
                                    hand As String = txt.Handle.ToString,
                                    val As String = txt.TextString,
                                    locX As String = txt.Position.X.ToString,
                                    locY As String = txt.Position.Y.ToString,
                                    locZ As String = txt.Position.Z.ToString
                            If hand &amp;lt;&amp;gt; Nothing And val &amp;lt;&amp;gt; Nothing And val.Contains(strFind) Then
                                txt.TextString = val.Replace(strFind, strReplace)
                                txt.RecordGraphicsModified(True)
                                tbl.Rows.Add(txt.TextString, val, "DBText", dwgName, hand, wksp, locX, locY, locZ)
                            End If

                        Case "MTEXT"
                            Dim mtxt As MText = CType(tr.GetObject(objId, OpenMode.ForWrite), MText),
                                    wksp As String = mtxt.BlockName.ToString,
                                    hand As String = mtxt.Handle.ToString,
                                    val As String = mtxt.Contents.ToString,
                                    locX As String = mtxt.Location.X.ToString,
                                    locY As String = mtxt.Location.Y.ToString,
                                    locZ As String = mtxt.Location.Z.ToString
                            If hand &amp;lt;&amp;gt; Nothing And val &amp;lt;&amp;gt; Nothing And val.Contains(strFind) = True Then
                                mtxt.Contents = val.Replace(strFind, strReplace)
                                mtxt.RecordGraphicsModified(True)
                                tbl.Rows.Add(mtxt.Contents, val, "MText", dwgName, hand, wksp, locX, locY, locZ)
                            End If

                        Case "Attribute"
                            Dim att As AttributeReference = CType(tr.GetObject(objId, OpenMode.ForWrite), AttributeReference),
                                    wksp As String = att.BlockName.ToString,
                                    hand As String = att.Handle.ToString,
                                    val As String = att.TextString,
                                    locX As String = att.Position.X.ToString,
                                    locY As String = att.Position.Y.ToString,
                                    locZ As String = att.Position.Z.ToString
                            If hand &amp;lt;&amp;gt; Nothing And val &amp;lt;&amp;gt; Nothing And hand &amp;lt;&amp;gt; Nothing And val.Contains(strFind) = True Then
                                att.TextString = val.Replace(strFind, strReplace)
                                att.RecordGraphicsModified(True)
                                tbl.Rows.Add(att.TextString, val, "Attribute", dwgName, hand, wksp, locX, locY, locZ)
                            End If
                    End Select
                Next
                tr.Commit()
                tr.Dispose()
            End Using
        End Using
    End Sub&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Feb 2018 08:39:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7750925#M27590</guid>
      <dc:creator>WPerciful</dc:creator>
      <dc:date>2018-02-05T08:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: Editing multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751320#M27591</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your second method, after having commited the transaction, you have to save the changes in the database:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;db.SaveAs(fn, DwgVersion.Current)&lt;/PRE&gt;
&lt;P&gt;Another thing, you won't get any AttributeReference directly in the model space. you have to get the block references first, and iterate through their AttributeCollection.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 11:19:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751320#M27591</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-02-05T11:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: Editing multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751482#M27592</link>
      <description>&lt;P&gt;Here's a quick example (C# but easily convertible).&lt;/P&gt;
&lt;P&gt;I'd rather use&amp;nbsp; a return value and arguments because I do not know where 'tbl', 'strFind' and 'strReplace' are from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        public List&amp;lt;string[]&amp;gt; FindReplace(string fileName, string find, string replace)
        {
            using (Database db = new Database(false, true))
            {
                try
                {
                    db.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
                }
                catch { return null; }
                var log = new List&amp;lt;string[]&amp;gt;();
                var textClass = RXObject.GetClass(typeof(DBText));
                var mtextClass = RXObject.GetClass(typeof(MText));
                var blockRefClass = RXObject.GetClass(typeof(BlockReference));
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var modelSpace = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                    foreach (ObjectId id in modelSpace)
                    {
                        if (id.ObjectClass.IsDerivedFrom(textClass))
                        {
                            var text = (DBText)tr.GetObject(id, OpenMode.ForWrite);
                            var str = text.TextString;
                            var pt = text.Position;
                            text.TextString = str.Replace(find, replace);
                            log.Add(new[] {
                                text.TextString,
                                str,
                                "DBText",
                                Path.GetFileName(fileName),
                                text.Handle.ToString(),
                                text.BlockName,
                                pt.X.ToString(),
                                pt.Y.ToString(),
                                pt.Z.ToString() });&lt;BR /&gt;                            continue;
                        }
                        if (id.ObjectClass.IsDerivedFrom(mtextClass))
                        {
                            var text = (MText)tr.GetObject(id, OpenMode.ForWrite);
                            var str = text.Contents;
                            var pt = text.Location;
                            text.Contents = str.Replace(find, replace);
                            log.Add(new[] {
                                text.Contents,
                                str,
                                "MText",
                                Path.GetFileName(fileName),
                                text.Handle.ToString(),
                                text.BlockName,
                                pt.X.ToString(),
                                pt.Y.ToString(),
                                pt.Z.ToString() });&lt;BR /&gt;                            continue;
                        }
                        if (id.ObjectClass.IsDerivedFrom(blockRefClass))
                        {
                            var blockRef = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                            foreach (ObjectId attId in blockRef.AttributeCollection)
                            {
                                var attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
                                var str = attRef.TextString;
                                var pt = attRef.Position;
                                attRef.TextString = str.Replace(find, replace);
                                log.Add(new[] {
                                attRef.TextString,
                                str,
                                "AttributeReference",
                                Path.GetFileName(fileName),
                                attRef.Handle.ToString(),
                                attRef.BlockName,
                                pt.X.ToString(),
                                pt.Y.ToString(),
                                pt.Z.ToString() });
                            }
                        }
                    }
                    tr.Commit();
                }
                db.SaveAs(fileName, DwgVersion.Current);
                return log;
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Feb 2018 12:43:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751482#M27592</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-02-05T12:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Editing multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751533#M27593</link>
      <description>&lt;P&gt;I've added that line of code, but it results in a 'eFileError'.&amp;nbsp; I'm on my office computer, but my app is written on my personal laptop which doesn't have internet access at the moment.&amp;nbsp; So I retyped the rest of my code so that you can see it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;tbl is set on form load, and the code works.&amp;nbsp; It's just so I can have a log of the changes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Dim fdb As New System.Windows.Forms.FolderBrowserDialog,
        strFind As string = txtFind.Text,
        strReplace As string = txtReplace.Text

If (fdb.ShowDialog = Windows.Forms.DialogResult.Ok) Then
        Dim FileEntries As String() = IO.Directory.GetFiles(fdb.SelectedPath, "*.dwg")
        For Each dwg As String In FileEntries
                test(dwg)
        Next
End If
dgv.DataSource = tbl    &lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Feb 2018 13:05:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751533#M27593</guid>
      <dc:creator>WPerciful</dc:creator>
      <dc:date>2018-02-05T13:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Editing multiple files</title>
      <link>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751671#M27594</link>
      <description>&lt;P&gt;Merci beaucoup!!!!!&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 13:51:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editing-multiple-files/m-p/7751671#M27594</guid>
      <dc:creator>WPerciful</dc:creator>
      <dc:date>2018-02-05T13:51:42Z</dc:date>
    </item>
  </channel>
</rss>

