;;;***********************************
;;; No.16-1 单行/多行文本加框 函数    
;;;***********************************
(defun C:ayTextBox (/	      SS1	n	  BoxType   roundSpace
		    i	      txtEntData	  txtEntName
		    txtEntType		tBox	  Pt_BL	    Pt_BR
		    PtTL      PtTR	Pt_MC	  xWidth    xHeight
		    xAngle
		   )
  (if (= #AY_TBOXRSPACE nil)
    (setq #AY_TBOXRSPACE 0.5)
  )
  (if (= #AY_TBOXBOXTYPE nil)
    (setq #AY_TBOXBOXTYPE "R")
  )
  (initget "Mtext Text All")
  (setq
    SelType (getkword
	      "\n选择类型 多行文本(Mtext)/单行文本(Text)/任意<All> "
	    )
  )
  (if (null SelType)
    (setq SelType "All")
  )					;endif
  (cond	((= SelType "Mtext") (setq SS1 (ssget '((0 . "MTEXT")))))
					;多行文本
	((= SelType "Text") (setq SS1 (ssget '((0 . "TEXT")))))
					;Text文本
	((= SelType "All") (setq SS1 (ssget '((0 . "*TEXT")))))
					;任意类型文本
  )					;endcond
  (if (null SS1)
    (progn (princ "\n没有文本实体被选择!") (exit))
  )
  (setq n (sslength SS1))
  (if (not (= nil n))			;No select objects
    (progn
      (initget "C R")
      (setq BoxType (getkword (strcat "\n边框类型 圆形(C)/矩形(R)<"
				      #AY_TBOXBOXTYPE
				      ">: "
			      )
		    )
      )
      (if (= BoxType nil)
	(setq BoxType #AY_TBOXBOXTYPE)
	(setq #AY_TBOXBOXTYPE BoxType)
      )
      (setq roundSpace
	     (getreal (strcat "\n文本距离边框偏移距<"
			      (rtos #AY_TBOXRSPACE 2)
			      ">: "
		      )
	     )
      )
      (if (= roundSpace nil)
	(setq roundSpace #AY_TBOXRSPACE)
	(setq #AY_TBOXRSPACE roundSpace)
      )
      (setq i 0)
      (while (< i n)
	(setq txtEntName (ssname SS1 i))
	(setq txtEntData (entget txtEntName))
	(setq i (+ i 1))
	(setq txtEntType (cdr (assoc 0 txtEntData)))
					;get Entity's name: "TEXT" or "MTEXT"
	(if (= txtEntType "TEXT")	;this object is simple line text
	  (progn
	    (vl-cmdf "ucs" "Object" txtEntName)
					;定义用户坐标系到文本的方向.
	    (setq tBox	(textbox (list (car txtEntData)))
					;must change to a list
		  Pt_BL	(car tBox)	;left bottom point coords
		  PtTR	(cadr tBox)	;right top point coords
		  PtTL	(list (car Pt_BL) (cadr PtTR))
		  Pt_BR	(list (car PtTR) (cadr Pt_BL))
		  Pt_MC	(polar Pt_BL
			       (angle Pt_BL PtTR)
			       (/ (distance Pt_BL PtTR) 2.0)
			)
	    )				;endsetq
	    (if	(= BoxType "C")		;圆形边框.
	      (vl-cmdf "_Circle"
		       "non"
		       Pt_MC
		       (+ (distance Pt_MC Pt_BL) roundSpace)
	      )
	    )				;End_if
	    (if	(= BoxType "R")		;矩形边框.
	      (progn
		(setq
		  Pt_BL
		   (polar Pt_BL PI roundSpace)
		)
		(setq
		  Pt_BL
		   (polar Pt_BL (* PI 1.5) roundSpace)
		)
		(setq
		  Pt_BR
		   (polar Pt_BR 0.0 roundSpace)
		)
		(setq
		  Pt_BR
		   (polar Pt_BR (* PI 1.5) roundSpace)
		)
		(setq
		  PtTL
		   (polar PtTL PI roundSpace)
		)
		(setq
		  PtTL
		   (polar PtTL (* PI 0.5) roundSpace)
		)
		(setq
		  PtTR
		   (polar PtTR 0.0 roundSpace)
		)
		(setq PtTR (polar PtTR (* PI 0.5) roundSpace))
		(vl-cmdf
		  "_PLine" "non" Pt_BL "non" Pt_BR "non" PtTR "non" PtTL
		  "C")
	      )
					; progn
	    )				; if
	    (vl-cmdf "ucs" "p")		;恢复原有坐标。
	  )				; progn
	)				; if
	(if (= txtEntType "MTEXT")
	  (progn
	    (vl-cmdf "_.JustifyText" txtEntName "" "TL")
					;处理为对对齐模式为: "左上".
	    (setq txtEntData (entget txtEntName))
	    (setq PtTL	  (cdr (assoc 10 txtEntData))
		  xWidth  (cdr (assoc 42 txtEntData))
		  xHeight (cdr (assoc 43 txtEntData))
		  xAngle  (cdr (assoc 50 txtEntData))
		  Pt_TC	  (polar PtTL xAngle (* xWidth 0.5))
		  PtTR	  (polar PtTL xAngle xWidth)
		  Pt_BL	  (polar PtTL (- xAngle (/ PI 2.0)) xHeight)
		  Pt_BC	  (polar Pt_BL xAngle (* xWidth 0.5))
		  Pt_BR	  (polar Pt_BL xAngle xWidth)
		  Pt_MC	  (polar Pt_BL
				 (angle Pt_BL PtTR)
				 (/ (distance Pt_BL PtTR) 2.0)
			  )
	    )				;endsetq
	    (if	(= BoxType "C")		;圆形边框.
	      (vl-cmdf "_Circle"
		       "non"
		       (trans Pt_MC 0 1)
		       (+ (distance Pt_MC Pt_BL) roundSpace)
	      )
	    )				;endif
	    (if	(= BoxType "R")		;矩形边框.
	      (progn
		(setq xAngle (cdr (assoc 50 txtEntData)))
		(setq Pt_BL (polar Pt_BL xAngle (- roundSpace)))
		(setq Pt_BL (polar Pt_BL
				   (+ xAngle (/ PI 2.0))
				   (- roundSpace)
			    )
		)
		(setq Pt_BR (polar Pt_BR xAngle roundSpace))
		(setq Pt_BR (polar Pt_BR
				   (+ xAngle (/ PI 2.0))
				   (- roundSpace)
			    )
		)
		(setq PtTL (polar PtTL xAngle (- roundSpace)))
		(setq
		  PtTL (polar PtTL (+ xAngle (/ PI 2.0)) roundSpace)
		)
		(setq PtTR (polar PtTR xAngle roundSpace))
		(setq
		  PtTR (polar PtTR (+ xAngle (/ PI 2.0)) roundSpace)
		)
		(vl-cmdf "_PLine"
			 "non"
			 (trans Pt_BL 0 1)
			 "non"
			 (trans Pt_BR 0 1)
			 "non"
			 (trans PtTR 0 1)
			 "non"
			 (trans PtTL 0 1)
			 "C"
		)
	      )				; progn
	    )				; if
	  )				; progn
	)				; if
      )					; while
    )					; progn
  )					;End_ if
)					; defun