;;----------------=={ Add Objects to Block }==----------------;;
;;                                                            ;;
;;  Adds all objects in the provided SelectionSet to the      ;;
;;  definition of the specified block.                        ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  doc   - Document Object in which block resides.           ;;
;;  block - Entity name of reference insert                   ;;
;;  ss    - SelectionSet of objects to add to definition      ;;
;;------------------------------------------------------------;;

(defun LM:AddObjectstoBlock ( doc block ss / lst mat )
  (setq lst (mapcar 'vlax-ename->vla-object (mai_ssget_2_list ss))
        mat (mai_blk_RevMatrix block)
        mat (vlax-tmatrix (append (mapcar 'append (car mat) (mapcar 'list (cadr mat))) '((0. 0. 0. 1.))))
  )
  (foreach obj lst (vla-transformby obj mat))
  (vla-CopyObjects doc (LM:SafearrayVariant vlax-vbobject lst)
     (vla-item (vlax-get doc 'Blocks) (mai_dxf 2 block))
  )
  (foreach obj lst (vla-delete obj))
  (vla-regen doc acAllViewports)
)

;;-----------------=={ Remove From Block }==------------------;;
;;                                                            ;;
;;  Removes an Entity from a Block Definition                 ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  ent - Entity name of Object to Delete from Block [ENAME]  ;;
;;------------------------------------------------------------;;

(defun LM:RemovefromBlock ( doc ent )
  (vla-delete (vlax-ename->vla-object ent))
  (vla-regen doc acAllViewports)
  (princ)
)

;;------------------=={ Safearray Variant }==-----------------;;
;;                                                            ;;
;;  Creates a populated Safearray Variant of a specified      ;;
;;  data type                                                 ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  datatype - variant type enum (eg vlax-vbDouble)           ;;
;;  data     - list of static type data                       ;;
;;------------------------------------------------------------;;
;;  Returns:  VLA Variant Object of type specified            ;;
;;------------------------------------------------------------;;
                         
(defun LM:SafearrayVariant ( datatype data )
  (vlax-make-variant
    (vlax-safearray-fill
      (vlax-make-safearray datatype (cons 0 (1- (length data)))) data
    )    
  )
)



;-------------------------------------------------------------;
;                   -- Test Functions --                      ;
;-------------------------------------------------------------;

(defun c:lm_Block_Add  ( / old_cmd acdoc ss e )
  (mini_start nil)
  (mai_last_com "lm_Block_Add")
  (setq acdoc (vlax-get (vlax-get-acad-object) 'ActiveDocument)
        ss (mai_select_first nil "Ñ¡ÔñÒªÌí¼Ó½ø¿éµÄÊµÌå:<»Ø³µÍË³ö>" "Ã»ÓÐÕÒµ½ÊµÌå,ÍË³ö.")
        e (car (mai_entsel nil "Ñ¡Ôñ¿é:<»Ø³µÍË³ö>" "Ñ¡ÔñµÄ²»ÊÇ¿é,ÍË³ö." '((0 . "INSERT")) nil))
  )
  (if e (LM:AddObjectstoBlock acdoc e ss))
  (mai_end)
)

;-------------------------------------------------------------;

(defun c:lm_Block_del ( / old_cmd acdoc e )
  (mai_last_com "lm_Block_del")
  (setq acdoc (vlax-get (vlax-get-acad-object) 'ActiveDocument))
  (while (setq e (car (nentsel "\nÑ¡ÔñÒªÉ¾³ýµÄ¿éÄÚÊµÌå:<»Ø³µÍË³ö>")))
    (mini_start nil)
    (LM:RemovefromBlock acdoc e)
    (mai_end)
  )
  (princ)
)
(princ)
