Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LSP request- calculating centroid

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
xianhui90
1249 Views, 4 Replies

LSP request- calculating centroid

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

4 REPLIES 4
Message 2 of 5
rkmcswain
in reply to: xianhui90

This is not what you asked for, but just wanted to throw out there that if you are only doing this on a few closed polylines - then you can use the REGION command to convert them to a region, and then use the MASSPROP command to report the centroid.

I have not tested this routine, but it may be what you're asking for:
http://cadtips.cadalyst.com/mass-properties/center-line-gravity

R.K. McSwain     | CADpanacea | on twitter
Message 3 of 5
dgorsman
in reply to: rkmcswain

Centroid is also a property of regions which can be read with VLA.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 4 of 5
Kent1Cooper
in reply to: xianhui90


@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.]

Kent Cooper, AIA
Message 5 of 5
xianhui90
in reply to: Kent1Cooper

Thanks Kent.

 

That is exactly what im looking for

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost