maahee Posted Friday at 07:30 AM Posted Friday at 07:30 AM (setq gct (osnap (vlax-curve-getStartPoint bm) "gcen")) The above code line gives only closed polyline How to find the geometric center of an open polyline and a single line Quote
DATVO Posted Friday at 08:52 AM Posted Friday at 08:52 AM 1 hour ago, maahee said: (setq gct (osnap (vlax-curve-getStartPoint bm) "gcen")) The above code line gives only closed polyline How to find the geometric center of an open polyline and a single line Only the geometric center is available for closed areas, since open polylines have no area and therefore no true center. If it’s just a line, the "geometric center” is the midpoint. 1 1 Quote
Saxlle Posted Friday at 09:48 AM Posted Friday at 09:48 AM You can actually calculate it, here is the code (you can change it for your purposes): (setq ptlist (mapcar 'cdr (vl-remove-if-not (function (lambda (x) (= (car x) 10))) (setq ent (entget (car (entsel "\nSelect the line:")))))) ;; point list sum_x 0 sum_y 0 vertices (cdr (assoc 90 ent)) ;; number of vertices for LWPOLYLINE ) (foreach pt ptlist (setq sum_x (+ sum_x (car pt))) ;; sum only the X vertices values ) (foreach pt ptlist (setq sum_y (+ sum_y (cadr pt))) ;; sum only the Y vertices values ) (setq sum_x (/ sum_x vertices) ;; dividing the total sum X by the number of vertices sum_y (/ sum_y vertices) ;; dividing the total sum Y by the number of vertices pt (list sum_x sum_y) ;; geometric center ) Below are the pictures with geometric center for open and closed polyline (picture 1 open, picture 2 closed). Picture 1. Picture 2. 50 minutes ago, DATVO said: If it’s just a line, the "geometric center” is the midpoint. This is true. Best regards. 1 1 1 Quote
BIGAL Posted Friday at 11:48 PM Posted Friday at 11:48 PM Not quite the middle of say 2 points. The red is the average answer. The green is gcen. so 4 answers. If the shape is say 4 points then you could force a close get the Gcen then do a undo. 1 Quote
lrm Posted Saturday at 02:26 PM Posted Saturday at 02:26 PM The geometric center (a.k.a. centroid) of an open polyline is deteremined by summing the length of each line segment times the coordinates of the midpoint of the respective segment. The result is then divided by the total length of the polyline. It is NOT the average of the point locations. ; Calculates the centroid for an open lwpolyline ; lrm 8/9/2025 (defun c:pl_centroid (/ ss pt_lst sum_x sum_y sum_d n i d mid_pt centroid) (setq ss (entget (car (Entsel "\nSelect Open Polyline"))) pt_lst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) ss) ) ) (setq sum_x 0 sum_y 0 sum_d 0.0 n (length pt_lst) i 0 ) (while (<= i (- n 2)) (setq d (distance (nth i pt_lst) (nth (+ i 1) pt_lst))) (setq mid_pt (mapcar '/ (mapcar '+ (nth i pt_lst) (nth (+ i 1) pt_lst)) '(2 2 2) ) ) (setq sum_x (+ sum_x (* (car mid_pt) d)) sum_y (+ sum_y (* (cadr mid_pt) d)) sum_d (+ d sum_d) i (+ i 1) ) ) (setq centroid (list (/ sum_x sum_d) (/ sum_y sum_d) ) ) (princ "\n Centroid at: ") (princ centroid) (command "_point" "_non" centroid) (princ) ) 1 1 Quote
devitg Posted 2 hours ago Posted 2 hours ago (edited) @Mahee , please give it a try , will work both for open and closed lwpolylines it get the Centroid from a temporary Region from a temporary closed polyline if poly is open get-center.LSP get-center.LSP get poly center.dwg Edited 2 hours ago by devitg miss spelling 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.