The Simplest Program Branch- the (if) Function with autolisp in AutoCAD

Branching is a fundamental part of programming. Based on the conditional statements from the previous topic, the (if) function creates a two-pronged branch: “if the condition is true, then do this; otherwise, do that.” The structure of the (if) function permits only one test. To evaluate multiple expressions, use the (progn) function.

Explanation: the (if) function

(if test-expression then-expression [else-expression])

The test-expression is the conditional expression; if it is non-nil, the then- expression is evaluated. If it is nil, the optional else-expression is evaluated if present, and program flow continues below the (if) statement.

Explanation: the (progn) function

(progn expression1 … expressionn)

The (progn) function evaluates each expression in order and returns the value of the last expression. To the (if) function, the series of expressions appears as only one.

Example

(defun c:Larger ()
      (setq A (getint “\nEnter an integer: “))
      (setq B (getint “\nEnter another integer: “))
      (if (< A B)
            (progn
                   (prompt “\nThe second number is larger.”)
                  (prompt “\nAnd the difference is “)
                  (prompt (itoa (- B A)))
                 (princ)
            )
(progn
             (prompt “\nThe second number is larger.”)
            (prompt “\nAnd the difference is “)
            (prompt (itoa (- B A)))
           )
)

)

  • A complete solution to this example is on your class disk as LARGER.LSP.
See also  AutoCAD and AutoLISP are Two Separate Programs

PRACTICE

Add an (if) statement to the TAG.LSP practice that will check to see if the layer KEYNOTE exists. If it does exist then set it current and if it does not exist create it. This requires a test expression, (tablesearch “layer” “keynote”) that we will cover more in-depth later.

When you have completed the routine, open a new drawing based on the PROTOMECH.DWT template and test it. Estimated time for completion: 10 minutes.

(defun c:TAG ()
(setq CE-SAVE (getvar “cmdecho”))
(setvar “cmdecho” 0)
(setq CLAYER-SAVE (getvar “clayer”))
(if (tblsearch “layer” “keynote”)
     (setvar “clayer” “keynote”)
     (progn (command “-layer” “n” “keynote” “c” “cyan” “keynote” “”)
     (setvar “clayer” “keynote”)
     ) ;_ end of progn
) ;_ end of if
(setq TAGNUMBER (getint “\nTag starting number: “))
(while (setq PT1 (getpoint “\nPick the leader endpoint [Press Enter to finish]: ”
) ;_ end of getpoint
) ;_ end of setq
(setq PT2 (getpoint PT1 “\nPick the tag center point: “))
(setq RADIUS (/ (getvar “dimscale”) 4.0))
(setq ANGLE1 (angle PT2 PT1))
(setq PT3 (polar PT2 ANGLE1 RADIUS))
(command “line” PT1 PT3 “” “circle” PT2 RADIUS
“text” “m” PT2 (* (getvar “dimscale”) 0.25)0 (itoa TAGNUMBER)
) ;  end of command
(setq TAGNUMBER (1+ TAGNUMBER)) )
; end of while
(setvar “cmdecho” CE-SAVE) (setvar “clayer” CLAYER-SAVE) (redraw)
(princ)
) ;_ end of defun

  • A complete solution to this exercise is on your class disk as TAG-C.LSP.
See also  Every List is Evaluated in the Same Specific Manner in Autolisp
Back to top button