Change Layer 變更圖層的小程式
此程式先讓 USER 選取要變更圖層的多個物件 . 而要變更的最終圖層 , 則以簡單的選取一個物件來指定所要圖層 . 所有先前選取的物件 , 都將被變更圖層到指定的圖層 . 要測試此程式 , 需先在不同圖層畫上多個圖元 .
(defun c:chlayer (/ a1 a2 n index b1 b2 d1 d2 b3)
(graphscr)
(prompt "nselect entities to be changed: ")
(setq a1 (ssget))
(prompt "npoint to entity on target layer: ")
(setq a2 (entsel))
(setq n (sslength a1))
(setq index 0)
(setq b2 (entget (car a2)))
(setq d2 (assoc 8 b2))
(repeat n
(setq b1 (entget (ssname a1 index)))
(setq d1 (assoc 8 b1))
(setq b3 (subst d2 d1 b1))
(entmod b3)
(setq index (+ index 1))
)
(princ)
)
現在 , 一行一行來檢查 , 說明 :
(defun c:chlayer (/ a1 a2 n index b1 b2 d1 d2 b3)
Defines the function with all local variables. 定義功能函數 , 並指定區域變數
(graphscr)
Changes to graphics screen. 切換到繪圖螢幕
(prompt "nSelect entities to be changed: ")
This is a prompt statement. 提示 : 選取要變更圖層的圖元
(setq a1 (ssget))
Allows you to select the objects to be changed. 由 USER 選取欲變更圖層的物件
The selection set is assigned to variable a1 . 將此選擇集指派給變數 a1
(prompt "npoint to entity on target layer: ")
This is a prompt statement. 提示 : 選取目標圖層的單一圖元
(setq a2 (entsel))
This is a special type of selection statement that allows you to select only one entity.
(setq n (sslength a1))
Measures the number of entities in the selection set in variable a1 .
將選集 a1 中的圖元個數值 , 指派給變數 n.
(setq index 0)
Sets the variable called index to 0 . 設定計數的起始變數 index 的值為 0.
(setq b2 (entget (car a2)))
This statement gets the entity list from a2 . Thus, a2 is assigned the entity list.
(setq d2 (assoc 8 b2))
This looks for the code 8 ( 圖層代碼 ) in entity list a2 , then assigns the sublist to d2 .( 取得目標圖層的圖層名稱 , 然後指定為替代圖層的變數 d2)
(repeat n
This begins the loop that pages through the selection set. 開始執行變更圖層的廻圈
(setq b1 (entget (ssname a1 index)))
This gets the entity list and assigns it to b1 . 依序取出各圖元的串列資料
(setq d1 (assoc 8 b1))
Gets the sublist code 8 (the layer). 取出圖層的串列資料
(setq b3 (subst d2 d1 b1))
Substitutes the new d2 layer for the old d1 layer in the entity list a1 , and assigns it to the new entity list b3 . 新圖層串列替代舊圖層串列資料 , 然後指派給新的圖元串列
(entmod b3)
Updates the new entity list in the database. 更新資料庫中的圖元資訊
(setq index (+ index 1))
Increases the index variable by 1 , making it ready for the next loop. 計數變數加 1, 繼續處理下一個圖元 , 直到和欲變更圖層圖元個數相同為止
The first ) closes the repeat loop. (princ) exits quitly. The second ) closes the function.
第一個右括號是結束廻圈的 , 第二個右括號則是結束功能函數 .