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

0 Members and 1 Guest are viewing this topic.

masao

  • Newt
  • Posts: 97
vl-cmdf or command?
« on: July 16, 2023, 01:13:32 AM »
if i write a lisp use command,than i change to vl-cmdf now.

else code is same

just command work and vl-cmdf work?have any different?

but everyone usually use command.

ex.

defun c:xx
setq .......
setq .......
(command "")

and

defun c:xx
setq .......
setq .......
(vl-cmdf "")
« Last Edit: July 16, 2023, 07:47:03 AM by masao »

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #1 on: July 16, 2023, 08:18:11 AM »
I usualy use (vl-cmdf) instead of (command) as it's slightly faster and speed is even better than (command-s)... I usualy don't have exactly the need to return T after execution of commands, so IMHO (vl-cmdf) useage is appropriate and justified... Just always watch that somewhere you don't make logical construction forgrtting that (vl-cmdf) returns T after execution, so if not predicted this way command main routine may fail...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #2 on: July 16, 2023, 09:52:24 AM »
I usualy use (vl-cmdf) instead of (command) as it's slightly faster and speed is even better than (command-s)... I usualy don't have exactly the need to return T after execution of commands, so IMHO (vl-cmdf) useage is appropriate and justified... Just always watch that somewhere you don't make logical construction forgrtting that (vl-cmdf) returns T after execution, so if not predicted this way command main routine may fail...

sorry,what means "returns T after execution"?

because i want  to convert command into vl-cmdf but my code usually end place use command.

because my code is easy so can not know both of different.

thanks so much.
« Last Edit: July 16, 2023, 10:13:22 AM by masao »

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #3 on: July 16, 2023, 10:57:43 AM »
(vl-cmdf) returns T after execution, so logic of (if) statements, or (cond), or (and), or (or), or (not) must be adequate and in relation that routine continues to execute till the end and that logic is correctly implemented in the code...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #4 on: July 16, 2023, 11:14:55 AM »
(vl-cmdf) returns T after execution, so logic of (if) statements, or (cond), or (and), or (or), or (not) must be adequate and in relation that routine continues to execute till the end and that logic is correctly implemented in the code...

thanks so much. but use (if) or (while) or (or) or (cond) not end in command code is can not work,so if my code can work ,convert command into vl-cmdf,it is ok.

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #5 on: July 16, 2023, 11:22:29 AM »
Look if your routine works with (command) correctly, and you had it placed where logic of statements influence to correct evaluation of that specific paragraph, it is not advisable to change (command) to (vl-cmdf) though you could always imitate nil as a return : (not (vl-cmdf tokens)) = (command tokens)... So if you have separate statements where it's not neccessary to distinguish returns of statements, you are ok with changing (command) => (vl-cmdf)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #6 on: July 16, 2023, 11:38:32 AM »
For more info what command function is adequate for usage, I strongly suggest that you analyze *.lsp template for coding placed here :
https://www.theswamp.org/index.php?topic=57865.msg611757#msg611757
In this reply it was adopted usage of syntax : (exe (list tokens)) instead of all 3 : (command), (vl-cmdf) and (command-s)... There are no speed interuptions, it's just that with (exe (list tokens)) you must write like as you are using (command-s) tokens - it's not advisable to interupt with this construction :
Code: [Select]
(exe (list "_.PLINE"))
(foreach p plst
  (exe (list "_non" p))
)
(exe (list "_C"))

