autolisp / visual lisp/ visual lisp - part 1part 1 · pdf fileautolisp® / visual lisp/...

77
AutoLISP ® / Visual LISP ® - Part 1 AutoLISP / Visual LISP Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert Green

Upload: lamthien

Post on 10-Feb-2018

456 views

Category:

Documents


38 download

TRANSCRIPT

Page 1: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

AutoLISP® / Visual LISP ® - Part 1AutoLISP / Visual LISP Part 1

Robert GreenCAD-Manager.com

AutoLISP/Visual LISP Part 1Robert Green

Page 2: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

My background …ME degree from Georgia TechAutoCAD user since 1985AutoCAD user since 1985AutoLISP programmer since 1990Cadalyst columnist since 1998Cadalyst columnist since 1998Self confessed computer geekComing to you from Atlanta GA so …Coming to you from Atlanta GA so …

AutoLISP/Visual LISP Part 1Robert Green

Page 3: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Let’s learn some LISP y’all!LISP y all!

AutoLISP/Visual LISP Part 1Robert Green

Page 4: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Course guide (password = LISP1)

AutoLISP/Visual LISP Part 1Robert Green

Page 5: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Asking a question …

AutoLISP/Visual LISP Part 1Robert Green

Page 6: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Some fundamentalsLISP – LISt ProcessorEverything is in lists

AutoLISP/Visual LISP Part 1Robert Green

Page 7: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Some history … LISP is an old languageIt was open source (cheap)It was open source (cheap)It made sense for AutoCAD because AutoCAD entitles are listsIt runs without compiling (simple)It made sense at the time (cheap) …

AutoLISP/Visual LISP Part 1Robert Green

Page 8: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Some advice … AutoLISP is still very valid!A HUGE base of LISP code existsA HUGE base of LISP code existsLISP shareware is abundantLISP does many things very easily as y g y ycompared to VB/VBALISP is weak on dialog controlsLISP is weak on graphical interfacesView it as an 80/20 language …

AutoLISP/Visual LISP Part 1Robert Green

Page 9: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Key files and why they matter … LSPLoad in orderLoad in orderWhat to mess withWhat not to!What not to!

AutoLISP/Visual LISP Part 1Robert Green

Page 10: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

All those wacky file names … ACAD20XX.LSP (XX is version)ACAD.LSPACAD.LSPACAD20XXDOC.LSP (XX is version)ACADDOC.LSPMENUNAME.MNLHelp …p

AutoLISP/Visual LISP Part 1Robert Green

Page 11: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

