<?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: How to perform Undo Operation in a Single Ctrl + Z Action in AutoCAD in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13419783#M405</link>
    <description>&lt;P&gt;You don't have to do everything within a single OpenCloseTransaction(). You can use multiple OpenCloseTransactions() as needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An OpenCloseTransaction does not create undo groups, so if the entire operation is still not undoable as a single group when using one or more OpenCloseTransactions, there is another problem. Since you show no code whatsoever, no one can tell you what that problem may be.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, what I can tell you in general, is that if you are relying on API's like SendStringToExecute(), it's impossible to have things done by commands that are executed that way to be part of a single, logical undo group.&lt;/P&gt;</description>
    <pubDate>Fri, 11 Apr 2025 18:40:26 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2025-04-11T18:40:26Z</dc:date>
    <item>
      <title>How to perform Undo Operation in a Single Ctrl + Z Action in AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13413069#M402</link>
      <description>&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;When a user draws an entity, my plugin listens to that event and performs additional operations, such as creating text entities and storing related data using multiple transactions.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;The problem is that during undo, the user has to press Ctrl + Z multiple times to fully undo everything related to that action. &lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Based on my requirements, I cannot perform all the additional operations in a single transaction.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Is there a way to perform the undo operation only for the user's action and exclude my plugin's transactions from the undo stack, so that the user can perform undo with a single Ctrl + Z action?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Any suggestions or best practices would be appreciated.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 07 Apr 2025 15:50:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13413069#M402</guid>
      <dc:creator>csharp_dev</dc:creator>
      <dc:date>2025-04-07T15:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform Undo Operation in a Single Ctrl + Z Action in AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13413094#M403</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Use StartOpenCloseTransaction() instead of StartTransaction().&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Apr 2025 16:02:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13413094#M403</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2025-04-07T16:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform Undo Operation in a Single Ctrl + Z Action in AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13419735#M404</link>
      <description>&lt;P&gt;Thanks for the reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I mentioned earlier, after the user creates an entity, my plugin performs some additional operations like creating other entities and storing data in named dictionaries. I’m unable to do all these operations within a single StartOpenCloseTransaction(), especially for large drawings where it causes noticeable delays.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any suggestions or alternative approaches to handle this more efficiently ?&lt;/P&gt;</description>
      <pubDate>Thu, 10 Apr 2025 18:40:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13419735#M404</guid>
      <dc:creator>csharp_dev</dc:creator>
      <dc:date>2025-04-10T18:40:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform Undo Operation in a Single Ctrl + Z Action in AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13419783#M405</link>
      <description>&lt;P&gt;You don't have to do everything within a single OpenCloseTransaction(). You can use multiple OpenCloseTransactions() as needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An OpenCloseTransaction does not create undo groups, so if the entire operation is still not undoable as a single group when using one or more OpenCloseTransactions, there is another problem. Since you show no code whatsoever, no one can tell you what that problem may be.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, what I can tell you in general, is that if you are relying on API's like SendStringToExecute(), it's impossible to have things done by commands that are executed that way to be part of a single, logical undo group.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Apr 2025 18:40:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13419783#M405</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2025-04-11T18:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform Undo Operation in a Single Ctrl + Z Action in AutoCAD</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13421720#M406</link>
      <description>&lt;P&gt;What I say above is not entirely true.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is a way to encapsulate your operations/changes into a single, logical undo group that includes the user-initiated operations that triggered your operations, but it is not trivial.&amp;nbsp; Below is a class that you can use to define logical undo groups without having to use commands (e.g., UNDO/Begin/End).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You must create an instance of the class &lt;STRONG&gt;&lt;EM&gt;before&lt;/EM&gt;&lt;/STRONG&gt; the user operation that initiates your operation starts, which is typically done in a CommandWillStart event handler.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After the user-initiated operation and your subsequent operations end, you dispose the instance. That should encapsulate the user-initiated operations and your operations that were triggered by the user operation into a single undo group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// AcMgdLib - https://github.com/ActivistInvestor/AcMgdLib
///
/// UndoGroup.cs
/// 
/// Activist Investor / Tony T
/// 
/// Distributed under the terms of the MIT license

using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Runtime.NativeInterop;
using AcRx = Autodesk.AutoCAD.Runtime;

namespace AcMgdLib.DatabaseServices
{

   /// &amp;lt;summary&amp;gt;
   /// Encapsulation of a logical undo group. 
   /// 
   /// This class can be used to group multiple
   /// operations into a single undo group without
   /// the need to execute AutoCAD commands. The
   /// instance starts an undo group when it is
   /// created, and ends the undo group when it
   /// is disposed.
   /// &amp;lt;/summary&amp;gt;

   public class UndoGroup : IDisposable
   {
      /// &amp;lt;summary&amp;gt;
      /// VERSION dependence:
      /// 
      /// This code uses P/Invoke to call native methods in
      /// acdbXX.dll, which has a version/release-dependent
      /// filename (e.g., acdb19.dll... acdb25.dll, etc).
      /// 
      /// Hence, you must replace the following string
      /// with the name of the file for the product or
      /// release you're using.
      /// 
      /// If you want to eliminate the version-dependent
      /// filename problem, you can use dynamic loading
      /// using classes from this library. 
      /// 
      /// See the following files in the Common folder:
      /// 
      ///   DllImport.cs
      ///   AcDbNativeMethods.cs
      ///   
      /// Eventually, this code will refactored to avoid
      /// the version-dependent filename issue, using the
      /// above classes.
      /// &amp;lt;/summary&amp;gt;

      const string ACDB_DLL = "acdb25.dll";  // AutoCAD 2025/2026

      Database db;
      bool disposed = false;

      public UndoGroup(Database db)
      {
         if(db is null)
            throw new ArgumentNullException(nameof(db));
         this.db = db;
         Begin(db);
      }

      public void Dispose()
      {
         if(!disposed)
         {
            disposed = true;
            End(db);
         }
      }

      [DllImport(ACDB_DLL, CallingConvention = CallingConvention.ThisCall,
         EntryPoint = "?undoController@AcDbDatabase@@QEBAPEAVAcDbUndoController@@XZ")]
      static extern IntPtr GetUndoController(IntPtr db);

      [DllImport(ACDB_DLL, CallingConvention = CallingConvention.ThisCall,
         EntryPoint = "?beginGroup@AcDbImpUndoController@@UEAA?AW4ErrorStatus@Acad@@XZ")]
      static extern ErrorStatus beginGroup(IntPtr controller);

      [DllImport(ACDB_DLL, CallingConvention = CallingConvention.ThisCall,
         EntryPoint = "?endGroup@AcDbImpUndoController@@UEAA?AW4ErrorStatus@Acad@@XZ")]
      static extern ErrorStatus endGroup(IntPtr controller);

      static ErrorStatus Begin(Database db)
      {
         var controller = GetUndoController(db.UnmanagedObject);
         if(controller == IntPtr.Zero)
            throw new AcRx.Exception(AcRx.ErrorStatus.NullPtr);
         return beginGroup(controller);
      }

      static ErrorStatus End(Database db)
      {
         var controller = GetUndoController(db.UnmanagedObject);
         if(controller == IntPtr.Zero)
            throw new AcRx.Exception(AcRx.ErrorStatus.NullPtr);
         return endGroup(controller);
      }

   }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Apr 2025 20:30:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-perform-undo-operation-in-a-single-ctrl-z-action-in/m-p/13421720#M406</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2025-04-11T20:30:20Z</dc:date>
    </item>
  </channel>
</rss>