So it must look like this :
Code: [Select]
(exe (cons "_.PLINE" (apply (function append) (append (mapcar (function (lambda ( p ) (list "_non" p)) plst) (list (list "_C"))))))
« Last Edit: July 16, 2023, 11:42:54 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 #7 on: July 16, 2023, 11:57:16 AM »
For more info what command function is adequate for usage, I strongly suggest that you analyze *.lsp template for coding placed here :
https://www.theswamp.org/index.php?topic=57865.msg611757#msg611757
In this reply it was adopted usage of syntax : (exe (list tokens)) instead of all 3 : (command), (vl-cmdf) and (command-s)... There are no speed interuptions, it's just that with (exe (list tokens)) you must write like as you are using (command-s) tokens - it's not advisable to interupt with this construction :
Code: [Select]
(exe (list "_.PLINE"))
(foreach p plst
  (exe (list "_non" p))
)
(exe (list "_C"))

So it must look like this :
Code: [Select]
(exe (cons "_.PLINE" (apply (function append) (append (mapcar (function (lambda ( p ) (list "_non" p)) plst) (list (list "_C"))))))

thank you so much.

i am not understand these code yet,but thank you provide information.
i just easy code now.
like this
Code: [Select]
(defun c:ss(/ ss p1 p2)
 (setq ss (ssget))
 (setq p1 (getpoint "\nbase point:"))
 (setq p2 (polar p1 0 0))
(vl-cmdf "_scale" ss "" p1 "_reference" p1 pause)
(princ)
)
i just convert command into vl-cmdf ,i just write easy code now. :laugh:

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #8 on: July 16, 2023, 11:59:06 AM »
One more info ab (exe (list tokens))...
It was hardcoded that it returns T upon execution, so it's very similar to (vl-cmdf), only it's little more advanced as it uses (command-s) where it's neccesity... (error handler)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #9 on: July 16, 2023, 12:14:00 PM »
Here, I've made test for you do confirm what I was speaking...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tttt ( / *error* tttt wcs initvalueslst ucsf ti p lst )
  2.  
  3.   (defun *error* ( m )
  4.     (if wcs
  5.       (if ucsf
  6.         (while
  7.           (not
  8.             (and
  9.               (equal (getvar (quote ucsxdir)) (car ucsf) 1e-6)
  10.               (equal (getvar (quote ucsydir)) (cadr ucsf) 1e-6)
  11.               (equal (trans (list 0.0 0.0 1.0) 1 0 t) (caddr ucsf) 1e-6)
  12.             )
  13.           )
  14.           (exe (list "_.UCS" "_P"))
  15.         )
  16.       )
  17.     )
  18.     (while (= 8 (logand 8 (getvar (quote undoctl))))
  19.       (if (not (exe (list "_.UNDO" "_E")))
  20.         (if doc
  21.           (vla-endundomark doc)
  22.         )
  23.       )
  24.     )
  25.     (if initvalueslst
  26.       (mapcar (function apply_cadr->car) initvalueslst)
  27.     )
  28.     (foreach fun (list (quote tttt) (quote vl-load) (quote exe) (quote cmdfun) (quote cmderr) (quote catch_cont) (quote apply_cadr->car) (quote ftoa))
  29.       (setq fun nil)
  30.     )
  31.     (if doc
  32.       (vla-regen doc acactiveviewport)
  33.     )
  34.     (if m
  35.       (prompt m)
  36.     )
  37.     (princ)
  38.   )
  39.  
  40.   (defun tttt ( wcs / sysvarpreset sysvarlst sysvarvals ) ;;; wcs (T/nil) ;;; cad, doc, alo, spc - global variables (Visual Lisp main VLA-OBJECT pointers) ;;; vl-load exe cmdfun cmderr catch_cont apply_cadr->car ftoa - library sub functions common for standard template initialization ;;;
  41.  
  42.     (defun vl-load nil
  43.       (or cad
  44.           (setq cad (vlax-get-acad-object))
  45.           (progn
  46.             (vl-load-com)
  47.             (setq cad (vlax-get-acad-object))
  48.           )
  49.         )
  50.       )
  51.       (or doc (setq doc (vla-get-activedocument cad)))
  52.       (or alo (setq alo (vla-get-activelayout doc)))
  53.       (or spc (setq spc (vla-get-block alo)))
  54.     )
  55.  
  56.     ;;; sometimes not needed to use/initialize AxiveX Visual Lisp extensions - (comment/uncomment) following line ;;;
  57.     (or (and cad doc alo spc) (vl-load))
  58.  
  59.     (defun exe ( tokenslist )
  60.       ( (lambda ( tokenslist / ctch )
  61.           (if (vl-catch-all-error-p (setq ctch (cmdfun tokenslist t)))
  62.             (progn
  63.               (cmderr tokenslist)
  64.               (catch_cont ctch)
  65.             )
  66.             (progn
  67.               (while (< 0 (getvar (quote cmdactive)))
  68.                 (vl-cmdf "")
  69.               )
  70.               t
  71.             )
  72.           )
  73.         )
  74.         tokenslist
  75.       )
  76.     )
  77.  
  78.     (defun cmdfun ( tokenslist flag / ctch ) ;;; tokenslist - command parameters list of strings ;;; flag - if "t" specified, upon successful execution returns t, otherwise if "nil" specified, return is always nil no matter what outcome of function execution is - it should be successful anyway if specified tokenslist was hardcoded correctly... ;;;
  79.       (if command-s
  80.         (if flag
  81.           (if (not (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function command-s) tokenslist))))
  82.             flag
  83.             ctch
  84.           )
  85.           (if (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function command-s) tokenslist)))
  86.             ctch
  87.           )
  88.         )
  89.         (if flag
  90.           (if (not (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function vl-cmdf) tokenslist))))
  91.             flag
  92.             ctch
  93.           )
  94.           (if (vl-catch-all-error-p (setq ctch (vl-catch-all-apply (function command) tokenslist)))
  95.             ctch
  96.           )
  97.         )
  98.       )
  99.     )
  100.  
  101.     (defun cmderr ( tokenslist ) ;;; tokenslist - list of tokens representing command syntax at which used (cmdfun) failed with successful execution ;;;
  102.       (prompt (strcat "\ncommand execution failure... error at used command tokenslist : " (vl-prin1-to-string tokenslist)))
  103.     )
  104.  
  105.     (defun catch_cont ( ctch / gr )
  106.       (prompt "\nleft mouse click to continue or enter to generate catch error - ESC to break...")
  107.       (while
  108.         (and
  109.           (vl-catch-all-error-p (or ctch (setq ctch (vl-catch-all-apply (function /) (list 1 0)))))
  110.           (setq gr (grread))
  111.           (/= (car gr) 3)
  112.           (not (equal gr (list 2 13)))
  113.         )
  114.       )
  115.       (if (vl-catch-all-error-p ctch)
  116.         ctch
  117.       )
  118.     )
  119.  
  120.     (defun apply_cadr->car ( sysvarvaluepair / ctch )
  121.       (setq ctch (vl-catch-all-apply (function setvar) sysvarvaluepair))
  122.       (if (vl-catch-all-error-p ctch)
  123.         (progn
  124.           (prompt (strcat "\ncatched error on setting system variable : " (vl-prin1-to-string (vl-symbol-name (car sysvarvaluepair))) " with value : " (vl-prin1-to-string (cadr sysvarvaluepair))))
  125.           (catch_cont ctch)
  126.         )
  127.       )
  128.     )
  129.  
  130.     (defun ftoa ( n / m a s b )
  131.       (if (numberp n)
  132.         (progn
  133.           (setq m (fix ((if (< n 0) - +) n 1e-8)))
  134.           (setq a (abs (- n m)))
  135.           (setq m (itoa m))
  136.           (setq s "")
  137.           (while (and (not (equal a 0.0 1e-6)) (setq b (fix (* a 10.0))))
  138.             (setq s (strcat s (itoa b)))
  139.             (setq a (- (* a 10.0) b))
  140.           )
  141.           (if (= (type n) (quote int))
  142.             m
  143.             (if (= s "")
  144.               m
  145.               (if (and (= m "0") (< n 0))
  146.                 (strcat "-" m "." s)
  147.                 (strcat m "." s)
  148.               )
  149.             )
  150.           )
  151.         )
  152.       )
  153.     )
  154.  
  155.     (setq sysvarpreset
  156.       (list
  157.         (list (quote cmdecho) 0)
  158.         (list (quote 3dosmode) 0)
  159.         (list (quote osmode) 0)
  160.         (list (quote unitmode) 0)
  161.         (list (quote cmddia) 0)
  162.         (list (quote ucsvp) 0)
  163.         (list (quote ucsortho) 0)
  164.         (list (quote projmode) 0)
  165.         (list (quote orbitautotarget) 0)
  166.         (list (quote insunits) 0)
  167.         (list (quote hpseparate) 0)
  168.         (list (quote hpgaptol) 0)
  169.         (list (quote halogap) 0)
  170.         (list (quote edgemode) 0)
  171.         (list (quote pickdrag) 0)
  172.         (list (quote qtextmode) 0)
  173.         (list (quote dragsnap) 0)
  174.         (list (quote angdir) 0)
  175.         (list (quote aunits) 0)
  176.         (list (quote limcheck) 0)
  177.         (list (quote gridmode) 0)
  178.         (list (quote nomutt) 0)
  179.         (list (quote apbox) 0)
  180.         (list (quote attdia) 0)
  181.         (list (quote blipmode) 0)
  182.         (list (quote copymode) 0)
  183.         (list (quote circlerad) 0.0)
  184.         (list (quote filletrad) 0.0)
  185.         (list (quote filedia) 1)
  186.         (list (quote autosnap) 1)
  187.         (list (quote objectisolationmode) 1)
  188.         (list (quote highlight) 1)
  189.         (list (quote lispinit) 1)
  190.         (list (quote layerpmode) 1)
  191.         (list (quote fillmode) 1)
  192.         (list (quote dragmodeinterrupt) 1)
  193.         (list (quote dispsilh) 1)
  194.         (list (quote fielddisplay) 1)
  195.         (list (quote deletetool) 1)
  196.         (list (quote delobj) 1)
  197.         (list (quote dblclkedit) 1)
  198.         (list (quote attreq) 1)
  199.         (list (quote explmode) 1)
  200.         (list (quote frameselection) 1)
  201.         (list (quote ltgapselection) 1)
  202.         (list (quote pickfirst) 1)
  203.         (list (quote plinegen) 1)
  204.         (list (quote plinetype) 1)
  205.         (list (quote peditaccept) 1)
  206.         (list (quote solidcheck) 1)
  207.         (list (quote visretain) 1)
  208.         (list (quote regenmode) 1)
  209.         (list (quote celtscale) 1.0)
  210.         (list (quote ltscale) 1.0)
  211.         (list (quote osnapcoord) 2)
  212.         (list (quote grips) 2)
  213.         (list (quote dragmode) 2)
  214.         (list (quote lunits) 2)
  215.         (list (quote pickstyle) 3)
  216.         (list (quote navvcubedisplay) 3)
  217.         (list (quote pickauto) 3)
  218.         (list (quote draworderctl) 3)
  219.         (list (quote expert) 5)
  220.         (list (quote auprec) 6)
  221.         (list (quote luprec) 6)
  222.         (list (quote pickbox) 6)
  223.         (list (quote aperture) 6)
  224.         (list (quote osoptions) 7)
  225.         (list (quote dimzin) 8)
  226.         (list (quote pdmode) 35)
  227.         (list (quote pdsize) -1.5)
  228.         (list (quote celweight) -1)
  229.         (list (quote cecolor) "BYLAYER")
  230.         (list (quote celtype) "ByLayer")
  231.         (list (quote clayer) "0")
  232.       )
  233.     )
  234.     (setq sysvarlst (mapcar (function car) sysvarpreset))
  235.     (setq sysvarvals (mapcar (function cadr) sysvarpreset))
  236.     (setq sysvarvals
  237.       (vl-remove nil
  238.         (mapcar
  239.           (function (lambda ( x )
  240.             (if (getvar x) (nth (vl-position x sysvarlst) sysvarvals))
  241.           ))
  242.           sysvarlst
  243.         )
  244.       )
  245.     )
  246.     (setq sysvarlst
  247.       (vl-remove-if-not
  248.         (function (lambda ( x )
  249.           (getvar x)
  250.         ))
  251.         sysvarlst
  252.       )
  253.     )
  254.     (setq initvalueslst
  255.       (apply (function mapcar)
  256.         (cons (function list)
  257.           (list
  258.             sysvarlst
  259.             (mapcar (function getvar) sysvarlst)
  260.           )
  261.         )
  262.       )
  263.     )
  264.       (cons (function setvar)
  265.         (list
  266.           sysvarlst
  267.           sysvarvals
  268.         )
  269.       )
  270.     )
  271.     (while (= 8 (logand 8 (getvar (quote undoctl))))
  272.       (if (not (exe (list "_.UNDO" "_E")))
  273.         (if doc
  274.           (vla-endundomark doc)
  275.         )
  276.       )
  277.     )
  278.     (if (not (exe (list "_.UNDO" "_M")))
  279.       (if doc
  280.         (vla-startundomark doc)
  281.       )
  282.     )
  283.     (if wcs
  284.       (if (= 0 (getvar (quote worlducs)))
  285.         (progn
  286.           (setq ucsf
  287.             (list
  288.               (getvar (quote ucsxdir))
  289.               (getvar (quote ucsydir))
  290.               (trans (list 0.0 0.0 1.0) 1 0 t)
  291.             )
  292.           )
  293.           (exe (list "_.UCS" "_W"))
  294.         )
  295.       )
  296.     )
  297.     wcs
  298.   )
  299.  
  300.   (setq wcs (tttt t)) ;;; starting "library" template sub function - initialization ;;;
  301.   (setq ti (car (_vl-times)))
  302.   (while (setq p (getpoint (if lst (car lst) "\nPick point - ENTER TO FINISH : ") "\nPick point - ENTER TO FINISH : "))
  303.     (setq lst (cons p lst))
  304.   )
  305.   ;|
  306.   (exe (list "_.PLINE"))
  307.   (foreach p lst
  308.     (exe (list "_non" p))
  309.   )
  310.   (exe (list "_C"))
  311.   |;
  312.   (if (exe (cons "_.PLINE" (apply (function append) (append (mapcar (function (lambda ( x ) (list "_non" x))) lst) (list (list "_C"))))))
  313.     (prompt "\n(exe) returned T")
  314.     (prompt "\n(exe) returned nil")
  315.   )
  316.   (prompt "\nElapsed time : ") (prompt (ftoa (- (car (_vl-times)) ti))) (prompt " milliseconds...")
  317.   (prompt "\nFor UNDO - type \"UNDO\" - \"Back\" option...")
  318.   (*error* nil)
  319. )
  320.  

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 #10 on: July 16, 2023, 01:00:07 PM »
One more info ab (exe (list tokens))...
It was hardcoded that it returns T upon execution, so it's very similar to (vl-cmdf), only it's little more advanced as it uses (command-s) where it's neccesity... (error handler)

