Author Topic: (AutoLISP the language) ARITHMETIC the AutoLISP way  (Read 2110 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(AutoLISP the language) ARITHMETIC the AutoLISP way
« on: June 17, 2016, 09:55:04 AM »
My intentions of this thread, and others, is to show the basics of using the AutoLISP programming language in AutoCAD.

I am basically following the book AutoLISP Programming Principles and Techniques by Rod Rawls and Mark Hagen.
https://www.amazon.com/Autolisp-Programming-Principles-Rod-Rawls/dp/1566371961?ie=UTF8&*Version*=1&*entries*=0

Please ask lots of questions.

If you are an experienced programmer please help by adding additional exercises.

Thanks!
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #1 on: June 17, 2016, 10:14:45 AM »
Basic math: 1 + 2 = 3
AutoLISP function: (+ 1 2) Make note of the spaces in the function.

1 + 2 + 3 = 6
(+ 1 2 3) = 6

3 - 1 = 2
(- 3 1) = 2
(- 20 10 5) = 5
(- 50 -5) = 55

1 * 2 = 3
(* 1 2) = 3
(* 10.525 25.125) = 264.441
(* 50.2 20 2.133) = 2141.53

16 ÷ 2 = 16
(/ 16 8) = 2
(/ 16 8.0) = 2.0



« Last Edit: June 17, 2016, 10:18:58 AM by Mark »
TheSwamp.org  (serving the CAD community since 2003)

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #2 on: June 17, 2016, 10:17:00 AM »
except for 1+ or 1-   :2funny:

Sorry couldn't help it, in LISP there's always a catch.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #3 on: June 17, 2016, 10:19:44 AM »
except for 1+ or 1-   :2funny:

Sorry couldn't help it, in LISP there's always a catch.
Show us an example please.
TheSwamp.org  (serving the CAD community since 2003)

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #4 on: June 17, 2016, 10:22:00 AM »
ok

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (1+ 2) = 3
  3. (1- 2) = 1
  4.  
  5.  

I usually use these in a while loop if i am counting either up or down, example.
Code - Auto/Visual Lisp: [Select]
  1. (setq cnt 0)
  2. (while (< cnt 20)
  3.   ..............
  4.   (setq cnt (1+ cnt))
  5. )
  6.  

« Last Edit: June 17, 2016, 10:28:07 AM by snownut2 »

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #5 on: June 17, 2016, 10:35:31 AM »
More advanced math. From the book

18 - [(3 + 6 + 9) ÷ (9 - 6)] - 12

Work from the deepest levels out.
(+ 3 6 9) (- 9 6)
(/ (+ 3 6 9) (- 9 6))
(- 18 (/ (+ 3 6 9) (- 9 6)))
(- (- 18 (/ (+ 3 6 9) (- 9 6))) 12)


TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #6 on: June 17, 2016, 10:45:26 AM »
Using arithmetic on the command line.

Code: [Select]
Command: OFFSET
Current settings: Erase source=No  Layer=Source  OFFSETGAPTYPE=0
Specify offset distance or [Through/Erase/Layer] <Through>: (* 0.235 15)
3.525

TheSwamp.org  (serving the CAD community since 2003)

ribarm

  • Gator
  • Posts: 3297
  • Marko Ribar, architect
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #7 on: June 17, 2016, 12:13:44 PM »
The topic that may be of interest, at least when arithmetic is an issue...

https://www.theswamp.org/index.php?topic=51268.0
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #8 on: June 17, 2016, 02:26:48 PM »
... Maybe using CODE tags is a good idea...

Small addition: Negation:
Code: [Select]
(- 1) => -1
(- -1) => 1

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #9 on: June 17, 2016, 06:49:45 PM »
Using arithmetic on the command line.

Code: [Select]
Command: OFFSET
Current settings: Erase source=No  Layer=Source  OFFSETGAPTYPE=0
Specify offset distance or [Through/Erase/Layer] <Through>: (* 0.235 15)
3.525




Note that you must include one real number if you want a real number result.
Example:

Code: [Select]

Command: OFFSET
Current settings: Erase source=No  Layer=Source  OFFSETGAPTYPE=0
Specify offset distance or [Through/Erase/Layer] <Through>: (/ 15 4)
3

***compared to ***

Command: OFFSET
Current settings: Erase source=No  Layer=Source  OFFSETGAPTYPE=0
Specify offset distance or [Through/Erase/Layer] <Through>: (/ 15 4.0)
3.75


Peter2

  • Swamp Rat
  • Posts: 654
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #10 on: June 18, 2016, 05:38:01 AM »
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2149
  • class keyThumper<T>:ILazy<T>
Re: (AutoLISP the language) ARITHMETIC the AutoLISP way
« Reply #11 on: June 18, 2016, 07:56:30 AM »
Mark was just making sure you were awake Peter .. well done.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.