(defun c:mini_shp2blk (/ oldcmd thaw0 doit shp ed nam ins scl rot spc
                    lay clr ltp blk cur_locked ss ent ent_locked 0_locked vec)
 
  (mai_start nil)
  (setq SHP (car (mai_entsel nil "选择要转换的形:" "选择的不是形." '((0 . "SHAPE")) nil)))
  (or SHP (exit))
  (if (and
          (= (logand 1 (mai_dxf 70 (tblsearch "layer" "0"))) 1) ; if layer 0 is frozen
          (not (= (logand 4 (getvar "cmdactive")) 4))               ; and no script is running,
      )
      (progn
		  (initget "Yes No" 128)
		  (setq ss (getkword "\n形变块命令不能在0层冻结的状态下运行，是否解冻0层? <是Y>:"))
		  (if (or (not ss) (= ss "Yes"))
			  (progn
				  (command "_.layer" "_thaw" "0" "")
				  (setq thaw0 t
						doit  t
				  )
			  )
		  )
      )
      (setq doit t)
  )
  (or doit (exit))
	(if (setq 0_locked (clone_acet_layer_locked "0"))
		(command "_.layer" "_unl" "0" "")
	)
	(setq ss (getvar "clayer"))
	(if (setq cur_locked (clone_acet_layer_locked ss))
		(command "_.layer" "_unl" ss "")
	)
  (setq nam (strcase (mai_dxf 2 shp))                ; get shape's name
		ss (bns_get_shapefile nam)
		blk (mai_shape_2_block (caadr ss) (car ss))
		ss (ssget "_x" (list (cons 0 "shape") (cons 2 nam)))
  )
  (foreach ent (mai_ssget_2_list ss)          ; step through each object in set
	(setq ed  (entget ent)
		  ins (assoc 10 ed)                    ; get shape's insertion point
		  scl (mai_dxf 40 ed)                  ; get shape's size
		  rot (assoc 50 ed)                    ; get shape's rotation
		  lay (assoc 8 ed)                     ; get shape's layer
		  clr (assoc 62 ed)                    ; get shape's color
		  ltp (assoc 6 ed)                     ; get shape's linetype
		  spc (assoc 67 ed)                    ; get shape's space
		  vec (assoc 210 ed)                   ; get shape's extrusion vector
	)
	(if (clone_acet_layer_locked (cdr lay))                   ; if shape's layer is locked
		(progn
			(command "_.layer" "_unl" (cdr lay) "")      ; unlock it
			(setq ent_locked t)
		)
	)
	(entdel ENT)
	(entmake (list
			   (cons 0 "insert")
			   (cons 2 blk)
			   ins
			   (cons 41 scl)
			   (cons 42 scl)
			   (cons 43 scl)
			   rot
			   lay
			   (if clr clr (cons 62 256))
			   (if ltp ltp (cons 6 "bylayer"))
			   (if spc spc (cons 67 0))
			   vec
			 )
	)
	(if ent_locked
		(progn
			(command "_.layer" "_lock" (cdr lay) "")     ; relock if needed
			(setq ent_locked nil)
		)
	)
  )
  (if cur_locked (command "_.layer" "_lock" (getvar "clayer") "")) ; relock current if needed
  (if 0_locked (command "_.layer" "_lock" "0" ""))                 ; relock 0 if needed
  (prompt (strcat "\n所有形[ " NAM " ]被替换成块[ " BLK " ]."))
  (if THAW0
	  (progn
		  (command "_.layer" "_freeze" "0" "")
		  (prompt "\n 0 层重新被冻结.")
	  )
  )
  (mai_end)
)
