POLYLINE COORDINATES USING MAPCAR

POLYLINE COORDINATES USING MAPCAR

vishshreevT578L
Advocate Advocate
2,643 Views
13 Replies
Message 1 of 14

POLYLINE COORDINATES USING MAPCAR

vishshreevT578L
Advocate
Advocate

HELLO ASPIRANTS

 

MY QUERY IS....AFTER I USING MAPCAR IN  THIS CODE I JUST GET A FIRST COORDINATE OF EACH POLYLINE WHEREIN I NEED ALL THE COORDINATES

 

;;;;;CODE;;;;

 

(DEFUN PL:EXTR-PNT-FROM-LINE ( / _LWPOLY TEST )
(SETQ _LWPOLY (SSGET '((0 . "LWPOLYLINE"))))
(SETQ _LWPOLY (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX _LWPOLY))))
(SETQ TEST (ENTGET (CAR _LWPOLY)))
(MAPCAR '(LAMBDA (X) (CDR (ASSOC 10 (ENTGET X)))) _LWPOLY)
)

 

PLEASE SUGGEST

 

 

Shreev
0 Likes
Accepted solutions (1)
2,644 Views
13 Replies
Replies (13)
Message 2 of 14

vishshreevT578L
Advocate
Advocate

SORRY 

 

HELLO ASPIRANTS

 

MY QUERY IS....AFTER I USING MAPCAR IN  THIS CODE I JUST GET A FIRST COORDINATE OF EACH POLYLINE WHEREIN I NEED ALL THE COORDINATES

 

;;;;;CODE;;;;

 

(DEFUN PL:EXTR-PNT-FROM-LINE ( / _LWPOLY TEST )
(SETQ _LWPOLY (SSGET '((0 . "LWPOLYLINE"))))
(SETQ _LWPOLY (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX _LWPOLY))))
(MAPCAR '(LAMBDA (X) (CDR (ASSOC 10 (ENTGET X)))) _LWPOLY)
)

 

I KNOW I AM USING MAPCAR SOMEWHERE WRONG BUT LOST FROM LONG TIME NOW

 

PLEASE SUGGEST

Shreev
0 Likes
Message 3 of 14

ВeekeeCZ
Consultant
Consultant

You did not paid enough attention when Kent tried to explain to you HERE

You need the two more operation with lisp - one (vl-remove-if-not) to extract all 10' code (10 x-coord y-coord) and then mapcar to extract (x-coord y-coord) from your  (10 x-coord y-coord)

 

(DEFUN PL:EXTR-PNT-FROM-LINE ( / _LWPOLY TEST )
  (SETQ _LWPOLY (SSGET '((0 . "LWPOLYLINE"))))
  (SETQ _LWPOLY (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX _LWPOLY))))
  (MAPCAR '(LAMBDA (x)

             (mapcar
               'cdr
               (vl-remove-if-not
                 '(lambda (y) (= (car y) 10))
                 (entget x))))
          
          _LWPOLY))

Blue is Kent's code.

 

 

And the red part is wrong... because LWPOLYLINE contains multiple 10 code, but (assoc 10 (entget en)) always takes just a first one.

 

(DEFUN PL:EXTR-PNT-FROM-LINE ( / _LWPOLY TEST )
(SETQ _LWPOLY (SSGET '((0 . "LWPOLYLINE"))))
(SETQ _LWPOLY (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX _LWPOLY))))
(MAPCAR '(LAMBDA (X) (CDR (ASSOC 10 (ENTGET X)))) _LWPOLY)
)

 

Message 4 of 14

vishshreevT578L
Advocate
Advocate

SIR I GOT THE RESULTS BUT FOR ME AS A BEGINNER I WAS A BOUNCER...

CAN I DO THIS USING A SIMPLE WAY THAT YOU CAN SUGGEST

Shreev
0 Likes
Message 5 of 14

Anonymous
Not applicable

(DEFUN PL:EXTR-PNT-FROM-LINE ( / _LWPOLY TEST )
(SETQ _LWPOLY (SSGET '((0 . "LWPOLYLINE"))))
(SETQ _LWPOLY (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX _LWPOLY))))
(SETQ _LWPPTS (MAPCAR '(LAMBDA (_LWPENT) (VL-REMOVE-IF-NOT
      '(LAMBDA(_LWPITM)(= (car _LWPITM) 10)) (ENTGET _LWPENT))) _LWPOLY))
)

Message 6 of 14

Ranjit_Singh
Advisor
Advisor

Here is another method after getting selection set _LWPOLY (I call it SS1)

 

(mapcar '(lambda (y) (setq a ()) (last (mapcar '(lambda (x) (setq a (append (if (= (car x) 10) (list x)) a))) (entget (cadr y))))) (member (nth (- (sslength ss1) 1)  (ssnamex ss1)) (reverse (ssnamex ss1))))

 Both the methods are pretty simple and intuitive. Read some background on how mapcar and lambda works. See discussion here

 

Message 7 of 14

ВeekeeCZ
Consultant
Consultant
Accepted solution

Read the comments from the end... there are 3 operations with lisp... 

(copy-paste into Word or something with wider screen)

 

(mapcar
  '(lambda (z)     
   
     (mapcar 		; result would be list of all '(x1 y2) then '(x2 y2))
       '(lambda (x)     
          (cdr x)) ; if removes a first member of x - if x is (10 . x1 y2), the makes '(x1 y2)

       (vl-remove-if-not			; -> this vl-remove-if-not works similary as mapcar, but mapcar keeps ALL y, but this can same filter out.. by the function on next line     -> result would be list of points '((10 . x1 y2) '(10 . x1 y2)) -> but you still need get rid of 10 code.. -> each became x for next funtion
         '(lambda (y) (= (car y) 10))   ; so if (car y) = 10 then it keeps it, all the others it removes.... 

         (entget z))))		; from each z (= ename) makes a list of ename's definition (entget x) = '((0 . type) '(8 . layer) '(10 . x1 y2) '(10 . x1 y2)) -> each '(code . value) became y
  _lwpoly)  ; _lwpoly is the list of enames '(ename1 ename2) -> each ename1 would be z

