LISP for making a square

LISP for making a square

Anonymous
Not applicable
3,253 Views
15 Replies
Message 1 of 16

LISP for making a square

Anonymous
Not applicable

Working on a LISP that would make a perfect square at a user defined spot. Attached is what I have so far.

 

;makes a square
; CREATED BY AARON CRUZ
;d= distance size
;d1 = half distance
;p1 = ceter of sqaure
;p2 & p3 = square corners
;a1 & a2 = x,y of p2 respectivily
;b1 & b2 = x,y of p3 respectivily

(defun c:SQ (/d d1 a1 a2 b1 b2 pl p2 p3)
(setq d (getreal "\nInput size of square: "))
(setq p1 (getpoint "\nSelect center of Square: "))
(setq d1 / d 2)
(setq a1 (- (car p1) d1))
(setq a2 (- (cadr p1) d1))
(setq b1 (+ (car p1) d1))
(setq b2 (+ (cadr p1) d1))
(setq p2 (a1)(a2))
(setq p3 (b1)(b2))
(command "rectangle" p2 p3)
(type "Created by Aaron Cruz")
(type "1oz Gomme Syrup, 1/2oz Tequilla, 1/2oz White Rum, 1oz lemon juice, 1/2oz triple sec, 1oz gin, 1/2oz vodka, splash of Cola. Mix and call a day.")
(princ)
)

 

This is my second LISP so don't laugh too hard.

0 Likes
Accepted solutions (1)
3,254 Views
15 Replies
Replies (15)
Message 2 of 16

john.uhden
Mentor
Mentor

What is the name of that concoction?  No, not the lisp, but the drink.

When you say cola, do you mean like Coke or Pepsi, or do you mean like club soda or seltzer?

Also, you say to "mix and call it a day."  What about the drinking part?  You're not going to make me wait until the next day, are you?

John F. Uhden

0 Likes
Message 3 of 16

Kent1Cooper
Consultant
Consultant

....
(setq d (getreal "\nInput size of square: "))
(setq p1 (getpoint "\nSelect center of Square: "))
(setq d1 (/ d 2))
(setq a1 (- (car p1) d1))
(setq a2 (- (cadr p1) d1))
(setq b1 (+ (car p1) d1))
(setq b2 (+ (cadr p1) d1))
(setq p2 (list a1 a2))
(setq p3 (list b1 b2))
(command "rectangle" p2 p3)

....


I would also suggest using (getdist) instead of (getreal) for the size of the square.  You can then, if you like, set the distance by picking two points on-screen, and if you use Imperial Architectural units or Fractional units, you can enter it in feet-inches-fractions or just fractions format.

Kent Cooper, AIA
0 Likes
Message 4 of 16

CodeDing
Advisor
Advisor

@Anonymous ,

 

I believe Kent has put you in a good direction, since i didn't see a specific question in your original post?

And I can see that you put these in here for fun:

 

(type "Created by Aaron Cruz")
(type "1oz Gomme Syrup, 1/2oz Tequilla, 1/2oz White Rum, 1oz lemon juice, 1/2oz triple sec, 1oz gin, 1/2oz vodka, splash of Cola. Mix and call a day.")

...so just to be clear (i don't know if you know or not already) but these strings you are passing to the (type ...) function, are returning the STR atom (for stings). Not sure if that part was intentional or not lol.

 

Good luck with your future lisps!

Best,

~DD

 

0 Likes
Message 5 of 16

Anonymous
Not applicable

-Long Island

-I'm like Switzerland on the issue of Coke or Pepsi

-You mix it and leave it on the table for the misses. Trying to keep you safe at home!

0 Likes
Message 6 of 16

Anonymous
Not applicable

 

@Kent1Cooper

Just ran it and I'm getting the error code 'too few arguments'.

 

 

 

0 Likes
Message 7 of 16

Sea-Haven
Mentor
Mentor

A 1x1 block just change scale to desired size.

 

A dynamic block d2=d1.

 

A very old also box with dimensions uses multi getvals.lsp for size entry.

 

 

; simple draw a box and dimension it 
; By Alan H updated adding multi getval dcl March 2019
' info@alanh.com.au

(defun ah:box ( / 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 "Simple rectang" "Enter length" 8 7 "1" "Enter height " 8 7 "2")))
(setq ahL (atof (nth 0 ans)))
(setq ahH (atof (nth 1 ans)))
(setq pt2 (polar pt1 0.0 ahl))
(setq pt3 (polar pt2 (/ pi 2.0) ahH))
(command "rectang" pt1 pt3)
(setq ahoff (* 2.0 (* (getvar 'dimasz)(getvar 'dimscale)))) ; change offset as required
(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)
)
(ah:box)

 

