Lisp "SECTION GRID" - help

Lisp "SECTION GRID" - help

Edwin.Saez
Advisor Advisor
4,107 Views
17 Replies
Message 1 of 18

Lisp "SECTION GRID" - help

Edwin.Saez
Advisor
Advisor

Hello everyone, I have always received good help in this forum, and I would like you to help me with a lisp to generate a section grid; Eh attached the dwg, where eh is placed what I want the lisp can perform.


The sequence of use should be:

 

- ask the user to indicate the insertion point of the section grid
- ask the user to indicate each quantum (distance) to place the vertical lines, and the quantity
- ask the user to indicate each quantum (distance) to place the horizontal lines, and the quantity
- ask the user for the start quota and place the texts at the ends of each horizontal line
- at the lower end of the vertical lines generated, place the texts in a progressive form

So that they better understand the result of what is wanted, attached the dwg.

 

thanks for your help!

 

 

 

 

grillaa.jpg

 

 

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

0 Likes
4,108 Views
17 Replies
Replies (17)
Message 2 of 18

john.uhden
Mentor
Mentor

I just wrote one a few weeks ago for a stream cross section.  It works a little differently... you just feed it the list of coordinate points and it figures out the rest itself.  The grid spacing is hard-wired but we can fix that.  You'll notice that I prefer entmake to command because you can include properties like layer, color, and text alignment.

 

  (defun @grid (spts / x y xmin xmax ymin ymax)
    (setq xmin (apply 'min (mapcar 'car spts))
               xmin (- xmin (rem xmin 5) 5)
               xmax (apply 'max (mapcar 'car spts))
               xmax (+ xmax (- 5 (rem xmax 5)))
               ymin (apply 'min (mapcar 'cadr spts))
               ymin (- ymin (rem ymin 2) 2)
               ymax (apply 'max (mapcar 'cadr spts))
               ymax (+ ymax (- 2 (rem ymax 2)))
               x xmin y ymin
    )
    (while (<= x xmax)
      (entmakex (list '(0 . "LINE")'(8 . "GRID")(list 10 x ymin)(list 11 x ymax)))
      (if (zerop (rem x 10))
        (entmakex
          (list '(0 . "TEXT") '(67 . 0) '(410 . "Model") '(8 . "GRID")
            (list 10 x (- ymin 1)) '(40 . 1) (cons 1 (rtos x 2 0)) '(50 . 0.0) '(41 . 1.0) 
            '(51 . 0.0) '(7 . "Standard") '(71 . 0) '(72 . 4) (list 11 x (- ymin 1)) '(210 0.0 0.0 1.0)
            '(73 . 0)
          )
        )
      )
      (if (zerop (rem x 10))
        (entmakex
          (list '(0 . "TEXT") '(67 . 0) '(410 . "Model") '(8 . "GRID")
            (list 10 x (+ ymax 1)) '(40 . 1) (cons 1 (rtos x 2 0)) '(50 . 0.0) '(41 . 1.0) 
            '(51 . 0.0) '(7 . "Standard") '(71 . 0) '(72 . 4) (list 11 x (+ ymax 1)) '(210 0.0 0.0 1.0)
            '(73 . 0)
          )
        )
      )
      (setq x (+ x 5))
    )
    (while (<= y ymax)
      (entmakex (list '(0 . "LINE")'(8 . "GRID")(list 10 xmin y)(list 11 xmax y)))
      (if (zerop (rem y vsf))
        (entmakex
          (list '(0 . "TEXT") '(67 . 0) '(410 . "Model") '(8 . "GRID")
            (list 10 (- xmin 3) y) '(40 . 1) (cons 1 (rtos (/ y vsf) 2 0)) '(50 . 0.0) '(41 . 1.0) 
            '(51 . 0.0) '(7 . "Standard") '(71 . 0) '(72 . 4) (list 11 (- xmin 2) y) '(40 . 1) '(210 0.0 0.0 1.0)
            '(73 . 0)
          )
        )
      )
      (if (zerop (rem y vsf))
        (entmakex
          (list '(0 . "TEXT") '(67 . 0) '(410 . "Model") '(8 . "GRID")
            (list 10 (+ xmax 3) y) '(40 . 1) (cons 1 (rtos (/ y vsf) 2 0)) '(50 . 0.0) '(41 . 1.0) 
            '(51 . 0.0) '(7 . "Standard") '(71 . 0) '(72 . 4) (list 11 (+ xmax 2) y) '(40 . 1) '(210 0.0 0.0 1.0)
            '(73 . 0)
          )
        )
      )
      (setq y (+ y 2))
    )
    (command "_.zoom" "_E")
  )

