View Full Version : get coordinates of first and last point of a polyline
wbreedveld
2007-06-07, 03:14 PM
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.. :(
(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
tyshofner
2007-06-07, 04:18 PM
Hello, try this out:
(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 :mrgreen:
wbreedveld
2007-06-07, 04:42 PM
thanks.. that works like a charm!
now to figure out what it actually means ;-)
tyshofner
2007-06-07, 05:24 PM
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:
(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:
(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:
(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 :mrgreen:
kennet.sjoberg
2007-06-07, 08:27 PM
Here is an other way to do it
(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
wbreedveld
2007-06-07, 09:45 PM
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 :mrgreen:
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 ..
wbreedveld
2007-06-07, 09:47 PM
Here is an other way to do it
: ) Happy Computing !
kennet
thanks! will look into that as well
CAB2k
2007-06-07, 09:50 PM
One more variant:
(and (setq Ent (car (entsel)))
(setq StartPoint (vlax-curve-getstartpoint Ent))
(setq EndPoint (vlax-curve-getendpoint Ent))
)
kennet.sjoberg
2007-06-07, 09:53 PM
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 (http://forums.augi.com/showthread.php?p=680072#post680072)
: ) Happy Computing !
kennet
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.