0 Likes
Message 8 of 16

john.uhden
Mentor
Mentor
"for the misses?"
I'm too old to be messing with teenage girls. It's against the law anyway.
So you mean that's like the Long Island Tea that Penny served to Sheldon?

John F. Uhden

0 Likes
Message 9 of 16

Kent1Cooper
Consultant
Consultant
Accepted solution

I had just suggested corrections of things I noticed, but now I also see that you are missing the space after the slash in the localized variables list:


(defun c:SQ (/{space here}d d1 a1 a2 b1 b2 pl p2 p3)

 

And I would also suggest that you prevent unexpected results if you have running Osnap modes in effect and there are any other things in the vicinity that it might snap to:

 

(command "rectangle" "_non" p2 "_non" p3)

 

[which is easier in a simple routine like this than the other way, which would be to save the OSMODE System variable value, set it to 0, do the drawing part, then restore OSMODE to what it was before].

Kent Cooper, AIA
Message 10 of 16

Kent1Cooper
Consultant
Consultant

@CodeDing wrote:

....

And I can see that you put these in here for fun:

 

(type "Created by Aaron Cruz")
(type "1oz Gomme Syrup, 1/2oz Tequilla, 1/2oz White Rum, 1oz lemon juice, 1/2oz triple sec, 1oz gin, 1/2oz vodka, splash of Cola. Mix and call a day.")

...so just to be clear (i don't know if you know or not already) but these strings you are passing to the (type ...) function, are returning the STR atom (for stings). Not sure if that part was intentional or not lol.

....


I had just ignored that, but if the idea is to have them shown at the Command line, the function needed would be (prompt) or one of those that start with (prin...).

Kent Cooper, AIA
0 Likes
Message 11 of 16

Kent1Cooper
Consultant
Consultant

I was also going to suggest the simplifications that:

1.  You can set more than one variable within one (setq) function, and

2.  A variable that is used only once may as well not be set as a variable, but its calculation just used directly where applicable, eliminating several variables.

 

But in the process of adjusting to demonstrate, it also occurred to me that you might like to see the square at its actual size  as you go to specify its center, i.e. drag it into place.  This does that by drawing it in the middle of the screen, then MOVEing it, leaving you in the Move command to place it.

 

(defun c:SQ (/ d d1 pl)
  (setq
    d (getdist "\nInput size of square: ")
    p1 (getvar 'viewctr)
    d1 (/ d 2)
  ); setq
  (command
    "_.rectang"
      "_non" (list (- (car p1) d1) (- (cadr p1) d1))
      "_non" (list (+ (car p1) d1) (+ (cadr p1) d1))
    "_.move" "_last" "" "_non" p1
  ); command
); defun

 

Kent Cooper, AIA
Message 12 of 16

Anonymous
Not applicable

It's alive! Had no idea a space could throw the whole thing off

0 Likes
Message 13 of 16

Anonymous
Not applicable

@Kent1Cooper  What is the "_non" input doing? 

0 Likes
Message 14 of 16

CodeDing
Advisor
Advisor

@Anonymous ,

 


What is the "_non" input doing? 

The "_non" input, when run within a command prompting for a point, temporarily sets the running osnaps to NONe.

This ensures that when a point is selected by your routine, it will not accidentally snap to another nearby object.

The underscore prefix "_" is designating that the English version of the following command is used. this is good practice in programming withing AutoCAD so that if your routine is used by someone that is running a non-English version of AutoCAD then the command would still work as intended.

 

Best,

~DD

 

0 Likes
Message 15 of 16

devitg
Advisor
Advisor

@Anonymous  a NON PERFECT square , is not a SQUARE . 

 

0 Likes
Message 16 of 16

Anonymous
Not applicable

Downloaded fine, loaded it, but when i ran it , it runs as HATCH an oject or area...??

0 Likes