<?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: eWasOpenForWrite in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316150#M24637</link>
    <description>&lt;P&gt;Hi Gile,&lt;/P&gt;&lt;P&gt;That second section of code was moved in from another subroutine to try to show the problem a little more simply.&lt;/P&gt;&lt;P&gt;Without changing the code what would I need to add to 'close' the layer table.&lt;/P&gt;&lt;P&gt;I've tried:&lt;/P&gt;&lt;P&gt;lyrTbl.DownradeOpen&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;lyrTbl.Dispose&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;but&amp;nbsp;they&amp;nbsp;don't&amp;nbsp;work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I use StartTransaction instead of StartOpenCloseTransaction&amp;nbsp;the problem goes away but it's in an event handler so I believe I need to use the latter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Oct 2018 17:21:50 GMT</pubDate>
    <dc:creator>SRSDS</dc:creator>
    <dc:date>2018-10-05T17:21:50Z</dc:date>
    <item>
      <title>eWasOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8315976#M24635</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone please try to spot what's wrong&amp;nbsp;to get an eWasOpenForWrite on the line&lt;/P&gt;&lt;P&gt;&amp;nbsp;db.Clayer = acLyrTbl("DIM")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; Private Sub doc_CommandWillStart(ByVal sender As Object, ByVal e As CommandEventArgs)
        If PauseEvents = False Then
            If e.GlobalCommandName = "DIMLINEAR" Or e.GlobalCommandName = "DIMALIGNED" Or e.GlobalCommandName = "DIMANGULAR" Or e.GlobalCommandName = "DIMCONTINUE" Then
                Using trans As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartOpenCloseTransaction : trcnt = trcnt + 1
                    Dim LayerName As String = "DIM"
                    Dim lyrId As ObjectId = Nothing
                    Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
                    Try
                        Dim lyrTbl As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead, False)
                        Using ltypTbl As LinetypeTable = trans.GetObject(db.LinetypeTableId, OpenMode.ForRead, False)
                                Using myLayer As LayerTableRecord = New LayerTableRecord
                                If lyrTbl.Has(LayerName) = False Then
                                    myLayer.Name = LayerName
                                    lyrTbl.UpgradeOpen()
                                    lyrTbl.Add(myLayer)
                                    trans.AddNewlyCreatedDBObject(myLayer, True)
                                End If
                                lyrId = lyrTbl.Item(LayerName)
                                End Using
                            End Using
                        Dim acLyrTbl As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
                        If acLyrTbl.Has("DIM") = True Then
                            db.Clayer = acLyrTbl("DIM")
                        End If
                        trans.Commit() 
                    Catch ex As Exception
                    MsgBox(Reflection.MethodBase.GetCurrentMethod.Name() + " Exception: " + ex.Message)
                    End Try
                End Using
            End If
        End If
    End Sub&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Oct 2018 16:18:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8315976#M24635</guid>
      <dc:creator>SRSDS</dc:creator>
      <dc:date>2018-10-05T16:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: eWasOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316094#M24636</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/786488"&gt;@SRSDS&lt;/a&gt;&amp;nbsp; a écrit&amp;nbsp;:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone please try to spot what's wrong&amp;nbsp;to get an eWasOpenForWrite on the line&lt;/P&gt;
