# Thread: get coordinates of first and last point of a polyline

1. Login to Give a bone

## get coordinates of first and last point of a polyline

How can i assign the coordinates of a drawn polyline to variables (firstPoint and lastPoint)? I've tried some stuff, but cannot get it to work..

Code:
```(defun c:test (/)
(command "pline")
(while (= (getvar "CMDNAMES") "PLINE")
(command pause)
)
(setq firstPoint (cdr (assoc 10 (reverse (car (entlast))))) ) ;first point of polyline
(setq lastPoint (cdr (assoc 10 (car (entlast)))) ) ;last point of polyline
);defun```

2. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

Hello, try this out:

Code:
```
(defun c:test (/)
(command "pline")
(while (= (getvar "CMDNAMES") "PLINE")
(command pause)
)
(setq vertex_lst (vl-remove nil (mapcar '(lambda (x) (if (= (car x) 10) (cdr x))) (entget (entlast)))))
(setq firstpoint (nth 0 vertex_lst))
(setq lastpoint  (nth (- (length vertex_lst) 1) vertex_lst))
)
);defun```
Ty

3. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

thanks.. that works like a charm!
now to figure out what it actually means

4. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

The syntax of the "mapcar" function can be somewhat hard to understand. I probaly should have used a more easily understandable function, but I'll try to explain. "Mapcar" will apply a function to each element of a list. I'll break it down piece by piece:
Code:
```
(setq vertex_lst (vl-remove nil (mapcar '(lambda (x) (if (= (car x) 10) (cdr x))) (entget (entlast)) ) ) )```
BLUE:
The portion in blue returns the entity list. The entity list is a list of dotted pairs.

RED:
the "lamda" function is essentially just like a "defun" but it can be used inside other functions (here it is used inside mapcar). So if written as it's own routine it would look like this:
Code:
```
(defun (x)
(if (= (car x) 10)
(cdr x)
)
)```
This function looks at the the first element of a list, if it equals 10, the function returns the rest of the list with the first element stripped out.

GREEN:
This is the "mapcar" function. All it does is apply the "lambda" function to each element of the entity list obtained in the BLUE portion of the code. It then returns a list of the results.

ORANGE:
Here I use the "vl-remove" function to remove the "nil" results from the list returned by the "mapcar" function.

So, first I grab the entity list (BLUE). Then use a combanation of the "mapcar" and "lambda" functions to parse the entity list (MAPCAR, GREEN) according to my conditions (LAMBDA, RED). This returns a list of the vertexes.

Then I use the "nth" function to extract the desired points from the list.

I hope I have explained it well enough. Probably the easiest way to see what is happening is to open the lisp in VLIDE and use the inspect tool to step through each portion of the code. Also search the help files for "mapcar".

This task could be done a few different ways, I only used "mapcar" because well, I like to use it.

Before I discovered "mapcar" and "lambda" I used to do this:
Code:
```(defun c:test (/)
(command "pline")
(while (= (getvar "CMDNAMES") "PLINE")
(command pause)
)
(setq ent (entget (entlast)))
(setq vertex_lst nil)
(foreach dp ent
(if (= (car dp) 10)
(setq vertex_lst (append vertex_lst (list (cdr dp))))
)
)
(setq firstpoint (nth 0 vertex_lst))
(setq lastpoint (nth (- (length vertex_lst) 1) vertex_lst))
);defun```
This does exactly the same thing, only using the "foreach" function. You can see though it is essentially the same code. About the only difference is I have to make sure the "vertex_lst" variable is "nil" before I build the list and I it won't give any "nil" results so I don't have to remove them.

Ty

5. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

Here is an other way to do it
Code:
```(if (setq Ent (entsel))
(progn
(setq VL-Obj (vlax-ename->vla-object (car Ent )))
(setq StartPoint (vlax-curve-getStartPoint VL-Obj ))
(setq EndPoint (vlax-curve-getEndPoint VL-Obj ))
)
(princ "..no object selected" )
)```
: ) Happy Computing !

kennet

6. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

Originally Posted by tyshofner
The syntax of the "mapcar" function can be somewhat hard to understand. I probaly should have used a more easily understandable function, but I'll try to explain. "Mapcar" will apply a function to each element of a list. I'll break it down piece by piece:
Ty
thanks for taking time to explain. tomorrow i will print out some stuff about the functions you described, and see if I get them You never know when they'll come in handy again ..

7. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

Originally Posted by kennet.sjoberg
Here is an other way to do it
: ) Happy Computing !

kennet
thanks! will look into that as well

8. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

One more variant:
Code:
```(and (setq Ent (car (entsel)))
(setq StartPoint (vlax-curve-getstartpoint Ent))
(setq EndPoint (vlax-curve-getendpoint Ent))
)```

9. Login to Give a bone

## Re: get coordinates of first and last point of a polyline

Originally Posted by wbreedveld
thanks! will look into that as well
ok, you may need to run (vl-load-com) if you not already done it.
---------
And MAPCAR and LAMBDA . . is a long story to read from this LINK

: ) Happy Computing !

kennet

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•