View Full Version : Determine the coordinates of the midpoint of a Polyline?
ccowgill
2007-03-09, 05:03 PM
Is there an easy way to determine the coordinates of the midpoint of a polyline. especially If it has more than one vertex, and a way to find the angle of the polyline at that point?
Is there an easy way to determine the coordinates of the midpoint of a polyline. especially If it has more than one vertex, and a way to find the angle of the polyline at that point?
Well, I can help you with the first part:
(defun c:test (/ objPline midpt)
(setq objPline (vlax-ename->vla-object (car (entsel "\nSelect the pline: "))))
(setq midpt (vlax-curve-getPointAtDist objPline (/ (vlax-get-property objPline 'length) 2.0)))
(command "point" midpt)
)
The second part is a little more difficult. I'll see if I can help later, or maybe someone else has already written something for that.
GuinnessCAD
2007-03-09, 08:51 PM
Is there an easy way to determine the coordinates of the midpoint of a polyline. especially If it has more than one vertex, and a way to find the angle of the polyline at that point?How do you want the angle to read? Bearings and distance? Or the standrard X degrees.
And, let's say that the midpoint falls on or very near a vertex. What angle would you want? Bisect it?
GuinnessCAD
2007-03-09, 09:35 PM
May I ask exactly what are you going to be using this routine for? May help in the understanding of what is being developed.
ccowgill
2007-03-09, 09:57 PM
Once upon a time I was enrolled in an ATP course for LISP Programming (April 06). It was very close to the time of my graduation, so I did not participate as much as I would have liked to, or should have. So now, I have to work through the program on my own.
The main program is designed to do many things, I figured I would break it into smaller tasks. The first being a program I would like to call LengthText. The user will select multiple polylines and the program places a piece of text at the midpoint, rotated parallel to it that displays the length among other info. I can get the length, I could place the text, I just couldn't get it to place it always at the midpoint, and I could never get it rotated parallel. If the midpoint is a vertex, bisect the angle. The angle for now is just for the text rotation.
Here is what I have so far:
(defun c:lengthtext ()
(setq
object
(ssget
(list
(cons 0
"POLYLINE,*CONTOUR,ARC,LINE,LWPOLYLINE,2DPOLYLINE"
)
)
)
)
(while (setq listobj (ssname object 0))
(setq vlobj (vlax-ename->vla-object listobj)
vlobjlen (rtos (vla-get-length vlobj) 2 0)
MidPoint (vlax-curve-getpointatdist
vlObj
(/ (getvar "PERIMETER") 2)
)
Param (vlax-curve-getParamAtPoint VlObj MidPoint)
startpline (vlax-curve-getStartParam VlObj)
endpline (vlax-curve-getEndParam VlObj)
angle1 (vlax-curve-getFirstDeriv VlObj ( / (- endpline startpline) 2))
Ang (/ (* (atan (/ (cadr angle1) (car angle1)))
180)
pi)
TH 0.08
textObj (vla-addtext
acadModelSpace
vlobjlen
(vlax-3d-point Midpoint)
TH
) ;end vla-addtext
) ;end setq
(vla-put-color textobj 256) ;change color
(vla-put-alignment textobj 13) ;change justification
(vla-put-textalignmentpoint textobj (vlax-3d-point Midpoint))
;change insetion point
(vla-put-rotation textobj Ang)
;change rotation
(vla-put-layer textobj "0")
(ssdel listobj object)
) ;end while
) ;end defun
aaronic_abacus
2007-03-09, 10:46 PM
Here are some routines to automatically label all polylines by layer name using the divide command.
This is really (maybe too) simplistic, but it may give you a couple of ideas, and may point out an Area of Potential Error or two.
I took out the *contour because it doesn't have a length property (one of the APEs); It's going to take some work to get the length of them, and I don't know if the vlax-curve-* functions work on them.
Also, doing it this way rotates the text around the vertex as you get closer to it. I don't know if that's desireable or not. Your call. And there is no plan readable capability, I think you can handle that.
It looked like you had a handle on the text creation, so I went the simple route.
It's just a start...
(defun c:test (/ cnt obj ss midpt len tp1 tp2 ang)
(setq ss (ssget (list (cons 0 "POLYLINE,ARC,LINE,LWPOLYLINE,2DPOLYLINE"))))
(repeat (setq cnt (sslength ss))
(setq obj (vlax-ename->vla-object (ssname ss (setq cnt (1- cnt)))))
(if (vlax-property-available-p obj 'arclength)
(setq len (/ (vla-get-arclength obj) 2.0))
(setq len (/ (vla-get-length obj) 2.0))
);APE
(setq midpt (vlax-curve-getPointAtDist obj len)
tp1 (vlax-curve-getPointAtDist obj (+ len 0.1))
tp2 (vlax-curve-getPointAtDist obj (- len 0.1))
ang (* (angle tp1 tp2) (/ 180.0 pi))
)
(command "text" "bc" midpt ang (rtos (* len 2.0)))
)
)
ccowgill
2007-03-12, 01:30 PM
I must have borrowed that part of the code from someone else, because I would have used *POLYLINE instead of listing them off separately. I'll give that a try and report back as to how well it works
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.