Undo group

Undo group

svucic
Advocate Advocate
2,200 Views
8 Replies
Message 1 of 9

Undo group

svucic
Advocate
Advocate

Hello,

 

I'm trying to figure out the undo command in autolisp. I can't find any good documentation with examples although there are many posts on forums.

I cant get my head around Begin and Back. As far as I understood it does the same. But I can't make (command-s "._UNDO" "BE" )  to work in my code.

(command-s "._UNDO" "B" ) works fine.

 

Here is my code.

 

(defun C:InsertAtrLin ()

*
*
*
*

(defun C:VPTFL(/ *error*)

(defun *error*(errmsg)
	(if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
	  
	)
	(command-s "._UNDO" "B" )
	(vl-bt)
)

	(setq *acad* (vlax-get-acad-object))
	(setq Doc (vla-get-ActiveDocument *acad*))
	(vla-startundomark Doc)

;(command "._UNDO" "_Begin" )
;(command-s "._UNDO" "_End")
(if (= rb 0)
	(progn
		(prompt "\nNacrtajte liniju!")
		(command "._PLINE")
		(while (eq 1 (logand 1 (getvar "CMDACTIVE")))
			(command pause)
		)
		(setq selline (entlast))
		(setq ssline (ssadd))   
		(ssadd selline ssline)
		
		(C:choosecodeFL)
		
		(_lindata)
		
		(_lstyle)
		
		(C:InsertAtrLin)
		
		(C:appendatrfullline)
	)
)

(if (= 8 (logand (getvar 'undoctl) 8))
(princ "\nGroup is active!")
(princ "\nGroup is not active!")
)

(if (= rb 1)
	(progn

		(C:pickline)
		
		(C:choosecodeFL)
		
		(_lindata)
		
		(_lstyle)
		
		(C:InsertAtrLin)
		
		(C:appendatrfullline)

	)
)
;(command "._UNDO" "_End")
;(command-s "._UNDO" "_Begin")
(vla-endundomark Doc)
)

(defun C:VPTIL()

(if (= rb 0)
	(progn
		(prompt "\nNacrtajte liniju!")
		(command "._PLINE")
		(while (eq 1 (logand 1 (getvar "CMDACTIVE")))
			(command pause)

*
*
*
*

)
0 Likes
Accepted solutions (1)
2,201 Views
8 Replies
Replies (8)
Message 2 of 9

Alexander.Rivilis
Mentor
Mentor

@svucic wrote:

I cant get my head around Begin and Back. As far as I understood it does the same.


No. Undo Begin is start undo group, Undo End is stop undo group, but Undo Back is execute Undo to last Undo Mark (or Undo all commands if no marks).

Undo Begin is the same as:

(vla-startundomark Doc)

and Undo End is the same as: 

(vla-endundomark Doc)

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 9

svucic
Advocate
Advocate

Thank you for your kind response.

 

OK, so Undo Back undoes everything to the last Undo Mark.  So what is the command to undo everything to Undo Begin? Is it  (command "UNDO" "BE")?

 

0 Likes
Message 4 of 9

Alexander.Rivilis
Mentor
Mentor

No. Undo Mark and Undo Begin is totally different commands. Undo Mark create mark for Undo Back command, but Undo Begin begin a group of commands to undo it as one command with Undo 1 (or U) command.

 

http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-2729A466-B199-4840-B92B-4D8A38A8ADB8

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 5 of 9

svucic
Advocate
Advocate

I think I get it now.

It's confusing me that when I group some code and use command U, it doesn't undo all the changes.

Could this be due nested functions. My code strucuture is something like this.

(defun C:MainFunction()

;; Main function code here
*
*
*
*
;; Main function code here

    (defun C:SubFunction( / *error*)

        (defun *error*(errmsg)
             *
             *
             *
         (command-s "U")
         (vl-bt) 
		) *error* END

		(command "._UNDO" "_Begin" ) 
		;; SubFunction code 
		(C:SomeFunction) 
		(C:SomeFunction2) 
		(_SomeFunction3) 
		* 
		* 
		* 
		;; SubFunction code 
		(command-s "._UNDO" "_End") 
			
	) ;SubFunction END 
	
) ;MainFunction END
 

 

0 Likes
Message 6 of 9

Alexander.Rivilis
Mentor
Mentor

Oh... I can not recommend using command method in *error* function.

What about catching errors with help of (vl-catch-all-apply) function?

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 7 of 9

svucic
Advocate
Advocate

I'll have a look at that function. Anyway, I saw a lot of posts in which people are using UNDO and *error* to undo the changes made by a routine if escape is pressed or an error occurs. I just can't make that work for me and I really would like to.

0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

@svucic wrote:

....

It's confusing me that when I group some code and use command U, it doesn't undo all the changes.

....

....
        (defun *error*(errmsg)
             *
             *
             *
         (command-s "U")
         (vl-bt) 
		) *error* END
....

Since your main function includes an Undo Begin, and concludes with an Undo End if all goes well, you need to give it that Undo End in the *error* handler if anything goes wrong, before giving it the "plain" U command, which will Undo everything sandwiched between the Begin and End as one "item".  Try adding a line:

 

....
        (defun *error*(errmsg)
             *
             *
             *

         (command "_.undo" "_end")
         (command-s "U")
         (vl-bt)
       ) *error* END
....

 

[or the (vla-...) equivalent].

Kent Cooper, AIA
Message 9 of 9

svucic
Advocate
Advocate

That's it! Thank you very much Kent1Cooper!

0 Likes