LISP GRID COORDINATE

LISP GRID COORDINATE

chan230984
Advocate Advocate
4,043 Views
3 Replies
Message 1 of 4

LISP GRID COORDINATE

chan230984
Advocate
Advocate

I have LISP, but E Not Show

Please Help me Edit this lisp

Picture below

Untitled.png

 

 

(defun c:Ggrid()
 (setvar "cmdecho" 0)
   (if (not lxg) (setq lxg 100.0))
   (if (not lts) (setq lts 2.5))
   (if (not llt) (setq llt 10.0))
   (setq p nil)
   (setq p (append p (list (getpoint "\nBottom left corner: "))))
   (setq p (append p (list (getpoint (nth 0 p) "\nBottom right corner: "))))
   (setq bg (+ (angle (nth 0 p) (nth 1 p)) (/ pi 2)))
   (setq cp (getpoint (nth 0 p) "\nHeight: "))
   (setq dis (distance cp (inters cp (polar cp bg 10.0) (nth 0 p) (nth 1 p) nil)))
   (setq p (append p (list (polar (nth 1 p) bg dis))))
   (setq p (append p (list (polar (nth 0 p) bg dis))))
   (setq p (append p (list (nth 0 p))))
   (setq abase (getvar "angbase") adir (getvar "angdir"))
   (setvar "angbase" (/ pi 2))
   (setvar "angdir" 1)
   (setvar "blipmode" 0)
   (setq xg (getreal (strcat "\nGrid interval <" (rtos lxg 2 0) ">: ")))
   (if (not xg) (setq xg lxg) (setq lxg xg))
   (setq ts (getreal (strcat "\nText height <" (rtos lts 2 2) ">: ")))
   (if (not ts) (setq ts lts) (setq lts ts))
   (setq lt (getreal (strcat "\nTick length <" (rtos llt 2 2) ">: ")))
   (if (not lt) (setq lt llt) (setq llt lt))
   (setq minx (car (nth 0 p)) miny (cadr (nth 0 p)) maxx minx maxy miny)
   (setq n 1)
   (repeat 3
      (progn
         (if (< (car (nth n p)) minx) (setq minx (car (nth n p))))
         (if (< (cadr (nth n p)) miny) (setq miny (cadr (nth n p))))
         (if (> (car (nth n p)) maxx) (setq maxx (car (nth n p))))
         (if (> (cadr (nth n p)) maxy) (setq maxy (cadr (nth n p))))
         (setq n (1+ n))
      )
   )
   (setq xs (+ xg (* (fix (/ minx xg)) xg)) ys (+ xg (* (fix (/ miny xg)) xg)))
;;;do 'x' grid  (bearing = pi/2)
   (while (<= xs maxx)
      (setq n 0 plist nil)
      (repeat 4   ;;;;; find the 2 intersecting grid points with boundary
       (progn
         (if (setq ip (inters (list xs miny) (list xs maxy) (nth n p) (nth (1+ n) p)))
            (setq plist (append plist (list ip)))
         )
         (setq n (1+ n))
       )
      )
      (if (> (cadr (nth 0 plist)) (cadr (nth 1 plist)))
         (setq p2 (nth 0 plist) p1 (nth 1 plist))
         (setq p1 (nth 0 plist) p2 (nth 1 plist))
      )
      (command "line" p1 (polar p1 (/ pi 2) lt) "")
      (command "text" (polar p1 (/ pi 2 ) (* ts 3.0)) ts (angtos (/ pi 2)) (strcat "E " (rtos xs 2 0) ))
      (setq xs (+ xs xg))
   ) ;;; end while
;;;do 'y' grid  (bearing = 0)
   (while (<= ys maxy)
      (setq n 0 plist nil phil "done")
      (repeat 4   ;;;;; find the 2 intersecting grid points with boundary
       (progn
         (if (setq ip (inters (list minx ys) (list maxx ys) (nth n p) (nth (1+ n) p)))
            (setq plist (append plist (list ip)))
         )
         (setq n (1+ n))
       )
      )
      (if (> (car (nth 0 plist)) (car (nth 1 plist)))
         (setq p2 (nth 0 plist) p1 (nth 1 plist))
         (setq p1 (nth 0 plist) p2 (nth 1 plist))
      )
;;;do '+' grid marks
      (setq sx (car p1) ex (car p2) fx (+ xg (* (fix (/ sx xg)) xg)))
      (while (<= fx ex)
         (setq p0 (list fx ys))
         (command "line" (polar p0 pi (/ lt 2.0)) (polar p0 0.0 (/ lt 2.0)) "")
         (command "line" (polar p0 (/ pi 2) (/ lt 2.0)) (polar p0 (* pi 1.5) (/ lt 2.0)) "")
         (setq fx (+ fx xg))
         (command "text" (polar p0 0 (* ts 3.0)) ts (angtos 0) (strcat "N "(rtos ys 2 0) ))
      )
      (setq ys (+ ys xg))    
   ) ;;; end while

   (setvar "angbase" abase)
   (setvar "angdir" adir)
   (setvar "blipmode" 1)
   (princ)
)