What do they do … They load on startup of AutoCADThey load in a certain order (listed onThey load in a certain order (listed on previous slide)Some have code in them and some don’t (ACADDOC.LSP and ACAD.LSP don’t as an example)They reside in the SUPPORT folder …

AutoLISP/Visual LISP Part 1Robert Green

Page 12: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

So what should I do … Use ACADDOC.LSP to get startedYou create your own ACADDOC.LSP soYou create your own ACADDOC.LSP so you can’t really mess it upIt loads with every new drawingy gPut in in the SUPPORT folder and start hacking awayIf you mess up too bad, just delete!

AutoLISP/Visual LISP Part 1Robert Green

Page 13: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Syntax Basics Lists and ArgumentsRules of AutoLISPVariablesFunctionsAccessing the command lineSpecial charactersUser inputUser input

AutoLISP/Visual LISP Part 1Robert Green

Page 14: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Lists and Arguments (+ 20 30)

Here the + is a FUNCTION and the two numbers are ARGUMENTS

(command “line” “0,0” “1,1” “”) Here COMMAND is the function all others are ARGUMENTSHere COMMAND is the function, all others are ARGUMENTS

AutoLISP/Visual LISP Part 1Robert Green

Page 15: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Some Rules“For every ( there is an equal and opposite )” – Isaac Newton

Like this: (setq A 3.0)Not like this: (setq A 3.0)) or (setq A 3.0( q )) ( q

Same goes for quote marks! Like this: (setq name “Robert Green”)N t lik thi ( t “R b t G )Not like this: (setq name “Robert Green)

(setq name Robert Green”)

When formatting numbers always avoid “invalid dotted pairs”When formatting numbers always avoid invalid dotted pairsLike this: (setq A 0.5)Not like this: (setq A .5)

AutoLISP/Visual LISP Part 1Robert Green

Page 16: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

The New Math Standard AutoLISP Result8 + 4 (+ 8 4) 128 4 ( 8 4) 128 + 2.3 (+ 8 2.3) 10.35.0 ÷ 4.0 (/ 5.0 4.0) 1.25( )5 ÷ 4.0 (/ 5 4.0) 1.255.0 ÷ 4 (/ 5.0 4) 1.25( )5 ÷ 4 (/ 5 4) 1

AutoLISP/Visual LISP Part 1Robert Green

Page 17: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

The New Math (con’t) Standard AutoLISP Result(8 + 4) * 3 (* (+ 8 4) 3) 36(8 4) 3 ( ( 8 4) 3) 36

(3.0 * 5.0) ÷ 4.0 (/ (* 3.0 5.0) 4.0) 3.75( ) ( ( ) )

((4 + 4) * 3) ÷ 2 (/ (* (+ 4 4) 3) 2) 12(( ) ) ( ( ( ) ) )

AutoLISP/Visual LISP Part 1Robert Green

Page 18: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

The Command Line How would you work normally?Draw a line between two user points

Type in LINE to start the line commandClick POINT for first point locationCli k POINT f d i t l tiClick POINT for second point locationType in ENTER to halt the command

In AutoLISP: (command “line” pause pause “”)PAUSE instruction waits for the userPAUSE instruction waits for the userThe “” is equal to hitting the ENTER key. Why? Because that’s they way it is …

AutoLISP/Visual LISP Part 1Robert Green

Page 19: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

What’s Going On Here?(command “viewres” “y” “5000”) (command “-color” “BYLAYER”)(command color BYLAYER )(command “-linetype” “set” “BYLAYER” “”)(command “menu” “menuname.mnc”) ( )(command “viewres” “y” pause)

That’s not so bad …intuitive actually …

AutoLISP/Visual LISP Part 1Robert Green

Page 20: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Variables and Data Types …RealIntegerStringUser input

AutoLISP/Visual LISP Part 1Robert Green

Page 21: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Variables and data typesSETQ (SET eQual)(setq VALUE 12.7) – real number(setq VALUE 12.7) real number(setq VALUE 1) – integer number(setq VALUE “Happy”) – string variable( q ppy ) g(setq VALUE 1.0) - ?(setq VIEWRES_VALUE 5000) - ?( q _ )

AutoLISP/Visual LISP Part 1Robert Green

Page 22: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Variables and Commands(setq VIEWRES_VALUE 5000)

(command “viewres” “y” viewres value) ( y _ )What would happen in this case?

(setq VIEWRES_VALUE “Happy Bday!”)(command “viewres” “y” viewres_value) What would happen in this case?

V i bl TYPES d h th i t t ithVariable TYPES and how they interact with commands are key …

AutoLISP/Visual LISP Part 1Robert Green

Page 23: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

User Input(getdist “\nInput a distance: ”) (getreal “\nInput a real number: ”)(getreal \nInput a real number: )(getint “\nInput number of items: ”) (getstring “\nInput text: ”) (g g p )What’s the \n – a line feedWhy? yJust because …

AutoLISP/Visual LISP Part 1Robert Green

Page 24: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

User Input (reference handout)(setq user_dist (getdist “\nInput a distance: ”)) (setq user_real (getreal “\nInput a real number: ”)) ( t i t ( ti t “\ I t i t b ”))(setq user_int (getint “\nInput an integer number: ”))(setq user_string (getstring “\nInput text: ”))(setq wheel dia (getdist “\nEnter wheel diameter: ”))( q _ (g ))(setq spokes (getint “\nEnter number of spokes: ”))

Get the ideaGet the idea …

AutoLISP/Visual LISP Part 1Robert Green

Page 25: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

User functions Speed for the userLower support for youA win-win scenarioLet’s put everything we’ve l d i t ti t b ildlearned into action to build some functions.

AutoLISP/Visual LISP Part 1Robert Green

Page 26: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

User Function Examples

(defun C:ZA ()( ()(command “.zoom” “a”)(princ)(p )

)

AutoLISP/Visual LISP Part 1Robert Green

Page 27: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

User Function Examples(defun C:VR ()

(command “viewres” “y” “5000”) ))

(defun C:BL ()(defun C:BL ()(command “-color” “BYLAYER”) (command “-linetype” “set” “BYLAYER” “”) (princ)

)

AutoLISP/Visual LISP Part 1Robert Green

Page 28: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Fillet Zero FunctionFillet Zero(defun c:fz () ( ()(setvar “filletrad” 0.0)(command “.fillet” pause pause)( p p )(princ)

))

AutoLISP/Visual LISP Part 1Robert Green

Page 29: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Improved Fillet Zero (defun c:fz () (setq old filletrad (getvar “filletrad”))( q _ (g ))(setvar “filletrad” 0.0) (command “.fillet” pause pause) ( p p )(setvar “filletrad” old_filletrad)(princ)(p )

)

AutoLISP/Visual LISP Part 1Robert Green

Page 30: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Auto Purge FunctionAuto Purge(defun c:atp () ( p ()(command “-purge” “a” “*” “n” “.qsave”)(princ)(p )

)

AutoLISP/Visual LISP Part 1Robert Green

Page 31: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

More Bits and Bytes …y

UndefineDot formRedefine Al tAlertsCMDECHO

AutoLISP/Visual LISP Part 1Robert Green

Page 32: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Undefining … (command “.undefine” “LINE”)(command “.undefine” “TORUS”)( )Don’t want them messing with a command? Just undefine it …

Now you can SUBTRACT from the AutoCAD yCommand set in your ACADDOC.LSP file.

AutoLISP/Visual LISP Part 1Robert Green

Page 33: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

The DOT form … Invoke commands like this: .LINENote the dot “.” character?This allows you to invoke a command whether it has been undefined or not!This is our little secret right ...

AutoLISP/Visual LISP Part 1Robert Green

Page 34: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Redefining … (command “.redefine” “LINE”)(command “.redefine” “TORUS”)( )Want to be sure that a command is active?Just redefine it …

Now you can UNSUBTRACT from the yAutoCAD Command set with ease.

AutoLISP/Visual LISP Part 1Robert Green

Page 35: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Undefining revisted … What if your users find out about REDEFINE and start REDEFINING your UNDEFINES?yJust undefine the redefine like this:(command “.undefine” “REDEFINE”)That’ll show ‘em …

AutoLISP/Visual LISP Part 1Robert Green

Page 36: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Redefining … You can undefine a command and redefine it like this:(command “.undefine” “TORUS”)(defun C:TORUS ()(alert “Don’t use that command!”)(princ)

)

Now you do whatever you want!

AutoLISP/Visual LISP Part 1Robert Green

Page 37: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

What Does This Do?(command “.undefine” “QSAVE”)(defun c:qsave () ( q ()(command “-purge” “b” “*” “n”)(command “.qsave”)( q )(princ)

))

AutoLISP/Visual LISP Part 1Robert Green

Page 38: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Alerting the user … You can send a message to the user like this:

(alert “Message goes here”)

AutoLISP/Visual LISP Part 1Robert Green

Page 39: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Command echo (CMDECHO) Run in STEALTH mode like this:(defun C:BL ()(setvar “cmdecho” 0) (command “-color” “BYLAYER”)( d “ li t ” “ t” “BYLAYER” “”)(command “-linetype” “set” “BYLAYER” “”)

(setvar “cmdecho” 1)(princ)(princ))

AutoLISP/Visual LISP Part 1Robert Green

Page 40: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Conditional operators These functions allow your program to “think” y p gand “react” to user input.

Loops and branches formLoops and branches form the basis for the conditional operators we’llconditional operators we ll look at.

AutoLISP/Visual LISP Part 1Robert Green

Page 41: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

IF – it all starts here …

(IF (TEST EXPRESSION)(IF (TEST EXPRESSION)(TRUE CASE)(FALSE CASE)( )

)

AutoLISP/Visual LISP Part 1Robert Green

Page 42: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

IF – Pseudo Code … A user is prompted for an integer variable called SPOKESIf the variable has a value less than 20 then print “LESS THAN 20” to the screenIf the variable has a value greater than or equal to 20 then print “MORE THAN 20” to ththe screen …

AutoLISP/Visual LISP Part 1Robert Green

Page 43: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

IF – Real Code(setq spokes (getint “\nEnter number of spokes: “)) (if (< spokes 20)

(alert “LESS THAN 20”) ; true case(alert “MORE THAN 20”) ; false case

))

Note how a decision is made using a branching logicNote how a decision is made using a branching logic structure …

AutoLISP/Visual LISP Part 1Robert Green

Page 44: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

COND – IF on steroids

(COND( (TEST EXPRESSION 1) (OUTCOME) )( (TEST EXPRESSION 2) (OUTCOME) )( (TEST EXPRESSION 3) (OUTCOME) )( (TEST EXPRESSION 3) (OUTCOME) )

)

AutoLISP/Visual LISP Part 1Robert Green

Page 45: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

COND – Pseudo Code … A user is prompted for an integer variable called SPOKESIf the variable has a value less than 10 then print “SMALL WHEEL” to the screenIf the variable is between 10 and 15 printIf the variable is between 10 and 15 print “MEDIUM WHEEL” If the variable is greater than 15 print g p“LARGE WHEEL” …

AutoLISP/Visual LISP Part 1Robert Green

Page 46: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

COND – Real Code(setq spokes (getint “\nEnter number of spokes: “))(cond( (< spokes 10) (alert “SMALL WHEEL”) )( (< spokes 10) (alert SMALL WHEEL ) )( (and (>= spokes 10) (<= spokes 15)) (alert “MEDIUM WHEEL”)

))( (> spokes 15) (alert “LARGE WHEEL”) )

)

This is just like an IF but with more than two possible logic branches …

AutoLISP/Visual LISP Part 1Robert Green

Page 47: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

WHILE – Looping logic

(OBTAIN VARIABLE)(WHILE (TEST EXPRESSION ON VARIABLE)

(Statement 1).

(Statement N)))

AutoLISP/Visual LISP Part 1Robert Green

Page 48: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

WHILE – Pseudo Code Prompt the user for a number of spokes but the number of spokes needs to be no less pthan 20 for our purposes. We need to make sure the user gives us a valid integer number greater than or equal to 20 …

AutoLISP/Visual LISP Part 1Robert Green

Page 49: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

WHILE – Real Code

(setq spokes 0)(while (< spokes 20)(setq spokes (getint “\nEnter number of spokes: “))

))

