<?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: VB.Net getting eLockViolation error with OpenMode.ForWrite in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/vb-net-getting-elockviolation-error-with-openmode-forwrite/m-p/6861239#M32837</link>
    <description>&lt;P&gt;What&amp;nbsp; &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;said will fix the problem you are facing ( &lt;SPAN&gt;eLockViola&lt;/SPAN&gt; &lt;SPAN&gt;tion)&lt;/SPAN&gt; but&amp;nbsp;I'd make two more changes in your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Make sure MdiActiveDocument is set:&lt;/P&gt;&lt;PRE&gt;Dim doc As Document = dm.Open(CurrentDrawingFile, False)
					&lt;BR /&gt;Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = doc
					&lt;/PRE&gt;&lt;P&gt;2) Add an using statement to your transaction to make sure it's properly disposed.&lt;/P&gt;&lt;P&gt;Not ending a transaction leads to weird errors in AutoCad Interface (see &lt;A href="http://adndevblog.typepad.com/autocad/2012/05/two-common-mistakes-with-transactions.html" target="_self"&gt;this&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;      Using tr As Transaction = db.TransactionManager.StartTransaction
        tr.Commit()
      End Using&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Feb 2017 22:28:02 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2017-02-07T22:28:02Z</dc:date>
    <item>
      <title>VB.Net getting eLockViolation error with OpenMode.ForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-getting-elockviolation-error-with-openmode-forwrite/m-p/6860210#M32835</link>
      <description>&lt;P&gt;I have a VB.NET routine that is supposed to batch process drawing files finding and replacing text. I am having the user create select drawing files the routine writes the file names and locations to a text file.&amp;nbsp;Once the drawing list written, it is opened and the drawings are processed one by one. The processing is handled by a subroutine that collect all text and mtext entities. Then the routine searches and replaces all matches, save and close. If I manually open and run the findandreplace subroutine it works fine. If it runs though the BatchRoutine I'm getting eLockViolation error on the OpenMode.ForWrite portion of the routine. I can resume on error and it does not crash out but does not make changes. In some of the other posts I was reading about AcAxDocLock but only found examples of C++.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help or insight would be much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Imports Autodesk.AutoCAD.ApplicationServices&lt;BR /&gt;Imports Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension&lt;BR /&gt;Imports Autodesk.AutoCAD.DatabaseServices&lt;BR /&gt;Imports Autodesk.AutoCAD.EditorInput&lt;BR /&gt;Imports Autodesk.AutoCAD.Windows&lt;BR /&gt;Imports System.IO&lt;BR /&gt;Public Class Main&lt;BR /&gt;&amp;lt;Autodesk.AutoCAD.Runtime.CommandMethod("Test")&amp;gt;&lt;BR /&gt;Public Sub BatchRoutine()&lt;BR /&gt;If File.Exists(FileList) Then&lt;BR /&gt;If File.ReadAllLines(FileList).Length = 0 Then&lt;BR /&gt;MsgBox("No drawing files found!" &amp;amp; vbCrLf &amp;amp; "Aborting!")&lt;BR /&gt;Exit Sub&lt;BR /&gt;Else&lt;BR /&gt;Dim sr = New StreamReader(FileList)&lt;BR /&gt;Dim CurrentDrawingFile As String&lt;BR /&gt;Do While sr.Peek() &amp;gt;= 0&lt;BR /&gt;CurrentDrawingFile = sr.ReadLine()&lt;BR /&gt;If File.Exists(CurrentDrawingFile) Then&lt;BR /&gt;Dim dm As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager&lt;BR /&gt;Dim doc As Document = dm.Open(CurrentDrawingFile, False)&lt;BR /&gt;Dim ActiveDrawing As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;If ActiveDrawing.IsReadOnly = False And ActiveDrawing.IsActive = True Then&lt;BR /&gt;Call FindandReplace()&lt;BR /&gt;Else&lt;BR /&gt;MsgBox(ActiveDrawing.Name &amp;amp; " is Read Only")&lt;BR /&gt;End If&lt;BR /&gt;ActiveDrawing.SendStringToExecute("QSAVE" &amp;amp; vbCr &amp;amp; "QSAVE" &amp;amp; vbCr, True, False, False)&lt;BR /&gt;DocumentExtension.CloseAndDiscard(ActiveDrawing)&lt;BR /&gt;End If&lt;BR /&gt;Loop&lt;BR /&gt;sr.Close()&lt;BR /&gt;End If&lt;BR /&gt;Else&lt;BR /&gt;MsgBox("File list was not found!" &amp;amp; vbCrLf &amp;amp; "Aborting!")&lt;BR /&gt;Exit Sub&lt;BR /&gt;End If&lt;BR /&gt;End Sub&lt;BR /&gt;Public Sub FindandReplace()&lt;BR /&gt;Dim CurrentFind As String = "TEXT TO FIND"&lt;BR /&gt;Dim CurrentReplace As String = "TEXT TO REPLACE"&lt;BR /&gt;Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor&lt;BR /&gt;Dim acTypValAr(3) As TypedValue&lt;BR /&gt;acTypValAr.SetValue(New TypedValue(DxfCode.Operator, "&amp;lt;or"), 0)&lt;BR /&gt;acTypValAr.SetValue(New TypedValue(DxfCode.Start, "TEXT"), 1)&lt;BR /&gt;acTypValAr.SetValue(New TypedValue(DxfCode.Start, "MTEXT"), 2)&lt;BR /&gt;acTypValAr.SetValue(New TypedValue(DxfCode.Operator, "or&amp;gt;"), 3)&lt;BR /&gt;Dim acSelFtr As SelectionFilter = New SelectionFilter(acTypValAr)&lt;BR /&gt;Dim acSSPrompt As PromptSelectionResult&lt;BR /&gt;acSSPrompt = ed.SelectAll(acSelFtr)&lt;BR /&gt;If acSSPrompt.Status = PromptStatus.OK Then&lt;BR /&gt;Dim acSSet As SelectionSet = acSSPrompt.Value&lt;BR /&gt;Dim trans As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction&lt;BR /&gt;For Each selObj In acSSet&lt;BR /&gt;Select Case selObj.ObjectId.ObjectClass.Name&lt;BR /&gt;Case "AcDbText"&lt;BR /&gt;Dim WtextObj As DBText = trans.GetObject(selObj.ObjectId, OpenMode.ForWrite, False, True)&lt;BR /&gt;Dim RtextObj As DBText = trans.GetObject(selObj.ObjectId, OpenMode.ForRead, False, True)&lt;BR /&gt;Dim OldTextString As String = RtextObj.TextString&lt;BR /&gt;Dim NewTextString As String = Replace(OldTextString, CurrentFind, CurrentReplace, 1, -1, CompareMethod.Text)&lt;BR /&gt;RtextObj.Dispose()&lt;BR /&gt;If OldTextString &amp;lt;&amp;gt; NewTextString Then&lt;BR /&gt;WtextObj.TextString = NewTextString&lt;BR /&gt;End If&lt;BR /&gt;WtextObj.Dispose()&lt;BR /&gt;Case "AcDbMText"&lt;BR /&gt;Dim WmtextObj As MText = trans.GetObject(selObj.ObjectId, OpenMode.ForWrite, False, True)&lt;BR /&gt;Dim RmtextObj As MText = trans.GetObject(selObj.ObjectId, OpenMode.ForRead, False, True)&lt;BR /&gt;Dim OldTextString As String = RmtextObj.Contents&lt;BR /&gt;Dim NewTextString As String = Replace(OldTextString, CurrentFind, CurrentReplace, 1, -1, CompareMethod.Text)&lt;BR /&gt;RmtextObj.Dispose()&lt;BR /&gt;If OldTextString &amp;lt;&amp;gt; NewTextString Then&lt;BR /&gt;WmtextObj.Contents = NewTextString&lt;BR /&gt;End If&lt;BR /&gt;WmtextObj.Dispose()&lt;BR /&gt;End Select&lt;BR /&gt;Next selObj&lt;BR /&gt;trans.Commit()&lt;BR /&gt;End If&lt;BR /&gt;End Sub&lt;BR /&gt;End Class&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>Tue, 07 Feb 2017 16:01:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-getting-elockviolation-error-with-openmode-forwrite/m-p/6860210#M32835</guid>
      <dc:creator>Jedimaster</dc:creator>
      <dc:date>2017-02-07T16:01:31Z</dc:date>
    </item>
    <item>
      <title>Re : VB.Net getting eLockViolation error with OpenMode.ForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-getting-elockviolation-error-with-openmode-forwrite/m-p/6860250#M32836</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cannot attentively read your code (unformated VB is too much for me) but if you want to be able to open multiple documents in the editor you have to use the CommandFlags.Session argument in the CommandMethod attribute and lock each document you want to edit.&lt;/P&gt;