0 Likes
4,044 Views
3 Replies
Replies (3)
Message 2 of 4

Kent1Cooper
Consultant
Consultant

It's a little hard to figure out all of what's going on, but I'm guessing that this test:

 

  (while (<= xs maxx)

 

is returning nil right from the start, so it doesn't do the whole thing that would put in the "E"-starting Text at all.  And that makes me wonder about what is setting the xs variable:

 

  (setq xs (+ xg (* (fix (/ minx xg)) xg)) ....

 

It looks to me like this piece of it:

 

  (fix (/ minx xg))

 

is meant to calculate the number of grid spacings in the overall width.  If minx is the X coordinate of the left edge of the area, dividing that by the grid size in the X direction would be meaningless, and could be vastly different depending on where you picked that bottom left corner.  If I'm correct in my assumptions, should it not be this instead?

  (fix (/ (- maxx minx) xg))

 

to divide the width of the area by the grid size in the X direction?  This part [not corrected as above]:

  (* (fix (/ minx xg)) xg)

 

apparently multiplies that number of spaces by the grid size, which should give a result similar to the width of the area, adjusted to a whole multiple of the grid size.  Then adding one grid-spacing to that:

 

  (+ xg (* (fix (/ minx xg)) xg))

 

seems like the wrong thing to do, if you're looking for a starting point for the first "E"-starting Text.  Depending on where the bottom left corner was set, the xs variable could be greater than the maxx variable right from the start, so the  (while (<= xs maxx)  loop would end without doing anything.

 

Should it just be adding the grid size to the minimum X coordinate instead for an initial location?

 

  (setq xs (+ xg minx) ....

 

Some tests, for your current code:

 

If you pick the bottom left corner in greatly different locations relative to the origin [positive and negative], does it put in "E" Texts at some locations, and not at others?

 

After you run it, what is contained in the xs and maxx variables?

Kent Cooper, AIA
Message 3 of 4

chan230984
Advocate
Advocate

@Kent1Cooper

 

please write txt

i don’t know how to change 

thanks

0 Likes
Message 4 of 4

Kent1Cooper
Consultant
Consultant

@chan230984 wrote:

....

please write txt

i don’t know how to change 

....


I would want to know more about what it's supposed to do.  There are things in it that I think can be done much more simply, but those are easy fixes compared to understanding the intent.  I tried it, and since it has something to deal with a non-orthogonal bottom edge, I did it that way.  [And I did it with running Osnap off -- the code should ensure that.]  I changed the density of the grid [smaller than your default], to better see the relationship to the defined area.

 

The magenta are the points I picked, and the green is the working area -- the effective rectangle defined by the points in the 'p' variable.  [I think the "Height" prompt should say something like "Point along top edge" instead, because the rubber-banding when picking it does not  represent the resulting height of the area.]

 

It does  put in some  "E" Texts, where they intersect the bottom edge of the rectangle, and one  along the left end [should the other (red) one also be drawn?].  Is that enough, and what it's intended to do, rather than put "E" Texts at all grid locations?  That part doesn't show in your sample image.  If you want it to put them at all grid locations, should it still put them along the edges?

 

The red parts were not  drawn by the routine [I added them], but I wonder whether they should be.

 

GGRID.PNG

Kent Cooper, AIA
0 Likes