i change command to vl-cmdf,but cant use esc exit lisp

if pause change getpoint use esc exit lisp,but i want see  graphics.

Code: [Select]
(defun c:past_cen(/ 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)

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

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

);while

(princ)
)
(princ)

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: vl-cmdf or command?
« Reply #11 on: July 16, 2023, 03:22:28 PM »
just command work and vl-cmdf work?have any different?
yes, they are different

compare
(command "_line" "0,0" pause (getpoint "\npick third point: ") "")
with
(vl-cmdf "_line" "0,0" pause (getpoint "\npick third point: ") "")

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: vl-cmdf or command?
« Reply #12 on: July 17, 2023, 04:53:56 AM »
From help: vl-cmdf (AutoLISP)

Executes an AutoCAD command
Supported Platforms: Windows and Mac OS
Signature(vl-cmdf [arguments ...])
argumentsType: Integer, Real, String, List, or Ename (entity name)


AutoCAD commands and their options.
The arguments to the vl-cmdf function can be strings, reals, integers, or points, as expected by the prompt sequence of the executed command. A null string ("") is equivalent to pressing Enter on the keyboard. Invoking vl-cmdf with no argument is equivalent to pressing Esc and cancels most AutoCAD commands.

Return ValuesType: T
Always returns T.

RemarksThe vl-cmdf function is similar to the command function, but differs from command in the way it evaluates the arguments passed to it. The vl-cmdf function evaluates all the supplied arguments before executing the AutoCAD command, and will not execute the AutoCAD command if it detects an error during argument evaluation. In contrast, the command function passes each argument in turn to AutoCAD, so the command may be partially executed before an error is detected.