&lt;P&gt;See &lt;A href="http://help.autodesk.com/view/ACD/2017/FRA/?guid=GUID-330A8DCB-626F-4271-8B89-9773A7631D87" target="_blank"&gt;here&lt;/A&gt; and &lt;A href="http://help.autodesk.com/view/ACD/2017/FRA/?guid=GUID-A2CD7540-69C5-4085-BCE8-2A8ACE16BFDD" target="_blank"&gt;there&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;lt;Autodesk.AutoCAD.Runtime.CommandMethod("Test", CommandFlags.Session)&amp;gt;
Public Sub BatchRoutine()
    ...
    Dim doc As Document = dm.Open(CurrentDrawingFile, False)
    Using(doc.LockDocument())
        ' edit the document here
        ...
    End Using
    ...
End Sub&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2017 16:23:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-getting-elockviolation-error-with-openmode-forwrite/m-p/6860250#M32836</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-02-07T16:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: VB.Net getting eLockViolation error with OpenMode.ForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/vb-net-getting-elockviolation-error-with-openmode-forwrite/m-p/6861239#M32837</link>
      <description>&lt;P&gt;What&amp;nbsp; &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;said will fix the problem you are facing ( &lt;SPAN&gt;eLockViola&lt;/SPAN&gt; &lt;SPAN&gt;tion)&lt;/SPAN&gt; but&amp;nbsp;I'd make two more changes in your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Make sure MdiActiveDocument is set:&lt;/P&gt;&lt;PRE&gt;Dim doc As Document = dm.Open(CurrentDrawingFile, False)
					&lt;BR /&gt;Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = doc
					&lt;/PRE&gt;&lt;P&gt;2) Add an using statement to your transaction to make sure it's properly disposed.&lt;/P&gt;&lt;P&gt;Not ending a transaction leads to weird errors in AutoCad Interface (see &lt;A href="http://adndevblog.typepad.com/autocad/2012/05/two-common-mistakes-with-transactions.html" target="_self"&gt;this&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;      Using tr As Transaction = db.TransactionManager.StartTransaction
        tr.Commit()
      End Using&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2017 22:28:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/vb-net-getting-elockviolation-error-with-openmode-forwrite/m-p/6861239#M32837</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-02-07T22:28:02Z</dc:date>
    </item>
  </channel>
</rss>