The WHILE function loops around until the test case is satisfied …

AutoLISP/Visual LISP Part 1Robert Green

Page 50: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Let’s Try a Problem Using the concepts we’ve learned so far for branching gand looping logic.

We’ll use the pseudo codeWe ll use the pseudo code approach to build our logic as we go.as we go.

AutoLISP/Visual LISP Part 1Robert Green

Page 51: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Example Problem (Pseudo Code)Prompt the user for a number of spokesBe sure they give you at least 8 spokes but not more than 24 spokesWrite SMALL MEDIUM or LARGE to the screen based on the rules we used in the CONDbased on the rules we used in the COND example

Hint: Use WHILE to qualify the input, use COND to evaluate the cases …

AutoLISP/Visual LISP Part 1Robert Green

Page 52: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Example Problem (Real Code)(setq spokes 0)(while (or (< spokes 8) (> spokes 24))(setq spokes (getint “\nEnter number of spokes [from 8 to 24]: “))

))

(cond( (< spokes 10) (alert “SMALL WHEEL”) )( ( p ) ( ) )( (and (>= spokes 10) (<= spokes 15)) (alert “MEDIUM WHEEL”) )( (> spokes 15) (alert “LARGE WHEEL”) )

)

AutoLISP/Visual LISP Part 1Robert Green

