Help creating an array with LISP

Help creating an array with LISP

ChrisSXYPP
Explorer Explorer
1,666 Views
9 Replies
Message 1 of 10

Help creating an array with LISP

ChrisSXYPP
Explorer
Explorer

Hello,

I need some help to create an array with for the rectangle 3 (the one laying on the bottom), having 1 column, 1 row and 6 levels with a total height of the user's height input (95 or 119) -0.75", so the top rectangle is flush with the walls holding it.

 

I'm new using LISP, so I don't know completely how the syntaxis for calling commands and entering their values works. I hit the wall when I tried to call the ARRAYREC command, just can't make it work. I also don't know if it would be easier to create copies of the entity and spacing them equally instead to get the same result, because I would need to explode the array after anyway. Like I said I have been using LISP only for a few hours.

 

This is the code that I managed to make work so far.


Any help is appreciated.
Thank you.

 

(defun c:Createfiller (/ pt len ht rect1-base rect2-base rect3-base rect4-base rect5-base rect1 rect2 rect3 rect4 rect5)
  ;; Prompt for the insertion point (base point)
  (setq pt (getpoint "\nSpecify insertion point: "))

  ;; Prompt for the length
  (setq len (getdist "\nEnter length of the rectangle: "))

  ;; Prompt for the height (either 95 or 119)
  (setq ht (getint "\nEnter height (95 or 119 inches): "))
  
  ;; Ensure height is either 95 or 119
  (while (not (or (= ht 95) (= ht 119)))
    (setq ht (getint "\nInvalid height. Enter height (95 or 119 inches): "))
  )

  ;; Draw the first rectangle:
  (setq rect1-base pt)
  (command "_.rectangle" rect1-base (list (+ (car rect1-base) 0.25) (+ (cadr rect1-base) len)))
  (setq rect1 (entlast))  ;; Capture the first rectangle
  ;; Extrude rectangle 1 to the user height
  (command "_.extrude" rect1 "" (rtos ht 2 2))

  ;; Draw the second rectangle:
  (setq rect2-base (list (+ (car pt) 0.25) (cadr pt)))
  (command "_.rectangle" rect2-base (list (+ (car rect2-base) 2.5) (+ (cadr rect2-base) 0.75)))
  (setq rect2 (entlast))  ;; Capture the second rectangle
  ;; Extrude rectangle 2 to the user height
  (command "_.extrude" rect2 "" (rtos ht 2 2))

  ;; Draw the third rectangle: 
  (setq rect3-base (list (+ (car pt) 0.25) (+ (cadr pt) 0.75)))
  (command "_.rectangle" rect3-base (list (+ (car rect3-base) 2.5) (+ (cadr rect3-base) (- len 1.5))))
  (setq rect3 (entlast))  ;; Capture the third rectangle
  ;; Extrude rectangle 3 to 0.75"
  (command "_.extrude" rect3 "" "0.75")

  ;; Draw the fourth rectangle: 
  (setq rect4-base (list (+ (car pt) 0.25) (+ (cadr pt) (- len 0.75)))) ;; Fixed the base point
  (command "_.rectangle" rect4-base (list (+ (car rect4-base) 2.5) (+ (cadr rect4-base) 0.75)))
  (setq rect4 (entlast))  ;; Capture the fourth rectangle
  ;; Extrude rectangle 4 to the user height
  (command "_.extrude" rect4 "" (rtos ht 2 2))

  ;; Draw the fifth rectangle: 
  (setq rect5-base (list (+ (car pt) 2.75) (cadr pt))) ;; Fixed the base point
  (command "_.rectangle" rect5-base (list (+ (car rect5-base) 0.25) (+ (cadr rect5-base) len)))
  (setq rect5 (entlast))  ;; Capture the fifth rectangle
  ;; Extrude rectangle 5 to the user height
  (command "_.extrude" rect5 "" (rtos ht 2 2))

  ;; Finished
  (princ "\nFiller created.")
  (princ)
)
0 Likes
Accepted solutions (1)
1,667 Views
9 Replies
Replies (9)
Message 2 of 10

doni49
Mentor
Mentor

PLEASE edit your post to REPASTE the lisp code formatting it as LISP.  This will make it MUCH easier for someone to try and help you as it'll be easier for us to read.

 

doni49_0-1747760904111.png

.

doni49_1-1747760936266.png

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

0 Likes
Message 3 of 10

doni49
Mentor
Mentor

It appears that you're looking to be able to write a "command" (something a that a general user might use) that will call other sub-routines.  Correct?

 

Let's start with a few basics:

  1. You can type AutoLisp code directly into the command line or you can load it from the lsp file.
  2. The code can be one-time use -- meaning that if you wanted to run it again, you'd have to RELOAD the lisp code either at the command line or from the lsp file.
  3. Within that code, you can define a function.  That's PROBABLY the part of code that you want your command to run.
  4. A function can be defined as a "command" by putting C: in front of the function name.

So here are a few examples:

 

This first example is something I've typed MANY times at the command line over the years.  If I want to offset a line that is exactly half the distance between 2 points and I want to keep reusing that distance, I type the following when the offset command asks for a distance.  The GETDIST function prompts for a distance.  Then the divide function (the slash) divides it in half.

 

(/ (getdist) 2)

 

 

This is an example of a function that could be used from within a command.  It accepts an argument (an angle in radians) and returns the angle in degrees.  By including x within the first set of parentheses after the function name, autolisp recognizes it as an argument and replaces x with the value (provided at runtime) within the function itself.

 

(defun rtd (x) (* x (/ 180.0 pi)))

 

 

Now we have a command (a special type of function) that will use the function from the last example:

 

(defun c:AngleInDegrees()
  (princ (rtd (getangle "Angle:  " (getPoint "Start Point"  ))))
)

 

.  The Getangle function returns a value in RADIANS.  RTD will convert that to degrees.  Then because it's the last thing the function outputs, that's what will be displayed to you.

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

0 Likes
Message 4 of 10

doni49
Mentor
Mentor

One more quick example.  There have been a few rare instances in which I wanted to call a command (the special type of autolisp function listed above).  So if for some reason I wanted my lisp routine to run the AngleInDegrees function, I'd call it by way of the following code:

(c:AngleInDegrees)

 

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

0 Likes
Message 5 of 10

Sea-Haven
Mentor
Mentor

To get to your example dwg its probably simpler just using lisp rather than array, the solid is copied vertically ends added, top bottom added, just working in 3D. 

 

Copy vertical, select object, base point 0,0,0 new point 0,0,Y Enter.

 

Have a look at this very similar, you make a front end entering all the values then draw it.

 

 

0 Likes
Message 6 of 10

Moshe-A
Mentor
Mentor
Accepted solution

@ChrisSXYPP  hi,

 

check this 3DW command.

 

Wall direction can go to right (+X) or left (-X) as the wall width can go to up (+Y) or down (-Y)

also height can go up (+Z) or down (-Z)

you can undo the all process with one undo (like any other std command)

 

enjoy

Moshe

 

