Hi all,
Can someone here please kindly write or point me in the correct direction for the following code.
Select a polyline and based on its vertexs to calculate the centroids of the vertexs.
Previously I was using LIST on the polyline and extract the output from the command window finally using excel to calculate the centroids of the points. If someone can assist me in this or point me in the correct direction i would be grateful.
Thanks
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Centroid is also a property of regions which can be read with VLA.
@xianhui90 wrote:
....
Select a polyline and based on its vertexs to calculate the centroids of the vertexs.
....
Here's one way to average the locations of all vertices. It draws a Point entity there on the current Layer, but could be made to leave that location in the vertavg variable for use by something further [remove vertavg from the localized variables list at the top].
However, it is only that -- an average of the vertex locations -- which will not always be the "center of gravity" or the centroid that would be returned from a Region made from the same Polyline. That will be different if there are any arc segments in the Polyline, and possibly for other reasons [such as on a self-intersecting one].
(defun C:PLC (/ pl verts qua vertsum vertavg); = PolyLine Centroid
(if (setq pl (entsel "\nSelect Polyline: "))
(progn ; 'then'
(setq
verts (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car pl)))); list of vertices
qua (length verts); quantity of vertices
vertsum (car verts); first one
); setq
(foreach vert (cdr verts) (setq vertsum (mapcar '+ vert vertsum)))
(setq vertavg (mapcar '/ vertsum (list qua qua))); average of vertex locations
(entmake (list '(0 . "POINT") (cons 10 vertavg)))
); progn
); if
); defun
It doesn't check that the User selected an actual Polyline, but that and the usual other bells and whistles can be worked in. [And I think there must be a way to sum the vertex X and Y coordinates all at once, using (mapcar)/(apply)/(lambda), but I couldn't come up with the right combination quickly.]