Jump to content

Recommended Posts

Posted

Hi All

I would like a little help to finish this Lisp routine

 

Creates an "XLine" and copies it as many times as I press enter

(defun c:qqq (/ Pt1 Pt2 obline)
  (savevartoold) ;Saves system variables
  (constructionlayer)  ;Creates a Construction Layer
    (setq Pt1 (getpoint "\nPick First Point:")
          Pt2 (getpoint Pt1 "\nPick Second Point:"))
    (command "_xline" Pt1 Pt2 "")
    (setq obline (entlast))
    (while 1
    (command "copy" obline "" Pt2 pause))
  (resetoldvar)
 (princ)
)

It works all the way through except it won't reset the system variables if I push "Escape"

 

Regards

Tony

Posted (edited)

Implement into routine error handler in which you should put call to reset variables sub...

Edited by marko_ribar
Posted

When you are pressing Escape, the program is interrupted and function (resetoldvar) does not work. The cycle of copy should be other.
For example:

(setq flag T)
(while flag
  (setq obline (entlast))
  (command "_.copy" obline "" pt2 pause)
  (initget 1 "Y N")
  (setq reply (getkword "\nRepeat? [Y/N]: "))
  (setq flag (= (strcase reply) "Y"))
)

 

Posted

Change the while loop to:

(while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause))

vl-cmdf will evaluate the supplied input before passing it to the command; Esc will therefore exit the loop cleanly.

  • Like 1
Posted

Thanks Lee

 

Using your line I have this but it still won't reset the variables

(defun c:sss (/ Pt1 Pt2 obline)
  (savevartoold) (constructionlayer)
    (setq Pt1 (getpoint "\nPick First Point:")
          Pt2 (getpoint Pt1 "\nPick Second Point:"))
    (command "_xline" Pt1 Pt2 "")
    (setq obline (entlast))
   (while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause))
  (resetoldvar)
 (princ)
)

Hope you can help

Regards

Posted
(defun c:sss (/ *error* Pt1 Pt2 obline)
  (defun *error* (x)
    (resetoldvar)
    (princ "\n*Cancel*")
    )
  (savevartoold) (constructionlayer)
    ;;; ....etc
  

 

  • Like 1
Posted

Thanks Tharwat

I tried this but with no luck at getting the variables to reset

(defun c:sss (/ *error* Pt1 Pt2 obline)
  (defun *error* (x)
    (resetoldvar)
    (princ "\n*Cancel*")
    )
  (savevartoold) (constructionlayer)
    (setq Pt1 (getpoint "\nPick First Point:")
          Pt2 (getpoint Pt1 "\nPick Second Point:"))
    (command "_xline" Pt1 Pt2 "")
    (setq obline (entlast))
   (while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause))
  (resetoldvar)
 (princ)
)

 

Posted
3 minutes ago, TunzaGibbo said:

I tried this but with no luck at getting the variables to reset

Your problem is related to one of your previous threads that I already indicated to you and it seems is that you did not fix it and still repeating the same process and due to this silly and ridicules software of this forum, I can't find that thread.

Posted

I can't find it either

What can I do from here

Posted
1 minute ago, TunzaGibbo said:

I can't find it either

What can I do from here

Post the codes of the two functions savevartoold and restoldvar to check them out for you.

Posted
(defun savevartoold ()
  (setq *error*  DeltaError)
  (setq oldlayer    (getvar "clayer") 
        oldcolor    (getvar "cecolor")
        oldltype    (getvar "celtype")
        oldltscale  (getvar "ltscale")
        olddimscale (getvar "dimscale")
        oldattdia   (getvar "attdia")
        oldosmode   (getvar "osmode")
        oldcmdecho  (getvar "cmdecho"))
 (princ) 
)

(defun resetoldvar ()
  (setvar "clayer" oldlayer)
  (setvar "cecolor" oldcolor)
  (setvar "celtype" oldltype)
  (setvar "ltscale" oldltscale)
  (setvar "dimscale" olddimscale)
  (setvar "attdia" oldattdia)
  (setvar "osmode" oldosmode)
  (setvar "cmdecho" oldcmdecho) 
  (setq *error* nil)   
 (princ)
)

(defun DeltaError ()
 (resetoldvar)
  (princ ("\nCommand has been canceled:"))
  (princ "\nOrignal values are reset.\n")
 (princ)
)

 

Posted (edited)

Remove this line from (savevartoold) :

(setq *error*  DeltaError)

 

Remove this line from (resetoldvar) :