Page 53: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Points and Lists How do points work?

H dHow can we decompose points and put them back together again?together again?

AutoLISP/Visual LISP Part 1Robert Green

Page 54: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Get some points like this … (setq pt1 (getpoint “\nSelect a point: “))(setq pt2 (getpoint “\nSelect a point: “))(setq pt2 (getpoint \nSelect a point: ))

You now have two 3D points stored in the pvariables PT1 and PT2 …

AutoLISP/Visual LISP Part 1Robert Green

Page 55: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

CAR, CADR and CADDR … Decompose points into their X, Y and Z coordinates like this:

The X value of PT1 would be: (car pt1)The Y value of PT1 would be: (cadr pt1)The Z value of PT1 would be: (caddr pt1)The Z value of PT1 would be: (caddr pt1)

Why? CAR and CDR concatenationWhy? CAR and CDR concatenation Just memorize these functions …

AutoLISP/Visual LISP Part 1Robert Green

Page 56: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

LIST: Pseudo Code …You’d like to draw a circle with an origin point that is at the X coordinate of PT1 and the Y and Z coordinate of PT2 (points supplied by the user). Further, the radius of the circle should have a value of 11.5 …

AutoLISP/Visual LISP Part 1Robert Green

Page 57: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

LIST: Real Code …(setq pt1 (getpoint “\nSelect a point: “))(setq pt2 (getpoint “\nSelect a point: “))(setq pt2 (getpoint \nSelect a point: ))(command “.circle” (list (car pt1) (cadr pt2) (caddr pt2))( ( p ) ( p ) ( p ))“11.5”

))

