Getting Start and End Coordinates on the first segment of a PolyLine

Getting Start and End Coordinates on the first segment of a PolyLine

greglcroth
Contributor Contributor
562 Views
8 Replies
Message 1 of 9

Getting Start and End Coordinates on the first segment of a PolyLine

greglcroth
Contributor
Contributor

I want to select the last entity drawn, which will always be a polyline in the routine, and get the direction of the first segment.  Below is a two segment poly, and as you can see there are 3 (assoc 10) coordinates.

 

((-1 . <Entity name: 32edbe0>) (0 . "LWPOLYLINE") (5 . "25D4") (330 . <Entity name: 4814be80>) (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "NOTES") (100 . "AcDbPolyline") (90 . 3) (70 . 0) (43 . 0.0) (38 . 0.0) (39 . 0.0) (10 4699.0 5099.0) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 4760.xxx-xxxxxxxx 5074.xxx-xxxxxxxx) (40 . 0.0) (41 . 0.0) (42 . 0.153731169572485) (91 . 0) (10 4802.xxx-xxxxxxxx 5070.xxx-xxxxxxxx) (40 . 0.0) (41 . 0.0) (42 . 3.99931167657414) (91 . 0) (210 0.0 0.0 1.0))

 

(EDIT...

 

I don't know why all those "xxxx" showed up.  Those are just regular coordinate numbers on my screen, for some reason the cut/paste isn't translating.)

 

I'd like to get the first and second points ... I've got this so far...

 

(setq Line1 (entlast))

(setq P1 (cdr (assoc 10 (entget Line1))))

 

>>How do I get P2 since they are both "assoc 10" in the list? <<

 

(setq Dir1 (angle P1 P2))

 

Thanks for the help.

 

 

0 Likes
Accepted solutions (2)
563 Views
8 Replies
Replies (8)
Message 2 of 9

johnyDFFXO
Advocate
Advocate
Accepted solution

You need to use some other technique.

This example reads all the vertices (= 10 codes) and saves them to the l list. Then just grab the first and second point.

 

(defun c:test ( / l)

 (setq e (entlast))
 (setq d (entget e))

 

(foreach i d
  (if (= 10 (car i))
  (setq l (cons i l))))

 (setq l (reverse l))

0 Likes
Message 3 of 9

komondormrex
Mentor
Mentor
Accepted solution
(setq vertices_list (mapcar 'cdr (vl-remove-if-not '(lambda (group) (= 10 (car group))) (entget (entlast)))))
(setq 1st_segment_direction (angle (car vertices_list) (cadr vertices_list)))
0 Likes
Message 4 of 9

greglcroth
Contributor
Contributor

Thank you!  

 

I thought this would be easy and I was just missing something, so I appreciate the help.

0 Likes
Message 5 of 9

johnyDFFXO
Advocate
Advocate

If you are working with lwpolylines look and learn vlax-curve-* functions. 

https://help.autodesk.com/view/ACD/2022/ENU/?guid=GUID-4684C76F-02F7-4989-AA53-C886E528350A

Then you can use a parameter to get coordinates. par=0 for 1st vertex, par=1 for the second....

0 Likes
Message 6 of 9

Kent1Cooper
Consultant
Consultant

@johnyDFFXO wrote:

If you are working with lwpolylines look and learn vlax-curve-* functions. 

....


That's how I would do it.  But it's not limited to LWPolylines -- the same works for "heavy" 2D and 3D Polylines, with integer parameter values being at the vertices.  [It works for other things, too, but what the parameter values represent varies with the object type.]

Kent Cooper, AIA
0 Likes
Message 7 of 9

greglcroth
Contributor
Contributor

Ended up doing this.  The third line returns what I would call the "x y deltas", so I can get the direction from those easy enough.  Thanks for all the advice.

 

(setq Line1 (entlast))
(setq pt1 (vlax-curve-getStartPoint line1))
(setq dir (vlax-curve-getFirstDeriv line1 0.0))
(setq dir (angle '(0.0 0.0 0.0) dir))

0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant

@greglcroth wrote:

.... (10 4802.xxx-xxxxxxxx 5070.xxx-xxxxxxxx) ....

I don't know why all those "xxxx" showed up.  Those are just regular coordinate numbers on my screen, for some reason the cut/paste isn't translating.) ....


As for that, it happens to numbers of a certain number of digits, which "the system" suspects are AutoCAD serial numbers, and it changes them to the x's to prevent divulging such sensitive information.  The hyphen it adds is a component of their serial numbers, so it's a little odd that it suspects that with strings of numbers that don't include a hyphen, but somehow it does.  If you reduce the number of decimal places [it's hard to imagine you could really need so many -- are you working with angstroms or something?], that won't happen.

Kent Cooper, AIA
0 Likes
Message 9 of 9

Kent1Cooper
Consultant
Consultant

@greglcroth wrote:

I want to select the last entity drawn, which will always be a polyline in the routine, and get the direction of the first segment.  ....

Ended up doing this.  ....


One advantage of the approach you ended up with [which may not matter to your specific usage] is that it would work even if the Polyline starts with an arc segment, and also with lots of other entity types -- a Line, Spline, Ray, Xline, and though it might get the direction from the end you don't intend, an Arc or partial Ellipse, and though it might be meaningless for your purposes, even a Circle.

 

But given your original description, it looks like your solution has more to it than you need.  To simply get the starting direction of the last object drawn, you could do just this much:


(setq dir (angle '(0.0 0.0 0.0) (vlax-curve-getFirstDeriv (entlast) 0)))

Kent Cooper, AIA
0 Likes