Petar_Grozni Posted April 21 Posted April 21 (edited) Hello all, I am bit frustrated with autolisp. Tried my first coding and i cannot see what is wrong. I am trying to make automated array of rectangles, where i define dimensions of rectangle, number of rows, columns , and spacing between them. Here is the code Ive written. Yet, when I start it, after entereing parameters, i need to click in and get wierd results. here is the code, if you could hepl me i would really appreciate it Thanx Petar from Croatia (defun c:stamps (/ w h c r a b) (setq w (getreal "\nEnter the width of the rectangle: ")) (setq h (getreal "\nEnter the height of the rectangle: ")) (setq c (getint "\nEnter the number of columns: ")) (setq r (getint "\nEnter the number of rows: ")) (setq a (getreal "\nEnter the distance between columns: ")) (setq b (getreal "\nEnter the distance between rows: ")) (setq startX 0) (setq startY 0) (repeat r (setq startX 0) (repeat c (command "_RECTANG" startX startY (+ startX w) (+ startY h)) (setq startX (+ startX (+ w a))) ) (setq startY (+ startY (+ h b))) ) ) Edited April 22 by SLW210 Added Code Tags! Quote
pkenewell Posted April 21 Posted April 21 @Petar_Grozni Try This: (defun c:stamps (/ w h c r a b) (setq w (getreal "\nEnter the width of the rectangle: ")) (setq h (getreal "\nEnter the height of the rectangle: ")) (setq c (getint "\nEnter the number of columns: ")) (setq r (getint "\nEnter the number of rows: ")) (setq a (getreal "\nEnter the distance between columns: ")) (setq b (getreal "\nEnter the distance between rows: ")) (setq startX 0) (setq startY 0) (repeat r (setq startX 0) (repeat c ;; You nead to disable object snaps before points using "non" override, and coordinates should be a point list, not just separate x and y values. (command "_RECTANG" "_non" (list startX startY) "_non" (list (+ startX w) (+ startY h))) (setq startX (+ startX (+ w a))) ) (setq startY (+ startY (+ h b))) ) ) Quote
pkenewell Posted April 21 Posted April 21 @Petar_Grozni Here is a better way to write it FWIW: (defun c:stamps (/ w h c r a b start) (if (and (setq w (getreal "\nEnter the width of the rectangle: ")) (setq h (getreal "\nEnter the height of the rectangle: ")) (setq c (getint "\nEnter the number of columns: ")) (setq r (getint "\nEnter the number of rows: ")) (setq a (getreal "\nEnter the distance between columns: ")) (setq b (getreal "\nEnter the distance between rows: ")) ) (progn (setq start (list 0.0 0.0)) (repeat r (repeat c (command "_RECTANG" "_non" start "_non" (list (+ (car start) w) (+ (cadr start) h))) (setq start (list (+ (car start) w a) (cadr start))) ) (setq start (list 0.0 (+ (cadr start) h b))) ) ) ) ) Quote
Jonathan Handojo Posted April 21 Posted April 21 (edited) Just as the above has shown, it completely works. There are other ways as well, but since you have just started using AutoLISP, I thought I'd leave some comments: (defun c:stamps ( / _getfunc a b c h offset r start w) (defun _getfunc (fnc msg bit) ;; Define a function to make use of initget. ;| Initget is a function which can be used to restrict numerical user inputs: https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-9ED8841B-5C1D-4B3F-9F3B-84A4408A6BBF |; (initget bit) (fnc msg) ) (and ;| Using 'and' introduces the short-circuit evaluation, in which conditional processsing stops as soon as the first value returns nil. All get- functions (except getstring) returns nil upon pressing Enter |; (setq w (_getfunc getdist "\nEnter the width of the rectangle <exit>: " 6)) ;; A value of 6 (bits 2 and 4) ensure that the user enters a positive value and not 0 (setq h (_getfunc getdist "\nEnter the height of the rectangle <exit>: " 6)) ;; Using getdist allow the user to also specify two points to define the distance. (setq c (_getfunc getint "\nEnter the number of columns <exit>: " 6)) (setq r (_getfunc getint "\nEnter the number of rows <exit>: " 6)) (setq a (_getfunc getdist "\nEnter the distance between columns <exit>: " 4)) (setq b (_getfunc getdist "\nEnter the distance between rows <exit>: " 4)) ;; Comment one line or the other depending on the insertion point being located at (0.0 0.0), or to a point specified by the user. ;(setq start '(0.0 0.0)) (setq start (getpoint "\nSpecify point to place rectangles <exit>: ")) (progn (setq offset '(0.0 0.0)) (repeat r (repeat c (command "_RECTANG" "_non" (mapcar '+ start offset) "_non" (mapcar '+ start offset (list w h))) (while (not (zerop (getvar "cmdactive"))) (command "")) ;; This ensure that the Space/Enter key keeps getting pressed until the command is over. ;; Not entirely necessary, but I always use it for good practise. (setq offset (list (+ (car offset) w a) (cadr offset))) ) (setq offset (list 0.0 (+ (cadr offset) h b))) ) ) ) (princ) ;; Exit the command quitely. Always use this, good common AutoLISP programming practise for a clean command line exit. ) Edited April 21 by Jonathan Handojo 1 Quote
BIGAL Posted April 21 Posted April 21 (edited) Another why not draw 1 and use array ? (defun c:stamps (/ w h c r a b pt) (if (not AH:getvalsm)(load "Multi Getvals.lsp")) (setq ans (AH:getvalsm (list "Enter values " "Width " 5 4 "100" "height " 5 4 "100" "Number columns" 5 4 "5" "Number rows" 5 4 "5" "Dist between columns" 5 4 "20" "dist between rows" 5 4 "20"))) (setq w (atof (nth 0 ans)) h (atof (nth 1 ans)) c (atoi (nth 2 ans)) r (atoi (nth 3 ans)) a (atof (nth 4 ans)) b (atof (nth 5 ans)) ) (setq pt (getpoint "\nPick lower left point for rectang ")) (command "_RECTANG" "_non" pt "_non" (list (+ (car pt) w) (+ (cadr pt) h))) (command "array" (entlast) "" "Rectangular" r c (+ b h) (+ a w) ) (princ) ) Multi GETVALS.lsp Edited April 21 by BIGAL 1 Quote
Jonathan Handojo Posted April 22 Posted April 22 I think it's more of a practice. It's their first code after all. Always good to start somewhere. 1 Quote
BIGAL Posted April 22 Posted April 22 Good comment Johnathan, like the old saying crawl, walk then run. The array part can still be used. 1 Quote
Petar_Grozni Posted April 22 Author Posted April 22 guys, You have been most helpful. I now have further things and leads to study. Thank You very much 1 Quote
SLW210 Posted April 22 Posted April 22 Please use Code Tags in the future. (<> in the editor toolbar) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.