下面是另一种方法,通过一些注释来帮助解释代码的作用:
- [color=green];; This is a keyboard shortcut[/color]
- (defun c:RECA () (c:RectangleArea))
- [color=green];; This is the main function[/color]
- (defun c:RectangleArea ( / *error* oldCmdecho pt1 pt2 x y)
- [color=green] ;; This loads the visual lisp library, and makes ActiveX available[/color]
- (vl-load-com)
- [color=green] ;; This displays the command name at the command line.[/color]
- (princ "\rRECTANGLE+AREA ")
- [color=green] ;; This is an error handler which is only in use during this function.[/color]
- (defun *error* (msg)
- [color=green] ;; In the event of an error, this will restore the original[/color]
- [color=green] ;; system variable setting, *IF* that variable is defined.[/color]
- (and oldCmdecho (setvar 'cmdecho oldCmdecho))
- (cond ((not msg)) [color=green] ; Normal exit[/color]
- ((member msg '("Function cancelled" "quit / exit abort")))[color=green] ; <esc> or (quit)[/color]
- ((princ (strcat "\n** Error: " msg " ** ")))) [color=green] ; Fatal error, display it[/color]
- (princ))
- [color=green] ;; This stores the current system variable value, and sets it[/color]
- [color=green] ;; to not echo the command prompts that would normally be shown.[/color]
- (and (setq oldCmdecho (getvar 'cmdecho)) (setvar 'cmdecho 0))
- (if (and (setq pt1 (getpoint "\nPick top corner :"))
- [color=#008000] ;; This dashes the selection line from pt1[/color]
- (not (initget 32))
- (setq pt2 (getcorner pt1 "\nPick bottom corner :"))
- [color=green] ;; vl-cmdf returns T, whereas command returns NIL.[/color]
- [color=green] ;; Using command would prevent your code from completing the[/color]
- [color=green] ;; "AND" statement.[/color]
- (vl-cmdf "_.rectang" pt1 pt2))
- [color=green] ;; Here I've combined several steps into one for simplicity.[/color]
- (princ
- (strcat
- "\nLength of x is : "
- (rtos (setq x (abs (- (car pt2) (car pt1)))))
- "\nLenght of y is : "
- (rtos (setq y (abs (- (cadr pt2) (cadr pt1)))))
- "\nArea of rec is : "
- (rtos (/ (* x y) 12)))))
- [color=green];; If the code performs normally, and no errors (or Esc),[/color]
- [color=green] ;; this will restore the original system variable setting.[/color]
- (setvar 'cmdecho oldCmdecho)
- (princ))
希望这有帮助! |