Please help me with simple autplisp (Draw rectangle, input basic point, length and width)

Please help me with simple autplisp (Draw rectangle, input basic point, length and width)

Anonymous
Not applicable
3,109 Views
4 Replies
Message 1 of 5

Please help me with simple autplisp (Draw rectangle, input basic point, length and width)

Anonymous
Not applicable

(defun C:Test ()

(setq A (getpoint "\n Basic point: "))
(initget (+ 1 2 4))
(setq B (getreal "\n Input the length: "))
(initget (+ 1 2 4))
(setq C (getreal "\n Input the width: "))


(setq D (sqrt (+ (* B B) (* C C))))
(setq E (/ C D))


(setq A1 (polar A 0 B))
(setq A2 (polar A E D))
(setq A3 (polar A (/ pi 2) C))


(command ".pline" A A1 A2 A3 "close")

(princ)
)

 

-----------------------

The attached file is my autolisp, I input 200x100 but the rectangle is not correct! Please help me!

0 Likes
Accepted solutions (3)
3,110 Views
4 Replies
Replies (4)
Message 2 of 5

devitg
Advisor
Advisor
Accepted solution

Hi @Anonymous , see my way to skin the cat 

 

(defun C:draw-rectangle ()

(setq a '(0 0 0 ))

(setq b 200.0)

(setq c 100.0)
  
;;;(setq A (getpoint "\n Basic point: "))
;;;(initget (+ 1 2 4))
;;;(setq B (getdist  a "\n Input the length: "))
;;;(initget (+ 1 2 4))
;;;(setq C (getdist a "\n Input the width: "))
;;;
(setq ab (polar a 0 b))
(setq bc ( polar ab ( / pi 2.0) c ))

(setq osmode (getvar 'osmode))
(setvar 'osmode 0)   
  
;;;(setq D (sqrt (+ (* B B) (* C C))))
;;;(setq E (/ C D))
;;;
;;;
;;;(setq A1 (polar A 0 B))
;;;(setq A2 (polar A E D))
;;;(setq A3 (polar A (/ pi 2) C))
;;;
(command "rectangle" a bc)

  
;;;(command ".pline" A A1 A2 A3 "close")

(princ)
)

 

devitg_0-1604890547242.png

 

Message 3 of 5

Kent1Cooper
Consultant
Consultant
Accepted solution

You want E to be not just (/ C D), but the angle [in radians] whose sine is  (/ C D).  That could be done, but it's easier to find the angle whose tangent  is (/ C B), with the (atan) function.  But here's another way, that calculates the opposite corner directly, rather than through a trigonometric calculation or by way of the second corner, and draws the rectangle directly from just those two:

 

(defun C:Test ()
  (setq A (getpoint "\n Basic point: "))
  (initget (+ 1 2 4))
  (setq B (getreal "\n Input the length {X direction}: "))
  (initget (+ 1 2 4))
  (setq C (getreal "\n Input the width {Y direction}: "))

  (setq A2 (mapcar '+ A (list B C)))
  (command "_.rectang" "none" A "none" A2)

  (princ)
)

Kent Cooper, AIA
Message 4 of 5

Sea-Haven
Mentor
Mentor
Accepted solution

My $0.05

 

; simple draw a box and dimension it 
; By Alan H 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)

Needs Multi getvals for the dcl input.

 screenshot286.png

Message 5 of 5

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

....

  (setq B (getreal "\n Input the length {X direction}: "))
....
  (setq C (getreal "\n Input the width {Y direction}: "))

  (setq A2 (mapcar '+ A (list B C)))
  (command "_.rectang" "none" A "none" A2)

....


I would also suggest using (getdist) instead of (getreal) for the length and width.  That way, you can specify the distance by either  typing in or picking points on-screen, even just one additional point for each with rubber-banding from your 'A' location.  And if you use Architectural or Fractional Imperial units, you can type it in feet-inches-fractions format if you want.

 

Also, the A2 variable isn't really needed, since it's used only once -- may as well just put the calculation right in there.

 

(defun C:Test (/ A B C)
  (setq A (getpoint "\n Basic point: "))
  (initget (+ 1 2 4))
  (setq B (getdist A "\n Input the length {X direction}: "))
  (initget (+ 1 2 4))
  (setq C (getdist A "\n Input the width {Y direction}: "))

  (command "_.rectang" "none" A "none" (mapcar '+ A (list B C)))

  (princ)
)

 

BUT FURTHERMORE, try the RECTANG command and its Dimensions option, which gives you the same result [without rubber-banding] except for in addition allowing you to pick which quadrant the result falls in relative to the first corner.  No code required.  Even in your code, you could get other quadrants than only the upper-right one, by removing the 4's from the (initget) argument, to allow negative values.

Kent Cooper, AIA
0 Likes