Jump to content

Recommended Posts

Posted

How to get the center of gravity solids contained in the block.

 

 

 

 

I used

 

(vlax-safearray->list(vlax-variant-value(vla-get-centroid vlaobject)))

Unfortunately, I do not know how to convert to global coordinate system

Posted

Use gile's refgeom function to transform the point relative to the block reference geometry, for example:

(defun c:test ( / cen ent )
   (princ "\nSelect a block reference containing a solid: ")
   (if (setq ent (ssget "_+.:E:S" '((0 . "INSERT"))))
       (progn
           (setq ent (ssname ent 0))
           (vlax-for obj (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) (cdr (assoc 2 (entget ent))))
               (if (vlax-property-available-p obj 'centroid)
                   (setq cen (vlax-get obj 'centroid))
               )
           )
           (if cen
               (entmake
                   (list
                      '(0 . "POINT")
                       (cons 10 (apply '(lambda ( m v ) (mapcar '+ (mxv m cen) v)) (refgeom ent)))
                   )
               )
               (princ "\nNo block components found with Centroid property.")
           )
       )
   )
   (princ)
)

;; RefGeom (gile)
;; Returns a list whose first item is a 3x3 transformation matrix and
;; second item the object insertion point in its parent (xref, block or space)

(defun refgeom ( ent / ang enx mat ocs )
   (setq enx (entget ent)
         ang (cdr (assoc 050 enx))
         ocs (cdr (assoc 210 enx))
   )
   (list
       (setq mat
           (mxm
               (mapcar '(lambda ( v ) (trans v 0 ocs t))
                  '(
                       (1.0 0.0 0.0)
                       (0.0 1.0 0.0)
                       (0.0 0.0 1.0)
                   )
               )
               (mxm
                   (list
                       (list (cos ang) (- (sin ang)) 0.0)
                       (list (sin ang) (cos ang)     0.0)
                      '(0.0 0.0 1.0)
                   )
                   (list
                       (list (cdr (assoc 41 enx)) 0.0 0.0)
                       (list 0.0 (cdr (assoc 42 enx)) 0.0)
                       (list 0.0 0.0 (cdr (assoc 43 enx)))
                   )
               )
           )
       )
       (mapcar '- (trans (cdr (assoc 10 enx)) ocs 0)
           (mxv mat (cdr (assoc 10 (tblsearch "block" (cdr (assoc 2 enx))))))
       )
   )
)

;; Matrix Transpose  -  Doug Wilson
;; Args: m - nxn matrix

(defun trp ( m )
   (apply 'mapcar (cons 'list m))
)

;; Matrix x Matrix  -  Vladimir Nesterovsky
;; Args: m,n - nxn matrices

(defun mxm ( m n )
   ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
)

;; Matrix x Vector  -  Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n

(defun mxv ( m v )
   (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)

(vl-load-com) (princ)

Posted

This is a great function, thanks very much.

 

 

function takes the center of gravity of the first solid block

is there a way that no "union" to determine the center of gravity of the two\thre... solids ?

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