Disable Undo Recording

Disable Undo Recording

Anonymous
Not applicable
5,068 Views
14 Replies
Message 1 of 15

Disable Undo Recording

Anonymous
Not applicable

Hello,

 

I'm having trouble disabling the undo recording in my PlugIn. Because it executes many transactions, the undo history contains too many entries. The user is no longer able to use the undo functionality to undo his own actions.

 

I searched the forums, and found a possible solution:

 

Database db = HostApplicationServices.WorkingDatabase;
db.DisableUndoRecording(true);

But this doesn't seem to work. My code is

 

...
acadObj.acadDatabase.DisableUndoRecording(true);
using (DocumentLock acadDocumentLock = acadObj.acadDocument.LockDocument())
using (Transaction acadTransaction = acadObj.acadDocument.TransactionManager.StartTransaction())
{ 
    acadTransaction.Commit(); 
}

Even though I disabled the UndoRecording property, an entry still appears in the Undo history. No code is being executed from within the transaction, besides the Commit(). 

 

Does anyone have an idea?

 

I'd appreciate any help!

0 Likes
5,069 Views
14 Replies
Replies (14)
Message 2 of 15

SENL1362
Advisor
Advisor

Tried the NoUndoMarker commandFlag?

 

[CommandMethod("Test",CommandFlags.NoUndoMarker)]

 

0 Likes
Message 3 of 15

BKSpurgeon
Collaborator
Collaborator

If you don't want the ability to undo why not use StartOpenCloseTransaction instead?

 

using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{


/// etc.

tr.Commit()

}

 

 

0 Likes
Message 4 of 15

artc2
Autodesk
Autodesk
Regular transactions require undo, so they will turn it on even if you have disabled it. the OpenCloseTransaction is essentially a wrapper for open/close of multiple objects, so it shouldn't need to enable undo (I haven't looked at the source code to confirm that though).
0 Likes
Message 5 of 15

Anonymous
Not applicable

Thank's for the help, but I couldn't find a solution yet. I tried it with the StartOpenCloseTransaction() method, but the result was rather confusing. The following code will not add an entry to the undo history:

 

 acadObj.acadDatabase.DisableUndoRecording(true);
 using (DocumentLock acadDocumentLock = acadObj.acadDocument.LockDocument())
 using (Transaction tr = acadObj.acadDatabase.TransactionManager.StartOpenCloseTransaction())
    {
         tr.Commit();
    }

However, when I create a nested transaction the exact same way (copying and pasting the code), an entry will be added to the undo history. 

 

 acadObj.acadDatabase.DisableUndoRecording(true);
 using (DocumentLock acadDocumentLock = acadObj.acadDocument.LockDocument())
 using (Transaction tr = acadObj.acadDatabase.TransactionManager.StartOpenCloseTransaction())
 {
     acadObj.acadDatabase.DisableUndoRecording(true);
     using (DocumentLock acadDocumentLockS = acadObj.acadDocument.LockDocument())
     using (Transaction tra = acadObj.acadDatabase.TransactionManager.StartOpenCloseTransaction())
     {
         tra.Commit();
     }
     tr.Commit();
 }

Does anyone have a clue what might be going on here?

 

 

0 Likes
Message 6 of 15

BKSpurgeon
Collaborator
Collaborator

> The following code will not add an entry to the undo history:

 

That's correct. StartOpenClose should not add an entry. That's what you want, isn't it?

 

> However, when I create a nested transaction the exact same way (copying and pasting the code), an entry will be added to the undo history. 

 

I suspect that you might not be committing or aborting all of your transactions. Forgetting to commit or abort a single transaction - especially when you are dealing with nested transactions can do very funny things. Perhaps carefully review your code or alternatively paste it in here - someone with a fresh pair of eyes might be able to easily spot the issue.

 

rgds

 

Ben

0 Likes
Message 7 of 15

ActivistInvestor
Mentor
Mentor

How are you determining that entries are being added to the undo history?

 

 


@Anonymouswrote:

Thank's for the help, but I couldn't find a solution yet. I tried it with the StartOpenCloseTransaction() method, but the result was rather confusing. The following code will not add an entry to the undo history:

 

 acadObj.acadDatabase.DisableUndoRecording(true);
 using (DocumentLock acadDocumentLock = acadObj.acadDocument.LockDocument())
 using (Transaction tr = acadObj.acadDatabase.TransactionManager.StartOpenCloseTransaction())
    {
         tr.Commit();
    }

However, when I create a nested transaction the exact same way (copying and pasting the code), an entry will be added to the undo history. 

 

 acadObj.acadDatabase.DisableUndoRecording(true);
 using (DocumentLock acadDocumentLock = acadObj.acadDocument.LockDocument())
 using (Transaction tr = acadObj.acadDatabase.TransactionManager.StartOpenCloseTransaction())
 {
     acadObj.acadDatabase.DisableUndoRecording(true);
     using (DocumentLock acadDocumentLockS = acadObj.acadDocument.LockDocument())
     using (Transaction tra = acadObj.acadDatabase.TransactionManager.StartOpenCloseTransaction())
     {
         tra.Commit();
     }
     tr.Commit();
 }

Does anyone have a clue what might be going on here?

 

 


 

0 Likes
Message 8 of 15

ActivistInvestor
Mentor
Mentor

OpenCloseTransactions can't be nested. They're effectively just a list of opened DBObjects that are closed when the OpenCloseTransaction is committed, or disposed. That means they don't work the same way regular transactions do. E.g., if you commit a nested regular transaction, but then abort the enclosing transaction, it has the same effect as aborting the nested transaction. With multiple OpenCloseTransactions, each can be independently committed or aborted.

 


@BKSpurgeonwrote:

 

I suspect that you might not be committing or aborting all of your transactions. Forgetting to commit or abort a single transaction - especially when you are dealing with nested transactions can do very funny things. Perhaps carefully review your code or alternatively paste it in here - someone with a fresh pair of eyes might be able to easily spot the issue.

 

 


 

 

Message 9 of 15

Anonymous
Not applicable

Hey,

thank's for your ideas. I implemented them, but sadly they didn't fix the problem. While the tip about not nesting StartOpenCloseTransactions() was helpful, I found method call causing something to appear in the Undo history. A bit of background here: My code is called when a Document is created. 

 

However, I was able to determine that the problem appears somewhere else as well, without a transaction:

 

 

