Jump to content

lisp to find the centroid of a selected area


Elektrik

Recommended Posts

Is there any lisp to find/mark the centroid of any area you select, just when you run with the area command, you can select any area you want without polygons or regions. Is there any lisp find the centroid of an area when you specify points that define that area?

 

Thanks in advance.

Link to comment
Share on other sites

Study this example and it should be clear to you what to modify to get sub function work with point list... Note that it would be preferable to draw polygon in advance as segments must not cross each other... So actually you don't need to modify anything from Lee's examples - you have to provide LWPOLYLINE polygon ENAME as an argument...

http://www.lee-mac.com/polygoncentroid.html

 

HTH., M.R.

  • Like 1
Link to comment
Share on other sites

Hi,

Give this a shot and let me know.

(defun c:cnt ( / nxt pnt lst obj reg cen)
  ;;--------------------------------------------;;
  ;; 	Tharwat - Date: 08.12.2018		;;
  ;; Print Centriod point from picked points.	;;
  ;;--------------------------------------------;;
  (cond ((= 4 (logand 4 (cdr (assoc 70 (entget (tblobjname "LAYER" (getvar 'CLAYER))))))) (alert "Current layer is locked<!>"))
        ((and (setq nxt " ")
              (while (setq pnt (getpoint (strcat "\nSpecify" nxt "point : ")))
                (setq lst (cons pnt lst)
                      nxt " Next ")
                )
              lst
              (setq obj (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") (cons 90 (length lst)) '(70 . 1))
                                                (mapcar (function (lambda (p) (cons 10 p))) lst))))
              )
         (cond ((vl-catch-all-error-p (setq reg (vl-catch-all-apply 'vlax-invoke (list (vla-get-block (vla-get-activelayout (vla-get-ActiveDocument
                                                                                                         (vlax-get-acad-object)
                                                                                                         )
                                                                                  )
                                                                   )
                                                                 'Addregion
                                                                 (list (vlax-ename->vla-object obj))
                                                                 )
                            )))
                (alert "Error. Can't create the region <!>"))
               (t    (setq reg (car reg)
                           cen (vlax-get reg 'Centroid))
                (alert (strcat "Centriod = " (vl-princ-to-string cen)))
                (print cen)
                (vla-delete reg))
                ))
        )
  (and obj (entdel obj))
  (princ)
  ) (vl-load-com)

(princ "\nType CNT to start.")
                

 

  • Like 1
Link to comment
Share on other sites

2 hours ago, Tharwat said:

Hi,

Give this a shot and let me know.


(defun c:cnt ( / nxt pnt lst obj reg cen)
  ;;--------------------------------------------;;
  ;; 	Tharwat - Date: 08.12.2018		;;
  ;; Print Centriod point from picked points.	;;
  ;;--------------------------------------------;;
  (cond ((= 4 (logand 4 (cdr (assoc 70 (entget (tblobjname "LAYER" (getvar 'CLAYER))))))) (alert "Current layer is locked<!>"))
        ((and (setq nxt " ")
              (while (setq pnt (getpoint (strcat "\nSpecify" nxt "point : ")))
                (setq lst (cons pnt lst)
                      nxt " Next ")
                )
              lst
              (setq obj (entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") (cons 90 (length lst)) '(70 . 1))
                                                (mapcar (function (lambda (p) (cons 10 p))) lst))))
              )
         (cond ((vl-catch-all-error-p (setq reg (vl-catch-all-apply 'vlax-invoke (list (vla-get-block (vla-get-activelayout (vla-get-ActiveDocument
                                                                                                         (vlax-get-acad-object)
                                                                                                         )
                                                                                  )
                                                                   )
                                                                 'Addregion
                                                                 (list (vlax-ename->vla-object obj))
                                                                 )
                            )))
                (alert "Error. Can't create the region <!>"))
               (t    (setq reg (car reg)
                           cen (vlax-get reg 'Centroid))
                (alert (strcat "Centriod = " (vl-princ-to-string cen)))
                (print cen)
                (vla-delete reg))
                ))
        )
  (and obj (entdel obj))
  (princ)
  ) (vl-load-com)

(princ "\nType CNT to start.")
                

Thanks, this is just what I wanted, except it does not mark the centeroid. It would be better if it did. Thanks again.

 

Link to comment
Share on other sites

Just replace this:

(alert (strcat "Centriod = " (vl-princ-to-string cen)))

with this:

(entmake (list '(0 . "POINT")  (cons 10 (trans cen 0 1))))

And get sure is that the PDMODE system variable is other than zero to be able to see the mark point.

Edited by Tharwat
  • Like 1
Link to comment
Share on other sites

1 hour ago, Tharwat said:

Just replace this:


(alert (strcat "Centriod = " (vl-princ-to-string cen)))

with this:


(entmake (list '(0 . "POINT")  (cons 10 cen)))

And get sure is that the PDMODE system variable is other than zero to be able to see the mark point.

 

Thanks, but it marks somewhere outside the selected area I am not sure why.

Link to comment
Share on other sites

14 minutes ago, Elektrik said:

 

Thanks, but it marks somewhere outside the selected area I am not sure why.

You must have your UCS other than World system and that's why the mark point located somewhere else.

Anyway, I have updated my previous post and please replace them again then try again.

NOTE: its not necessarily to quote every reply to write a reply but its up to you anyway.

  • Like 1
Link to comment
Share on other sites

  • 3 years later...
2 hours ago, Maharshi555 said:

Simply use this:

(setq cen_pt (ade_expreval ent_one ".center" "point"))

ent_one is the entity

 

of course, ade_expreval is an AutoCAD 3d MAP command and won't work if they don't have that installed.

Link to comment
Share on other sites

I am assuming center point = geometric center

 

(setq poly (vlax-ename->vla-object (car (entsel "\nSelect polyline"))))
(setq CPT (osnap (vlax-curve-getStartPoint poly) "gcen"))

 

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...