(defun c:3dw (/ _allowed_height _depth askHeight draw_line 			; local functions
	        PANEL-WIDTH BEAMD-WIDTH savOrthoMode defhgt ss			; local variables
	        p0 p1 p2 p3 hgt wth ename g0 g1 g2 g3 idx interval pts^)	; local variables

 ; return T if n is 95 or 119
 (setq _allowed_height (lambda (n) (member n '(-95 95 -119 119)))) ; anonymous function
 (setq _depth (lambda (h d) (if (minusp h) (* -1 d) d)))   ; anonymous function
  
 ; return allowed height
 (defun askHeight (pt def / val)
  (while (not (_allowed_height val))
   (initget 2)
   (setq val (getdist pt "\nHeight (95 or 119): "))

   (cond
    ((not val)
     (setq val def)
    ); case
    ((not (_allowed_height val))
     (prompt "\nAllowed value of 95 or 119 only.")
     (setq val nil)
    ); case
    ( t
     (setq def val)
    ); case
   ); cond
  ); while

  val
 ); askHeight


 (defun draw_line (t0 t1)
  (entmakex
   (list
    '(0 . "LINE")
    '(100 . "AcDbLine")
    (cons '10 (trans t0 1 0))
    (cons '11 (trans t1 1 0))
   ); list
  ); entmakex
 ); draw_line
 
  
 ; here start c:3dw
 (setvar "cmdecho" 0)	     ; disable commands echo
 (command "._undo" "_begin") ; start undo

 (setq PANEL-WIDTH 0.25) ; const
 (setq BEAMD-WIDTH 0.75) ; const
  
 (setq savOrthoMode (getvar "orthomode")) ; save ortho mode
 (setvar "orthomode" 1) ; enable ortho

 ; set default height
 (if (= (getvar "userr1") 0.0)
  (setq defhgt 95)
  (setq defhgt (getvar "userr1"))
 )
  
 (setq ss (ssadd)) ; declar selection set
  
 (if (and
       ; handle user input
       (setq p0 (getpoint "\nSpecify insertion point: "))
       (setq p1 (getpoint p0 "\nSpecify length: "))
       (ssadd (draw_line p0 p1) ss)
       (setq p2 (getpoint p0 "\nSpecify width: "))
       (ssadd (draw_line p0 p2) ss)
       (setvar "userr1" (setq hgt (askHeight p0 defhgt)))
     )
  (progn
   ; clear selection
   (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
    (entdel ename)
   )
    
   (setq p3 (polar p1 (angle p0 p2) (distance p0 p2)))
   (setq wth (distance p0 p2))

   (setq g3 (polar p1 (angle p1 p3) PANEL-WIDTH))
   (command "._rectangle" "_None" p0 "_None" g3)
   (ssadd (entlast) ss) ; add to selection

   (setq g3 (polar p3 (angle p3 p1) PANEL-WIDTH))
   (command "._rectangle" "_None" p2 "_None" g3)
   (ssadd (entlast) ss);  ; add to selection

   (setq g0 (polar p0 (angle p0 p2) PANEL-WIDTH))
   (setq g1 (polar g0 (angle p0 p1) BEAMD-WIDTH))
   (setq g3 (polar g1 (angle p0 p2) (- wth (* PANEL-WIDTH 2))))
   (command "._rectangle" "_None" g0 "_None" g3)
   (ssadd (entlast) ss) ;  ; add to selection

   (setq g0 (polar p1 (angle p1 p3) PANEL-WIDTH))
   (setq g1 (polar g0 (angle p1 p0) BEAMD-WIDTH))
   (setq g3 (polar g1 (angle p1 p3) (- wth (* PANEL-WIDTH 2))))
   (command "._rectangle" "_None" g0 "_None" g3)
   (ssadd (entlast) ss) ;  ; add to selection
   (command ".extrude" "_si" ss "_None" hgt)

   (setq g0 (polar (polar p0 (angle p0 p1) BEAMD-WIDTH) (angle p0 p2) PANEL-WIDTH))
   (setq g3 (polar (polar p3 (angle p3 p2) BEAMD-WIDTH) (angle p3 p1) PANEL-WIDTH))
   
   ; bottom beam
   (command "._rectangle" "_None" g0 "_None" g3)
   (command "._extrude" "_si" "_Last" "_None" (_depth hgt BEAMD-WIDTH))
   (setq ename (entlast))

   (setq idx 1 interval (/ (- hgt (_depth hgt BEAMD-WIDTH)) 5))
   (repeat 5
    (setq pts^ (cons (list 0.0 0.0 (* idx interval)) pts^))
    (setq idx (1+ idx))
   ); repeat
   
   (command "._copy" "_si" ename "_Multiple" "_None" "0,0" "_None" (car (reverse pts^))) ; first copy
   ; other copies
   (foreach pt (cdr (reverse pts^))
    (command "_None" pt)
   ); foreach
   (command "")
  ); progn
 ); if

 (setvar "orthomode" savOrthoMode) ; restore ortho mode
  
 (command "._undo" "_end") ; undo end
 (setvar "cmdecho" 1) 	   ; enable command echo
  
 (princ) ; clean exit
) ; c:3dw

 

 

0 Likes
Message 7 of 10

Sea-Haven
Mentor
Mentor

Just a comment I use Ldata rather than "userr1" as some one elses code may overwrite the user variables.

 

(vlax-ldata-get "Window" "Defhgt")

(vlax-ldata-put "Window" "Defhgt" defhgt)
0 Likes
Message 8 of 10

Moshe-A
Mentor
Mentor

@Sea-Haven ,

 

Could it be, the OP did not like my solution? 😭

 

Moshe

0 Likes
Message 9 of 10

ChrisSXYPP
Explorer
Explorer

Thank you very much for the code, Moshe.

Now I need to figure it out how it works haha. But it can do the job faster, for sure.

 

 

0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

This would be my approach have a look at the video posted earlier.

 

SeaHaven_0-1748501230180.png

 

 

 

0 Likes