Author Topic: How to draw a line or a polyline with Foreach function  (Read 2803 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
How to draw a line or a polyline with Foreach function
« on: September 12, 2011, 03:31:44 PM »
Hello everybody .

I stock with this part of code , I want to use foreach function to draw a line or polyline .

Code: [Select]
(setq pnts '((11 34.614 7.48336 0.0) (11 18.7203 7.48336 0.0) (11 14.9239 12.8179 0.0) (11 7.42747 12.8179 0.0)))

(foreach p pnts
  (command "_.line" ......
           ......

Thanks in advance

David Bethel

  • Swamp Rat
  • Posts: 656
Re: How to draw a line or a polyline with Foreach function
« Reply #1 on: September 12, 2011, 04:12:13 PM »
There are tons of way to accomplish this

Assuming that you want to strip the 11 off of the front end of each atom:

Code: [Select]
  (while (> (length pnts) 1)
         (command "_.LINE" (cdr (nth 0 pnts)) (cdr (nth 1 pnts)) "")
         (setq pnts (cdr pnts)))

And that you don't mind destroying the list 'pnts....
-David
R12 Dos - A2K

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to draw a line or a polyline with Foreach function
« Reply #2 on: September 12, 2011, 04:26:43 PM »
Here is another way to consider:
Code: [Select]
(defun makelines (lst lay / lastpt newlines)
  (foreach pt lst
    (setq pt (cdr pt))                  ; strip 11
    (if lastpt
      (setq newlines
             (cons
               (entmakex
                 (list (cons 0 "LINE")  ;***
                       (cons 6 "BYLAYER")
                       (cons 8 lay)
                       (cons 10 pt)     ;***
                       (cons 11 lastpt) ;***
                       (cons 39 0.0)
                       (cons 62 256)
                       (cons 210 (list 0.0 0.0 1.0))
                 )
               )
               newlines
             )
      )
    )
    (setq lastpt pt)
  )
  newlines
)

Code: [Select]
(setq pnts '((11 34.614 7.48336 0.0) (11 18.7203 7.48336 0.0) (11 14.9239 12.8179 0.0) (11 7.42747 12.8179 0.0)))
(makelines pnts "0")
(<Entity name: 7ee8b608> <Entity name: 7ee8b600> <Entity name: 7ee8b5f8>)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Coder

  • Swamp Rat
  • Posts: 827
Re: How to draw a line or a polyline with Foreach function
« Reply #3 on: September 13, 2011, 12:28:08 AM »
Thank you , both are working great .

Will it be possible with MLINE and POLYLINE ?

Appreciated a lot .

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to draw a line or a polyline with Foreach function
« Reply #4 on: September 13, 2011, 02:45:21 AM »
Thank you , both are working great .

Will it be possible with MLINE and POLYLINE ?

Appreciated a lot .
Well, that should be quite simple. For a pline you could go with:
Code: [Select]
(defun makeplines (lst / )
  (command "_.PLINE")
  (foreach pt lst
    (command "_None" pt)
  )
)
I tested your list like this:
Code: [Select]
(setq pnts '((11 34.614 7.48336 0.0) (11 18.7203 7.48336 0.0) (11 14.9239 12.8179 0.0) (11 7.42747 12.8179 0.0)))
(makeplines (mapcar 'cdr pnts))
Note the "_None" object snap to allow it to work even if you've got running object snaps turned on. IMO it's a better idea than to temporarily set SnapMode, otherwise you need extra error traps and such.

But  ;D ... I'd advise going with CAB's idea of creating the line/pline programatically, instead of through a command call. It allows for more options and generally runs faster. How to accomplish that for an MLine might be a bit complex though.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

HofCAD

  • Guest
Re: How to draw a line or a polyline with Foreach function
« Reply #5 on: September 13, 2011, 05:43:08 AM »
Thank you , both are working great .

Will it be possible with MLINE and POLYLINE ?

Appreciated a lot .
Code: [Select]
(defun MakePlines (Pnts Lay Close / Z)
  (if (setq Z (cadddr (car Pnts)))
    Z
    (setq Z 0)
  )
  (entmakex
    (append (list '(0 . "LWPOLYLINE")
  '(100 . "AcDbEntity")
  '(100 . "AcDbPolyline")
  (cons 8 Lay)
  (cons 90 (length Pnts))
  (cons 38 Z)
  (cons 70 Close);;;0 = open 1 = closed
    )
    (mapcar (function (lambda (p) (cons 10 (cdr p))))
    Pnts
    )
    )
  )
  (princ)
)

Code: [Select]
(setq Pnts '((11 34.614 7.48336 0.0) (11 18.7203 7.48336 0.0) (11 14.9239 12.8179 0.0) (11 7.42747 12.8179 0.0)))
(makePlines Pnts "0" 0)

Regards HofCAD CSI

VVA

  • Newt
  • Posts: 166

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: How to draw a line or a polyline with Foreach function
« Reply #7 on: September 13, 2011, 09:00:20 AM »
Another quick example:

Code: [Select]
(defun _makepline ( lst )
    (entmakex
        (append
            (list
                (cons 0 "LWPOLYLINE")
                (cons 100 "AcDbEntity")
                (cons 100 "AcDbPolyline")
                (cons 90 (length lst))
                (cons 70 0)
            )
            (mapcar '(lambda ( p ) (cons 10 (cdr p))) lst)
        )
    )
)

Code: [Select]
(_makepline pnts)
You might find this thread of benefit.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: How to draw a line or a polyline with Foreach function
« Reply #8 on: September 13, 2011, 09:14:13 AM »
some other way....

Code: [Select]


(setq ptlist '((0.0 0.0)(10.0 0.0)(10.0 10.0)(20.0 10.0)))


;;EXAMPLE 1
(command "pline")
(mapcar '(lambda (x)
   (command x)
   )
ptlist)
(command "")


   

;;EXAMPLE 2
(command "_.pline")
(foreach pt PtList
 (command pt)
)
(command "")



;;EXAMPLE 3
   (apply 'command
    (append
       '(".pline")
        ptlist
       '("")
    )
)
Keep smile...

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: How to draw a line or a polyline with Foreach function
« Reply #9 on: September 13, 2011, 09:45:22 AM »
For the LINE portion:

Code: [Select]
(defun _lines (lst)
  (mapcar '(lambda (a b) (entmakex (list '(0 . "LINE") (cons 10 a) (cons 11 b))))
          lst
          (cdr lst)
  )
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to draw a line or a polyline with Foreach function
« Reply #10 on: September 13, 2011, 10:40:13 AM »
Very clever Alan.  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to draw a line or a polyline with Foreach function
« Reply #11 on: September 13, 2011, 11:06:52 AM »
Yep! Never thought of doing that. I'd have made some global variable (or rather local to surrounding defun), then setq'd from inside the lambda. Alan's is much neater!
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: How to draw a line or a polyline with Foreach function
« Reply #12 on: September 13, 2011, 11:34:55 AM »
 :-)

Code: [Select]
(defun _linesClosed (lst)
  (mapcar '(lambda (a b) (entmakex (list '(0 . "LINE") (cons 10 a) (cons 11 b))))
          lst
          (append (cdr lst) (list (car lst)))
  )
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to draw a line or a polyline with Foreach function
« Reply #13 on: January 09, 2012, 10:34:01 AM »
:-)

Code: [Select]
(defun _linesClosed (lst)
  (mapcar '(lambda (a b) (entmakex (list '(0 . "LINE") (cons 10 a) (cons 11 b))))
          lst
          (append (cdr lst) (list (car lst)))
  )
)

I recommend:
Code: [Select]
(defun _linesClosed (lst)
  (mapcar '(lambda (a b) (entmakex (list '(0 . "LINE") (cons 10 a) (cons 11 b))))
          (cons (last lst) lst)
          lst
  )
)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: How to draw a line or a polyline with Foreach function
« Reply #14 on: January 09, 2012, 10:44:47 AM »
:-)

Code: [Select]
(defun _linesClosed (lst)
  (mapcar '(lambda (a b) (entmakex (list '(0 . "LINE") (cons 10 a) (cons 11 b))))
          lst
          (append (cdr lst) (list (car lst)))
  )
)

I recommend:
Code: [Select]
(defun _linesClosed (lst)
  (mapcar '(lambda (a b) (entmakex (list '(0 . "LINE") (cons 10 a) (cons 11 b))))
          (cons (last lst) lst)
          lst
  )
)
nice
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox