本帖最后由 飞雪神光 于 2023-9-27 23:36 编辑
不考虑选择的图形中是否有文字 块名是否重复- (defun c:tt (/ km lm-ent-block lm-get-box mid pts ss ss-enlst)
- (defun lm-Ent-Block (ss name InsertionPoint / lm-active-document lm-model-space lm-vla-list->array block)
- (defun lm-active-document nil
- (eval (list 'defun 'lm-active-document 'nil (vla-get-activedocument (vlax-get-acad-object))))
- (lm-active-document)
- )
- (defun lm-model-space nil
- (eval (list 'defun 'lm-model-space 'nil (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))))
- (lm-model-space)
- )
- (defun lm-vla-List->Array (nList arraytype)
- (vlax-SafeArray-Fill
- (vlax-Make-SafeArray
- arraytype
- (cons 0 (1- (length nList)))
- )
- nList
- )
- )
- (setq ss (lm-vla-List->Array (mapcar 'vlax-ename->vla-object (vl-remove-if-not '(lambda(arg) (equal (type arg) 'ename)) (mapcar 'cadr (ssnamex ss)))) 9))
- (setq block (vla-add (vla-get-Blocks (lm-active-document)) (vlax-3d-point InsertionPoint) name))
- (vla-CopyObjects (lm-active-document) ss block)
- (vla-InsertBlock (lm-model-space) (vlax-3d-point InsertionPoint) (vla-get-Name block) 1 1 1 0)
- (foreach obj (vlax-safearray->list ss) (vla-delete obj))
- block
- )
- (defun ss-enlst(ss / enlst)
- (cond
- ((= (type ss) 'PICKSET)
- (vl-remove-if-not '(lambda (x) (= (type x) 'ENAME)) (mapcar 'cadr (ssnamex SS)))
- )
- ((= (type ss) 'LIST)
- (setq enlst (ssadd))
- (last (mapcar '(lambda (x) (ssadd x enlst)) ss))
- )
- )
- )
- (defun lm-get-box(SS1 / get-enbox get-Extents get-ssbox)
- (defun get-ssbox (ss / boxlst maxlst minlst objlst)
- (setq objlst (mapcar 'vlax-ename->vla-object (ss-enlst ss)))
- (setq boxlst (mapcar 'get-enbox objlst))
- (setq minlst (mapcar 'car boxlst))
- (setq maxlst (mapcar 'cadr boxlst))
- (list
- (apply 'mapcar (cons 'min minlst))
- (apply 'mapcar (cons 'max maxlst))
- )
- )
- (defun get-enbox (obj)
- (if (= (type obj) 'ENAME) (setq obj (vlax-ename->vla-object obj)))
- (vla-getboundingbox obj 'Minp 'Maxp)
- (mapcar 'vlax-safearray->list (list Minp Maxp))
- )
- (defun get-Extents(lst);返回点表最大外框两对角点的表
- (list
- (apply 'mapcar (cons 'min lst))
- (apply 'mapcar (cons 'max lst))
- )
- )
- (cond
- ((= (type SS1) 'PICKSET) (get-ssbox SS1)) ;集
- ((= (type SS1) 'ENAME) (get-enbox SS1)) ;图元
- ((= (type SS1) 'LIST) (get-Extents SS1)) ;点表
- (t nil)
- )
- )
- (defun get-dxf(en n)
- (if (not (listp en)) (setq en (entget en)))
- (cdr (assoc n en))
- )
- (defun Mid (p1 p2)(mapcar '(lambda (x) (* x 0.5)) (mapcar '+ p1 p2)))
- (setq ss (ssget '()))
- (foreach ty (ss-enlst ss)
- (if(wcmatch (get-dxf ty 0) "*TEXT")
- (setq km(get-dxf ty 1))
- )
- )
- (setq pts (lm-get-box ss))
- (lm-Ent-Block ss km (apply 'Mid pts))
- (princ)
- )
|