Draw line from a list of points

Draw line from a list of points

carlos_m_gil_p
Advocate Advocate
4,711 Views
16 Replies
Message 1 of 17

Draw line from a list of points

carlos_m_gil_p
Advocate
Advocate

Hello how are you.
I wanted to check, if there is any lambda function, to draw lines from a list of points.

 

I normally do a loop with car and dcr, but I want to know if there is something more direct. And the only thing that occurred to me was with the lambda function, but I didn't know how to do it.

This is what I am using.

 

(setq lst-pts '((-2495.52 -924.94 0.0)
                (-2397.64 -924.672 0.0)
                (-2297.97 -889.794 0.0)
                (-2200.08 -810.342 0.0)
                (-2108.74 -776.927 0.0)
                (-1997.02 -818.492 0.0)))

(defun draw-line (p1 p2) 
  (entmake 
    (list '(0 . "LINE") 
          '(100 . "AcDbLine")
          (cons 10 p1)
          (cons 11 p2))))

 

I was trying something like that.

Modifying the draw-line function to pass the list of points directly to it.
And creating another function to apply it, but it didn't work for me.

 

(defun d-line (lst) 
  (foreach pt lst 
    (ent-linea pt)))

(defun draw-line (lst-pts) 
  (entmake 
    (list '(0 . "LINE") 
          '(100 . "AcDbLine")
          (cons 10 (nth 0 lst-pts))
          (cons 11 (nth 1 lst-pts)))))

 

As far as you can help me, I thank you.

Thank you.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
4,712 Views
16 Replies
Replies (16)
Message 2 of 17

Lee_Mac
Advisor
Advisor
Accepted solution

Upon defining a function such as the following to construct a line from two supplied points:

(defun drawline ( p q ) (entmake (list '(0 . "LINE") (cons 10 p) (cons 11 q))))

You can evaluate the function with the successive points in your list using a mapcar expression in the following way:

(mapcar 'drawline lst-pts (cdr lst-pts))

 

 

Message 3 of 17

Kent1Cooper
Consultant
Consultant
Accepted solution

At the risk of being scoffed at by those with an irrational fear of the (command) function:

 

(command "_.line")

(foreach pt lst-pts (command "_non" pt))

(command "")

Kent Cooper, AIA
0 Likes
Message 4 of 17

Anonymous
Not applicable

About to go to fishing club have a beer I would have done it that way, same method for plines. 

 

Not sure where the point list came from but if file read-line and do line no need for a list, or if csv open in excel and can copy and paste a column of LINE x1,y1 x2,Y2 then blank line  to command line, use concatetnate in excel.

Message 5 of 17

hosneyalaa
Advisor
Advisor
Accepted solution
(setq lst-pts '((-2495.52 -924.94 0.0)
                (-2397.64 -924.672 0.0)
                (-2297.97 -889.794 0.0)
                (-2200.08 -810.342 0.0)
                (-2108.74 -776.927 0.0)
                (-1997.02 -818.492 0.0)))

;; Alan Pizzamando
(mapcar (function (lambda (a b) (entmakex (list '(0 . "LINE") (cons 10 a) (cons 11 b)))))
              lst-pts
              (cdr lst-pts)
      )


 

 

 

3.JPG

0 Likes
Message 6 of 17

carlos_m_gil_p
Advocate
Advocate

Hi @Lee_Mac , thank you very much, super compact and functional.

Hi @hosneyalaa , very good option too, thank you.

Hi @Kent1Cooper , if you knew about this option, but wanted to avoid the commands, due to errors. Thanks.

Hello @Anonymous , I find it very laborious to do that, but for the future it can be used for other things. Thanks.

 

Thank you very much as always to all of you for your teachings.

Greetings.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 7 of 17

Kent1Cooper
Consultant
Consultant

@carlos_m_gil_p wrote:

....

Hi @Kent1Cooper , if you knew about this option, but wanted to avoid the commands, due to errors. ....


[What errors?  You may have fallen for the irrational-fear brainwashing.  I use (command) functions all over the place in lots of routines, and they never result in errors.]

Kent Cooper, AIA
0 Likes
Message 8 of 17

Lee_Mac
Advisor
Advisor

I'm not sure that I follow the 'brainwashing' and 'irrational fear' sentiments, but performance considerations and control over object properties aside, command calls certainly have their place in scenarios where it makes sense to leverage AutoCAD functionality; personally I generally try to avoid dependence on commands where possible, mainly due to the possibility of the command prompts changing with each AutoCAD release.

0 Likes
Message 9 of 17

ronjonp
Mentor
Mentor

@Lee_Mac wrote:

.... , command calls certainly have their place in scenarios where it makes sense to leverage AutoCAD functionality; personally I generally try to avoid dependence on commands where possible, mainly due to the possibility of the command prompts changing with each AutoCAD release.


Agreed. 🍻 

0 Likes
Message 10 of 17

pbejse
Mentor
Mentor
Accepted solution

@carlos_m_gil_p wrote:

 

(defun d-line (lst) 
  (foreach pt lst 
    (ent-linea pt)))

 


As for your sub-function

(defun d-line (lst / p)
  (foreach pt lst-pts
    (if	p
      (draw-line p pt)
    )
    (setq p pt)
  )
)

HTH

0 Likes
Message 11 of 17

ronjonp
Mentor
Mentor
Accepted solution

Another:

(setq lst-pts '((-2495.52 -924.94 0.0)
		(-2397.64 -924.672 0.0)
		(-2297.97 -889.794 0.0)
		(-2200.08 -810.342 0.0)
		(-2108.74 -776.927 0.0)
		(-1997.02 -818.492 0.0)
	       )
)

(while (cadr lst-pts)
  (entmakex (list '(0 . "LINE") (cons 10 (car lst-pts)) (cons 11 (cadr lst-pts))))
  (setq lst-pts (cdr lst-pts))
)
0 Likes
Message 12 of 17

carlos_m_gil_p
Advocate
Advocate

Hi @Kent1Cooper , the issue is that sometimes I get a command or command-s prompt, that's why I avoid using it.

Hi @pbejse and @ronjonp , thank you for your new options.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 13 of 17

Kent1Cooper
Consultant
Consultant

@carlos_m_gil_p wrote:

Hi @Kent1Cooper , the issue is that sometimes I get a command or command-s prompt, that's why I avoid using it.

....


If you're talking about the error suggestion to change (command) functions to (command-s), that happens only if you use a (command) function inside an *error* handler [without taking another step that the message describes], and only if the *error* handler is triggered [whether by an actual error happening or by cancelling with the ESCape key], and only in versions newer than I think Acad2015.  An error that triggers the *error* handler [and therefore that suggestion] could be caused by something incorrect in the inputs to a (command) function in the code, or by myriad other possible problems, but never by the mere fact that the (command) function is what was used to do something.  So change those instances inside *error* handlers, as the message suggests.  That's one of the limited situations where (command) should be avoided, but is not a reason to avoid it elsewhere.

Kent Cooper, AIA
Message 14 of 17

carlos_m_gil_p
Advocate
Advocate

Hi @Kent1Cooper , thanks for your explanation and help, I will keep it in mind from now on.
Greetings.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 15 of 17

Anonymous
Not applicable

I know solved but why not make the points a pline.

(defun LWPoly (lst cls)
 (entmakex (append (list (cons 0 "LWPOLYLINE")
                         (cons 100 "AcDbEntity")
                         (cons 100 "AcDbPolyline")
                         (cons 90 (length lst))
                         (cons 70 cls))
                   (mapcar (function (lambda (p) (cons 10 p))) lst))))

(lwpoly lst-pts 0)

 

0 Likes
Message 16 of 17

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

I know solved but why not make the points a pline.


That can be done with the approach in Message 3, by simply adding one letter, changing "_.line" to "_.pline", with the proviso that the current PLINEWID width setting will be used, which could be other than 0 [you might sometimes want it other than 0].

 

[The possibility of wanting to Close it can be incorporated, but wasn't because of the nature of the example set of points.]

Kent Cooper, AIA
0 Likes
Message 17 of 17

Anonymous
Not applicable

Good pickup (setvar 'plinewid 0.0) before making pline.

 

Interesting Bicscad ver 20 

Start of polyline:

Unable to recognize command "NON". This error can occur when the command is not supported for the active license.

 

(setq lst-pts '((-2495.52 -924.94 0.0)
		(-2397.64 -924.672 0.0)
		(-2297.97 -889.794 0.0)
		(-2200.08 -810.342 0.0)
		(-2108.74 -776.927 0.0)
		(-1997.02 -818.492 0.0)
	       )
)
(setvar 'plinewid 0.0)
(setvar 'osmode 0)
(command "_.pline")
(foreach pt lst-pts (command  pt))
(command "")

 

That was why I used the other method I have found some problems in Bricscad making Plines using various methods.

0 Likes