Jump to content

Nhờ Viết Lisp Tạo Text

(defun C:00	(/ DAITHEP NDUNG OBJTHEPDAI PNTDAT)
	(vl-load-com)
	(defun *error* (msg)
		(if	Olmode
			(setvar 'osmode Olmode)
		)
		(if	(not (member msg '("*BREAK,*CANCEL*,*EXIT*")))
			(princ (strcat "\nError: " msg))
		)
		(princ)
	)
	(setvar 'CMDECHO 0)
	(setq Olmode (getvar "OSMODE"))
	(setvar "OSMODE" 0)
	(setq	ObjThepdai
				 (car
					 (LM:SelectIf
						 "\nCh\U+1ECDn th\U+00E9p \U+0111ai "
						 (lambda (x)
							 (or (eq "LWPOLYLINE" (cdr (assoc 0 (entget (car x)))))
									 (eq "POLYLINE" (cdr (assoc 0 (entget (car x)))))
									 (eq "LINE" (cdr (assoc 0 (entget (car x)))))
							 )
						 )
						 entsel
						 nil
					 )
				 )
	)
	(setq Daithep (length1 ObjThepdai))
	(setq	Kyhieuthep
				 (LM:GetXWithDefault_New
					 getstring "\nNh\U+1EADp k\U+00FD hi\U+1EC7u thanh th\U+00E9p "	'*Kyhieuthep*	"2"	nil	nil)
	)
	(setq	Duongkinh
				 (LM:GetXWithDefault_New
					 getdist "\nNh\U+1EADp \U+0111\U+01B0\U+1EDDng k\U+00EDnh "	'*Duongkinh* 10.0	nil	nil)
	)
	(setq	Kcrd
				 (LM:GetXWithDefault_New
					 getdist "\nNh\U+1EADp kho\U+1EA3ng c\U+00E1ch r\U+00E3nh \U+0111ai "	'*Kcrd*	150.0	nil	nil)
	)
	(setq	Caochu
				 (LM:GetXWithDefault_New
					 getdist "\nNh\U+1EADp kho\U+1EA3ng c\U+00E1ch r\U+00E3nh \U+0111ai "	'*Caochu*	100.0	nil	nil)
	)
	(setq	Ndung	(strcat	"-"
											(rtos (/ Daithep Kcrd) 2 0)
											"d"
											(rtos Duongkinh 2 0)
											"a"
											(rtos Kcrd 2 0)
											"-L="
											(rtos Daithep 2 0)
							)
	)
	(setq PntDat (getpoint "\nCh\U+1ECDn \U+0111i\U+1EC3m ghi Text:\n"))
	(MakeText PntDat Ndung Caochu 0 "MC" nil nil nil)
	(setvar "OSMODE" Olmode)
	(princ)
)
(defun Length1 (e) (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)))
(defun MakeText	(point string Height Ang justify Layer Style Color / Lst)
																														; Ang: Radial
	(setq	Lst			(list	'(0 . "TEXT")
											(cons 10 point)
											(cons 40 Height)
											(cons	8
														(if	Layer
															Layer
															(getvar "CLAYER")
														)
											)
											(cons 1 string)
											(if	Ang
												(cons 50 Ang)
											)
											(cons	7
														(if	Style
															Style
															(getvar "Textstyle")
														)
											)
											(cons	62
														(if	Color
															Color
															256
														)
											)
								)
				justify	(strcase justify)
	)
	(cond
		((= justify "C")
		 (setq Lst (append Lst (list (cons 72 1) (cons 11 point))))
		)
		((= justify "L")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 73 0) (cons 10 point)))
		 )
		)
		((= justify "R")
		 (setq Lst (append Lst (list (cons 72 2) (cons 11 point))))
		)
		((= justify "M")
		 (setq Lst (append Lst (list (cons 72 4) (cons 11 point))))
		)
		((= justify "TL")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 11 point) (cons 73 3)))
		 )
		)
		((= justify "TC")
		 (setq
			 Lst
				(append Lst (list (cons 72 1) (cons 11 point) (cons 73 3)))
		 )
		)
		((= justify "TR")
		 (setq
			 Lst
				(append Lst (list (cons 72 2) (cons 11 point) (cons 73 3)))
		 )
		)
		((= justify "ML")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 11 point) (cons 73 2)))
		 )
		)
		((= justify "MC")
		 (setq
			 Lst
				(append Lst (list (cons 72 1) (cons 11 point) (cons 73 2)))
		 )
		)
		((= justify "MR")
		 (setq
			 Lst
				(append Lst (list (cons 72 2) (cons 11 point) (cons 73 2)))
		 )
		)
		((= justify "BL")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 11 point) (cons 73 1)))
		 )
		)
		((= justify "BC")
		 (setq
			 Lst
				(append Lst (list (cons 72 1) (cons 11 point) (cons 73 1)))
		 )
		)
		((= justify "BR")
		 (setq
			 Lst
				(append Lst (list (cons 72 2) (cons 11 point) (cons 73 1)))
		 )
		)
	)
	(entmakex Lst)
)
(defun LM:SelectIf (msg pred func keyw / sel)
	(setq pred (eval pred))
	(while
		(progn
			(setvar 'ERRNO 0)
			(if	keyw
				(apply 'initget keyw)
			)
			(setq sel (func msg))
			(cond
				((= 7 (getvar 'ERRNO))
				 (princ
					 "\nB\U+1EA1n ch\U+1ECDn sai r\U+1ED3i! H\U+00E3y ch\U+1ECDn l\U+1EA1i."
				 )
				)
				((eq 'STR (type sel))
				 nil
				)
				((vl-consp sel)
				 (if (and pred (not (pred sel)))
					 (princ "")
				 )
				)
			)
		)
	)
	sel
)

;; GetX with Default  -  Lee Mac
;; fun -  Quoted user input function
;; pmt -  Prompt string
;; sym -  Quoted function to hold default value (not 'sym)
;; def -  Initial default value
;; ini -   List of initget arguments
;; arg -   List of arguments for user input function
;; Returns:  User input or default value

;;;;;;  (LM:GetXWithDefault_New getkword "\nEnter an Option  " '*opt* "Alpha" '("Alpha Beta Gamma") nil)
;;;;;;  (LM:GetXWithDefault_New getstring "\nEnter a String " '*str* "Lee Mac" nil '(T))
(defun LM:GetXWithDefault_New	(_function _prompt _symbol _default _initget _args / _toString)
	(vl-load-com)
	;; © Lee Mac 2010

	(setq	_toString
				 (lambda (x)
					 (cond
						 ((eq getangle _function) (angtos x))
						 ((eq 'REAL (type x)) (rtos x))
						 ((eq 'INT (type x)) (itoa x))
						 ((vl-princ-to-string x))
					 )
				 )
	)

	(if	_initget
		(apply 'initget _initget)
	)

	(set _symbol
			 (
				(lambda	(input)
					(if	(or (not input) (eq "" input))
						(eval _symbol)
						input
					)
				)
				 (apply	'_function
								(append	_args
												(list
													(strcat	_prompt
																	"<"
																	(_toString
																		(set _symbol
																				 (cond
																					 ((eval _symbol))
																					 (_default)
																				 )
																		)
																	)
																	"> : "
													)
												)
								)
				 )
			 )
	)
)
 

Giúp bạn chút. Còn sửa như thế nào thì nhờ mng sửa. Mình tranh thủ tý thôi

Cách dùng

  1. Tải tệp 462278_00.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh 00 để chạy lisp.

Mã nguồn 462278_00.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/191251-nh%E1%BB%9D-vi%E1%BA%BFt-lisp-t%E1%BA%A1o-text/
(defun C:00	(/ DAITHEP NDUNG OBJTHEPDAI PNTDAT)
	(vl-load-com)
	(defun *error* (msg)
		(if	Olmode
			(setvar 'osmode Olmode)
		)
		(if	(not (member msg '("*BREAK,*CANCEL*,*EXIT*")))
			(princ (strcat "\nError: " msg))
		)
		(princ)
	)
	(setvar 'CMDECHO 0)
	(setq Olmode (getvar "OSMODE"))
	(setvar "OSMODE" 0)
	(setq	ObjThepdai
				 (car
					 (LM:SelectIf
						 "\nCh\U+1ECDn th\U+00E9p \U+0111ai "
						 (lambda (x)
							 (or (eq "LWPOLYLINE" (cdr (assoc 0 (entget (car x)))))
									 (eq "POLYLINE" (cdr (assoc 0 (entget (car x)))))
									 (eq "LINE" (cdr (assoc 0 (entget (car x)))))
							 )
						 )
						 entsel
						 nil
					 )
				 )
	)
	(setq Daithep (length1 ObjThepdai))
	(setq	Kyhieuthep
				 (LM:GetXWithDefault_New
					 getstring "\nNh\U+1EADp k\U+00FD hi\U+1EC7u thanh th\U+00E9p "	'*Kyhieuthep*	"2"	nil	nil)
	)
	(setq	Duongkinh
				 (LM:GetXWithDefault_New
					 getdist "\nNh\U+1EADp \U+0111\U+01B0\U+1EDDng k\U+00EDnh "	'*Duongkinh* 10.0	nil	nil)
	)
	(setq	Kcrd
				 (LM:GetXWithDefault_New
					 getdist "\nNh\U+1EADp kho\U+1EA3ng c\U+00E1ch r\U+00E3nh \U+0111ai "	'*Kcrd*	150.0	nil	nil)
	)
	(setq	Caochu
				 (LM:GetXWithDefault_New
					 getdist "\nNh\U+1EADp kho\U+1EA3ng c\U+00E1ch r\U+00E3nh \U+0111ai "	'*Caochu*	100.0	nil	nil)
	)
	(setq	Ndung	(strcat	"["
											Kyhieuthep
											"]-"
											(rtos (/ Daithep Kcrd) 2 0)
											"d"
											(rtos Duongkinh 2 0)
											"a"
											(rtos Kcrd 2 0)
											"-L="
											(rtos Daithep 2 0)
							)
	)
	(setq PntDat (getpoint "\nCh\U+1ECDn \U+0111i\U+1EC3m ghi Text:\n"))
	(MakeText PntDat Ndung Caochu 0 "MC" nil nil nil)
	(setvar "OSMODE" Olmode)
	(princ)
)
(defun Length1 (e) (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)))
(defun MakeText	(point string Height Ang justify Layer Style Color / Lst)
																														; Ang: Radial
	(setq	Lst			(list	'(0 . "TEXT")
											(cons 10 point)
											(cons 40 Height)
											(cons	8
														(if	Layer
															Layer
															(getvar "CLAYER")
														)
											)
											(cons 1 string)
											(if	Ang
												(cons 50 Ang)
											)
											(cons	7
														(if	Style
															Style
															(getvar "Textstyle")
														)
											)
											(cons	62
														(if	Color
															Color
															256
														)
											)
								)
				justify	(strcase justify)
	)
	(cond
		((= justify "C")
		 (setq Lst (append Lst (list (cons 72 1) (cons 11 point))))
		)
		((= justify "L")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 73 0) (cons 10 point)))
		 )
		)
		((= justify "R")
		 (setq Lst (append Lst (list (cons 72 2) (cons 11 point))))
		)
		((= justify "M")
		 (setq Lst (append Lst (list (cons 72 4) (cons 11 point))))
		)
		((= justify "TL")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 11 point) (cons 73 3)))
		 )
		)
		((= justify "TC")
		 (setq
			 Lst
				(append Lst (list (cons 72 1) (cons 11 point) (cons 73 3)))
		 )
		)
		((= justify "TR")
		 (setq
			 Lst
				(append Lst (list (cons 72 2) (cons 11 point) (cons 73 3)))
		 )
		)
		((= justify "ML")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 11 point) (cons 73 2)))
		 )
		)
		((= justify "MC")
		 (setq
			 Lst
				(append Lst (list (cons 72 1) (cons 11 point) (cons 73 2)))
		 )
		)
		((= justify "MR")
		 (setq
			 Lst
				(append Lst (list (cons 72 2) (cons 11 point) (cons 73 2)))
		 )
		)
		((= justify "BL")
		 (setq
			 Lst
				(append Lst (list (cons 72 0) (cons 11 point) (cons 73 1)))
		 )
		)
		((= justify "BC")
		 (setq
			 Lst
				(append Lst (list (cons 72 1) (cons 11 point) (cons 73 1)))
		 )
		)
		((= justify "BR")
		 (setq
			 Lst
				(append Lst (list (cons 72 2) (cons 11 point) (cons 73 1)))
		 )
		)
	)
	(entmakex Lst)
)
(defun LM:SelectIf (msg pred func keyw / sel)
	(setq pred (eval pred))
	(while
		(progn
			(setvar 'ERRNO 0)
			(if	keyw
				(apply 'initget keyw)
			)
			(setq sel (func msg))
			(cond
				((= 7 (getvar 'ERRNO))
				 (princ
					 "\nB\U+1EA1n ch\U+1ECDn sai r\U+1ED3i! H\U+00E3y ch\U+1ECDn l\U+1EA1i."
				 )
				)
				((eq 'STR (type sel))
				 nil
				)
				((vl-consp sel)
				 (if (and pred (not (pred sel)))
					 (princ "")
				 )
				)
			)
		)
	)
	sel
)

;; GetX with Default  -  Lee Mac
;; fun - [sym] Quoted user input function
;; pmt - [str] Prompt string
;; sym - [sym] Quoted function to hold default value (not 'sym)
;; def - [any] Initial default value
;; ini - [lst] [Optional] List of initget arguments
;; arg - [lst] [Optional] List of arguments for user input function
;; Returns: [any] User input or default value

;;;;;;  (LM:GetXWithDefault_New getkword "\nEnter an Option [Alpha/Beta/Gamma] " '*opt* "Alpha" '("Alpha Beta Gamma") nil)
;;;;;;  (LM:GetXWithDefault_New getstring "\nEnter a String " '*str* "Lee Mac" nil '(T))
(defun LM:GetXWithDefault_New	(_function _prompt _symbol _default _initget _args / _toString)
	(vl-load-com)
	;; © Lee Mac 2010

	(setq	_toString
				 (lambda (x)
					 (cond
						 ((eq getangle _function) (angtos x))
						 ((eq 'REAL (type x)) (rtos x))
						 ((eq 'INT (type x)) (itoa x))
						 ((vl-princ-to-string x))
					 )
				 )
	)

	(if	_initget
		(apply 'initget _initget)
	)

	(set _symbol
			 (
				(lambda	(input)
					(if	(or (not input) (eq "" input))
						(eval _symbol)
						input
					)
				)
				 (apply	'_function
								(append	_args
												(list
													(strcat	_prompt
																	"<"
																	(_toString
																		(set _symbol
																				 (cond
																					 ((eval _symbol))
																					 (_default)
																				 )
																		)
																	)
																	"> : "
													)
												)
								)
				 )
			 )
	)
)
 

Thảo luận gốc

Bài viết gốc #462278 không còn trên diễn đàn.

Về Thư viện AutoLisp (trang 325)