Hint: LIST is simply a point in this case …p y p

AutoLISP/Visual LISP Part 1Robert Green

Page 58: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Let’s Try A Couple More ProblemsLets ramp up the complexity a bit using p y gtechniques we already know.

I’ll throw in some (command) line access to(command) line access to spice it up a bit.

AutoLISP/Visual LISP Part 1Robert Green

Page 59: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Let’s Draw a Wheel: Pseudo CodeLet’s now beef up our wheel creating program by adding a few more requirements. Here’s the pseudo

d f th lcode for the example:Prompt the user for a point for the wheel centerFor small wheels use spoke length of 8 medium getsFor small wheels use spoke length of 8, medium gets 10 and large gets 12Draw a first spoke at the center point Array the first spoke with the appropriate number of spokes …

AutoLISP/Visual LISP Part 1Robert Green

Page 60: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Let’s Draw a Wheel: Real Code(setq spokes 0)(while (or (< spokes 8) (> spokes 24))(setq spokes (getint "\nEnter number of spokes [from 8 to 24]: "))

))(setq wheel_center (getpoint "\nSelect center point of wheel: "))(cond( (< spokes 10) (command ".line" wheel center "@8<0" ""))( ( p ) ( _ @ ))( (and (>= spokes 10) (<= spokes 15)) (command ".line" wheel_center "@10<0" ""))( (> spokes 15) (command ".line" wheel_center "@12<0" ""))

)(command " array" "last" "" "p" wheel center spokes "360" "y")(command -array last p wheel_center spokes 360 y )

AutoLISP/Visual LISP Part 1Robert Green

Page 61: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

The Array syntax?How did I know how to write the ARRAY logic?Here are a few hints:

Use the –ARRAY command so you interact with the command prompt notUse the ARRAY command so you interact with the command prompt not a dialog boxUse the LAST object drawn to build the selection setUse the “” to close the selections

Use the WHEEL CENTER and SPOKES variables to passUse the WHEEL_CENTER and SPOKES variables to pass user input to the command lineGo through the command manually to get the sequencing and write it down …

AutoLISP/Visual LISP Part 1Robert Green

Page 62: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

A Better Wheel: Pseudo CodeHow would you change the program to assign all the spokes to the layer SPOKES g p ywhich has a color of YELLOW?

AutoLISP/Visual LISP Part 1Robert Green

Page 63: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

A Better Wheel: Real Code(defun c:wheel ()(setq spokes 0)(while (or (< spokes 8) (> spokes 24))( k ( i "\ E b f k [f 8 24] "))(setq spokes (getint "\nEnter number of spokes [from 8 to 24]: "))

)(setq wheel_center (getpoint "\nSelect center point of wheel: "))(setq old layer (getvar “clayer”))( q _ y (g y ))(command "-layer" "make" "wheels" "color" "yellow" "" "")(cond( (< spokes 10) (command ".line" wheel_center "@8<0" ""))( (and (>= spokes 10) (<= spokes 15)) (command " line" wheel center "@10<0" ""))( (and (>= spokes 10) (<= spokes 15)) (command .line wheel_center @10<0 ))( (> spokes 15) (command ".line" wheel_center "@12<0" ""))

)(command "-array" "last" "" "p" wheel_center spokes "360" "y")(setvar “clayer” old_layer)(princ))

AutoLISP/Visual LISP Part 1Robert Green

Page 64: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Working with Sets Working with sets of objects.j

SSGET

AutoLISP/Visual LISP Part 1Robert Green

Page 65: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Using SSGET (Pseudo Code)Let’s say that you wanted to build a program that would find all the circles in your drawing.y g

