<?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 Multiple Undos required under a single transaction in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/multiple-undos-required-under-a-single-transaction/m-p/9655376#M18906</link>
    <description>&lt;P&gt;I've asked this before but I confused the topic when I posted code that was unrelated.&lt;/P&gt;&lt;P&gt;Inserting multiple blocks and changing their dynamic block properties under the one transactions requires multiple undo's to reverse a single command.&lt;/P&gt;&lt;P&gt;I think if I change to StartOpenCloseTransaction from StartTransaction it might fix it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem with doing that is that that I get an eWasOpenForRead on the line:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt; destdb.WblockCloneObjects(ids, destdb.BlockTableId, iMap, DuplicateRecordCloning.Replace, False)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which doesn't occur when using StartTransaction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    Public Sub ImportBlock(ByVal blockFileName As String, ByVal Blockname As String, ByRef trans As Transaction)
        Dim destdb As Database = Application.DocumentManager.MdiActiveDocument.Database
        Try
            Using sourcedb As New Database(False, True)
                sourcedb.ReadDwgFile(blockFileName, FileShare.ReadWrite, True, "")
                Dim ids As New ObjectIdCollection()
                Using sourceTrans As Transaction = sourcedb.TransactionManager.StartOpenCloseTransaction()
                    Dim sourceBT As BlockTable
                    sourceBT = DirectCast(sourceTrans.GetObject(sourcedb.BlockTableId, OpenMode.ForRead), BlockTable)
                    If sourceBT.Has(Blockname) Then
                        ids.Add(sourceBT(Blockname))
                    Else
                        MsgBox("ImportBlock - Blockname not found in file" &amp;amp; vbCrLf &amp;amp; "Block must be incorrectly named.")
                        Cancel = True
                        Return
                    End If
                End Using
                'if found, add the block
                If ids.Count &amp;lt;&amp;gt; 0 Then
                    'get the current drawing database
                    Dim iMap As New IdMapping()
                    destdb.WblockCloneObjects(ids, destdb.BlockTableId, iMap, DuplicateRecordCloning.Replace, False)
                End If
            End Using
        Catch ex As Exception
            Cancel = True
            If ex.Message = "eFileSharingViolation" Then
                MsgBox("Close Blocks Drawing. ", MsgBoxStyle.Exclamation)
            ElseIf ex.Message = "eFileNotFound" Then
                MsgBox("Block File Not Found. ", MsgBoxStyle.Exclamation)
            Else
                MsgBox(Reflection.MethodBase.GetCurrentMethod.Name() + " Exception: " + ex.Message)
            End If
        End Try
    End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope someone can help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 25 Jul 2020 22:56:58 GMT</pubDate>
    <dc:creator>SRSDS</dc:creator>
    <dc:date>2020-07-25T22:56:58Z</dc:date>
    <item>
      <title>Multiple Undos required under a single transaction</title>
      <link>https://forums.autodesk.com/t5/net-forum/multiple-undos-required-under-a-single-transaction/m-p/9655376#M18906</link>
      <description>&lt;P&gt;I've asked this before but I confused the topic when I posted code that was unrelated.&lt;/P&gt;&lt;P&gt;Inserting multiple blocks and changing their dynamic block properties under the one transactions requires multiple undo's to reverse a single command.&lt;/P&gt;&lt;P&gt;I think if I change to StartOpenCloseTransaction from StartTransaction it might fix it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem with doing that is that that I get an eWasOpenForRead on the line:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt; destdb.WblockCloneObjects(ids, destdb.BlockTableId, iMap, DuplicateRecordCloning.Replace, False)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which doesn't occur when using StartTransaction.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    Public Sub ImportBlock(ByVal blockFileName As String, ByVal Blockname As String, ByRef trans As Transaction)
        Dim destdb As Database = Application.DocumentManager.MdiActiveDocument.Database
        Try
            Using sourcedb As New Database(False, True)
                sourcedb.ReadDwgFile(blockFileName, FileShare.ReadWrite, True, "")
                Dim ids As New ObjectIdCollection()
                Using sourceTrans As Transaction = sourcedb.TransactionManager.StartOpenCloseTransaction()
                    Dim sourceBT As BlockTable
                    sourceBT = DirectCast(sourceTrans.GetObject(sourcedb.BlockTableId, OpenMode.ForRead), BlockTable)
                    If sourceBT.Has(Blockname) Then
                        ids.Add(sourceBT(Blockname))
                    Else
                        MsgBox("ImportBlock - Blockname not found in file" &amp;amp; vbCrLf &amp;amp; "Block must be incorrectly named.")
                        Cancel = True
                        Return
                    End If
                End Using
                'if found, add the block
                If ids.Count &amp;lt;&amp;gt; 0 Then
                    'get the current drawing database
                    Dim iMap As New IdMapping()
                    destdb.WblockCloneObjects(ids, destdb.BlockTableId, iMap, DuplicateRecordCloning.Replace, False)
                End If
            End Using
        Catch ex As Exception
            Cancel = True
            If ex.Message = "eFileSharingViolation" Then
                MsgBox("Close Blocks Drawing. ", MsgBoxStyle.Exclamation)
            ElseIf ex.Message = "eFileNotFound" Then
                MsgBox("Block File Not Found. ", MsgBoxStyle.Exclamation)
            Else
                MsgBox(Reflection.MethodBase.GetCurrentMethod.Name() + " Exception: " + ex.Message)
            End If
        End Try
    End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope someone can help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jul 2020 22:56:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/multiple-undos-required-under-a-single-transaction/m-p/9655376#M18906</guid>
      <dc:creator>SRSDS</dc:creator>
      <dc:date>2020-07-25T22:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple Undos required under a single transaction</title>
      <link>https://forums.autodesk.com/t5/net-forum/multiple-undos-required-under-a-single-transaction/m-p/9655592#M18907</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should try calling sourceDb.WBlockCloneObjects() instead of destDb.WBlockCloneObjects() and do it outside of the transaction.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example which returns the ObjectId of the imported BlockTableRecord (or ObjectId.Null).&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        public static ObjectId TryImportBlock(string fileName, string blockName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var destDb = doc.Database;
            var ed = doc.Editor;
            try
            {
                using (var sourceDb = new Database(false, true))
                {
                    sourceDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);
                    var ids = new ObjectIdCollection();

                    using (var tr = sourceDb.TransactionManager.StartOpenCloseTransaction())
                    {
                        var bt = (BlockTable)tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
                        if (!bt.Has(blockName))
                        {
                            ed.WriteMessage("Block not found");
                            return ObjectId.Null;
                        }
                        ids.Add(bt[blockName]);
                        tr.Commit();
                    }

                    var mapping = new IdMapping();
                    sourceDb.WblockCloneObjects(ids, destDb.BlockTableId, mapping, DuplicateRecordCloning.Replace, false);
                    return mapping[ids[0]].Value;
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
                return ObjectId.Null;
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Jul 2020 07:14:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/multiple-undos-required-under-a-single-transaction/m-p/9655592#M18907</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2020-07-26T07:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple Undos required under a single transaction</title>
      <link>https://forums.autodesk.com/t5/net-forum/multiple-undos-required-under-a-single-transaction/m-p/9658226#M18908</link>
      <description>&lt;P&gt;Found the fix.&lt;/P&gt;&lt;P&gt;Don't understand why there was a problem to begin with but...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been starting a 'master' transaction on each command that gets fed into each subroutine&lt;/P&gt;&lt;P&gt;Given that most of the routines require a transaction I figured that was sensible.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The 'master' transaction was used to insert then modified multiple blocks.&lt;/P&gt;&lt;P&gt;The combination of inserting then modify dynamic block properties under a master transaction caused an undo operation required for each. Inserting the blocks alone wasn't a problem but modifying them was.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Creating an independent transaction for inserting each block seems to have fixed it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Massive headache/confusion solved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jul 2020 19:56:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/multiple-undos-required-under-a-single-transaction/m-p/9658226#M18908</guid>
      <dc:creator>SRSDS</dc:creator>
      <dc:date>2020-07-27T19:56:50Z</dc:date>
    </item>
  </channel>
</rss>

