Auto Lisp 快速上手 - 006

  • Share this:

Entities from a list

當你寫 (setq a (getpoint)), 意思是 指定點座標的 X 和 Y 座標值給變數 a . 這時的變數是一個串列 ( list ) , 看起來會類似 (5 10) . 如果要查看變數 a 的串列 list, AutoLISP 提供一個很方便的方式 , 可以直接在指令列查得 .

!a ( 在變數 a 的前面加上 ! , 就會顯示變數 a 的變數值或內容 .

(car) -> X COORDINATE (list element)

這是擷取 list 中一部份元素的常用指令 , (car) 是取得 list 中的第一個元素 , 常用來取得點座標中的 X 座標值 . 如果變數 a 的值為 list : (5 10)

則 (setq b (car a)) 將指定 variable b 的值為變數 a 的第一個元素 5 .

(cdr) SECOND AND REMAINING

這是第二個重要的擷取串列元素的指令 . (cdr) 是取得串列中第二個及其後的所有元素所組成的串列 . 也就是將第一個元素挑除後 , 剩下的元素再重組起來的串列 . If variable a is a list of

(2 5 7 9 11)

Then (setq b (cdr a)) would assign to variable b the second and remaining elements of the list in variable a

(5 7 9 11) .

(cadr) Y COORDINATE (2nd element)

這是取得串列中第二個元素的指令 , 常用在取得點座標值中的 Y 座標值 . Assuming a still has the list (5 10) ,

Then (setq b (cadr a)) would produce 10 .

(caddr) Z COORDINATE (3rd element)

此指令擷取 list 中的第三個元素 , 一般常用在點座標中取得 Z 座標值 . Assuming a has a list (3 7 5)

Then (setq c (caddr a)) would produce 5 .

下列程式是由 2 點來畫矩形的範例 :

(defun c:retan (/ pl p2 p3 p4)

(setq pl (getpoint "nfirst corner of rectangle: "))

(setq p3 (getcorner "nsecond corner of rectangle: "))

(setq p2 (list (car pl)(cadr p3)))

(setq p4 (list (car p3)(cadr pl)))

(command "line" pl p2 p3 p4 "c")

(princ)

)

DTR converts degrees to radians.( 度度量換算成弳度量 )

(defun dtr (a)

(* pi (/ a 180)) )

RTD converts radians to degrees. ( 弳度量換算成度度量 )

(defun rtd (a) (/ (* a 180) pi) )

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Things to strings 字串常用的指令

strcase (string case)

Changes a string of text from lower case to upper case 小寫轉大寫 , leaving upper case characters as they are. eg:

(strcase "Hello") returns "HELLO"

(strcase a) returns the alphabetic characters in variable a from lower case to upper case. 將變數 a 裡字串的字母由小寫轉大寫 .

strcat (string cat) 結合多個字串成為一個字串

Returns two or more separate strings as one. eg:

(strcat "H" "ello") returns "Hello"

(strcat a b) returns two strings in variable a & b as one.

strlen (string length) 求字串的長度 ( 單一字母的總數量 )

Returns the length, of the characters of a string. eg:

(strlen "hello") returns 5 .

(strlen a) returns the length of a string in variable a .

substr (substitute string) 擷取部分的字串

Returns a part of a string, from a specified position, ending either at the end or another specified position. eg:

(substr "Hello 2) returns "ello" .

(substr "Hello 2 1) returns "e" .

(substr "Hello" 3 2) returns "ll" .

######################################################################################################################################################3

List Manipulation 串列的運用

The apostrophe ' 在 AutoLISP 當中提供特別的功能 , for example if a group of items is preceded by an apostrophe ' it is treated as a list. eg:

'(20 10 5) 視同 list.

Angle 取得角度值

由兩點回應角度值 , 單位為弳度 . 要在 AutoCAD 中使用這個值 , 必須先轉換回十進位的度度量值 . eg:

(setq a (angle pntl pnt2)) sets the angle between pntl and pnt2 to the variable a. To use a :

(command "text" pntl "40" a t) text 指令設插入點為 pnt1, 字高為 40, 旋轉角度在此指定為變數 a , 文字內容為 variable t . 但是 , a 值在此是不正確的角度值 . 因為在 AutoLisp 的值都是弳度量 , 而在 AutoLisp 中的角度是以度度量為準 .

Append 附加串列

Takes any number of specified lists and joins them together as one list. eg:

(append '(10 20) '(30 40)) returns the list: (10 20 30 40) .

(append a b) returns the list in variable a and the list in variable b as one.

Distance 距離值

Measures the distance from two known points. eg:

(setq distl (distance pntl pnt2)) 取得 pntl and pnt2 兩點間的距離 , 並指定為變數 dist1 的值 .

Length

Returns the number of elements in a list. ( 回應串列中所含的元素數量 ) 例 :

(length '(a b c d)) returns 4 .

Member

Looks for a duplicate and returns that and the rest of the list

找尋串列中的某個元素 , 如果找到 , 則回應此元素及其後所有元素所組成的串列 eg:

(member 'c '(a b c d e)) returns (C D E) .

nth

Returns the nth element in a list, where n is the number of the element to return 取得串列中第 n 個元素 . (Zero is the first element 零是第一個元素 , 意思是 : 在 Auto Lisp 裡 , 第幾個的算法是由零起算 , 即 0, 1, 2, 3, 4, 5, ... ). eg:

(nth 3 '(a b c d)) returns d .

Assoc (associative) 串列的組合

經常與 (subst) 指令一起使用 , 此指令首先搜尋特定的元素 , 然後將其指定給一個變數 .

Lets assume variable b is the list ((10 5.5 2.7)(40 5)) and you looking for a code 40 . You want to pull out the entire element and assign it to a variable c . eg:

(setq c (assoc 40 b))

先搜尋 list b 裡面有 40 這個元素的串列 , 然後指定給 variable c . 所以 c 是一個串列 , 其值為 (40 5) .

Subst ( substitute ) 代替元素

Allows you to substitute one aspect for another. ( 替換新舊串列後的串列 ) When substituting ALWAYS substitute the new item for the old in the list. 如上例 , 現在用 20 來代替變數 c 裡面的 5 .

(setq bl (subst '(40 20) c b))

則 bl 是新的串列 .

'(40 20) 是取代串列 b 裡面的舊元素 - c 串列 (40 5) 的新元素 .

If you want to use a variable which represents the value.

(setq bl (subst '(40 h) c b)) , It looks like it should work, but it does not.

The new element will look like this: (40 h) .

(subst) 無法解譯變數 . You need to construct a new variable containing the entire list element, then use new variable in the (subst) command.

Cons (Construct) 將新元素結合到串列

Constructs a new list with the new element placed at the begining. 將新元素結合到串列

假設 variable c 為串列 : (40 5) .

variable h 為實數 20.0

則 :

(setq d (cons (car c) h)) 記得 (car c) 得到 40 . 所以 , (car c) 是新串列的第一個元素 , 後面跟隨的是變數 h 的值 20 . 因此 , 這個指令產生一個新的串列 d (40 20.0) .

現在 , 我們可以用新的串列來取代舊的串列 :

(setq bl (subst d c b)) 在 variable d 的新元素替代舊元素中的 variable c 值 . ( 在 variable b 的串列裡 ) 並指定新的串列為 variable bl .


Tags: