Is it possible to undo setq with the undo command?

Is it possible to undo setq with the undo command?

jamieq
Collaborator Collaborator
1,451 Views
7 Replies
Message 1 of 8

Is it possible to undo setq with the undo command?

jamieq
Collaborator
Collaborator

I wrote a program that changes the width of attributes in a block, but also changes the value of a symbol to the same number. When I undo, it puts the width of the attributes back, but leaves the value of the symbol. Is there a way to use the undo command to undo lisp commands, like setq? 

0 Likes
Accepted solutions (1)
1,452 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

@jamieq wrote:

I wrote a program that ... changes the value of a symbol .... When I undo, it ... leaves the value of the symbol. Is there a way to use the undo command to undo lisp commands, like setq? 



I don't think you can with (setq) specifically.  But U or UNDO will  reverse some AutoLisp functions, such as (setvar).

Kent Cooper, AIA
0 Likes
Message 3 of 8

doaiena
Collaborator
Collaborator

In order to avoid leaving set variables after your lisp commands run, you should localize them in the function. When localizing a variable, after the function ends, it's immediately cleared out of memory.

(defun SomeFunction ( / var1 var2 );localize variables here!!!

(setq var1 5)
(setq var2 "someString")
;do some other stuff here
)
0 Likes
Message 4 of 8

jamieq
Collaborator
Collaborator

The problem is, I have a global variable that gets changed in program so it can be used in another. The program that changes the variable also updates the width of attributes. If I undo, the attributes revert back, but the global variable stays the same. When I run the program that inserts that block - using entmake - it will insert with different width attributes. 

0 Likes
Message 5 of 8

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Instead of using a global variable, you can store your data in a dictionary ; the simpler way is using LISP data (ldata).

 

(defun c:foo ()
(vl-load-com) (vla-StartUndoMark (vla-get-ActiveDocument (vlax-get-acad-object))) (vlax-ldata-put "SomeDictionary" "SomeKey" 42) (vla-EndUndoMark (vla-get-ActiveDocument (vlax-get-acad-object))) (princ) )


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 8

jamieq
Collaborator
Collaborator

This will work! This is the first I've learned about dictionaries. It will be helpful for some other programs I've written as well. Thank you!

0 Likes
Message 7 of 8

scot-65
Advisor
Advisor
As a recap for storage and retrieval...

Session Gremlin - (setq a ...) where "a" is not in the defun local variable.
Embed information into an object - Extended data and REGAPP.
Embed into drawing's dictionary VLAX-LDATA-PUT.
Write to external configuration file SETCFG (obsolete).
Write to the registry VL-REGISTRY-WRITE.
Write to an external file OPEN / WRITE-LINE.

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 8 of 8

john.uhden
Mentor
Mentor

I recently implemented using ldata for my pool liner programs.  Each requires about 12-16 inputs via dialog.  They get saved in the drawing and used as defaults for subsequent runs.  That way, if the user needs to correct one of the inputs, at least all the others are still good.  And yes, since the drawing database is modified, an Undo undoes the modification.

John F. Uhden

0 Likes