Lisp to automatically draw a rectangle based off width and height

Lisp to automatically draw a rectangle based off width and height

bailey_paulsen
Contributor Contributor
719 Views
3 Replies
Message 1 of 4

Lisp to automatically draw a rectangle based off width and height

bailey_paulsen
Contributor
Contributor

I have this old lisp that creates a flattened elevation of a curved surface.

 

Im trying to modify it to draw a flattened elevation of a regular surface. I realize i could just trace it with a rectangle but sometimes the ucs isn't the right direction so you have to change views, and then draw the rectangle which is just extra steps. Or sometimes the surface is not perpendicular to the x and y axis and is at an angle, so you cant draw a rectangle on it unless you use the ucs command and set it to the face of that object.

 

The original code is at the top. Then below is 5 versions of the qelev command ive wrote. All of them should pretty much do the same thing. With each new iteration i tried to simplify it more and more. The 5th version is about as simple as i think i could get it. I got rid of some parts like drawing a line, and then deleting that line later on. Or creating an object, then setting a variable to that object, then creating another variable of the vla number of that object.

 

None of these commands are even showing up when i start typing it into my command bar. Im sure theres something simple to fix that. Mostly what im looking for is someone to look at the 5 versions of this command and help me decide which one is most "properly" written. I understand sometimes theres a better way to do something and simple isnt always the answer.


Then also at the bottom theres a qfab command im working on that somewhat does the same thing but makes it 3 dimensional. I just cant figure out how to get the user input for the face selection.

 

; Make a flattened elevation

(defun dtr ( deg ) (* pi (/ deg 180.0)))

; Make a flattened elevation of an arc

(defun c:elevflat ()

(SETQ a1 (GETPOINT "\nPICK start piont of ARC:"))
	 (SETQ a2 (GETPOINT "\nPICK 2nd piont of ARC:"))
	  (SETQ a3 (GETPOINT "\nPICK end point of ARC:"))
	(command "arc" a1 a2 a3)

(setq cm (getvar "cmdecho"))
(setvar"cmdecho" 0)
(setq arcel (entsel "\nSelect an Arc: "))
(while
 (if
   (/= (cdr(assoc 0 (entget(car arcel)))) "ARC") 
    (progn
      (prompt "\nSelected Object is not an ARC")
      (setq arcel (entsel "\nSelect Arc: "))
    )
 )
)
(setq obj (vlax-ename->vla-object (car arcel)))
(setq LEN (vla-get-ArcLength obj))
(command "erase" "last" "")
(setvar "cmdecho" cm)
(princ)

(prompt (strcat "\nThe arc length is "
                                 (rtos len 2 4)
                         )
                 )
		 (princ)

(SETQ Ver (Getdist "\nEnter Panel Height to draw rectangle: <94.5>: "))
	(IF (= Ver nil) (setq ver 94.5))
		(SETQ P1 (GETPOINT "\nPick location for lower left corner of panel:"))
		 (SETQ P2 (POLAR P1 (dtr 0) LEN))
		  (SETQ P3 (Polar P2 (dtr 90) ver))
		   (SETQ P4 (Polar P1 (dtr 90) ver))
	
		 (COMMAND "PLINE" P1 P2 P3 P4 "c"))
;


(defun c:qelev1 ()

(setq a1 (getpoint "\nPick Start Point:")
      a2 (getpoint "\nPick End Point:")
         (command "line" a1 a2)
      linesel (entsel "\nSelect an line: ")
      obj (vlax-ename->vla-object (car linecel))
      len (vla-get-Length obj)
          (command "erase" "last" "")
      ver (getdist "\nEnter Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
      p2 (polar p1 (dtr 0) len))
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
         (command "pline" p1 p2 p3 p4 "c")
)
)

;

(defun c:qelev2 ()

(setq a1 (getpoint "\nPick Start Point:")
      a2 (getpoint "\nPick End Point:")
      linesel (command "line" a1 a2)
      obj (vlax-ename->vla-object (car linecel))
      len (vla-get-Length obj)
          (command "erase" "last" "")
      ver (getdist "\nEnter Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
      p2 (polar p1 (dtr 0) len))
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
         (command "pline" p1 p2 p3 p4 "c")
)
)

;

(defun c:qelev3 ()

(setq a1 (getpoint "\nPick Start Point:")
      a2 (getpoint "\nPick End Point:")
      obj (vlax-ename->vla-object (command "line" a1 a2))
      len (vla-get-Length obj)
          (command "erase" "last" "")
      ver (getdist "\nEnter Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
      p2 (polar p1 (dtr 0) len))
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
         (command "pline" p1 p2 p3 p4 "c")
)
)

