Lessons

Basic Programming & Control Flow

Today, we’re diving deep into the realm of Basic Programming and control Flow in AutoLISP. This lesson will empower you to develop structured scripts and programs, manage data flow, and make conditional decisions within your CAD designs. By the end of this session, you will have the foundational knowledge to code efficiently and handle data seamlessly in AutoLISP.


Learning Outcomes

By the close of this lesson, you will be able to:

  • Define and declare functions using defun and function.
  • Manage control flow with conditional statements such as if and cond.
  • Group multiple expressions using progn.
  • Implement loops using repeat and foreach.
  • Design and structure your CAD scripts for optimal performance and clarity.

Functions: The Building Blocks of AutoLISP

defun

Definition: defun is used to define a new function in AutoLISP.

Syntax:

(defun function_name (arguments) …body…)

Examples:

  • Simple Function without arguments:
(defun greet ()
(print “Hello, World!”)
)

This function, when called with (greet), will print “Hello, World!”.

  • Function with arguments:
(defun add (x y)
(+ x y)
)

Using (add 3 4), will return 7.

  • Nested function calls:
(defun greetWithName (name)
(strcat “Hello, ” name “!”)
)
(defun greetUser ()
(print (greetWithName “Alice”))
)

Calling (greetUser) will print “Hello, Alice!”.


Conditional Statements: Directing the Flow

if

Definition: The if function evaluates a condition and executes the consequent expression if the condition is true, and the alternate expression if provided and the condition is false.

Syntax:

(if condition then_expression [else_expression])

Examples:

  • Basic if without an else:
(defun isEven (n)(if (= (rem n 2) 0)
(print “Even”)
)
)

(isEven 4) will print “Even”, but (isEven 3) won’t print anything.

  • if with an else:
(defun describeNumber (n)
(if (> n 0)
(print “Positive”)
(print “Non-Positive”)
)
)

(describeNumber 5) will print “Positive”, while (describeNumber -3) will print “Non-Positive”.

  • Nested if statements:
(defun describeAge (age)
(if (< age 13)
(print “Child”)
(if (< age 20)
(print “Teenager”)
(print “Adult”)
)
)
)

(describeAge 15) will print “Teenager”, while (describeAge 25) will print “Adult”.


cond

Definition: The cond function is a multi-way decision statement, similar to a switch-case in other programming languages.

Syntax:

(cond (test1 result1) (test2 result2) … (testn resultn))

Examples:

  • Basic cond example:
(defun describeTemp (temp)
(cond
((< temp 0) (print “Freezing”))
((< temp 10) (print “Cold”))
((< temp 20) (print “Cool”))
(t (print “Warm”))
)
)

(describeTemp 5) will print “Cold”.

  • Using cond for mathematical operations:
(defun mathOperation (op x y)
(cond
((= op 1) (+ x y))
((= op 2) (- x y))
((= op 3) (* x y))
((= op 4) (/ x y))
(t “Invalid Operation”)
)
)

(mathOperation 1 3 4) will return 7, while (mathOperation 5 3 4) will return “Invalid Operation”.

  • Nested cond expressions:
(defun advancedDescription (age height)
(cond
((and (< age 20) (< height 160)) (print “Young and short”))
((or (> age 30) (> height 180)) (print “Old or tall”))
(t (print “Average”))
)
)

(advancedDescription 18 155) will print “Young and short”, while (advancedDescription 25 185) will print “Old or tall”.


Grouping Expressions: progn

Definition: progn allows multiple expressions to be grouped together and evaluated in sequence, returning the value of the last expression.

Syntax:

(progn expr1 expr2 … exprn)

Examples:

  • Basic usage of progn:
(progn
(print “Starting…”)
(setq x 5)
(setq y 10)
(+ x y)
)

This will print “Starting…” and then evaluate the expressions, returning 15.

  • Using progn within if:
(if (> 5 3)
(progn
(print “5 is greater than 3”)
(setq z 15)
)
)

This will print the message and set the value of z to 15.

  • Using progn within a function definition:
(defun computeArea (l w)
(progn
(print “Computing area…”)
(* l w)
)
)

(computeArea 4 5) will print “Computing area…” and return 20.


Looping Constructs: Repeating Tasks

repeat

Definition: The repeat function executes an expression a specified number of times.

Syntax:

(repeat count expr)

Examples:

  • Simple repeat loop:
(setq sum 0)
(repeat 5
(setq sum (+ sum 1))
)

After execution, the value of sum will be 5.

  • Using repeat to compute factorial:
(defun factorial (n)
(setq result 1)
(repeat n
(setq result (* result n))
(setq n (- n 1))
)
result
)

(factorial 5) will return 120.

  • repeat for string repetition:
(defun repeatString (s times)
(setq result “”)
(repeat times
(setq result (strcat result s))
)
result
)

(repeatString “Hi” 3) will return “HiHiHi”.


foreach

Definition: The foreach function iterates over each item in a list and applies an expression to it.

Syntax:

(foreach var list expr)

Examples:

  • Basic usage of foreach:
(setq sum 0)
(foreach item ‘(1 2 3 4 5)
(setq sum (+ sum item))
)

After execution, sum will be 15.

  • Using foreach to concatenate strings:
(setq result “”)
(foreach str ‘(“a” “b” “c”)
(setq result (strcat result str))
)

After this, result will be “abc”.

  • Using foreach with nested lists:
(setq flattenedList ‘())
(foreach sublist ‘((1 2) (3 4) (5 6))
(foreach item sublist
(setq flattenedList (append flattenedList (list item)))
)
)

This will create a flattenedList containing (1 2 3 4 5 6).


Conclusion

With the understanding of Basic Programming & Control Flow in AutoLISP, you’re now equipped to script intricate routines, manage data flow, and make effective decisions within your CAD projects. These tools, though basic, form the foundation of any advanced program you’ll write in the future. Always remember: the key to mastery is practice. So, keep experimenting, write your programs, and let the power of AutoLISP elevate your CAD designs to the next level!