mstb Posted August 18, 2020 Posted August 18, 2020 Hello every body (setq xtot 0.0) (setq ytot 0.0) (setq i 0) (foreach a (entget (car (entsel))) (if (= (car a) 10) (progn (setq i (1+ i)) (setq x (nth 1 a)) (setq y (nth 2 a)) (setq xtot (+ xtot x)) (setq ytot (+ ytot y)) ) ) ) (setq pm (list (/ xtot i) (/ ytot i))) I get center of polygon . in this way. But It's not accurate. Is there another way?! Thanks all. Quote
Tharwat Posted August 18, 2020 Posted August 18, 2020 Hi, Here is a very simplified method that should be clear for you to understand and learn from and hope it helps you as you wish. NOTE: You can remove the creation of point object and I added it for you to know how the code works and the outcome of them. (if (setq s (car (entsel "\nSelect polygon :"))) (progn (setq l 0 x 0.0 y 0.0 ) (foreach p (entget s) (if (= (car p) 10) (setq l (1+ l) x (+ x (cadr p)) y (+ y (caddr p)) ) ) ) (setq ctr (list (/ x l) (/ y l) 0.0)) (entmake (list '(0 . "POINT") (cons 10 ctr))) ) ) Quote
mstb Posted August 18, 2020 Author Posted August 18, 2020 1 hour ago, Tharwat said: Hi, Here is a very simplified method that should be clear for you to understand and learn from and hope it helps you as you wish. NOTE: You can remove the creation of point object and I added it for you to know how the code works and the outcome of them. (if (setq s (car (entsel "\nSelect polygon :"))) (progn (setq l 0 x 0.0 y 0.0 ) (foreach p (entget s) (if (= (car p) 10) (setq l (1+ l) x (+ x (cadr p)) y (+ y (caddr p)) ) ) ) (setq ctr (list (/ x l) (/ y l) 0.0)) (entmake (list '(0 . "POINT") (cons 10 ctr))) ) ) Thanks . But I mean The point is not exactly in the middle!! Quote
Tharwat Posted August 18, 2020 Posted August 18, 2020 If you are after the Centroid point then you need to convert the curve object into region then get the Centroid point from there. Quote
BIGAL Posted August 18, 2020 Posted August 18, 2020 (edited) As you hinted Tharwat the average of the vertices is not centroid another is Polycentroid by lee-mac. Again does not take into account curves. This may do what you want apologise no Author name. Pline centroid.lsp Edited August 18, 2020 by BIGAL Quote
mstb Posted August 19, 2020 Author Posted August 19, 2020 7 hours ago, Tharwat said: If you are after the Centroid point then you need to convert the curve object into region then get the Centroid point from there. Thank you very much. Quote
mstb Posted August 19, 2020 Author Posted August 19, 2020 3 hours ago, BIGAL said: As you hinted Tharwat the average of the vertices is not centroid another is Polycentroid by lee-mac. Again does not take into account curves. This may do what you want apologise no Author name. Pline centroid.lsp 2.69 kB · 2 downloads Thank you very much. Quote
satishrajdev Posted August 19, 2020 Posted August 19, 2020 Try the following from Lee Mac: http://www.lee-mac.com/polygoncentroid.html Quote
mstb Posted August 19, 2020 Author Posted August 19, 2020 2 hours ago, satishrajdev said: Try the following from Lee Mac: http://www.lee-mac.com/polygoncentroid.html Thank you very much. Quote
ronjonp Posted August 19, 2020 Posted August 19, 2020 Here is some very old code (defun c:cent (/ obj rgn pt) (if (and (setq obj (car (entsel "\nSelect object to calculate centroid: "))) (setq spc (vlax-ename->vla-object (cdr (assoc 330 (entget obj))))) (setq obj (vlax-ename->vla-object obj)) (= 'list (type (setq rgn (vl-catch-all-apply 'vlax-invoke (list spc 'addregion (list obj)))))) ) (progn (setq pt (vlax-get (setq rgn (car rgn)) 'centroid)) (vl-catch-all-apply 'vla-delete (list rgn)) (entmake (list '(0 . "POINT") (cons 10 pt) '(8 . "centroid"))) ) ) (princ) ) 1 Quote
mstb Posted August 20, 2020 Author Posted August 20, 2020 On 8/19/2020 at 11:34 PM, ronjonp said: Here is some very old code (defun c:cent (/ obj rgn pt) (if (and (setq obj (car (entsel "\nSelect object to calculate centroid: "))) (setq spc (vlax-ename->vla-object (cdr (assoc 330 (entget obj))))) (setq obj (vlax-ename->vla-object obj)) (= 'list (type (setq rgn (vl-catch-all-apply 'vlax-invoke (list spc 'addregion (list obj)))))) ) (progn (setq pt (vlax-get (setq rgn (car rgn)) 'centroid)) (vl-catch-all-apply 'vla-delete (list rgn)) (entmake (list '(0 . "POINT") (cons 10 pt) '(8 . "centroid"))) ) ) (princ) ) Thank you very much Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.