Jump to content

Lisp lệnh G2D G3D trong chủ đề “Đường cong bậc 2 trong cad”

Có 1 chút nhầm lẫn, tôi đã sửa ở link cũ. Và bạn có thể down trực tiếp cái này.
Lisp này dùng để vẽ đồ thị của các hàm số y=f(x) trong 2D và z=f(x,y) trong 3D.
Cách nhập: bạn xem trích dẫn dưới đây đối với ví dụ của bạn:
Command: g2d
Nhap cong thuc ham so theo dang bieu thuc CAL: x^2-2*x+1
Nhap gia tri Xmin: 0
Nhap gia tri Xmax: 10
Nhap so khoang chia: 10
Chon kieu ve <S>: S

Cách dùng

  1. Tải tệp 220298_g2d_g3d.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 G2D G3D để chạy lisp.

Mã nguồn 220298_g2d_g3d.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/63472-%C4%91%C6%B0%E1%BB%9Dng-cong-b%E1%BA%ADc-2-trong-cad/
;http://xaydungit.vn/diendan/showthread.php?7940-V%E1%BA%BD-%C4%91%E1%BB%93-th%E1%BB%8B-c%C3%A1c-h%C3%A0m-s%E1%BB%91-y-f%28x%29-v%C3%A0-z-f%28x-y%29-trong-kh%C3%B4ng-gian-2D-v%C3%A0-3D
;Down file nay ve may, sau do chuyen font sang TCVN3-ABC de doc huong dan cach su dung.
;Doan Van Ha - XaydungIT.vn - Ngay 15/5/2012
;Môc ®Ých: vÏ ®å thÞ hµm sè y=f(x) vµ z=f(x,y) trong kh«ng gian 2D vµ 3D.
;Ghi chó chung:
;----- BiÕn cña hµm sè quy ®Þnh lµ X(x) ®èi víi hµm 1 biÕn; hoÆc X(x) vµ Y(y) ®èi víi hµm 2 biÕn.
;----- C¸c to¸n tö: + - * / ^
;----- C¸c hµm cã s½n: sin cos tan asin acos atan exp sqrt abs log fix float
;----- C¸c hµm bæ sung thªm: sin2 cos2 tan2 ctan ln lg sqr
;----- C¸c hµm ch­a dïng ®­îc:  min max rem expt logand logior (sÏ update khi cã thÓ)
;----- C¸c hµm l­îng gi¸c cã ®¬n vÞ tÝnh lµ radian, riªng hµm cã ch÷ sè "2" ®i sau th× tÝnh víi ®¬n vÞ lµ ®é.
;C¸ch thùc hiÖn:
;----- A). C¸ch vÏ ®å thÞ hµm 1 biÕn y=f(x) trong 2D:
;----- 1). C¸ch 1:  dïng lÖnh G2D vµ nhËp hµm d­íi d¹ng biÓu thøc CAL.
;----- ¦u ®iÓm: nhËp ®¬n gi¶n; phï hîp víi nh÷ng ng­êi ch­a rµnh lisp.
;----- Nh­îc ®iÓm: kh«ng sö dông ®­îc 1 sè hµm nh­: min max rem expt logand logior
;----- 2). C¸ch 2: viÕt hµm phô
;----- ¦u ®iÓm: kh«ng giíi h¹n bëi c¸c hµm cña lÖnh CAL.
;----- Nh­îc ®iÓm: ph¶i cã kh¶ n¨ng hiÓu lisp ®Ó viÕt hµm phô.
;----- VÝ dô vÒ c¸ch viÕt hµm phô vµ c¸ch dïng. VD vÏ ®å thÞ hµm: y=100*sin2(x)^3 ; trong kho¶ng (0 720) ; sè kho¶ng chia lµ 100.
;----- Hµm phô:
(defun fun2(x)
(* 100 (expt (sin2 x) 3)))
;----- C¸ch dïng: (G2D 'fun2 '(0 720) 40 "S") ;(c¸c kiÓu vÏ cña "S": "PO"=Points; "L"=Lines; "P"=Pline; "S"=Spline)
;----- B). C¸ch vÏ ®å thÞ hµm 2 biÕn z=f(x,y) trong 3D:
;----- 1). C¸ch 1: dïng lÖnh G3D vµ nhËp hµm d­íi d¹ng biÓu thøc CAL.
;----- ¦u vµ Nh­îc ®iÓm: nh­ trªn.
;----- 2). C¸ch 2: viÕt hµm phô
;----- ¦u ®iÓm: kh«ng giíi h¹n bëi c¸c hµm cña lÖnh CAL.
;----- Nh­îc ®iÓm: ph¶i cã kh¶ n¨ng hiÓu lisp ®Ó viÕt hµm phô.
;----- VÝ dô vÒ c¸ch viÕt hµm phô vµ c¸ch dïng. VD vÏ ®å thÞ hµm: z=cos(sqrt(2*x^2+2*y^2)) ; trong kho¶ng (-20 20) ; sè kho¶ng chia lµ 40.
;----- Hµm phô:
(defun fun3(x y)
(cos (sqrt (+ (* 2 x x) (* 2 y y)))))
;----- C¸ch dïng: (G3D 'fun3 '(-20 20) '(-20 20) 40)
;----- 3). C¸ch 3: dïng hµm Lambda
;----- ¦u vµ Nh­îc ®iÓm: nh­ c¸ch 2.
;----- VÝ dô vÒ c¸ch viÕt hµm phô vµ c¸ch dïng. VD vÏ ®å thÞ hµm: z=exp(-(x^2+y^2)) ; trong kho¶ng (-2 2) ; sè kho¶ng chia lµ 20.
;----- C¸ch viÕt: (G3D '(lambda (x y) (exp (- (+ (* x x) (* y y))))) '(-2 2) '(-2 2) 20)
;-------------------------------------------------------------------- VÏ ®å thÞ hµm sè 1 biÕn y=f(x) trong 2D.
;EX: y=100*sin2(x)^3 ; tõ 0 ®Õn 720 ; num=100
(defun C:G2D (/ hamso xmin xmax num x y lst kieu)
(vl-load-com) (if (not cal) (arxload "geomcal"))
(setq hamso (strcat (getstring "\nNhap cong thuc ham so theo dang bieu thuc CAL: "))
  		xmin (getreal "\nNhap gia tri Xmin: ")
  		xmax (getreal "\nNhap gia tri Xmax: ")
  		num (getint "\nNhap so khoang chia: ")
  		x xmin
;   		y (c:cal (LM:StringSubst (vl-prin1-to-string x) "X" hamso))
  		y (eval (infix->prefix (LM:StringSubst (vl-prin1-to-string x) "X" (HA:Thay hamso))))
  		lst (list (list x y)))
(while (< x xmax)
 (setq x (+ x (/ (- xmax xmin) num))
;            y (c:cal (LM:StringSubst (vl-prin1-to-string x) "X" hamso))
           y (eval (infix->prefix (LM:StringSubst (vl-prin1-to-string x) "X" (HA:Thay hamso))))
           lst (cons (list x y) lst)))
(setq lst (reverse lst))
(initget "PO L P S") (setq kieu (getkword "\nChon kieu ve [POints/Lines/Pline/Spline] <S>: "))
(if (not kieu) (setq kieu "S"))
(cond
 ((= kieu "PO") (foreach pt lst (HA:Point pt)))
 ((= kieu "L") (setq x 0) (repeat (1- (length lst)) (HA:Line (nth x lst) (nth (1+ x) lst)) (setq x (1+ x))))
 ((= kieu "P") (HA:Pline lst))
 ((= kieu "S") (HA:Spline lst)))
(princ))
;-------------------------------------------------------------------- VÏ ®å thÞ hµm sè 2 biÕn z=f(x,y) trong 3D (chó ý: sè kho¶ng chia: 1<num<255).
;EX1: z=cos(sqrt(2*x^2+2*y^2)) ; xmin=ymin=-20 ; xmax=ymax=+20 ; num=40
;EX2: z=exp(-(x^2+y^2)) ; xmin=ymin=-2 ; xmax=ymax=+2 ; num=20
(defun C:G3D ( / hamso xmin xmax ymin ymax num stepx stepy i j)
(vl-load-com) (if (not cal) (arxload "geomcal")) (setq cmd (getvar "cmdecho")) (setvar "cmdecho" 0)
(setq hamso (strcat (getstring "\nNhap cong thuc ham so theo dang bieu thuc CAL: "))
  		xmin (getreal "\nNhap gia tri Xmin: ")
  		xmax (getreal "\nNhap gia tri Xmax: ")
  		ymin (getreal (strcat "\nNhap gia tri Ymin: " "<" (vl-prin1-to-string xmin) "> "))
  		ymax (getreal (strcat "\nNhap gia tri Ymax: "  "<" (vl-prin1-to-string xmax) "> "))
  		num (getint "\nNhap so khoang chia: "))
(if (not ymin) (setq ymin xmin))
(if (not ymax) (setq ymax xmax))
(setq stepx (/ (- xmax xmin) num) stepy (/ (- ymax ymin) num) i 0 x xmin num (1+ num))
(command "_.3DMESH" num num)
(while (< i num)
 (setq j 0 y ymin)
 (while (< j num)
;   (command (list x y (c:cal (LM:StringSubst (vl-prin1-to-string y) "Y" (LM:StringSubst (vl-prin1-to-string x) "X" hamso)))))
  (command (list x y (eval (infix->prefix (LM:StringSubst (vl-prin1-to-string y) "Y" (LM:StringSubst (vl-prin1-to-string x) "X" (HA:Thay hamso)))))))
  (setq j (1+ j) y (+ y stepy)))
 (setq i (1+ i) x (+ x stepx)))
(setvar "cmdecho" cmd))
;------------------------------------------------------------------------------------ C¸c hµm c¬ b¶n ------------------------------------------------------------------------------
; Arguments:
; fun: Hµm sè 2D.
; range: Kho¶ng gi¸ trÞ X. VD: '(-20 20)
; res: §é ph©n gi¶i (®é mÞn). VD: 100
; type: KiÓu vÏ. VD: Spline.
(defun G2D (fun range res type / step i x lst)
(setq x (car range) step (/ (- (cadr range) (car range)) (float res)) i 0 res (1+ res))
(while (< i res)
 (setq lst (cons (list x (apply fun (list x))) lst))
 (setq i (1+ i) x (+ x step)))
(setq lst (reverse lst))
(setq a lst)
(cond
 ((= type "PO") (foreach pt lst (HA:Point pt)))
 ((= type "L") (setq x 0) (repeat (1- (length lst)) (HA:Line (nth x lst) (nth (1+ x) lst)) (setq x (1+ x))))
 ((= type "P") (HA:Pline lst))
 ((= type "S") (HA:Spline lst)))
(princ))
;-----
; Arguments:
; fun: Hµm sè 3D.
; xrange: Kho¶ng gi¸ trÞ X. VD: '(-20 20)
; yrange: Kho¶ng gi¸ trÞ Y. VD: '(-20 20)
; res: §é ph©n gi¶i (®é mÞn). VD: 100
(defun G3D (fun xrange yrange res / ce stepx i j x y)
(setq cmd (getvar "cmdecho")) (setvar "cmdecho" 0)
(setq x (car xrange) stepx (/ (- (cadr xrange) x) (float res)) stepy (/ (- (cadr yrange) (car yrange)) (float res)) i 0 res (1+ res))
(command "_.3DMESH" res res)
(while (< i res)
 (setq j 0 y (car yrange))
 (while (< j res)
  (command (list x y (apply fun (list x y))))
  (setq j (1+ j) y (+ y stepy)))
 (setq i (1+ i) x (+ x stepx)))
(setvar "cmdecho" cmd))
;----- C¸c hµm vÏ c¬ b¶n.
(defun HA:Point (pt) (entmake (list (cons 0 "POINT") (cons 10 pt))))
(defun HA:Line (p1 p2) (entmake (list (cons 0 "LINE") (cons 10 p1) (cons 11 p2))))
(defun HA:Pline (lst) (entmake (append (list (cons 0 "LWPOLYLINE") (cons 100 "AcDbEntity") (cons 100 "AcDbPolyline") (cons 90 (length lst))) (mapcar (function (lambda (p) (cons 10 p))) lst))))
(defun HA:Spline (lst) (entmake (append (list '(0 . "SPLINE") '(100 . "AcDbEntity") '(100 . "AcDbSpline") (cons 71 3) (cons 74 (length lst))) (mapcar (function (lambda (p) (cons 11 p))) lst))))
;----- Thay tÊt c¶ old bëi new trong string, EX: (LM:StringSubst "lisp" "be" "To be or not to be") => "To lisp or not to lisp"
(defun LM:StringSubst ( new old string / l i ) (setq l (strlen new) i 0) (while (setq i (vl-string-search old string i)) (setq string (vl-string-subst new old string i) i (+ i l))) string)
;----- Thay tªn hµm ®Ó kh«ng bÞ trïng ký tù "X".
(defun HA:Thay(hamso / i)
(setq TT1 EXPT TT2 EXP TT3 FIX TT4 MAX lst_old (list "EXPT" "EXP" "FIX" "MAX") lst_new (list "TT1" "TT2" "TT3" "TT4") i 0)
(repeat 4
 (setq hamso (LM:StringSubst (nth i lst_new) (nth i lst_old) hamso))
 (setq i (1+ i)))
hamso)
;-----
;http://xaydungit.vn/diendan/showthread.php?7923-Gi%E1%BA%A3i-ph%C3%A1p-t%C3%ADnh-to%C3%A1n-string&p=14881160#post14881160. by Irneb. Thanks
(defun lg (d) (* 0.4342944819032518276511289189166 (log d)))
(defun sqr (d) (* d d))
(defun asin (d) (atan d (sqrt (- 1 (* d d)))))
(defun acos (d) (atan (sqrt (- 1 (* d d))) d))
(defun tan (d) (/ (sin d) (cos d)))
(defun ctan (d) (/ (cos d) (sin d)))
(defun sin2 (d) (sin (* d (/ pi 180))))
(defun cos2 (d) (cos (* d (/ pi 180))))
(defun tan2 (d / r) (setq r (* d (/ pi 180))) (/ (sin r) (cos r)))
(setq ^ expt ln log)
;-----    
(defun split-list  (lst n / l1 l2 i)
(setq i (length lst))
(foreach item (reverse lst)
 (setq i (1- i))
 (if (< i n)
  (setq l1 (cons item l1))
  (setq l2 (cons item l2))))
(cons l1 l2))
;-----    
(defun infix->prefix (infix-expression / str->lst inf->pre-help placeholders operators functions placeholder-revert)
(setq infix-expression (strcat (LM:StringSubst "))" ")" (LM:StringSubst "((" "(" infix-expression)) "+0")) ;HA them dong nay!!!!!!!!!!!!!!!!
(setq operators '(+ - * / ^)
  		functions '(ABS ACOS ASIN ATAN COS COS2 CTAN DEC EXP FIX FLOAT INC LG LN LOG
                             LOGAND LOGIOR MAX MIN REM SIN SIN2 SQR SQRT TAN TAN2))
;----- Derived from format1 by HyflyingBird
(defun str->lst  (str / char funs lastfun lst tmp lastchar)
(vl-load-com)
(setq lastfun "(")
(setq funs '("+" "-" "*" "/" "^" "%" "(" ")" " "))
(setq tmp "")
(while (/= str "")
 (setq char (substr str 1 1)
           str  (substr str 2))
 (if (and (member char funs)
               (not (and lastfun (/= lastfun ")") (= char "-")))
               (not (and lastchar (or (= char "-") (= char "+")))))
  (progn
   (setq lst (vl-list* char tmp lst)
             tmp ""
             lastfun char
             lastchar nil)
   (princ))
  (progn
   (setq tmp (strcat tmp char))
   (setq lastfun nil)
   (setq lastchar (= char "e"))
   (princ))))
(read (vl-princ-to-string (vl-remove "" (vl-remove " " (reverse (cons tmp lst)))))))
;----- Helper to recursively prefix the infix list
(defun inf->pre-help  (infix-expression / split)
(cond
 ((not (listp infix-expression)) infix-expression)
 ((= (length infix-expression) 1) (inf->pre-help (car infix-expression)))
 ((member (car infix-expression) functions)
  (setq split (list (read (strcat "$placeholder" (itoa (length placeholders)))) (car infix-expression) (cadr infix-expression)) placeholders (cons split placeholders))
  (inf->pre-help (cons (car split) (cddr infix-expression))))
 ((> (setq split (car (vl-remove-if 'null (mapcar (function (lambda (item) (vl-position item infix-expression))) operators)))) 0)
  (list (cadr (setq split (split-list infix-expression split))) (inf->pre-help (car split)) (inf->pre-help (cddr split))))
 (t (mapcar (function inf->pre-help) infix-expression))))
;----- Helper to add back the placeholders
(defun placeholder-revert  (prefix-expression / place)
(mapcar (function (lambda (item)
   (cond
((listp item) (placeholder-revert item))
((setq place (assoc item placeholders))
     (if (listp (caddr place))
  	(cons (cadr place) (placeholder-revert (mapcar 'inf->pre-help (caddr place))))
  	(cdr place)))
(t item))))
 prefix-expression))
(placeholder-revert
 (inf->pre-help
  (if (listp infix-expression)
   infix-expression
   (str->lst infix-expression)))))
;----- EX:
;_$ (infix->prefix "sin(5)^cos(1.23)/-3+max((A/5) 2)*(abs 8+7*min(4 (9+5))-atan(5))")
;(+ (/ (^ (SIN 5) (COS 1.23)) -3) (* (MAX (/ A 5) 2) (+ (ABS 8) (- (* 7 (MIN 4 (+ 9 5))) (ATAN 5)))))
;_$ (infix->prefix '(sin (5) ^ cos (1.23) / -3 + max ((A / 5) 2) * (abs 8 + 7 * min (4 (9 + 5)) - atan (5))))
;(+ (/ (^ (SIN 5) (COS 1.23)) -3) (* (MAX (/ A 5) 2) (+ (ABS 8) (- (* 7 (MIN 4 (+ 9 5))) (ATAN 5)))))
;Dung (eval...) => ket qua.

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Đường cong bậc 2 trong cad. Xem đúng bài viết #220298 để đọc hướng dẫn và trao đổi thêm.

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