TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Sam on May 27, 2016, 02:16:19 PM

Title: WHAT IS WRONG IN CODE ??? afralisp
Post by: Sam on May 27, 2016, 02:16:19 PM
DEAR ALL
WHAT IS WRONG IN CODE ???
Code: [Select]
AUTOCAD 2016, WINDOWS 10, 64 BIT
Quote
(defun c:retan (/ pl p2 p3 p4)
(setq pl (getpoint "\nfirst corner of rectangle: "))
(setq p3 (getcorner "\nsecond corner of rectangle: "))
(setq p2 (list (car pl)(cadr p3)))
(setq p4 (list (car p3)(cadr pl)))
(command "line" pl p2 p3 p4 "c")
(princ)
)

Quote
Command: RETAN

first corner of rectangle: ; error: base point is required

http://www.afralisp.net/autolisp/tutorials/quick-start.php (http://www.afralisp.net/autolisp/tutorials/quick-start.php)
Title: Re: WHAT IS WRONG IN CODE ??? afralisp
Post by: ChrisCarlson on May 27, 2016, 02:20:39 PM
Try adding _non and the base point for p3. Also I changed pl to p1 for clarity.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:retan (/ p1 p2 p3 p4)
  2. (setq p1 (getpoint "\nfirst corner of rectangle: "))
  3. (setq p3 (getcorner p1 "\nsecond corner of rectangle: "))
  4. (setq p2 (list (car p1)(cadr p3)))
  5. (setq p4 (list (car p3)(cadr p1)))
  6. (command "line" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "c")
  7. )


https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-AutoLISP/files/GUID-21BE8290-7F11-400B-AC39-62A110F07545-htm.html
Quote
The getcorner function takes a base point argument, based on the current UCS, and draws a rectangle from that point as the user moves the crosshairs on the screen.
Title: Re: WHAT IS WRONG IN CODE ??? afralisp
Post by: BKT on May 27, 2016, 02:49:08 PM
As a point of interest, I've noticed that some of the code on AfraLISP mixes lower case "L" with the numeral "1", so you have to pay special attention if you're doing a cut/paste from the examples.  The result is that it "looks" (as in this case) like "P1" but it's actually "Pl".

It shouldn't affect the OP's code any - just bringing it up because it will cause problems if you don't notice this and try to use the variable later on, but enter it incorrectly.

Edit: Sorry Chris, missed your note about the change!
Title: Re: WHAT IS WRONG IN CODE ??? afralisp
Post by: CAB on May 27, 2016, 03:01:04 PM
Chris's code should work fine but I prefer additional steps to catch errors.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:retan (/ p1 p2 p3 p4)
  2.   (if (and
  3.         (setq p1 (getpoint "\nfirst corner of rectangle: "))
  4.         (setq p3 (getcorner p1 "\nsecond corner of rectangle: "))
  5.         (setq p2 (list (car p1) (cadr p3)))
  6.         (setq p4 (list (car p3) (cadr p1)))
  7.       )
  8.     (command "line" "_non" p1 "_non" p2 "_non" p3 "_non" p4 "c")
  9.   )
  10.   (princ)
  11. )