&lt;P&gt;&amp;nbsp;db.Clayer = acLyrTbl("DIM")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt; Private Sub doc_CommandWillStart(ByVal sender As Object, ByVal e As CommandEventArgs)
        If PauseEvents = False Then
            If e.GlobalCommandName = "DIMLINEAR" Or e.GlobalCommandName = "DIMALIGNED" Or e.GlobalCommandName = "DIMANGULAR" Or e.GlobalCommandName = "DIMCONTINUE" Then
                Using trans As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartOpenCloseTransaction : trcnt = trcnt + 1
                    Dim LayerName As String = "DIM"
                    Dim lyrId As ObjectId = Nothing
                    Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
                    Try&lt;BR /&gt;                        &lt;FONT color="#339966"&gt;' here you open the layer table for read&lt;/FONT&gt;
                        Dim lyrTbl As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead, False)
                        Using ltypTbl As LinetypeTable = trans.GetObject(db.LinetypeTableId, OpenMode.ForRead, False)
                                Using myLayer As LayerTableRecord = New LayerTableRecord
                                If lyrTbl.Has(LayerName) = False Then
                                    myLayer.Name = LayerName&lt;BR /&gt;                                    &lt;FONT color="#339966"&gt;' here you upgrade open the layer table (for write)&lt;/FONT&gt;
                                    lyrTbl.UpgradeOpen()
                                    lyrTbl.Add(myLayer)
                                    trans.AddNewlyCreatedDBObject(myLayer, True)
                                End If
                                lyrId = lyrTbl.Item(LayerName)
                                End Using
                            End Using&lt;BR /&gt;                        &lt;FONT color="#FF0000"&gt;' here you open layer table again which is already opened for write&lt;/FONT&gt; 
                        Dim acLyrTbl As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
                        If acLyrTbl.Has("DIM") = True Then
                            db.Clayer = acLyrTbl("DIM")
                        End If
                        trans.Commit() 
                    Catch ex As Exception
                    MsgBox(Reflection.MethodBase.GetCurrentMethod.Name() + " Exception: " + ex.Message)
                    End Try
                End Using
            End If
        End If
    End Sub&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Oct 2018 17:00:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316094#M24636</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-10-05T17:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: eWasOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316150#M24637</link>
      <description>&lt;P&gt;Hi Gile,&lt;/P&gt;&lt;P&gt;That second section of code was moved in from another subroutine to try to show the problem a little more simply.&lt;/P&gt;&lt;P&gt;Without changing the code what would I need to add to 'close' the layer table.&lt;/P&gt;&lt;P&gt;I've tried:&lt;/P&gt;&lt;P&gt;lyrTbl.DownradeOpen&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;lyrTbl.Dispose&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;but&amp;nbsp;they&amp;nbsp;don't&amp;nbsp;work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I use StartTransaction instead of StartOpenCloseTransaction&amp;nbsp;the problem goes away but it's in an event handler so I believe I need to use the latter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Oct 2018 17:21:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316150#M24637</guid>
      <dc:creator>SRSDS</dc:creator>
      <dc:date>2018-10-05T17:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: eWasOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316209#M24638</link>
      <description>&lt;P&gt;&lt;SPAN class=""&gt;I'm not very comfortable with VB (actually, I hate VB)&lt;/SPAN&gt; but try this 'corrected' code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;    Private Sub doc_CommandWillStart(ByVal sender As Object, ByVal e As CommandEventArgs)
        If Not PauseEvents Then 'Not booleanValue is clearer and less verbose than than booleanValue = False
            If e.GlobalCommandName = "DIMLINEAR" Or e.GlobalCommandName = "DIMALIGNED" Or e.GlobalCommandName = "DIMANGULAR" Or e.GlobalCommandName = "DIMCONTINUE" Then
                Using trans As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartOpenCloseTransaction : trcnt = trcnt + 1
                    Dim LayerName As String = "DIM"
                    Dim lyrId As ObjectId = ObjectId.Null 'ObjectId type is a non nullable type
                    Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
                    Try
                        Dim lyrTbl As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead) 'False arg is useless a table cannot be erased
                        If Not lyrTbl.Has(LayerName) Then
                            Using myLayer As LayerTableRecord = New LayerTableRecord
                                myLayer.Name = LayerName
                                lyrTbl.UpgradeOpen()
                                lyrTbl.Add(myLayer)
                                trans.AddNewlyCreatedDBObject(myLayer, True)
                                lyrId = lyrTbl.Item(LayerName)
                            End Using
                        End If
						'use the lyrTbl variable which already contains the current layer table
                        If lyrTbl.Has("DIM") Then 'lyrTbl.Has("DIM") = True is verbose, simply use: lyrTbl.Has("DIM") instead
                            db.Clayer = lyrTbl("DIM")
                        End If
                        trans.Commit() 
                    Catch ex As Exception
                        MsgBox(Reflection.MethodBase.GetCurrentMethod.Name() + " Exception: " + ex.Message)
                    End Try
                End Using
            End If
        End If
    End Sub&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Oct 2018 18:00:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316209#M24638</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-10-05T18:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: eWasOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316314#M24639</link>
      <description>&lt;P&gt;I shouldn't have tried to compact the code to explain the issue.&lt;/P&gt;&lt;P&gt;The problem I'm facing is that I have a one subroutine to add a new layer&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;and another that sets the current layer.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After I add a new layer the layer table is left open; for write I guess and I can't close it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And when I try to set the current layer using the SetCurrentLayer subroutine it creates the error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    Public Sub SetCurrentLayer(ByVal sLayerName As String, ByRef trans As Transaction)
        Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Try
            Dim acLyrTbl As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead)
            If acLyrTbl.Has(sLayerName) = True Then
                db.Clayer = acLyrTbl(sLayerName)
            End If
        Catch ex As Exception
            MsgBox(Reflection.MethodBase.GetCurrentMethod.Name() + " Exception: " + ex.Message)
        End Try
    End Sub&lt;/PRE&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Oct 2018 18:41:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316314#M24639</guid>
      <dc:creator>SRSDS</dc:creator>
      <dc:date>2018-10-05T18:41:38Z</dc:date>
    </item>
    <item>
      <title>Re: eWasOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316383#M24640</link>
      <description>&lt;P&gt;If you call two different method, it would be safer to use a different 'transaction' for each method.&lt;/P&gt;
&lt;PRE&gt;        public ObjectId GetOrCreateLayer(Database db, string layerName)
        {
            ObjectId id;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (layerTable.Has(layerName))
                {
                    id = layerTable[layerName];
                }
                else
                {
                    layerTable.UpgradeOpen();
                    var layer = new LayerTableRecord();
                    layer.Name = layerName;
                    id = layerTable.Add(layer);
                    tr.AddNewlyCreatedDBObject(layer, true);
                }
                tr.Commit();
            }
            return id;
        }

        public bool TrySetCurrentLayer(Database db, string layerName)
        {
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (layerTable.Has(layerName))
                {
                    db.Clayer = layerTable[layerName];
                    return true;
                }
            }
            return false;
        }&lt;/PRE&gt;
&lt;P&gt;But, if I do not misunderstand, using the upper GetOrCreatelayer() method, you do not need the second method, you can simply do:&lt;/P&gt;
&lt;PRE&gt;db.Clayer = GetOrCreatelayer(db, "DIM");&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Oct 2018 19:27:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8316383#M24640</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-10-05T19:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: eWasOpenForWrite</title>
      <link>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8318195#M24641</link>
      <description>&lt;P&gt;That&amp;nbsp;clears things up and also given me several tips.&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Oct 2018 15:12:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ewasopenforwrite/m-p/8318195#M24641</guid>
      <dc:creator>SRSDS</dc:creator>
      <dc:date>2018-10-07T15:12:01Z</dc:date>
    </item>
  </channel>
</rss>

