Author Topic: Polyline's centre of gravity  (Read 1970 times)

0 Members and 1 Guest are viewing this topic.

Amsterdammed

  • Guest
Polyline's centre of gravity
« on: October 19, 2005, 09:07:32 AM »
Hello everyboby.

I want to place a block with a room number simply in the centre of an existing Polyline.

Is there some property or function for the centre of gravity as example?

Thanks in Advance

Bernd

LE

  • Guest
Re: Polyline's centre of gravity
« Reply #1 on: October 19, 2005, 10:00:08 AM »
Here is one way [I don't like to much, but it may work for you]

Code: [Select]
(vl-load-com)

;; returns the centroid from an closed area [object]
;;
;; (gb_getcentroid (vlax-ename->vla-object (car (entsel "\nSelect closed polyline: "))))
;;
(defun gb_getcentroid  (obj / array new_array centroid_pt base)
  (setq array (vlax-make-safearray vlax-vbObject '(0 . 0)))
  (vlax-safearray-put-element array 0 obj)
  (setq new_array (vlax-invoke-method
    (vla-objectidtoobject
      (setq base (vla-get-database obj))
      (vla-get-ownerid obj))
    'AddRegion
    array))
  (vlax-release-object base)
  (setq centroid_pt
(vlax-get (setq region (vlax-safearray-get-element
  (vlax-variant-value new_array)
  0))
   'centroid))
  ;; erase the auxiliar region object
  (vla-delete region)
  centroid_pt)

;; load the modeler dll just once
(if (not (vl-bb-ref 'gb_loaded))
  (progn
    (vl-bb-set 'gb_loaded t)
    (setq model (vla-get-modelspace
  (setq doc (vla-get-activedocument
      (setq acad_obj (vlax-get-acad-object))))))
    (if (not (vl-catch-all-error-p
       (setq vla_circle
      (vl-catch-all-apply
'vla-addcircle
(list model
      (vlax-3d-point (list 0.0 0.0 0.0))
      1.0)))))
      (progn
(gb_getcentroid vla_circle)
(vla-delete vla_circle)))
    (setq model nil
  doc nil
  acad_obj nil
  vla_circle nil)))
(princ)

LE

  • Guest
Re: Polyline's centre of gravity
« Reply #2 on: October 19, 2005, 10:28:20 AM »
A much better form will be if you could implement a code base on this:

"The conventionally used algorithm for determining a centroid (which is identical to the center of gravity for cases involving homogeneous materials) is the average of the known X, Y and Z surface coordinates."

Amsterdammed

  • Guest
Re: Polyline's centre of gravity
« Reply #3 on: October 19, 2005, 10:48:05 AM »
Luis,
I just needed to place a block in a room defined by a ploy, so it doesn't need to be in the centroid, just nicely in the room. So i
Code: [Select]
(DEFUN mir_VERTEXEN_uit (EL)
      (setq vertexen (list ())
            ele      el
            vertex   (list (trans (cdr (assoc 10 ele)) 0 1))
            ) ;_ end of setq
      (while vertex
        (setq vertexen (append vertexen vertex)
              ele      (cdr (member (assoc 10 ele) ele))
              ) ;_ end of setq
        (if (cdr (assoc 10 ele))
          (setq vertex (list (trans (cdr (assoc 10 ele)) 0 1)))
          (setq vertex nil)
          ) ;_ end of if
        ) ;_ end of while
      (setq vertexen (vl-remove nil vertexen))
                                        ; (sort_vertexen)
      )


(defun sort_vertexen (vertexen )
      (setq vertexen (cdr vertexen))
      (setq xp (list ()))
      (setq yp  (list ())
            cnt 0
            ) ;_ end of setq
      (repeat (length vertexen)
        (setq vertex (nth cnt vertexen)
              x      (list (car vertex))
              y      (list (cadr vertex))
              xp     (append xp x)
              yp     (append yp y)
              cnt    (1+ cnt)
              ) ;_ end of setq
        ) ;_ end of repeat
      (setq xp (cdr xp)
            yp (cdr yp)
            ) ;_ end of setq
      (setq xmin (vl-sort xp '<)
            ymin (vl-sort yp '<)
            xmax (vl-sort xp '>)
            ymax (vl-sort yp '>)
            ) ;_ end of setq
      (setq pt1 (list (car xmin) (car ymin))
            pt2 (list (car xmax) (car ymax))
            ) ;_ end of setq
      )



So i just used this code i had for some differend use and found with
Code: [Select]
(setq pti (polar pt1(angle pt1 pt2)
                   (/( distance pt1 pt2)2)))

a point nice in the room.

Thanks, Luis