(defun c:lm_Block_Add  ( / LM:AddObjectstoBlock LM:SafearrayVariant old_cmd acdoc ss e ee x )


;;----------------=={ 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 obj )
	(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)
)


;;------------------=={ 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
		)
	)
)


	(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))
	)
	(or e (exit))
	(setq
		ee (entget e)
		x (mai_dxf 41 ee)
	)
	(if (and
			(equal x (mai_dxf 42 ee) 0.001)
			(equal x (mai_dxf 43 ee) 0.001)
		)
		(entmod
			(setq
				ee (subst (cons 42 x) (assoc 42 ee) ee)
				ee (subst (cons 43 x) (assoc 43 ee) ee)
			)
		)
		(progn
			(princ  "\n图块为不等比块，无法操作。")
			(exit)
		)
	)
	(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)
		(vla-delete (vlax-ename->vla-object e))
		(vla-regen acdoc acAllViewports)
		(mai_end)
	)
	(princ)
)