(setq *error* nil)

 

Don't use (deltaerror) sub...

 

HTH., M.R.

Edited by marko_ribar
Posted

Thank you Marko

It works like a dream now

 

Thnks to everyone else as well

Posted

Try it this way.

(defun c:sss (/ *error* lst Pt1 Pt2 obline)
  (defun *error* (x)
    (if lst (resetoldvar lst))
    (princ "\n*Cancel*")
    )
  ;;		;;
  (defun savevartoold nil
    (mapcar 'getvar '("clayer" "cecolor" "celtype" "ltscale" "dimscale" "attdia" "osmode" "cmdecho"))
    )
  ;;		;;
  (defun resetoldvar (vars)
    (mapcar 'setvar '("clayer" "cecolor" "celtype" "ltscale" "dimscale" "attdia" "osmode" "cmdecho") vars)
    )
  ;;		;;
  (constructionlayer)

  (setq lst (savevartoold))

  (if (and (setq Pt1 (getpoint "\nPick First Point:"))
           (setq Pt2 (getpoint Pt1 "\nPick Second Point:"))
           )
    (progn
      (command "_xline" Pt1 Pt2 "")
      (setq obline (entlast))
      )
    )
  
  (while (and obline (vl-cmdf "_.copy" obline "" "_non" Pt2 pause)))
  
  (if lst (resetoldvar lst))
  
  (princ)
  )

Although you can localise the sub-functions as well and its up to you.

Posted (edited)
13 hours ago, TunzaGibbo said:

Thanks Lee

 

Using your line I have this but it still won't reset the variables


(defun c:sss (/ Pt1 Pt2 obline)
  (savevartoold) (constructionlayer)
    (setq Pt1 (getpoint "\nPick First Point:")
          Pt2 (getpoint Pt1 "\nPick Second Point:"))
    (command "_xline" Pt1 Pt2 "")
    (setq obline (entlast))
   (while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause))
  (resetoldvar)
 (princ)
)

 

 

If you are using vl-cmdf, a local error function (*error*) is not required, as vl-cmdf will account for the user pressing ESC during command evaluation, flag this as invalid input (by returning nil), and the remainder of the program will continue to be evaluated.

 

With that said, I do not know why you are saving & resetting system variables that you are not changing in your program; presumably you are blindly copying & pasting the same functions for each of your programs.

Edited by Lee Mac
Posted
2 hours ago, Lee Mac said:

 

If you are using vl-cmdf, a local error function (*error*) is not required, as vl-cmdf will account for the user pressing ESC during command evaluation, flag this as invalid input (by returning nil), and the remainder of the program will continue to be evaluated.

 

I guess the vl-cmdf will always return true once the arguments are valid and to while function as in the above case and even if the user tries to hit ESC, the command won't stop at all until the user forcing AutoCAD to exit with the use of Windows Task Manager as I tried this myself  just now with AutpCAD 2017.

 

So even though the error function would not useful in this case because it is not reachable and I don't know how did that work with the user anyway.

 

 

 

 

Posted
1 hour ago, Tharwat said:

I guess the vl-cmdf will always return true once the arguments are valid and to while function as in the above case and even if the user tries to hit ESC, the command won't stop at all until the user forcing AutoCAD to exit with the use of Windows Task Manager as I tried this myself  just now with AutpCAD 2017.

 

So even though the error function would not useful in this case because it is not reachable and I don't know how did that work with the user anyway.

 

It would seem that Autodesk have broken this function with later versions of AutoCAD 🙄

Posted
4 minutes ago, Lee Mac said:

 

It would seem that Autodesk have broken this function with later versions of AutoCAD 🙄

You may encounter the same issue since you are using later version than mine as shown into your profile if you have time to test that out.

 

 

Posted
10 hours ago, Lee Mac said:

 

If you are using vl-cmdf, a local error function (*error*) is not required, as vl-cmdf will account for the user pressing ESC during command evaluation, flag this as invalid input (by returning nil), and the remainder of the program will continue to be evaluated.

 

With that said, I do not know why you are saving & resetting system variables that you are not changing in your program; presumably you are blindly copying & pasting the same functions for each of your programs.

 

You make a good point about saving and resetting variables. In some routines I change the layers and linetypes many times, so I thought it was easier to have those 2 functions running rather than having separate reset functions each time I change a variable.

I guess that's just the way I think. 

What would be a better way? 

Posted

Maybe an old fashioned way (while (= (setq test (getstring "Press any key to exit <Cr> to continue")) nil)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...