Line measure using two end points?

Line measure using two end points?

Anonymous
Not applicable
891 Views
3 Replies
Message 1 of 4

Line measure using two end points?

Anonymous
Not applicable

Is it possible to get the length of a line by selecting 2 end points and calculating from there the length of the line selected? For the code I am writing I would need this to work on both horizontal and vertical lines but I am unsure how to do this. If possible could it save the end points selected for later use in the code without re-selection? Any help would be appreciated!

(the Measure feature does not work as the lines I am working with are all connected on a foundation plan and therefore single lines cannot be selected)

0 Likes
Accepted solutions (1)
892 Views
3 Replies
Replies (3)
Message 2 of 4

SeeMSixty7
Advisor
Advisor
Accepted solution

There are a number of ways to get the end points from just selecting the line. It sounds like you are actually using a pline though, same applies.

 

For your request though simply try the following.

(defun c:mylinelen()
  (setq pt1 (getpoint "\nSelect First End Point: ")
      pt2 (getpoint "\nSelect Other Endpoint: " pt1)
      linelen (distance pt1 pt2)
  )
)

good luck

0 Likes
Message 3 of 4

Kent1Cooper
Consultant
Consultant

If as mentioned it's really a Polyline and not a Line entity, you can also get what you want with a single  selection on the segment in question, without the need to select at both ends of it:

(vl-load-com)
(defun C:SegEndsLen (/ plsel pl selpar) (setq plsel (entsel "\nSelect Polyline on desired segment: ") pl (car plsel) selpar (vlax-curve-getParamAtPoint pl (osnap (cadr plsel) "_nea")) pt1 (vlax-curve-getPointAtParam pl (fix selpar)) pt2 (vlax-curve-getPointAtParam pl (1+ (fix selpar))) seglen (distance pt1 pt2) ); setq ); defun

That assumes you select it on a line  segment, not an arc segment, but it could be made to verify that if needed, and that you don't pick it exactly at  a vertex.  It could also be made to verify selection of a Polyline to begin with.

 

It leaves the endpoints saved in the pt1 and pt2 variables [pt1 will be the "upstream" end of the segment in relation to its drawn direction, and pt2 the "downstream" end], as well as the length [which it shows you at the command line, since it's the last thing that happens] in the seglen variable.

 

[[Something similar can be done with a Line entity, but some of the code would need to be different.]]

Kent Cooper, AIA
Message 4 of 4

Sea-Haven
Mentor
Mentor

If you pick near an end of a line or pline you can swap the end & startpoint answers this way only need 1 pick it just compares the distance between the start and end points to the pick point, the OP wanted to keep the points for later.

 

Pt1 start pt2 end pt3 is pick point look at 

(setq e (entsel))
Select object: (<Entity name: 1e4b4a0c4b0> (318550.0 7.39623e+06 0.0))

(cadr e) =  (318550.0 7.39623e+06 0.0) = pt3

 

(setq d1 (distance pt1 pt3))
	(setq d2 (distance pt2 pt3))
		(if (> d1 d2)
		(progn 
			(setq temp pt1)
			(setq pt1 pt2)
			(setq pt2  temp)
  )
  )

 

0 Likes