internal static void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
{
TypedValue[] filterlist = new TypedValue[2]; filterlist[0] = new TypedValue((int)DxfCode.ExtendedDataRegAppName, "ASDF");
  filterlist[1] = new TypedValue((int)DxfCode.ExtendedDataRegAppName, "ASDF-TWO");
SelectionFilter filter = new SelectionFilter(filterlist); ACADObj acadObj = new ACADObj(); //Provides access to the active document and the associated Database object
acadObj.acadDatabase.DisableUndoRecording(true);
PromptSelectionResult psr = acadObj.acadDocument.Editor.SelectAll(filter); //This line adds something to the Undo history }

 

I'm not using a transaction, and I disabled the Undo recording, but it doesn't seem to do anything. Does anyone have any other ideas? I'm getting rather desperate here.

 

Thanks for all the help so far!

 

0 Likes
Message 10 of 15

jaboone
Advocate
Advocate

Can you see all your transactions on the screen?

you might try turning off numutt and cmdecho.  Also set a undomarker.  This makes acad act as one undo mark.

 

AcadDoc.StartUndoMark()
echo = AcadDoc.GetVariable("cmdecho")
mutt = AcadDoc.GetVariable("nomutt")

Learning as I go
0 Likes
Message 11 of 15

ActivistInvestor
Mentor
Mentor

The OP is not using the ActiveX API, he's using the AutoCAD .NET API, and there is no way to turn off undo recording with the ActiveX API..

 

This forum focuses mainly on using the AutoCAD .NET API, as opposed to using the .NET framework and languages to program other API's like ActvieX.

 


@jaboonewrote:

Can you see all your transactions on the screen?

you might try turning off numutt and cmdecho.  Also set a undomarker.  This makes acad act as one undo mark.

 

AcadDoc.StartUndoMark()
echo = AcadDoc.GetVariable("cmdecho")
mutt = AcadDoc.GetVariable("nomutt")


 

0 Likes
Message 12 of 15

ActivistInvestor
Mentor
Mentor

@Anonymouswrote:

 

I'm not using a transaction, and I disabled the Undo recording, but it doesn't seem to do anything. Does anyone have any other ideas? I'm getting rather desperate here.

 

 


You still didn't answer the question I asked above:

 

How are you determining that entries are being added to the undo history?

 

Next, the UNDO history includes more than just changes made to objects in a Database. It includes changes made to system variables (that are not stored in a database, including things like CMDECHO), and changes to the state of the Drawing editor.

 

The API you are calling only disables undo recording of changes to the database it's called on, and nothing else.

 

And with that I would also ask, what is the purpose of avoiding the adding of entries to the undo history, in the first place?

 

 

0 Likes
Message 13 of 15

ActivistInvestor
Mentor
Mentor

If disabling undo recording at the application-level is what you seek, this should do it, with the caveat that a user can't undo back beyond the point where it is done:

 


Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;

ed.Command("._UNDO", "_Control", "_None");

// do stuff here with undo completely disabled

ed.Command("._UNDO", "_All");

// The user will not be able to undo beyond this point.

 

0 Likes
Message 14 of 15

Anonymous
Not applicable

Sorry. I'm determining that entries are added by checking it in AutoCAD itself. As soon as I open a new drawing (and thus calling the method above), an entry appears in the Undo list. While it isn't a problem with only one entry, we disabled most of the functionality of our plugin. Normally, dozens of entries would be added to the Undo history, meaning that the user would no longer be able to undo his own actions (because there was too much of our stuff going on in the background).

 

Therefore, I want to disable Undo recording for everything happening in our plugin. 

 

I took a screenshot immediately after opening a new drawing:

 

UndoCut.png

 

I use the German version of AutoCAD, but I guess "Befehlsgruppe" translates roughly to "Group of Commands". When I googled the issue again, I found this: https://knowledge.autodesk.com/support/autocad/troubleshooting/caas/sfdcarticles/sfdcarticles/UNDO-l... bug report. We were using ObjectARX 2014 for development, but when I switched it to 2015, it didn't change anything. So I'm not sure if my problem is the one from the report. 

 

I appreciate the help so far. 

0 Likes
Message 15 of 15

ActivistInvestor
Mentor
Mentor

Unfortunately, without knowing more about what your app is doing, its difficult to say how to solve this, but normally, if you encapsulate all of the operations done by your app into a single transaction (not an OpenCloseTransaction, but a regular transaction started with StartTransaction), that should encapsulate all of the operations into a single undo group, not many of them.

 

My guess is that your app is starting and ending many top-level transactions, each of which will result in a distinct undo group. 

 

If your app is doing things when a DWG file is opened, using the UNDO subcommands shown above after your app finishes its work will clear out all previous undo groups and cause AutoCAD to act as if nothing was done, but that's only usable if your app is acting when a drawing is opened.

 


@Anonymouswrote:

Sorry. I'm determining that entries are added by checking it in AutoCAD itself. As soon as I open a new drawing (and thus calling the method above), an entry appears in the Undo list. While it isn't a problem with only one entry, we disabled most of the functionality of our plugin. Normally, dozens of entries would be added to the Undo history, meaning that the user would no longer be able to undo his own actions (because there was too much of our stuff going on in the background).

 

Therefore, I want to disable Undo recording for everything happening in our plugin. 

 

I took a screenshot immediately after opening a new drawing:

 

 

 

I use the German version of AutoCAD, but I guess "Befehlsgruppe" translates roughly to "Group of Commands". When I googled the issue again, I found this: https://knowledge.autodesk.com/support/autocad/troubleshooting/caas/sfdcarticles/sfdcarticles/UNDO-l... bug report. We were using ObjectARX 2014 for development, but when I switched it to 2015, it didn't change anything. So I'm not sure if my problem is the one from the report. 

 

I appreciate the help so far. 


 

0 Likes