Automatically drawing a grid

Discussion in 'AutoCAD' started by SkintSubby, Dec 2, 2004.

  1. SkintSubby

    SkintSubby Guest

    I got this code from earlier threads on these newsgroups, and it works well, however my problem is that I want to just randomly pick 2 points (ie 2345,3678 and 12345, 3678), then pick a grid size (ie 100) I then need it to draw the lines on the relative grid lines of 100 .ie 2400, 2500, 2600 etc in the easting direction and 3700, 3800, 3900 etc in the northing direction.

    I know I have to do getpoint to input my random pickpoints, but how do I get the lisp to round up to my grid size? Then stop when it reaches it's max?

    MG




    ;**************************MAPGRID.LSP*******************************

    ; this program written by David Sledd for Energy Fuels
    ; will create a coordinate grid and label each grid line
    ; with the appropriate value.
    ; * * You will be prompted for the Lower Left corner of the grid
    ; * * MAKE SURE THIS POINT IS DIVISIBLE BY THE SPACING VALUE YOU SPECIFY!!
    ; FOR INSTANCE - IF YOU SPECIFY A SPACING OF 100, YOUR LOWER LEFT
    ; COORDINATE SHOULD BE SOMETHING LIKE 400,300 NOT 430,320.


    ; **********************************************************************


    (DEFUN C:MAPGRID ()

    (setq BM (getvar "BLIPMODE"))
    (setvar "BLIPMODE" 0)
    (setq OS (getvar "OSMODE"))
    (setvar "OSMODE" 0)




    ;*****Define Parameters*****

    (command "layer" "m" "grid" "")
    (setq LL (getpoint
    "\nEnter Coordinates for Lower Left corner of grid: "))
    (setq GW (getdist "\nEnter width of grid: "))
    (setq GH (getdist "\Enter height of grid: "))
    (setq SP (getdist "\nEnter spacing of Lines: "))
    (setq TZ (getdist "\nEnter TEXT Height: "))
    (setq TP (/ TZ 2.0))

    (setq PT1 LL)
    (setq PT2 (list (+ (car LL) GW)(cadr LL)))
    (setq PT3 (list (car LL)(+ (cadr LL) GH)))
    (setq TX1 (list (-(car PT3) TP) (- (cadr PT3) TP)))
    (setq TX2 (list (+ (car PT1) TP) (+ (cadr PT1) TP)))

    (command "pline" PT1 PT2 "")
    (command "text" TX2 TZ "0" (strcat (rtos (cadr PT1) 2 0) " N") "")

    (command "Pline" PT1 PT3 "")
    (command "text" "R" TX1 TZ "90" (strcat (rtos (car PT1) 2 0) " E") "")


    ;*****Draw Grid****

    (setq NH (/ GW SP))
    (setq NV (/ GH SP))
    (setq NH (fix NH))
    (setq NV (fix NV))
    (setq C SP)

    (repeat NH
    (setq P1 (list (+ (car LL) C) (cadr LL)))
    (setq P2 (list (car P1) (+ (cadr P1) GH)))
    (setq P3 (list (-(car P2) TP) (- (cadr P2) TP)))
    (setq P4 (list (-(car P1) TP) (+ (cadr P1) TP)));;
    (command "Pline" P1 P2 "")
    (command "text" "R" P3 TZ "90" (strcat (rtos (car P1) 2 0) " E") "")
    (command "text" P4 TZ "90" (strcat (rtos (car P1) 2 0) " E") "");;
    (setq C (+ C SP))
    )

    (setq C SP)
    (repeat NV
    (setq P1 (list (car LL) (+ (cadr LL) C)))
    (setq P2 (list (+ (car P1) GW) (cadr P1)))
    (setq P3 (list (+ (car P1) TP) (+ (cadr P1) TP)))
    (setq P4 (list (-(car P2) TP) (+ (cadr P2) TP)));;
    (command "pline" P1 P2 "")
    (command "text" P3 TZ "0" (strcat (rtos (cadr P1) 2 0) " N") "")
    (command "text" "R" P4 TZ "0" (strcat (rtos (cadr P1) 2 0) " N") "")
    (setq C (+ C SP))
    )

    (setvar "BLIPMODE" BM)
    (setvar "OSMODE" OS)
    (command "zoom" "E")
    (PRINC)

    )
     
    SkintSubby, Dec 2, 2004
    #1
  2. You can extract the X and Y coordinates of the lower left point, divide by
    100, use (fix) to round down to the next lower integer, add one, and
    multiply that by 100. If you setq the corner as "lowerleft" at your
    (2345,3678), then

    (* (1+ (fix (/ (car lowerleft) 100))) 100)

    should return 2400. Do the same with the Y coordinate (cdr lowerleft).

    If you save that 2400 to a value, use a (while) loop to check whether it's
    still lower than 12345, draw the first line, and increase it by 100.

    Etc.....
     
    Kent Cooper, AIA, Dec 2, 2004
    #2
  3. You could also simply use your two corners to make a Rectangle, and do the
    grid as a hatch pattern (user-defined, spacing of 100, zero degrees
    rotation, double) in it, then erase the rectangle. Make sure SNAPBASE is
    0,0 so the hatch lines will be at the multiples of 100. You'd still have to
    do calculations for the text parts, but it would save you loads of line (or
    pline as you have it, which I don't think gains you anything) commands to
    make the grid itself, and all the calculations that go into those. And it
    would ensure that nobody can accidentally alter one of the grid lines
    (unless they do something they shouldn't to the entire grid, of course).
    --
    Kent Cooper, AIA


    .....
     
    Kent Cooper, AIA, Dec 2, 2004
    #3
  4. SkintSubby

    SkintSubby Guest

    Thanks KC

    Your first suggestion helped a lot.

    MG
     
    SkintSubby, Dec 3, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.