HTH

 

0 Likes
Message 8 of 14

vishshreevT578L
Advocate
Advocate

Thank you sir

Shreev
0 Likes
Message 9 of 14

Anonymous
Not applicable

For just the coordinates, excludes the dxf code number.

 

(DEFUN PL:EXTR-PNT-FROM-LINE ( / _LWPOLY TEST )
(SETQ _LWPOLY (SSGET '((0 . "LWPOLYLINE"))))
(SETQ _LWPOLY (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX _LWPOLY))))
(SETQ _LWPPTS (MAPCAR '(LAMBDA (_LWPENT) (MAPCAR 'CDR (VL-REMOVE-IF-NOT
      '(LAMBDA(_LWPITM)(= (car _LWPITM) 10)) (entget _LWPENT)))) _LWPOLY))
)

0 Likes
Message 10 of 14

vishshreevT578L
Advocate
Advocate

done sir....thank you so much....today i lost hopes i decided to quit but mapcar in mapcar gave me a breath...thanks for the breath sir...and all who posted here  

hope i will be perfect some day......

i dont undrstand why i am taking so late to grasp certain things though i practice regularly....practice make the man perfect but i believe perfect practise does the man perfect....i think my practice is is on the wrong path....i have gone through afralisp....jpsanders....visual lisp developers bible after going through from all here i decided to go on illustrations...

i dont know what is going to be in future please suggest me any way to cross this river of my life

Shreev
Message 11 of 14

ВeekeeCZ
Consultant
Consultant
Glad it helped!

Lee Mac has a lot of examples... but I would catch some real issues e.g. from the Forum, or your own issues, and try to program your solution. Even if someone else post an another solution faster, no matter, that's even better, you can learn from that too 🙂
0 Likes
Message 12 of 14

vishshreevT578L
Advocate
Advocate
Yaa...sir...guru (In indian language a teacher)

But i always feel not being from background of programming...that am i not
going wrong

Thanks sir for all your support
Shreev
0 Likes
Message 13 of 14

john.uhden
Mentor
Mentor

Since you are also very interested in learning about functions, note that (mapcar) won't help you with heavy or 3D polylines, but Visual Lisp will.

 

Heavies and 3Ds don't have multiple DXF 10 values, they have subenities called vertices, each with its own code 10, but as VLA-OBJECTs they have a Coordinates property.

 

(Vlax-get object 'Cordinates) will return a flat list of all the vertex coordinates.
"Flat" meaning '(x1 y1 z1 x2 y2 z2 x3 y3 z3 ... xn yn zn)

For a Lightweight polyline, the coordinate flat list excludes Z values. as in
'(x1 y1 x2 y2 x3 y3 ... xn yn)

Thanks to the help of Ken Alexander back in 2003, I came up with a way of converting the flat lists into a list of lists in the format
'((x1 y1 z1)(x2 y2 z2) etc.) or '((x1 y1)(x2 y2) etc.)

;; where old is the flat list and
;; n is the number of items in each group
  (defun group_list (old n / item new)
    (setq old (reverse old))
    (while old
      (repeat n
        (setq item (cons (car old) item)
              old (cdr old)
        )
      )
      (setq new (cons item new)
            item nil
      )
    )
    new
  )

So for a 3D flat list use (group_list coords 3)
And for a 2D flat list use (group_list coords 2)

It's up to you to recognize whether the flat list of coordinates will be 2D or 3D.

John F. Uhden

Message 14 of 14

john.uhden
Mentor
Mentor

Oops... major typo by me.

 

(Vlax-get object 'Cordinates)
^

should be

(Vlax-get object 'Coordinates)
^^
Sorta like the difference between Pop and Poop.
I hope my grandkids don't make the same mistake. :/

John F. Uhden