John F. Uhden

0 Likes
Message 3 of 18

john.uhden
Mentor
Mentor

Oops.  The function uses a symbol, vsf, which is the vertical scale factor asked outside the function.  I typically use a vertical scale factor of 10.  The input argument, spts, represents the list of coordinate points exaggerated vertically using the function...

 

  (defun @exaggerate (p)
    (list (car p)(* vsf (cadr p)))
  )

as in...

(setq spts (mapcar '@exaggerate pts))

John F. Uhden

0 Likes
Message 4 of 18

Edwin.Saez
Advisor
Advisor

@john.uhden,

 

I would appreciate it if you could put the complete code, since I do not have any knowledge of lisp, sorry. Smiley Embarassed

thanks for helping

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

0 Likes
Message 5 of 18

john.uhden
Mentor
Mentor

I'm sorry.  I thought we were helping you to develop your own code.  My mistake.

John F. Uhden

0 Likes
Message 6 of 18

Edwin.Saez
Advisor
Advisor

@john.uhden,

 

I hoped he could share his routine, anyway thank you for responding.

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

0 Likes
Message 7 of 18

john.uhden
Mentor
Mentor

He did share his routine.  He was hoping you could put it to use.

John F. Uhden

Message 8 of 18

CADaSchtroumpf
Advisor
Advisor

Isn't exactely that you want, but can be used...

 

Example:

 

In a new drawing, draw a rectang with two corner; (0,3900) and (300,4075)

Make a zoom all, then zoom 0.5x.

Appload the routine

In command line type CARROYAGE

At message: Spécifiez le coin haut droit ou: [E] pour un nouvel Emplacement du coin bas gauche,[R] pour effectuer une Rotation, [+/-] un changement d'échelle <1000.000>:

Type E (without ENTER, just E)

Then type coordinates 0,3900

Use touch "-" for obtain scale (échelle) to 250.0

Move your cursor for encaps your rectang (you mus have dyn Rectang draw at screen: is format ISO A4)

When is good click your point and grid is draw with coordinate.

 

After you can erase the internal hatch nammed QUADISO, and the two polyline inside, the hatch REPQUADISO is be extend 

 

Sorry for my poor english

0 Likes
Message 9 of 18

hencoop
Advisor
Advisor

Hi @Edwin.Saez,

Autodesk has not yet started a "Write Free Code For Me" forum.  This forum is for those who want help learning to write code and help solving problems they have run into while writing their code.  Everyone is welcome, even those who have never written code before.

If you would like to learn how to code this for yourself I am sure that @john.uhden, myself and the many other folks who frequent this forum will be happy to help you.

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
Message 10 of 18

Edwin.Saez
Advisor
Advisor

@hencoop@john.uhden@CADaSchtroumpf,

 

The problem is that I still have no knowledge of lisp, so to speak, because I'm just beginning to study it. And since I need the lisp now, it would take me a long time to have the level needed to program what I need.
I also appreciate your comments.
Could you explain the sequence well to try to develop the lisp?

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

0 Likes
Message 11 of 18

hencoop
Advisor
Advisor

You have done the first step already:

1) Determine what you need to construct the grid;

2) Determine what information (variables) you want the user to input.

 

For your grid you need an origin and horizontal and vertical lines at some spacing, all of which your user will specify.  These values need to be stored in memory so that you can recall them for your line endpoint calculations.

 

Your program needs to be entirely contained in a (DEFUN <command name> ( / ) ...) statement.

The origin can be queried and stored using (SETQ origin_pt (GETPOINT "\nPick the origin: "))

The horizontal grid line spacing is the Y-distance desired between successive horizontal lines.  This can be queried using:

(SETQ Y-spacing (GETREAL "\nVertical spacing between horizontal lines: "))

The number of horizontal lines can be queried using: (SETQ horiz# (GETINT "\nNumber of horizontal lines: "))

The vertical grid line spacing can be done similarly:

(SETQ X-spacing (GETREAL "\nHorizontal spacing between vertical lines: "))

The number of vertical lines can be queried using: (SETQ vert# (GETINT "\nNumber of vertical lines: "))

 

Likewise, the elevation of the bottom horizontal line can be queried using: (SETQ base-elev (GETINT "\nBase grid elevation: ")); and

the starting station of the left vertical grid line can be queried using: (SETQ start-sta (GETINT "\nFirst station: "))

 

With the collected information you can calculate the horizontal and vertical line endpoints and the text insertion points.

 

If your grid should have a vertical scale exaggeration then you will need to query this too.

 

As a beginner, you can easily use the (COMMAND ...) function to draw the lines and text since you are already familiar with the LINE and TEXT commands; however, A better method is to use (ENTMAKE ...) to create them as @john.uhden mentioned.

 

The above put inside a (DEFUN ...) statement:

(DEFUN c:mygrid ( / )
  (SETQ origin_pt (GETPOINT "\nPick the origin: "))
  (SETQ Y-spacing (GETREAL "\nVertical spacing between horizontal lines: "))
  (SETQ horiz# (GETINT "\nNumber of horizontal lines: "))
  (SETQ X-spacing (GETREAL "\nHorizontal spacing between vertical lines: "))
  (SETQ vert# (GETINT "\nNumber of vertical lines: "))
  (SETQ base-elev (GETINT "\nBase grid elevation: "))
  (SETQ start-sta (GETINT "\nFirst station: "))
  (SETQ end1 origin_pt end2 (POLAR end1 0.0 (* (1- vert#) X-spacing)))
  (REPEAT horiz#
    (COMMAND "._LINE" "non" end1 "non" end2 "")
    (SETQ txt-pt (POLAR end1 PI (* X-spacing 0.25)))
    (COMMAND "._TEXT" "j" "mc" "non" txt-pt "0.1" 0.0 (RTOS base-elev 2 0));Use RTOS here to handle calculated values that are not integers
    ; Text height is hard coded 0.1 here.  May need to calculate text height to fit user input for grid.
    (SETQ base-elev (+ base-elev Y-spacing))
    (SETQ end1 (POLAR end1 (* PI 0.5) Y-spacing) end2 (POLAR end1 0.0 (* (1- vert#) X-spacing)))
  )
  (SETQ end1 origin_pt end2 (POLAR end1 (* PI 0.5) (* (1- horiz#) Y-spacing)))
  (REPEAT vert#
    (COMMAND "._LINE" "non" end1 "non" end2 "")
    (SETQ txt-pt (POLAR end1 (* PI 1.5) (* Y-spacing 0.25)))
    (COMMAND "._TEXT" "j" "mc" "non" txt-pt "0.1" 0.0 (RTOS start-sta 2 0));Use RTOS here to handle calculated values that are not integers
    ; Text height is hard coded 0.1 here.  May need to calculate text height to fit user input for grid.
    (SETQ start-sta (+ start-sta X-spacing))
    (SETQ end1 (POLAR end1 0.0 X-spacing) end2 (POLAR end1 (* PI 0.5) (* (1- horiz#) Y-spacing)))
  )
  (PRINC)
)

 Remaining issues are: appropriate text height; formatting Station text; Layering; Major label text for horizontal and vertical, etc.

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
0 Likes
Message 12 of 18

hencoop
Advisor
Advisor

P.S. If your text style has a height other than 0.0 then the text height specification is not included in your TEXT command and should be removed from the code I posted..

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
0 Likes
Message 13 of 18

Edwin.Saez
Advisor
Advisor

Please, could you place a comment for each line of code, to understand step by step what you did?

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

0 Likes
Message 14 of 18

hencoop
Advisor
Advisor
(DEFUN c:mygrid ( / origin_pt Y-spacing horiz# X-spacing vert# base-elev start-sta end1 end2 txt-pt); make symbols (variables) local so they are cleared from memory upon completion of the function
  ; store the origin point as symbol 'origin_pt.
  (SETQ origin_pt (GETPOINT "\nPick the origin: "))
  ; store the vertical grid spacing between horizontal grid lines as symbol 'Y-spacing.
  (SETQ Y-spacing (GETREAL "\nVertical spacing between horizontal lines: "))
  ; store the number of horizontal grid lines as symbol 'horiz#.
  (SETQ horiz# (GETINT "\nNumber of horizontal lines: "))
  ; store the horizontal grid spacing between vertical grid lines as symbol 'X-spacing.
  (SETQ X-spacing (GETREAL "\nHorizontal spacing between vertical lines: "))
  ; store the number of vertical grid lines as symbol 'vert#.
  (SETQ vert# (GETINT "\nNumber of vertical lines: "))
  ; store the bottom horizontal grid line elevation as symbol 'base-elev.
  (SETQ base-elev (GETINT "\nBase grid elevation: "))
  ; store the left vertical grid line station as symbol 'start-sta.
  (SETQ start-sta (GETINT "\nFirst station: "))
  ; store the left end point of the first horizontal grid line as symbol 'end1 and
  ; store the right end point of the first horizontal grid line as symbol 'end2
  ; calculated using the (POLAR <point> <angle> <distance>) function from end1 at angle 0.0° and
  ; for the number of vertical grid line spaces multiplied by 'X-spacing.
  (SETQ end1 origin_pt end2 (POLAR end1 0.0 (* (1- vert#) X-spacing)))
  ; Draw horizontal lines and place elevation text at the left end for the number of horizontal lines specified.
  (REPEAT horiz#
    (COMMAND "._LINE" "non" end1 "non" end2 "") ; place a horizontal line
    (SETQ txt-pt (POLAR end1 PI (* X-spacing 0.25))) ; calculate the left elevation text insertion point (for middle center "MC" justification).
    ; place the text (note that text height "0.1" is not necessary if your style text height is not 0.0).
    (COMMAND "._TEXT" "j" "mc" "non" txt-pt "0.1" 0.0 (RTOS base-elev 2 0)); Use RTOS here to handle subsequently calculated values that are not integers.
    ; Note: you can similarly calculate the text insertion point for the right end of the grid line and place the same text there.
    ; set the next horizontal grid line elevation (increment by Y-spacing) Note: if you add a vertical exaggeration account for it here
    (SETQ base-elev (+ base-elev Y-spacing))
    ; set the next horizontal grid line left and right end points (end1 & end2).
    (SETQ end1 (POLAR end1 (* PI 0.5) Y-spacing) end2 (POLAR end1 0.0 (* (1- vert#) X-spacing)))
  )
  ; store the bottom end point of the first vertical grid line as symbol 'end1 and
  ; store the top end point of the first vertical grid line as symbol 'end2
  ; calculated using the (POLAR <point> <angle> <distance>) function from end1 at angle (/ PI 2.0) = 90.0° and
  ; for the number of horizontal grid line spaces multiplied by 'Y-spacing.
  (SETQ end1 origin_pt end2 (POLAR end1 (* PI 0.5) (* (1- horiz#) Y-spacing)))
  ; Draw vertical lines and place station text at the bottom end for the number of vertical lines specified
  (REPEAT vert#
    (COMMAND "._LINE" "non" end1 "non" end2 ""); place a vertical line
    (SETQ txt-pt (POLAR end1 (* PI 1.5) (* Y-spacing 0.25))) ; calculate the station text insertion point (for middle center "MC" justification).
    ; place the text (note that text height "0.1" is not necessary if your style text height is not 0.0).
    (COMMAND "._TEXT" "j" "mc" "non" txt-pt "0.1" 0.0 (RTOS start-sta 2 0));Use RTOS here to handle subsequently calculated values that are not integers
    ; set the next vertical grid line station (increment by X-spacing)
    (SETQ start-sta (+ start-sta X-spacing))
    ; set the next vertical grid line bottom and top end points (end1 & end2).
    (SETQ end1 (POLAR end1 0.0 X-spacing) end2 (POLAR end1 (* PI 0.5) (* (1- horiz#) Y-spacing)))
  )
  (PRINC); cleanly exit the function (no command prompt remnants)
)

 

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
0 Likes
Message 15 of 18

john.uhden
Mentor
Mentor

Sheesh.  I'll bet you're a javelin catcher in your spare time.

John F. Uhden

0 Likes
Message 16 of 18

hencoop
Advisor
Advisor

@john.uhden Lol, I said I would be happy to help @Edwin.Saez learn so I thought it important to prove it.  It is not a complete solution to what he wants but it is a complete framework and explanation of how to make it a complete solution without much more code.

 

If @Edwin.Saez learns to create the complete grid solution he wants from this then he will have a whole new ability opened up for him as he goes forward.  That's my objective.

 

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024
0 Likes
Message 17 of 18

Edwin.Saez
Advisor
Advisor

@hencoop,

 

I really appreciate your help
With this I will be able to understand a bit the lodge of the sequence that I must have to learn the lisp.
As you know I'm just beginning to study the lisp language, then it will take me a while to understand and make some modifications.
Maybe there are other functions I need to learn as the "entmake", since I need to put everything in layers, text styles.
But I will continue, thank you for helping me!
Sorry for some mistakes in my english

Edwin Saez


LinkedIn / AutoCAD Certified Professional


EESignature


 


Si mi respuesta fue una solución para usted, por favor seleccione "Aceptar Solución", para que también sirva a otro usuarios.

0 Likes
Message 18 of 18

hencoop
Advisor
Advisor

@Edwin.Saez you are welcome.

 

The most basic use of Autolisp is the COMMAND function.  Whatever you would do at the command line of AutoCAD can be put in a (COMMAND ...) function.  IF you need to set the layer or style or whatever you can put what you would have entered at the command line in a (COMMAND ...) function.

 

In the c:mygrid function, if your command input needs to happen with every line drawn then put it in the (REPEAT ...) functions; otherwise, put it before or after as the sequence of your command line input would normally occur.

 

Points can be calculated using the (POLAR ...) command as I have done in the code. Your major axis label points and the right elevation points can easily be calculated using (POLAR ...).  The function (DISTANCE <point> <point>) can give you the lengths of the horizontal and vertical lines and half of this distance is a length you need to find the major axis' label points.  Math functions are coded using the operators thus: (operator <number> <number>).  Operators are: -; +; *; /

 

Once you have stored the additional text insertion points you can use the TEXT command just as I have.  Just copy it and change the insertion point, text, and angle if necessary for your remaining labels.

 

You don't have too much to learn or very far to go to finish your routine.

AutoCAD User since 1989. Civil Engineering Professional since 1983
Product Version: 13.6.1963.0 Civil 3D 2024.4.1 Update Built on: U.202.0.0 AutoCAD 2024.1.6
                        27.0.37.14 Autodesk AutoCAD Map 3D 2024.0.1
                        8.6.52.0 AutoCAD Architecture 2024