<?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: Editor.Command from palette in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10169676#M17224</link>
    <description>&lt;P&gt;Is there any way to execute an undo synchronously while not in document context?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Mar 2021 10:36:40 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2021-03-19T10:36:40Z</dc:date>
    <item>
      <title>Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10151983#M17222</link>
      <description>&lt;P&gt;Hey all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is very similar to this question:&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/net/editor-command-from-palette/td-p/6376430" target="_blank"&gt;https://forums.autodesk.com/t5/net/editor-command-from-palette/td-p/6376430&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Q:&amp;nbsp;&lt;/STRONG&gt;Can I run a&amp;nbsp;Editor.Command from a Palette, without using the construction of creating a&amp;nbsp;&lt;SPAN&gt;CommandMethod&amp;nbsp;and using&amp;nbsp;SendStringToExecute()?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In our scenario, it's not possible to use the&amp;nbsp;&lt;SPAN&gt;CommandMethod/SendStringToExecute construction, because it's not synchronously executed.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The problem we're trying to solve is the following:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;STRONG&gt;Scenario&lt;/STRONG&gt;&lt;BR /&gt;Via a palette we insert a component, this insert component will do all kinds of things in acad (via the acad API).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;We surround this logic with an undo group, to create this undo group we&lt;/SPAN&gt;&lt;SPAN&gt; use&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;"Autodesk.AutoCAD.Internal.Utils.SetUndoMark( ... );".&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Then at the end of everything, if the insert failed, we want to rollback all our changes.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;To do this we just need to do "Undo 1", due to the undo group.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;To do the "Undo 1" we use "SendStringToExecute" since there is no ACAD API to do the undo synchronously (as far as we know).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Problem&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;When you scroll in acad during the insert.&lt;/P&gt;&lt;P&gt;When the insert fails&lt;/P&gt;&lt;P&gt;Then ACaD will first handle the scroll&lt;BR /&gt;And then ACaD will handle the undo 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result -&amp;gt; Only the scroll is undone&lt;BR /&gt;Expected -&amp;gt; The complete insert changes (which are grouped) are undone&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Why creating a new command won't work&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;We want to keep our application decoupled from ACAD, we don't want to write all our insert component logic in an ACAD command.&amp;nbsp;&lt;BR /&gt;We merely use ACAD as a visualizer, all our application logic doesn't know it's executed in ACAD.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is also another post about this scroll issue interacting with the SendString:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://forums.autodesk.com/t5/net/problem-with-execute-sendstringtoexecute/td-p/5186201" target="_blank"&gt;https://forums.autodesk.com/t5/net/problem-with-execute-sendstringtoexecute/td-p/5186201&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 15:53:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10151983#M17222</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-03-12T15:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10152625#M17223</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Editor.Command can only run in document context this is why it cannot be directly called from a palette (modeless UI) which is application context.&lt;/P&gt;
&lt;P&gt;You do not show the code you run in AutoCAD, but typically, undoing actions is managed with a Transaction by commiting or aborting it.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 20:01:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10152625#M17223</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2021-03-12T20:01:16Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10169676#M17224</link>
      <description>&lt;P&gt;Is there any way to execute an undo synchronously while not in document context?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Mar 2021 10:36:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10169676#M17224</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-03-19T10:36:40Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10169719#M17225</link>
      <description>&lt;P&gt;As said upper, aborting a transaction (i.e., not commiting it) rolls back all the changes made within this transaction.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Mar 2021 10:58:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10169719#M17225</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2021-03-19T10:58:34Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10215406#M17226</link>
      <description>&lt;P&gt;Thanks for the reply Gile!&lt;/P&gt;&lt;P&gt;I created a small code snippet which represents a simplification of our code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;...
var success = true;
using ( var undoGroup = new HandleUndoGroup( "InsertComponent" ) )
{
	success = InsertComponent(Component);
	
	// The call below will update the model in the Acad word.
    // To do this we draw geomerty/connections which depend on the component added.
	// This is done by performing multiple transactions.
	// Here we don't know which transactions will be performed to do this. 
	success &amp;amp;= SyncAcadModel(_system);
	...
}
   
if ( !success )
{
     UndoLastActions( 1, true );
}
...&lt;/LI-CODE&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;As you can see here it's, sadly,&amp;nbsp;not easy for us to perform the undo-action by aborting the transaction, since the insert component exists of a dynamic set of transactions (depending on the component which is added).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When, during the insert, the user performs a scroll in acad the undo 1 will undo the scroll i.o. the insert component group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to solve this?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Apr 2021 13:30:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10215406#M17226</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-06T13:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10215721#M17227</link>
      <description>&lt;P&gt;For completeness here you have the UndoLastActions code:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public void UndoLastActions( int rollbackCount, bool clearRedoStack )
{
	if ( rollbackCount &amp;gt; 0 )
	{
		var command = $"_.UNDO {rollbackCount}";
		Document.SendStringToExecute( $"{command} ", true, false, false );
	}
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 06 Apr 2021 14:59:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10215721#M17227</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-06T14:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10216410#M17228</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;did you try something like this:&lt;/P&gt;
&lt;LI-CODE lang="general"&gt;using (var topTr = eb.TransactionManager.StartTransaction())
using ( var undoGroup = new HandleUndoGroup( "InsertComponent" ) )
{
	success = InsertComponent(Component);
	
	// The call below will update the model in the Acad word.
    // To do this we draw geomerty/connections which depend on the component added.
	// This is done by performing multiple transactions.
	// Here we don't know which transactions will be performed to do this. 
	success &amp;amp;= SyncAcadModel(_system);
	...
        if (success)
            topTr.Commit();
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 06 Apr 2021 18:14:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10216410#M17228</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2021-04-06T18:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10278913#M17229</link>
      <description>&lt;P&gt;Thanks for the response, Gilles!&lt;BR /&gt;My response is quite late, some other activities were raised in priority.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;We were not aware it was possible to create a top-level transaction like this.&lt;BR /&gt;To add this to our application was still quite a challenge since we have a custom undo mechanism in place (to connect our application to the undo of Acad). But we succeeded to add this.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Apr 2021 15:50:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10278913#M17229</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-29T15:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Editor.Command from palette</title>
      <link>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10280891#M17230</link>
      <description>&lt;P&gt;I do have 1 more question related to this solution.&lt;BR /&gt;I also wrapped my transaction with a document lock, see code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;            using ( Document.LockDocument( DocumentLockMode.Write, actionName, actionName, false ) )
            {
                using ( var transaction = Document.Database.TransactionManager.StartTransaction() )
                {
                    var success = action();
                    if ( success )
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        // Make sure this action is not in the undo stack
                    }

                    return success;
                }
            }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;When I don't commit my transaction I still see the action is the undo stack.&lt;BR /&gt;&lt;STRONG&gt;Q:&lt;/STRONG&gt; Is it possible to remove this action from the undo stack when the action failed?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Apr 2021 08:18:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/editor-command-from-palette/m-p/10280891#M17230</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-30T08:18:36Z</dc:date>
    </item>
  </channel>
</rss>