;

(defun c:qelev4 ()

(setq a1 (getpoint "\nPick Start Point:")
      a2 (getpoint "\nPick End Point:")
      len (distance a1 a2)
      ver (getdist "\nEnter Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
      p2 (polar p1 (dtr 0) len))
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
         (command "pline" p1 p2 p3 p4 "c")
)
)

;

(defun c:qelev5 ()

(setq hor (getdist "\nSet Elevation Width: ")
      ver (getdist "\nSet Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
      p2 (polar p1 (dtr 0) hor))
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
)
(command "ucs" "world")
(command "pline" p1 p2 p3 p4 "c")
)

;

(defun c:qfab ()

(setq a1 (getpoint "\nPick 1st Point:")
      a2 (getpoint "\nPick 2nd Point:")
      a3 (getpoint "\nPick 3rd Point:")
      len (distance a1 a2)
      ver (distance a2 a3)
      p1 (a1)
      p2 (polar p1 (dtr 0) len))
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
         (command "pline" p1 p2 p3 p4 "c")
)
(command "ucs" "face" USER INPUT "")
(command "pline" p1 p2 p3 p4 "c")
(command "extrude" "last" "0.25")
)

;
0 Likes
720 Views
3 Replies
Replies (3)
Message 2 of 4

annoisscary
Advocate
Advocate

Without delving to deep into everything, right of the bat I have a couple of tips. Firstly, if you are drawing rectangles why not just use the rectangle command? 2 points, calculated with width and height. Just a couple of lines shorter for the same effect. Secondly, for user input during a command call just use pause, for instance

(command "._UCS" "face" pause "")

 

Also as far as none of the commands showing up, if this is all in a single lisp file its because you have a bunch of extra closing parenthesis spread out through the whole thing.

0 Likes
Message 3 of 4

bailey_paulsen
Contributor
Contributor

Okay so i applied your notes about using rectangles instead of a pline. I also added the pause in the ucs command.

 

As far as the commands not showing up when i type them in i still cant fix it. I opened it up in visual studio so it paired parenthesis for me and im not seeing any extra parenthesis anymore.

(defun c:qelev5 ()

(setq hor (getdist "\nSet Elevation Width: ")
      ver (getdist "\nSet Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
      p2 (polar p1 (dtr 0) hor)
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
)
(command "ucs" "world")
(command "pline" p1 p2 p3 p4 "c")
)

;

(defun c:qelev6 ()

(setq hor (getdist "\nSet Elevation Width: ")
      ver (getdist "\nSet Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
)
(command "ucs" "world")
(command "rect" p1 hor ver)
)

;

(defun c:qfab1 ()

(setq a1 (getpoint "\nPick 1st Point:")
      a2 (getpoint "\nPick 2nd Point:")
      a3 (getpoint "\nPick 3rd Point:")
      len (distance a1 a2)
      ver (distance a2 a3)
      p1 (a1)
      p2 (polar p1 (dtr 0) len)
      p3 (polar p2 (dtr 90) ver)
      p4 (polar p1 (dtr 90) ver)
         (command "pline" p1 p2 p3 p4 "c")
)
(command "ucs" "face" pause "")
(command "pline" p1 p2 p3 p4 "c")
(command "extrude" "last" "0.25")
)

;

(defun c:qfab2 ()

(setq a1 (getpoint "\nPick 1st Point:")
      a2 (getpoint "\nPick 2nd Point:")
      a3 (getpoint "\nPick 3rd Point:")
      len (distance a1 a2)
      ver (distance a2 a3)
)
(command "ucs" "face" pause "")
(command "rect" a1 hor ver)
(command "extrude" "last" "0.25")
)
0 Likes
Message 4 of 4

annoisscary
Advocate
Advocate

qfab 1 you have a command call inside the setq block, move it outside. that should fix the issue with them not appearing.

 

also for the rectangle part personally I prefer using 2 points like so,

(defun c:qelev6 (/)

(setq 
      hor (getdist "\nSet Elevation Width: ")
      vert (getdist "\nSet Elevation Height: ")
      p1 (getpoint "\nEnter Location:")
      p2 (mapcar '+ p1 (list hor vert 0))
)
(command "_.UCS" "world")
(command "_.Rectangle" p1 p2)
)
0 Likes