Author Topic: Trying to make a basic LISP that makes a square  (Read 1436 times)

0 Members and 1 Guest are viewing this topic.

Curious Drafter

  • Mosquito
  • Posts: 18
  • Marine r try use big brain not stronk arms...
Trying to make a basic LISP that makes a square
« on: June 09, 2022, 11:16:19 AM »
I am doing this to learn to make LISP commands.

I am trying to make my first LISP command and I am teaching myself from online information and tutorials. There is a lot of information out there and I know a command like this already exists, but I am trying to make sure I understand the basics. So far the command I have is as follows:

 

Code - Auto/Visual Lisp: [Select]
  1. (Defun C:ESQ ()
  2. (setq InitLen (getreal (strcat "Would you kindly enter a length?("rtos InitLen 2 0)")))
  3. (if Len (setq initLen Len)(setq Len 4))
  4. (While (setq user_point (getpoint "Select Desired End Point: "))
  5. (command "polygon, 4" user_point "L" Len "")
  6. )

 

I can't get this to work and I know it has problems, but I am unsure of what they are. I  just need a good step in the right direction if possible.

 

For a little bit more background, I took a little bit of what I learned and a little bit of what looked like the right command from different LISP commands to make this Frankenstein's monster.

 

Any and all feedback is greatly appreciated.



EDIT (John): Added code tags.
« Last Edit: June 09, 2022, 12:45:43 PM by JohnK »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Trying to make a basic LISP that makes a square
« Reply #1 on: June 09, 2022, 12:31:53 PM »
Welcome to TheSwamp .. see if this sheds any light:

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Trying to make a basic LISP that makes a square
« Reply #2 on: June 09, 2022, 12:48:13 PM »
General question: What text editor are you using to type your code in? -e.g. Visual Studio Code, the "VLIDE", notepad, Notepad++, etc. This question is sort of related to the issues ronjonp is helping you identify.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Curious Drafter

  • Mosquito
  • Posts: 18
  • Marine r try use big brain not stronk arms...
Re: Trying to make a basic LISP that makes a square
« Reply #3 on: June 09, 2022, 12:52:25 PM »
Currently I am using VLIDE

Curious Drafter

  • Mosquito
  • Posts: 18
  • Marine r try use big brain not stronk arms...
Re: Trying to make a basic LISP that makes a square
« Reply #4 on: June 09, 2022, 01:40:13 PM »
Welcome to TheSwamp .. see if this sheds any light:


That string makes more sense, but I can't seem to get it to work and register the command "ESQ". If it helps I am trying to use VLIDE to work out the kinks.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Trying to make a basic LISP that makes a square
« Reply #5 on: June 09, 2022, 02:08:11 PM »
I'd like to start with the first line (the setting of the InitLen variable).

Is that variable already set before this function runs?
If so you can fix the double quote locations and have this line:
Code - Auto/Visual Lisp: [Select]
  1. (setq InitLen (getreal (strcat "Would you kindly enter a length?" (rtos InitLen 2 0))))

Next I want to talk about the logic behind line two:
Code - Auto/Visual Lisp: [Select]
  1. (if Len (setq initLen Len)(setq Len 4))
  2.  
is saying:
"If the `Len` variable doesn't evaluate to nothing, set the `initLen` variable equal to the `Len` variable, otherwise set the `Len` variable equal to 4."
Is that the statement you want to make or were you intending something else? My guess, this line is NOT correct so we will have to change it.

Also, note that you can select any BLUE text in the code tags I am using to get help on the function (like SETQ, GETREAL, STRCAT, etc).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Curious Drafter

  • Mosquito
  • Posts: 18
  • Marine r try use big brain not stronk arms...
Re: Trying to make a basic LISP that makes a square
« Reply #6 on: June 09, 2022, 02:29:29 PM »
I'd like to start with the first line (the setting of the InitLen variable).

Is that variable already set before this function runs?
If so you can fix the double quote locations and have this line:
Code - Auto/Visual Lisp: [Select]
  1. (setq InitLen (getreal (strcat "Would you kindly enter a length?" (rtos InitLen 2 0))))

Next I want to talk about the logic behind line two:
Code - Auto/Visual Lisp: [Select]
  1. (if Len (setq initLen Len)(setq Len 4))
  2.  
is saying:
"If the `Len` variable doesn't evaluate to nothing, set the `initLen` variable equal to the `Len` variable, otherwise set the `Len` variable equal to 4."
Is that the statement you want to make or were you intending something else? My guess, this line is NOT correct so we will have to change it.

Also, note that you can select any BLUE text in the code tags I am using to get help on the function (like SETQ, GETREAL, STRCAT, etc).

The variable isn't set prior. I would like to have that be user defined after the command ESQ is entered and after the starting point is defined. I think I screwed up the order of that though.

I see how that line is contradictory on itself, I thought I was setting it up to continue the polygon to have equal lengths for all 4 sides.

Is there a way to get more information on the 2 0 after the initlen? I assume they have to do with an internal setting, but I haven't found a good resource for what changes.


JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Trying to make a basic LISP that makes a square
« Reply #7 on: June 09, 2022, 02:34:48 PM »
----->%
Is there a way to get more information on the 2 0 after the initlen? I assume they have to do with an internal setting, but I haven't found a good resource for what changes.

If you click on the RTOS function in the code block you will be brought here: https://www.theswamp.org/Sources/doc/avlisp/#rtos
Which tells you that the 2 is the MODE and the 0 is the precision.

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Curious Drafter

  • Mosquito
  • Posts: 18
  • Marine r try use big brain not stronk arms...
Re: Trying to make a basic LISP that makes a square
« Reply #8 on: June 09, 2022, 02:38:05 PM »
----->%
Is there a way to get more information on the 2 0 after the initlen? I assume they have to do with an internal setting, but I haven't found a good resource for what changes.

If you click on the RTOS function in the code block you will be brought here: https://www.theswamp.org/Sources/doc/avlisp/#rtos
Which tells you that the 2 is the MODE and the 0 is the precision.

Just clicked, I feel so blonde now. Thank you for the clarification though.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Trying to make a basic LISP that makes a square
« Reply #9 on: June 09, 2022, 02:47:06 PM »
Okay on to the first statement then.

The variable is undefined so you have a choice to make:
- have a default value or not. Meaning, you can control the program with this statement; if the user does not enter a value, your program can gracefully quit OR you can gracefully quit during the GETPOINT portion later.

Here is a demonstration:
1. no default value for `initlen`.
Code - Auto/Visual Lisp: [Select]
  1. (if (setq initlen (getreal (strcat "Would you kindly enter a length: ")))
  2. ...
OR a default value (of 4):
Code - Auto/Visual Lisp: [Select]
  1. (setq initlen
  2.       (cond
  3.         ((getreal "\nWould you kindly enter a length [4]: "))
  4.         (T 4)
  5.         )
  6.       )
Run both (copy paste into the command line) and decide.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Curious Drafter

  • Mosquito
  • Posts: 18
  • Marine r try use big brain not stronk arms...
Re: Trying to make a basic LISP that makes a square
« Reply #10 on: June 09, 2022, 03:59:20 PM »
Okay on to the first statement then.

The variable is undefined so you have a choice to make:
- have a default value or not. Meaning, you can control the program with this statement; if the user does not enter a value, your program can gracefully quit OR you can gracefully quit during the GETPOINT portion later.

Here is a demonstration:
1. no default value for `initlen`.
Code - Auto/Visual Lisp: [Select]
  1. (if (setq initlen (getreal (strcat "Would you kindly enter a length: ")))
  2. ...
OR a default value (of 4):
Code - Auto/Visual Lisp: [Select]
  1. (setq initlen
  2.       (cond
  3.         ((getreal "\nWould you kindly enter a length [4]: "))
  4.         (T 4)
  5.         )
  6.       )
Run both (copy paste into the command line) and decide.




I was able to make it work pretty well with the following"

Code - Auto/Visual Lisp: [Select]
  1.  (defun C:ESQ (/ InitLen user_point)
  2.  
  3.   (if (setq initlen (getreal (strcat "Would you kindly enter a length: ")))
  4.  
  5.     (while (setq user_point (getpoint ""))
  6.  
  7.       (command "polygon" 4 "_edge" user_point (polar user_point 0 Len))
  8.  
  9.     )
  10.  
  11.   )
  12.  
  13.   (princ)
  14.  
  15. )

but I am wondering why it asks for length twice. I am hoping to make it to where it's set at the start and I just have to locate the square as needed. thank you for the help and guidance.
« Last Edit: June 10, 2022, 08:20:22 AM by JCK47 »

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: Trying to make a basic LISP that makes a square
« Reply #11 on: June 09, 2022, 04:14:51 PM »
Ah looks like you're on the right track.

But here are the two methods I was leading you down. The first is ronjonp's method he posted an image of (I had to retype :p) and the second is the default value method I was hinting at. Both are fine but I leave it up to you to find the difference in how they operate.
Code - Auto/Visual Lisp: [Select]
  1. ; NOTE: uncomment out the last PRINC statement.
  2. (defun c:esq ( / initlen user_point)
  3.   (if (setq initlen (getreal (strcat "Would you kindly enter a length: ")))
  4.     (progn
  5.       (while (setq user_point (getpoint "Select desired end point: "))
  6.              (command "polygon" 4 user_point "c" (/ initlen 2)))
  7.       ) ;_end progn
  8.     ) ;_end if
  9.   ; (princ)
  10.   )

Code - Auto/Visual Lisp: [Select]
  1. (defun c:esq ( / initlen user_point)
  2.   (setq initlen (cond
  3.                   ((getreal "\nEnter a length [4]: "))
  4.                   (T 4)
  5.                   ) ;_end cond
  6.         ) ;_end setq
  7.   (while (setq user_point (getpoint "Select desired end point: "))
  8.          (command "polygon" 4 user_point "c" (/ initlen 2))
  9.          ) ;_end while
  10.   )


Also, see here to learn about the Swamps code tags: https://www.theswamp.org/index.php?topic=48309.0
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: Trying to make a basic LISP that makes a square
« Reply #12 on: June 09, 2022, 08:11:43 PM »
Here is another example going 1 step further adding dimensions. It also uses a DCL for input.

Code: [Select]
; simple draw a box and dimension it
; By Alan H March 2019
' info@alanh.com.au

(defun c:2box ( / pt1 pt2 pt3 ahl ahh ahoff )
(setq oldsnap (getvar 'osmode))
(setq oldang (getvar 'angdir))
(setq pt1 (getpoint "\nPick lower left"))
(setvar 'osmode 0)
(if (not AH:getvalsm)(load "Multi Getvals.lsp"))
(setq ans (AH:getvalsm (list "Enter values" "Length " 5 4 "150" "Height " 5 4 "100")))
(setq ahL (atof (nth 0 ans)))
(setq ahw (atof (nth 1 ans)))
(setq pt2 (polar pt1 0.0 ahl))
(setq pt3 (polar pt2 (/ pi 2.0) ahw))
(command "rectang" pt1 pt3)
(setq ahoff (* 2.0 (* (getvar 'dimasz)(getvar 'dimscale))))
(setq pt4 (polar pt2  (* pi 1.5) ahoff))
(command "dim" "hor" pt1 pt2 pt4 "" "exit")
(setq pt4 (polar pt3 0.0 ahoff))
(command "dim" "Ver" pt2 pt3 pt4 "" "exit")
(setvar 'osmode oldsnap)
(princ)
)
(c:2box)

You need multi getvals.lsp its a library routine that makes dcl's on the fly based on a list. Either save it to a support path or change the (load "\\yourdirectory where saved\\multi getvals.lsp")

A man who never made a mistake never made anything

mhupp

  • Bull Frog
  • Posts: 250
Re: Trying to make a basic LISP that makes a square
« Reply #13 on: June 09, 2022, 11:22:42 PM »
Another way to set a default option.

Code - Auto/Visual Lisp: [Select]
  1. (or (setq initlen (getreal "\nWould you kindly enter a length [4]: ")) (setq initlen 4))