下面是一个简单的示例,并附上一些注释:
- (defun C:test ( / lst p1 p2 )
- (setq lst (list)) ; construct a empty list to store the points
- (initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
- (setq n (getint "\nInput number of points"))
- (repeat n
- (setq p1 (getpoint "\nSpecify point"))
- (setq lst (cons p1 lst)) ; add the point to the list
- ); end of repeat, all points are stored in the list
- (command "_.pline"
- (foreach pt lst (command "_non" pt)) ; draw each point from the list
- ) ; draw the polyline
- (princ) ; exit silently
- );defun
编辑1:这里有一点更复杂(因此例程将更加用户友好):
- (defun C:test ( / lst p1 )
- (setq lst (list)) ; construct a empty list to store the points
- (initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
- (setq n (getint "\nInput number of points"))
- (repeat n
- (if (not p1) ; check if there p1 does not exist
- (setq p1 (getpoint "\nSpecify first point")) ; ask for p1 if it does not exist
- (setq p1 (getpoint "\nSpecify next point" (last (reverse lst)))) ; to display the vector from previous point
- )
- (setq lst (cons p1 lst)) ; add the point to the list
- ); end of repeat, all points are stored in the list
- (command "_.pline"
- (foreach pt lst (command "_non" pt)) ; draw each point from the list
- ) ; draw the polyline
- (princ) ; exit silently
- );defun
编辑2:根据我从塔瓦的代码中学到的,这里甚至更复杂:
- (defun C:test ( / oldcmd lst p1 p2 col )
- (setq oldcmd (getvar 'cmdecho)) ; store this variable
- (setq lst (list)) ; construct a empty list to store the points
- (initget (+ 1 2 4)) ; disallow in the prompt: pressing enter, zero or negative value
- (if
- (setq n (getint "\nInput number of points: ")) ; ask for the number of points
- (progn ; if we get the number of points, do all the stuff within the progn function
- (setvar 'cmdecho 0) ; set this variable to 0, to avoid any useless prompts
- (repeat n
- (if (not p1) ; check if there p1 does not exist
- (setq p1 (getpoint "\nSpecify first point")) ; ask for p1 if it does not exist
- (progn
- (setq p1 (getpoint "\nSpecify next point" (setq p2 (last (reverse lst))))) ; to display the vector from previous point
- (grdraw p1 p2 (if (not col) (setq col 1) (setq col (+ col 1))) 0) ; visualize the segments, with some colors
- )
- )
- (setq lst (cons p1 lst)) ; add the point to the list
- ); end of repeat, all points are stored in the list
- (command "_.pline"
- (foreach pt lst (command "_non" pt)) ; draw each point from the list
- ) ; draw the polyline
- (redraw) ; this is used as "regen" for the grdraw function
- (setvar 'cmdecho oldcmd) ; reset the variable
- ); end of progn
- );if
- (princ) ; exit silently
- );defun
这是我第一次使用grdraw函数,所以我发现他的代码是一个很好的例子。 |