Author Topic: vl-cmdf or command?  (Read 2809 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3312
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #15 on: July 17, 2023, 09:13:11 AM »
You put in error handler old sys variables values and in main body of routine usually at start, you change sys vars to what you need... At the end, just call error handler and sys vars should be restored; this also means that if routine cancels, breaks, or similar - error handler will restore them...

Code: [Select]
(defun c:foo ( / *error* cmd osm ... )

  (defun *error* ( m )
    (if cmd
      (setvar 'cmdecho cmd)
    )
    (if osm
      (setvar 'osmode osm)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  ;;; MAIN ;;;
  (setq osm (getvar 'osmode))
  (setvar 'osmode 0)
  (setq cmd (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  ...
  do stuff...
  ...
  (*error* nil) ; clean exit - quiet - last statement in error handler is (princ)
)

HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #16 on: July 17, 2023, 09:23:26 AM »
sorry , i am wrong. command not change to vl-cmdf so can not end loop yet.

Code: [Select]
(defun c:past_cen(/ ss osm past_mono_base aa)

(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cmdecho" "osmode")))

(defun *error* (msg)

(command-s "_.undo" "_end")

(mapcar 'eval e_lst)

(princ "")

)

(setq osm (getvar "osmode"))

(setvar "cmdecho" 0)

(princ "\nselect:")

(setq ss (ssget))

        (vl-cmdf "_.undo" "_begin")

(setvar "osmode" 37)

(setq past_mono_base (getpoint "\n basepoint: "))

(vl-cmdf "._COPYBASE" past_mono_base ss "")

        (setq aa T)

        (while (= aa T)

        (setvar "osmode" 4)

(command "._pasteclip" "_cen" pause)

        (if (null pause)

        (progn

        (setq aa nil)

        (setvar "osmode" osm)

        )

        )

        (vl-cmdf "_.undo" "_end")

);while

(princ)
)
(princ)

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #17 on: July 17, 2023, 10:06:09 AM »
You put in error handler old sys variables values and in main body of routine usually at start, you change sys vars to what you need... At the end, just call error handler and sys vars should be restored; this also means that if routine cancels, breaks, or similar - error handler will restore them...

Code: [Select]
(defun c:foo ( / *error* cmd osm ... )

  (defun *error* ( m )
    (if cmd
      (setvar 'cmdecho cmd)
    )
    (if osm
      (setvar 'osmode osm)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  ;;; MAIN ;;;
  (setq osm (getvar 'osmode))
  (setvar 'osmode 0)
  (setq cmd (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  ...
  do stuff...
  ...
  (*error* nil) ; clean exit - quiet - last statement in error handler is (princ)
)

HTH., M.R.

if use vl-cmdf have this promble,i usually use command ,so not this promble.

use command if has error ,it is end lisp.but vl-cmdf not end lisp.

thanks so much.

{(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cmdecho" "osmode")))

(defun *error* (msg)

(mapcar 'eval e_lst)

(princ "")

)}=enter code first remember now system variables if error recovery these system variables

                                 (setq osm (getvar "osmode"))

                                 (setq ocmd (getvar "cmdecho"))
                                 .................................
                                 .................................

                                 (setvar "osmode" osm )

                                 (setvar "cmdecho" ocmd)

with your code same function?

« Last Edit: July 17, 2023, 10:57:57 AM by masao »

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #18 on: July 17, 2023, 07:32:59 PM »
1.if I use pause how to exit loop use ESC?

2.if I use getpoint how can I preview paste graphics?

ribarm

  • Gator
  • Posts: 3312
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #19 on: July 18, 2023, 03:08:58 AM »
1. Force command function to use pause "\\" all the time until hit ENTER, or SPACE - check should be with 'cmdactive sys var...
Code - Auto/Visual Lisp: [Select]
  1. (vl-cmdf "_.PLINE")
  2. (while (< 0 (getvar 'cmdactive))
  3.   (vl-cmdf "\\")
  4. ) ;;; for termination you can use ENTER, or "C" - close + ENTER
  5.  

2. You can preview graphics with functions (grdraw), or (grvecs) which is slightly more complex then (grdraw)...
Code - Auto/Visual Lisp: [Select]
  1. (while (and (not (initget 128)) (setq p (getpoint "\nPick or specify point - ENTER or \"C\" - close to FINISH : "))
  2.   (setq pl (cons p pl))
  3.   (if (and (= (type (car pl)) 'list) (cadr pl))
  4.     (grdraw (car pl) (cadr pl) 2 2)
  5.     (if (or (= (car pl) "c") (= (car pl) "C") (= (car pl) "_c") (= (car pl) "_C"))
  6.       (grdraw (last pl) (cadr pl) 2 2)
  7.     )
  8.   )
  9. )
  10. (vl-cmdf "_.PLINE")
  11.   (if (or (/= p "c") (/= p "C") (/= p "_c") (/= p "_C"))
  12.     (vl-cmdf "_non" p)
  13.     "_C"
  14.   )
  15. )
  16.  

[EDIT] Second code wasn't tested, so I've improved it further... [/EDIT]

Code - Auto/Visual Lisp: [Select]
  1. (defun c:ppl ( / p pl done )
  2.   (while (and (not done) (not (initget 128)) (setq p (getpoint (if (car pl) (car pl) "\nPick or specify point - ENTER or \"C\" - close to FINISH : "))))
  3.     (setq pl (cons p pl))
  4.     (if (and (= (type (car pl)) 'list) (cadr pl))
  5.       (grdraw (car pl) (cadr pl) 2 2)
  6.       (if (or (not (car pl)) (= (car pl) "c") (= (car pl) "C") (= (car pl) "_c") (= (car pl) "_C"))
  7.         (progn
  8.           (grdraw (last pl) (cadr pl) 2 2)
  9.           (setq done t)
  10.         )
  11.       )
  12.     )
  13.   )
  14.   (vl-cmdf "_.PLINE")
  15.   (foreach p (reverse pl)
  16.     (if (or (/= p "c") (/= p "C") (/= p "_c") (/= p "_C"))
  17.       (vl-cmdf "_non" p)
  18.       "_C"
  19.     )
  20.   )
  21.   (while (< 0 (getvar 'cmdactive))
  22.     (vl-cmdf "")
  23.   )
  24.   (princ)
  25. )
  26.  

HTH., M.R.
« Last Edit: July 19, 2023, 07:00:18 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #20 on: July 18, 2023, 11:30:03 AM »
1. Force command function to use pause "\\" all the time until hit ENTER, or SPACE - check should be with 'cmdactive sys var...
Code - Auto/Visual Lisp: [Select]
  1. (vl-cmdf "_.PLINE")
  2. (while (< 0 (getvar 'cmdactive))
  3.   (vl-cmdf "\\")
  4. ) ;;; for termination you can use ENTER, or "C" - close + ENTER
  5.  

2. You can preview graphics with functions (grdraw), or (grvecs) which is slightly more complex then (grdraw)...
Code - Auto/Visual Lisp: [Select]
  1. (while (and (not (initget 128)) (setq p (getpoint "\nPick or specify point - ENTER or \"C\" - close to FINISH : "))
  2.   (setq pl (cons p pl))
  3.   (if (and (= (type (car pl)) 'list) (cadr pl))
  4.     (grdraw (car pl) (cadr pl) 2 2)
  5.     (if (or (= (car pl) "c") (= (car pl) "C") (= (car pl) "_c") (= (car pl) "_C"))
  6.       (grdraw (last pl) (cadr pl) 2 2)
  7.     )
  8.   )
  9. )
  10. (vl-cmdf "_.PLINE")
  11.   (if (or (/= p "c") (/= p "C") (/= p "_c") (/= p "_C"))
  12.     (vl-cmdf "_non" p)
  13.     "_C"
  14.   )
  15. )
  16.  

HTH., M.R.

thanks so much.

i usually use command and while to repeat lisp,and use ESC exit lisp.

but i try to use vl-cmdf write,can not same with command.

because use "pause" to end can not exit loop in vl-cmdf so if my code use "pause" to end,i still use command.

like this

Code: [Select]
(defun c:past_cen(/ e_lst ss osm past_mono_base)

(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cmdecho" "osmode")))

(defun *error* (msg)

(command-s "_.undo" "_end")

(mapcar 'eval e_lst)

(princ "")

)

(setq osm (getvar "osmode"))

(setvar "cmdecho" 0)

(princ "\nselect:")

(setq ss (ssget))

        (vl-cmdf "_.undo" "_begin")

(setvar "osmode" 37)

(setq past_mono_base (getpoint "\n basepoint: "))

(vl-cmdf "._COPYBASE" past_mono_base ss "")

        (while t

        (setvar "osmode" 4)

(command "._pasteclip" "_cen" pause)

        (vl-cmdf "_.undo" "_end")

);while

(princ)
)
(princ)

i still use this "(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cmdecho" "osmode")))

(defun *error* (msg)

(mapcar 'eval e_lst)

(princ "")"
to error processing.

my code thinking is command now,vl-cmdf is difficult.  :-( :rip:

thank you so much.
« Last Edit: July 18, 2023, 12:42:31 PM by masao »

ribarm

  • Gator
  • Posts: 3312
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #21 on: July 18, 2023, 11:48:33 AM »
Sorry buddy, do you know that with this line you read and set already set sys vars...
The point of reseting sys vars is that you restore them to the state before routine executed...
What you showed and coded is that in main part of the code you changed sys var - cmdecho to 0 and osmode to 37 and after 4... If you execute routine, after finish, you'll have cmdecho=0 and osmode=4 to work with... So if you don't care for settings for working (drawing) environment then it's OK with me... The thing is that you shoud understand the point of error handlers and their proper way of writing...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #22 on: July 18, 2023, 12:39:43 PM »
Sorry buddy, do you know that with this line you read and set already set sys vars...
The point of reseting sys vars is that you restore them to the state before routine executed...
What you showed and coded is that in main part of the code you changed sys var - cmdecho to 0 and osmode to 37 and after 4... If you execute routine, after finish, you'll have cmdecho=0 and osmode=4 to work with... So if you don't care for settings for working (drawing) environment then it's OK with me... The thing is that you shoud understand the point of error handlers and their proper way of writing...

but i use ESC,sys vars is  reset old sys ,you can try it. i use this error code in command is not promble.

Code: [Select]
(defun c:past_cen(/ e_lst ss osm past_mono_base)

(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cmdecho" "osmode")))

(defun *error* (msg)

(command-s "_.undo" "_end")

(mapcar 'eval e_lst)

(princ "")

)

(setq osm (getvar "osmode"))

(setvar "cmdecho" 0)

(princ "\nselect:")

(setq ss (ssget))

        (vl-cmdf "_.undo" "_begin")

(setvar "osmode" 37)

(setq past_mono_base (getpoint "\n basepoint: "))

(vl-cmdf "._COPYBASE" past_mono_base ss "")

        (while t

        (setvar "osmode" 4)

(command "._pasteclip" "_cen" pause)

        (vl-cmdf "_.undo" "_end")

);while

(princ)
)
(princ)
« Last Edit: July 18, 2023, 12:43:08 PM by masao »

JohnK

  • Administrator
  • Seagull
  • Posts: 10666
Re: vl-cmdf or command?
« Reply #23 on: July 18, 2023, 12:55:02 PM »
Sorry buddy, do you know that with this line you read and set already set sys vars...
The point of reseting sys vars is that you restore them to the state before routine executed...
What you showed and coded is that in main part of the code you changed sys var - cmdecho to 0 and osmode to 37 and after 4... If you execute routine, after finish, you'll have cmdecho=0 and osmode=4 to work with... So if you don't care for settings for working (drawing) environment then it's OK with me... The thing is that you shoud understand the point of error handlers and their proper way of writing...

but i use ESC,sys vars is  reset old sys ,you can try it. i use this error code in command is not promble.

Code: [Select]
;; code removed

masao,
What ribarm is trying to tell you, that your code is not allowing the error handler to run. If you add (quit) to your code it would run as you expect. HOWEVER, the code you listed above is not good (it will never stop asking you to paste the item on the clipboard).  the loop "(while T ..." will never stop.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10666
Re: vl-cmdf or command?
« Reply #24 on: July 18, 2023, 12:58:37 PM »
In the link below, MP walks through a simple application with error handling.
https://www.theswamp.org/index.php?topic=4176.msg49936#msg49936
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: vl-cmdf or command?
« Reply #25 on: July 18, 2023, 01:51:44 PM »
masao,
What ribarm is trying to tell you, that your code is not allowing the error handler to run. If you add (quit) to your code it would run as you expect. HOWEVER, the code you listed above is not good (it will never stop asking you to paste the item on the clipboard).  the loop "(while T ..." will never stop.
masao uses ESC key to stop the routine, it initiates *error* function just fine

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: vl-cmdf or command?
« Reply #26 on: July 18, 2023, 02:02:29 PM »
what masao is trying to tell us is that when he changes
(command "._pasteclip" "_cen" pause)
to
(vl-cmdf "._pasteclip" "_cen" pause)
then he can not stop the routine with ESC key

ribarm

  • Gator
  • Posts: 3312
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #27 on: July 18, 2023, 02:59:10 PM »
what masao is trying to tell us is that when he changes
(command "._pasteclip" "_cen" pause)
to
(vl-cmdf "._pasteclip" "_cen" pause)
then he can not stop the routine with ESC key

There is always way to remedy erronous code...
Has he tried :

(not (vl-cmdf "._pasteclip" "_cen" pause))

it should behave like (command) return, but evaluation of tokens firstly prior feeding to (vl-cmdf)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #28 on: July 19, 2023, 12:44:29 AM »
thanks all.

i give up to use vl-cmdf on all code.

if use while and "(command "._pasteclip" "_cen" pause)" pause end code don't change to vl-cmdf.

else command change to vl-cmdf. make lisp can work smoothly.

thnaks VovKa,it my question.

thanks all.