center of gravity

center of gravity

Anonymous
Not applicable
1,678 Views
3 Replies
Message 1 of 4

center of gravity

Anonymous
Not applicable

I have a lisp witch calculates the center of gravity of a polyline.

 

(defun ax:Centroid (poly / pl ms va reg cen)
(vl-load-com)
(setq pl (vlax-ename->vla-object poly)
ms (vla-get-modelspace
(vla-get-activedocument (vlax-get-acad-object))
)
va (vlax-make-safearray vlax-vbObject '(0 . 0))
)
(vlax-safearray-put-element va 0 pl)
(setq reg (car (vlax-safearray->list
(vlax-variant-value (vla-addregion ms va))
)
)
cen (vla-get-centroid reg)
)
(vla-delete reg)
(vlax-safearray->list (vlax-variant-value cen))
)
(Defun c:zwaartepunt()
(setq punt (ax:Centroid (car (entsel))))
(princ "\nZwaartepunt is (x y): ")(princ punt)
(princ)
)

now the output is like: Zwaartepunt is (x y): (125058.0 -23818.9)

but can i automate it with drawing a circle with that center and a rudius of like 50mm or something?

0 Likes
Accepted solutions (1)
1,679 Views
3 Replies
Replies (3)
Message 2 of 4

dlanorh
Advisor
Advisor
Accepted solution

Try this. Additions are in red. Change rad to reflect your circles radius.

 

(defun ax:Centroid (poly / pl ms va reg cen)
(vl-load-com)
  (setq pl (vlax-ename->vla-object poly)
        ms (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
        va (vlax-make-safearray vlax-vbObject '(0 . 0))
  )
  (vlax-safearray-put-element va 0 pl)
  (setq reg (car (vlax-safearray->list (vlax-variant-value (vla-addregion ms va))))
        cen (vla-get-centroid reg)
  )
  (vla-delete reg)
  (vlax-safearray->list (vlax-variant-value cen))
)

(defun c:zwaartepunt()
  (setq punt (ax:Centroid (car (entsel)))
        rad 5.5
  )
  (princ "\nZwaartepunt is (x y): ")(princ punt)
  (command "_circle" punt rad)
(princ)
)

I am not one of the robots you're looking for

0 Likes
Message 3 of 4

Anonymous
Not applicable

super .. its does the trick!

0 Likes
Message 4 of 4

Kent1Cooper
Consultant
Consultant

No need for a variable that is used only once -- just give it the radius value directly:

....
(setq punt (ax:Centroid (car (entsel)))) (command "_.circle" "_non" punt 50) (princ "\nZwaartepunt is (x y): ")(princ punt) ....

[I also added the "none" Object Snap to prevent problems if you have running Osnap modes on.]

Kent Cooper, AIA
0 Likes