# TheSwamp

## Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Vikram on January 22, 2020, 02:14:45 AM

Title: How find of center of a random object?
Post by: Vikram on January 22, 2020, 02:14:45 AM
Is there any lisp or method to get center of a random object?
I want to find the center of an object and move its center to the 0,0 axis as I have shown it in the pic.

Title: Re: How find of center of a random object?
Post by: Lee Mac on January 22, 2020, 08:17:58 AM
I'm not sure why this is posted in the DCL forum.

Are you looking for the center of the bounding box or the centroid?
Title: Re: How find of center of a random object?
Post by: Vikram on January 22, 2020, 11:51:37 PM
I want center of the bounding box

But to get center of mass how can I do it?

My post is on AutoLisp forum
Title: Re: How find of center of a random object?
Post by: Tharwat on January 23, 2020, 01:54:56 AM
I want center of the bounding box

But to get center of mass how can I do it?

The following would get the center of the bounding box of the selected object but to get the Centroid of a curved object, you need to convert it to region object in prior of getting the Centroid property of it.
Code - Auto/Visual Lisp: [Select]
1. (defun c:ctr (/ sel lft rgt mid)
2.   ;; Tharwat - 23.Jan.2020      ;;
3.   (and
4.     (setq sel (car (entsel "\nPick an object : ")))
5.     (or
6.              (setq obj (vlax-ename->vla-object sel))
7.              'GetBoundingBox
8.            )
9.            (progn (vla-getboundingbox obj 'lft 'rgt)
10.                   (setq mid (mapcar '(lambda (p q) (/ (+ p q) 2.0)) (vlax-safearray->list lft)
11.                                                (vlax-safearray->list rgt)
12.                             )
13.                   )
14.            )
15.       )
16.         "The method 'Getboundingbox' isn't appplicable on the picked object <!>"
17.       )
18.     )
19.   )
20.   (if mid
21.     (entmake (list '(0 . "POINT") (cons 10 mid)))
22.   )
23.
24.
Title: Re: How find of center of a random object?
Post by: Vikram on January 23, 2020, 02:31:01 AM
I tried your lisp but Im not getting the center of the object I'm getting output (66594.1 1.678e+006) which is out of the bound. Please refer to the drawing I have attached to this reply.

My main motive is to shift the center of the bound box to 0,0 position. Like datum feature in NX cam.

I googled a bit and Geometric center can help me but I'm using AutoCAD 2012 and the feature is added in 2016

Title: Re: How find of center of a random object?
Post by: Tharwat on January 23, 2020, 02:53:08 AM
Yeah you are right, actually I used the multiply symbol instead of summation symbol.
I have revised the codes above and added a line of codes to create a point at the mid point of the bounding box border.
Title: Re: How find of center of a random object?
Post by: Vikram on January 23, 2020, 03:16:32 AM
Its still not working mate. Can you try it on yourself on the drawing I have attached?
I got the centeroid of that object by converting it into region and using MASSPROP. But Im not sure if that is the exact geometrical center.

I'm confused between geometrical center and centeroid. Whats the difference?
Title: Re: How find of center of a random object?
Post by: ronjonp on January 23, 2020, 11:54:37 AM
Its still not working mate. Can you try it on yourself on the drawing I have attached?
I got the centeroid of that object by converting it into region and using MASSPROP. But Im not sure if that is the exact geometrical center.

I'm confused between geometrical center and centeroid. Whats the difference?
Tharwats code returns the midpoint of the bounding box ( red ) and you want the centroid ( green ). Have you seen this? http://www.theswamp.org/index.php?topic=46820.msg518432#msg518432
Many examples with a Google search: centroid site:theswamp.org
Title: Re: How find of center of a random object?
Post by: Vikram on January 24, 2020, 04:52:59 AM
Thanks for the information!
Title: Re: How find of center of a random object?
Post by: ronjonp on January 24, 2020, 10:01:01 AM
Thanks for the information!
You're welcome. :-)
Title: Re: How find of center of a random object?
Post by: mailmaverick on February 19, 2020, 11:55:09 PM
Look at this LISP :-
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/when-centroid-is-ouside-a-polyline-what-is-the-inside-middle/m-p/8685783#M383045 (https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/when-centroid-is-ouside-a-polyline-what-is-the-inside-middle/m-p/8685783#M383045)