Bill Tillman 发表于 2022-7-5 16:18:31

程序循环

我正在使用一个小的LISP程序来获取两个输入点并处理它们。
 
我希望代码一直重复,直到用户完成。我认为while循环是有序的,但当我将while放置在第一个输入点的上方或下方时,我没有得到预期的结果。我需要代码来重置OSMODE和PICKBOX变量,当它完成时,但按下escape不允许这样做,除非我做一些更多的错误捕捉。如果我将while放在第一个输入之后,代码会一直尝试获取第二个点。如果用户在第一次输入时按enter键,我希望代码能够干净地退出。

tombu 发表于 2022-7-5 16:37:56

错误捕捉对于任何需要用户输入的lisp都很有用。
 
我们需要看看其余的代码,看看如何最好地循环它,以及在一个*错误*局部函数中放入什么。

Bill Tillman 发表于 2022-7-5 16:49:41

这是完整的代码。这是一个快速的工具,可以绘制围栏纠察桩,并沿给定长度的围栏等距分布。该代码工作正常,但现在它只工作一次,用户必须再次启动它才能执行另一段围栏。我想让它工作,让它运行,然后继续运行,只要用户选择一个新的第一点。如果他们按回车键,我希望它能干净地退出。我有一个*错误*捕获例程,它将处理用户按escape键。运行时很容易加载。我正在寻找的技巧是,当被要求输入点时,如何通过按Enter键完全退出。
 

(vl-load-com)

(defun C:pickets (/ picketthk picketoc)

(setq *current_osmode (getvar 'OSMODE)
   *current_pickbox (getvar 'PICKBOX)
   )

;;; Set up common variables
(setq picketthk 0.75
   picketoc 4.
   )

(prompt "\nSelect First Point\n")
(setq p1 (getpoint))
(prompt "\nSelect Second Point\n")
(setq p2 (getpoint))

(setvar 'OSMODE 0)
(setvar 'PICKBOX 0)
(setq    d (distance p1 p2)
   numpickets (/d picketoc)   
   firstbay (+ (/ (- d (* (fix numpickets) picketoc)) 2.) (/ picketthk 2.))
   )

(if (< firstbay (+ (/ picketoc 2.) (/ picketthk 2.)))
   (setq firstbay (+ firstbay (- (/ picketoc 2.) (/ picketthk 2.))))
   )

;;; if the number pf pickets divided by picketoc is has no remainder subtract 1 picket and increase firstbay dimension
(if (= (/ d (fix numpickets)) (fix (/ d (fix numpickets))))
   (setq numpickets (1- numpickets)
   firstbay (+ firstbay (- picketoc (/ picketthk 2.)))
   )
   )

(setq count 0)
(repeat (fix numpickets)
   (DrawPicket "picket" (polar p1 0 (+firstbay (* picketoc count))))
   (setq count (1+ count))
   )

(setvar 'OSMODE *current_osmode)
(setvar 'PICKBOX *current_pickbox)
(princ)
); end pickets

(defun DrawPicket (PicketBlk pt)
(entmakex (list
         (cons 0 "INSERT")
         (cons 2 PicketBlk)
         (cons 8 "Pickets")
         (cons 10 pt)
         (cons 41 1.0) ; Scale Factor
         (cons 42 1.0) ; Scale Factor
         (cons 43 1.0) ; Scale Factor
         )
       )
(princ)
); end DrawPicket

Grrr 发表于 2022-7-5 17:16:35

我猜是这样的(很快修改):
 


(vl-load-com)

(defun C:pickets (/ *error* picketthk picketoc p1 p2 d numpickets firstbay count )

(defun *error* ( m )
   (and vars (mapcar 'setvar '(osmode pickbox) vars))
   (princ m) (princ)
)

(setq vars (mapcar 'getvar '(osmode pickbox)))
;;; Set up common variables
(setq
   picketthk 0.75
   picketoc 4.
)

(while
   (and
   (setq p1 (getpoint "\nSelect First Point: "))
   (setq p2 (getpoint p1 "\nSelect Second Point: "))
   )
   (mapcar 'setvar '(osmode pickbox) '(0 0))
   (setq
   d (distance p1 p2)
   numpickets (fix (/ d picketoc))
   firstbay (+ (/ (- d (*numpickets picketoc)) 2.) (/ picketthk 2.))
   )
   
   (if (< firstbay (+ (/ picketoc 2.) (/ picketthk 2.)))
   (setq firstbay (+ firstbay (- (/ picketoc 2.) (/ picketthk 2.))))
   )
   
   ;;; if the number pf pickets divided by picketoc is has no remainder subtract 1 picket and increase firstbay dimension
   (if (= (/ d (fix numpickets)) (fix (/ d numpickets)))
   (setq numpickets (1- numpickets)
       firstbay (+ firstbay (- picketoc (/ picketthk 2.)))
   )
   )
   
   (repeat numpickets
   (DrawPicket "picket" (polar p1 0 (+firstbay (* picketoc numpickets))))
   (setq numpickets (1- numpickets))
   )
   
   (mapcar 'setvar '(osmode pickbox) vars)
   
); while
(princ)
); end pickets

(defun DrawPicket (PicketBlk pt)
(entmakex
   (list
   (cons 0 "INSERT")
   (cons 2 PicketBlk)
   (cons 8 "Pickets")
   (cons 10 pt)
   (cons 41 1.0) ; Scale Factor
   (cons 42 1.0) ; Scale Factor
   (cons 43 1.0) ; Scale Factor
   )
)
); end DrawPicket

BIGAL 发表于 2022-7-5 17:26:20

Grr打败了我我经常用这个
3
页: [1]
查看完整版本: 程序循环