First, let’s draw a circle in AutoCAD and use the GE function from the previous section to get an idea of how the circle is constructed.

AutoLISP/Visual LISP Part 1Robert Green

Page 66: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

The association listHere’s the resulting list from a circle entity:

((-1 . <Entity name: 7efa0390>) (0 . "CIRCLE") (330 . <Entity name: 7ef9ecf8>) (5 . "14A") (100 . "AcDbEntity") (67 0) (410 "Model") (8 "0") (100 "AcDbCircle") (10(67 . 0) (410 . Model ) (8 . 0 ) (100 . AcDbCircle ) (10 39.9536 25.0516 0.0) (40 . 8.1027) (210 0.0 0.0 1.0))

Note the pair (0 . "CIRCLE") which indicates entity type and (8 . "0") which indicates the layer.

AutoLISP/Visual LISP Part 1Robert Green

Page 67: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Use SSGET to FilterHere’s an example of finding all circles:(setq circle_set (ssget "x" '((0 . "CIRCLE")) ))

Note the “dotted pair” (0 . "CIRCLE")

AutoLISP/Visual LISP Part 1Robert Green

Page 68: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Use SSGET for multiple FiltersHere’s an example of finding circles on layer GEOM:(setq circle_set (ssget "x" '((0 . "CIRCLE")

(8 "GEOM")) )(8 . GEOM )) ))

Note: It’s all about using “dotted pairs” to stack up filtering criteria like entity type or layers …

AutoLISP/Visual LISP Part 1Robert Green

Page 69: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

What would this find?(setq circle_set

(ssget "x" '((0 . "CIRCLE") (62 . 2)) )( g (( ) ( )) ))

Hint: Nobody has this stuff memorized, you can find information on the formats in AutoCAD’s “Developer’s Help” section or use the GE function to figure it out.

AutoLISP/Visual LISP Part 1Robert Green

Page 70: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

What would these statements do?

(setq circle_set (ssget "x" '((0 . "CIRCLE") (62 . 2)) )) (command "erase" circle set "")(command erase circle_set )

Note: You can use the set with any AutoCAD command with the convenience of storing the sets as variables that you can create with filters.

AutoLISP/Visual LISP Part 1Robert Green

Page 71: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

What would these statements do?

(setq circle_set (ssget "x" '((0 . "CIRCLE") (62 . 2)) )) (command "change" circle_set “” "p" "la" "temp" "")

AutoLISP/Visual LISP Part 1Robert Green

Page 72: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Challenge:

Could you write a program that would ask the user for a layer th h ll bj t f d th t l ( iname then change all objects found on that layer (assuming

it exists) to the layer NEW that has a color of GREEN?

Pseudo code:Prompt for layer nameFind objects on layerUse –LAYER to create new layerUse CHANGE to change objectsUse CHANGE to change objects

AutoLISP/Visual LISP Part 1Robert Green

Page 73: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

More Bits and Bytes Loading programsCentralized loadingVLIDE environmentCompiling

AutoLISP/Visual LISP Part 1Robert Green

Page 74: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

VLIDE environment … You can write code in the VLIDE window like this:

AutoLISP/Visual LISP Part 1Robert Green

Page 75: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Compile your code … Use the VLIDE environment like this:

(vlisp-compile ‘st “c:\\test\\myprog lsp”)(vlisp-compile st c:\\test\\myprog.lsp )

You’ll get MYPROG.FAS as a result g

AutoLISP/Visual LISP Part 1Robert Green

Page 76: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Load compiled code … Use a LOAD statement like this:

(load “c:\\test\\myprog fas”)(load c:\\test\\myprog.fas )

Now your LSP code is secure!yBe sure not to lose your LSP code though!

AutoLISP/Visual LISP Part 1Robert Green

Page 77: AutoLISP / Visual LISP/ Visual LISP - Part 1Part 1 · PDF fileAutoLISP® / Visual LISP/ Visual LISP® - Part 1Part 1 Robert Green CAD-Manager.com AutoLISP/Visual LISP Part 1 Robert

Questions?Questions?

AutoLISP® / Visual LISP ® - Part 1AutoLISP / Visual LISP Part 1

Robert GreenCAD-Manager.com

AutoLISP/Visual LISP Part 1Robert Green