If your command call includes a call to another function, vl-cmdf executes the call before it executes your command, while command executes the call after it begins executing your command.
Some AutoCAD commands may work correctly when invoked through vl-cmdf, while failing when invoked through command. The vl-cmdf function mainly overcomes the limitation of not being able to use getXXX functions inside command.

Note: If you issue vl-cmdf from Visual LISP in AutoCAD on Windows, focus does not change to the AutoCAD window. If the command requires user input, you will see the return value (T) in the Console window, but AutoCAD will be waiting for input. You must manually activate the AutoCAD window and respond to the prompts. Until you do so, any subsequent commands will fail.
ExamplesThe differences between command and vl-cmdf are easier to see if you enter the following calls at the AutoCAD Command prompt:
Command: (command "._line" (getpoint "point?") '(0 0) "")line Specify first point: point?Specify next point or [Undo]:Command: nil
Using command, the LINE command executes first; then the getpoint function is called.
Command: (vl-cmdf "._line" (getpoint "point?") '(0 0) "")point?line Specify first point:Specify next point or [Undo]:Command: T
Using vl-cmdf, the getpoint function is called first (notice the “point?” prompt from getpoint); then the LINE command executes.


The following examples show the same commands, but pass an invalid point list argument to the LINE command. Notice how the results differ:
Command: (command "._line" (getpoint "point?") '(0) "")line Specify first point: point?Specify next point or [Undo]:Command: ERASE nilSelect objects: Specify opposite corner: *Cancel*0 found

The command function passes each argument in turn to AutoCAD, without evaluating the argument, so the invalid point list is undetected.
Command: (vl-cmdf "._line" (getpoint "point?") '(0) "")point?Application ERROR: Invalid entity/point list.nil

Because vl-cmdf evaluates each argument before passing the command to AutoCAD, the invalid point list is detected and the command is not executed.

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: vl-cmdf or command?
« Reply #13 on: July 17, 2023, 08:01:05 AM »
@masao,

I've already told you once before...
This line :
(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cmdecho" "osmode")))
does nothing when executed...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

masao

  • Newt
  • Posts: 97
Re: vl-cmdf or command?
« Reply #14 on: July 17, 2023, 08:42:36 AM »
@masao,

I've already told you once before...
This line :
(setq e_lst (mapcar (function (lambda (n) (list 'setvar n (getvar n)))) '("cmdecho" "osmode")))
does nothing when executed...

if i use ESC exit lisp,how to recovery osmode and else system variable? i usually use this and it can recovery.
« Last Edit: July 17, 2023, 09:01:04 AM by masao »

ribarm

  • Gator
  • Posts: 3310
  • 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: 3310
  • 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: 3310
  • 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: 10664
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: 10664
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: 3310
  • 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.