.NET

.NET

Reply
Valued Contributor
namin
Posts: 55
Registered: ‎09-19-2006
Message 1 of 16 (216 Views)

Detecting the Undo of a Custom Command Instance

216 Views, 15 Replies
09-16-2008 05:16 PM
I have a command which adds some entities to the drawing and changes some custom internal state. If the user undoes the command, I would like to also restore the custom internal state. Is there a way to detect a particular undo and hook into it in order to run a custom action?

Thanks.
*Tony Tanzillo
Message 2 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-16-2008 05:42 PM in reply to: namin
The easiest way to do that is to store your internal state
data in the drawing (a Dictionary perhaps), and access it
directly.

Because changes you make to the data stored in the
drawing are undone/redone along with everything else,
your state will always be in sync with the rest of the
drawing.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6032949@discussion.autodesk.com...
I have a command which adds some entities to the drawing and changes some custom internal state. If the user undoes the command, I would like to also restore the custom internal state. Is there a way to detect a particular undo and hook into it in order to run a custom action?

Thanks.
Valued Contributor
namin
Posts: 55
Registered: ‎09-19-2006
Message 3 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-16-2008 05:56 PM in reply to: namin
Thanks, Tony, for the quick reply.

I like your suggestion. Would you please tell me more about how I can store my internal state in the drawing? Do you have a pointer to an article, perhaps?

I am wondering whether I can store an arbitrary .NET object in the drawing, as I am already making heavy use of my internal state.

Thanks, again.
*Tony Tanzillo
Message 4 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-16-2008 06:11 PM in reply to: namin
Search the newsgroup for "XRecord" and you should find some examples.

XRecords are the storage medium used to store arbitrary data in a drawing. XRecords must be contained in a Dictionary and a Dictionary can be attached to any object, or be stored in the 'named objects dictionary'.

You can't store managed objects in a drawing directly, but can write an object's data or state to an XRecord manually and read it back into the object later (provided the data types you want to persist are supported
by the XRecord).

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6032971@discussion.autodesk.com...
Thanks, Tony, for the quick reply.

I like your suggestion. Would you please tell me more about how I can store my internal state in the drawing? Do you have a pointer to an article, perhaps?

I am wondering whether I can store an arbitrary .NET object in the drawing, as I am already making heavy use of my internal state.

Thanks, again.
Valued Contributor
namin
Posts: 55
Registered: ‎09-19-2006
Message 5 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-16-2008 06:15 PM in reply to: namin
Thanks, Tony. I found this example:
http://through-the-interface.typepad.com/through_the_interface/2006/11/linking_circles_1.html

However, Kean allows the data to get out of the sync with the internal state. Now, I am not really sure your suggestion is good, because it means that I have to constantly re-read my managed internal state from the XRecord in order to make sure the undos are taken into account. Do you agree that's quite painful?

Would you perhaps have a suggestion on how to go about with my original idea, which is to monitor the undos?

Thanks. I am a little lost about the best way to handle this.
*Tony Tanzillo
Message 6 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-16-2008 07:17 PM in reply to: namin
>> Now, I am not really sure your suggestion is good, because it means that I have to constantly re-read my managed internal state from the XRecord in order to make sure the undos are taken into account. <<

Sorry, I don't follow. The whole point of storing the data in
the drawing, is so that it will be undone/redone in order to
eliminate any need to 'make sure the undos are taken into
account', because the data is managed by AutoCAD.

Whenever you access it, it is correctly synchronized with
any other drawing data.

What Kean does in that article is in my opinion, unsound,
for the very reason cited. He stores the data in memory,
and in the drawing. I don't see the point to that, because
you have to read the data from the drawing anyway, since
the copy stored in memory is not necessarily up to date.

I just store the data in the drawing. The overhead of
reading/writing it is not too significant in most realistic
scenarios where the need to access it is infrequent.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm
Valued Contributor
namin
Posts: 55
Registered: ‎09-19-2006
Message 7 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-16-2008 07:28 PM in reply to: namin
OK, thanks. I appreciate your point, specially since Kean's example actually fails with undo.

I am thinking of just keeping a trace in the drawing, and by looking at the trace, I will know which 'version' of my internal state to use. It seems very tedious for me to have to convert all my internal state into the XRecord format. But you've convinced me that your approach is sound.

Thanks again.
*Roland Feletic
Message 8 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-17-2008 12:58 AM in reply to: namin
Thank you, Tony. Allways interesting to hear your opinion.

Roland
*Tony Tanzillo
Message 9 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-17-2008 08:07 AM in reply to: namin
Converting your class to store state in the drawing isn't
that difficult.

I use properties whose getter and setter methods read
and write the data to the XRecord directly.

The other advantage to storing state in the drawing, is
that if the state is document-specific (meaning that you
have different instances of your class with different state,
one for each open document), it solves that problem quite
nicely as well.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009

http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm

wrote in message news:6032999@discussion.autodesk.com...
OK, thanks. I appreciate your point, specially since Kean's example actually fails with undo.

I am thinking of just keeping a trace in the drawing, and by looking at the trace, I will know which 'version' of my internal state to use. It seems very tedious for me to have to convert all my internal state into the XRecord format. But you've convinced me that your approach is sound.

Thanks again.
Valued Contributor
namin
Posts: 55
Registered: ‎09-19-2006
Message 10 of 16 (216 Views)

Re: Detecting the Undo of a Custom Command Instance

09-17-2008 08:57 AM in reply to: namin
Thanks a lot, Tony. I like the idea of using getters and setters that read and write the XRecord directly.

I have a question about how to format a variable-length modifiable list into an XRecord, but I'll ask it in a separate thread, as it's somewhat orthogonal to this issue.

Thanks, again!
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.