Jump to content

center of polygon


mstb

Recommended Posts

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.

Link to comment
Share on other sites

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)))
  )
)

 

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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 by BIGAL
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)
)

 

  • Thanks 1
Link to comment
Share on other sites

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

 

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