;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;	程序名稱：圖層管理
;;	作   者：winwin
;;	時   間：2000.08.15
;;context：
;;	1.layer_on( layer_name )	打開圖層
;;	2.layer_off( layer_name )	關閉圖層
;;	3.createlayer( layer_name )	create層
;;特點：
;;	1.不用command命令，直接操作圖形數據庫，快速高效
;;	2.可以在其它命令執行的過程中使用
;;	3.可以使用通配符
;;
;;	歡迎使用!!!!
;;                                                 統贏資訊
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun T_layer( layer_name / tbdata lname echo c70 q70 c62 q62 have )
	(setq have 0)
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 )
	(setq tbdata ( tblnext "layer" T ) )
	(while  tbdata
		(setq lname (cdr (assoc 2 tbdata) ) )
		(if ( or (= lname (strcase layer_name) )
				( = lname ( strcase (strcat layer_name "_O" ) ) )
				( = lname ( strcase (strcat layer_name "_W" ) ) )
				)		
                           (progn
				(setq data (entget (tblobjname "layer" lname)))
				(setq c70 (logand (cdr (assoc 70 data)) 0))
				(setq c62 (abs (cdr (assoc 62 data))))
				(setq q70 (cons 70 c70))
				(setq q62 (cons 62 c62))
                		(setq data (subst q70 (assoc 70 data) data ) )
                		(setq data (subst q62 (assoc 62 data) data ))
				(entmod data)
				(setq have 1 )
			)
		)
		(setq tbdata ( tblnext "layer"))
	)
	(if (= have 0) (createlayer layer_name))
        (layer_on (strcat layer_name "_*_*"))   ;陔崝;;;;;;;;;;;;;;;;;
        (setvar "cmdecho" 0)
	(princ)
)

(defun add_layer( layer_name / tbdata lname echo c70 q70 c62 q62 have )
	(setq have 0)
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 )
	(setq tbdata ( tblnext "layer" T ) )
	(while  tbdata
		(setq lname (cdr (assoc 2 tbdata) ) )
		(if ( or (= lname (strcase layer_name) )
				( = lname ( strcase (strcat layer_name "" ) ) )
				)		
                           (progn
				(setq data (entget (tblobjname "layer" lname)))
				(setq c70 (logand (cdr (assoc 70 data)) 0))
				(setq c62 (abs (cdr (assoc 62 data))))
				(setq q70 (cons 70 c70))
				(setq q62 (cons 62 c62))
                		(setq data (subst q70 (assoc 70 data) data ) )
                		(setq data (subst q62 (assoc 62 data) data ))
				(entmod data)
				(setq have 1 )
			)
		)
		(setq tbdata ( tblnext "layer"))
	)
	(if (= have 0) (createlayer layer_name))
        
        (setvar "cmdecho" 0)
	(princ)
)

(defun lay_on( layer_name / tbdata lname echo c70 q70 c62 q62 have )
	(setq have 0)
	(setq tbdata ( tblnext "layer" T ) )
	(while  tbdata
		(setq lname (cdr (assoc 2 tbdata) ) )
		(if ( or (= lname (strcase layer_name) )
				( = lname ( strcase (strcat layer_name "_O" ) ) )
				( = lname ( strcase (strcat layer_name "_W" ) ) )
				( = lname ( strcase (strcat layer_name "_U" ) ) )
				( = lname ( strcase (strcat layer_name "_M" ) ) )
				( = lname ( strcase (strcat layer_name "_D" ) ) )
				( = lname "CENTER" )
			)
			(progn
				(setq data (entget (tblobjname "layer" lname)))
			        (setq c70 (logand (cdr (assoc 70 data)) 122))
				(setq c62 (abs (cdr (assoc 62 data))))
				(setq q70 (cons 70 c70))
				(setq q62 (cons 62 c62))
                                (setq data (subst q70 (assoc 70 data) data ) )
                                (setq data (subst q62 (assoc 62 data) data ))
				(entmod data)
				(if ( /= lname "CENTER") (setq have 1 ) )
			)
		)
		                (setq tbdata ( tblnext "layer"))
	)
	(if (= have 0) (createlayer layer_name))
	( if (and (= ( find layer_name "*" ) 0) (= ( find layer_name "?" ) 0) )	(setvar "Clayer" layer_name  ))
(layer_on (strcat layer_name "_*_*"))   ;陔崝;;;;;;;;;;;;;;;;;
  (setvar "cmdecho" 0)
        (princ)
)

(defun layer_on( layer_name / tbdata lname echo data c70 c62 q70 q62 have )
	(setq have 0)
	(setq tbdata ( tblnext "layer" T ) )
	(while  tbdata
		(setq lname (cdr (assoc 2 tbdata) ) )
		(if ( wcmatch lname ( strcase layer_name ) )
			(progn
				(setq data (entget (tblobjname "layer" lname)))
				(setq c70 (logand (cdr (assoc 70 data)) 122))
				(setq c62 (abs (cdr (assoc 62 data))))
				(setq q70 (cons 70 c70))
				(setq q62 (cons 62 c62))
				(setq data (subst q70 (assoc 70 data) data ) )
				(setq data (subst q62 (assoc 62 data) data ))
				(entmod data)
				(setq have 1 )
			)
		)
		(setq tbdata ( tblnext "layer"))
                (setvar "Sortents" 112)
	)
	(if (= have 0) (createlayer layer_name))
	( if (and (= ( find layer_name "*" ) 0) (= ( find layer_name "?" ) 0) )	(setvar "Clayer" layer_name ))
	(princ)
)

(defun layer_off( layer_name / tbdata lname echo data c70 c62 q70 q62 )
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 )
	(setq tbdata ( tblnext "layer" T ) )
	(while  tbdata
		(setq lname (cdr (assoc 2 tbdata) ) )
		(if ( wcmatch lname ( strcase layer_name ) )
			(progn
				(setq data (entget (tblobjname "layer" lname)))
				(setq c70 (logior (cdr (assoc 70 data)) 4))
				(setq c62 (- (abs (cdr (assoc 62 data)))))
				(setq q70 (cons 70 c70))
				(setq q62 (cons 62 c62))
				(setq data (subst q70 (assoc 70 data) data ) )
				(setq data (subst q62 (assoc 62 data) data ))
				(entmod data)
				(setq have 1 )
			)
		)
		(setq tbdata ( tblnext "layer"))
	)
	(setvar "CMDECHO" echo )
	(princ)
)

(defun find( str1 str2 / pos len found ret len1 )
	(setq pos 1 found 0)
	(setq len (strlen str1) )
	(setq len1 (strlen str2) )
	(while ( <= pos len )
		(if (= str2 (substr str1 pos len1 ) )
			(progn
				( setq found pos )
				( setq pos len )
			)
		)
		( setq pos (1+ pos ) )
	)
	( setq ret found )
)

(defun createlayer( layer_name / data c70 c62 q70 q62 )
	( if (and (= ( find layer_name "*" ) 0) (= ( find layer_name "?" ) 0) )
		(progn
			(setq data (entget (tblobjname "layer" "0")))
			(setq c70 (logand (cdr (assoc 70 data)) 122))
			(setq c62 1)
			(setq q70 (cons 70 c70))
			(setq q62 (cons 62 c62))
			(setq data (subst q70 (assoc 70 data) data ) )
			(setq data (subst q62 (assoc 62 data) data ))
			(setq q70 (cons 2 layer_name))
			(setq data (subst q70 (assoc 2 data) data ) )
			(entmake data)
		)
	)
	(princ)
)

(defun s_plate( layer_name / tbdata lname bHave echo data c62 q62 c70 q70 )
	(layer_off "*")
        (layer_on "center")
	(if (= ( find layer_name "*" ) 0)
        (layer_on (strcat layer_name "_*")))
        (layer_off (strcat layer_name "_p*"))
        (layer_on (strcat layer_name "_*_*"))   ;陔崝;;;;;;;;;;;;;;;;;
	(layer_on layer_name)
        (setvar "clayer" (strcat layer_name "_OM"))
	(princ)
)

(defun s_number( layer_name / tbdata lname bHave echo data c62 q62 c70 q70 )
	(layer_off "*")
        (layer_on "center")
	(if (= ( find layer_name "*" ) 0)
        (layer_on (strcat layer_name "")))
        (layer_off (strcat layer_name ""))
	(layer_on layer_name)
        (setvar "clayer" (strcat layer_name ""))
	(princ)
)

(defun sin_plate( layer_name / tbdata lname bHave echo data c62 q62 c70 q70 )
	(layer_off "*")
        (if (= ( find layer_name "" ) 0)
        (layer_on (strcat layer_name "")))
        (layer_on layer_name)
        (setvar "clayer" (strcat layer_name ""))
	(princ)
)

(defun fre_plate( layer_name / tbdata lname echo data c70 q70 )
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 )
	(setq tbdata ( tblnext "layer" T ) )
	(while  tbdata
		(setq lname (cdr (assoc 2 tbdata) ) )
		(if ( or (= lname (strcase layer_name) )
				( = lname ( strcase (strcat layer_name "_P" ) ) )
				( = lname ( strcase (strcat layer_name "_O" ) ) )
				( = lname ( strcase (strcat layer_name "_W" ) ) )
				( = lname ( strcase (strcat layer_name "_PM" ) ) )
				( = lname ( strcase (strcat layer_name "_OM" ) ) )
				( = lname ( strcase (strcat layer_name "_WM" ) ) )
			)
			(progn
				(setq data (entget (tblobjname "layer" lname)))

				(setq c62 (- (abs (cdr (assoc 62 data)))))
				(setq q62 (cons 62 c62))
				(setq data (subst q62 (assoc 62 data) data ))

				(setq c70 (logior (cdr (assoc 70 data)) 4))
				(setq q70 (cons 70 c70))
                (setq data (subst q70 (assoc 70 data) data ))

				(entmod data)
			)
		)
		(setq tbdata ( tblnext "layer"))
	)
	
	(setvar "CMDECHO" echo )
	(princ)
)

(defun pl_off( layer_name / cl tbdata lname echo data c62 q62 c70 q70 )
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 )
	(setq tbdata ( tblnext "layer" T ) )
	(while  tbdata
		(setq lname (cdr (assoc 2 tbdata) ) )
		(if ( or (= lname (strcase layer_name) )
				( = lname ( strcase (strcat layer_name "_P" ) ) )
				( = lname ( strcase (strcat layer_name "_O" ) ) )
				( = lname ( strcase (strcat layer_name "_W" ) ) )
				( = lname ( strcase (strcat layer_name "_PM" ) ) )
				( = lname ( strcase (strcat layer_name "_OM" ) ) )
				( = lname ( strcase (strcat layer_name "_P" ) ) )
				( = lname ( strcase (strcat layer_name "_d" ) ) )
				( = lname ( strcase (strcat layer_name "_m" ) ) )
				( = lname ( strcase (strcat layer_name "_u" ) ) )
			)
			(progn
				(setq data (entget (tblobjname "layer" lname)))

				(setq c70 (logior (cdr (assoc 70 data)) 4))
				(setq q70 (cons 70 c70))
                                (setq data (subst q70 (assoc 70 data) data ))

				(setq c62 (- (abs (cdr (assoc 62 data)))))
				(setq q62 (cons 62 c62))
                                (setq data (subst q62 (assoc 62 data) data ))

				(entmod data)
			)
		)
		(setq tbdata ( tblnext "layer"))
	)
	
	(setvar "CMDECHO" echo )
	(princ)
)

(defun l_ctrl( layer_name / tbdata lname echo data c62 q62 pos mainlayer )
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 ) 
        
        (if (= (strcase layer_name) "SIDE")
		(progn
			(layer_off "*")
				
			(layer_on "SIDE")	
		        (setvar "CMDECHO" echo )
;			(exit)
		)
;	)


      (if (= (strcase layer_name) "PURCHASE")
		(progn
			(layer_off "*")
				
			(layer_on "PURCHASE")
                        (layer_on "PURCHASE_OM")
                        (setvar "clayer" "PURCHASE")
	
		        (setvar "CMDECHO" echo )
;			(exit)
		)
;	)

        (if (= (strcase layer_name) "MATER")
		(progn
			(layer_off "*")
				
			(layer_on "MATER")	
			
		
			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)

        (if (= (strcase layer_name) "PUNCH")
		(progn
			(layer_off "*")
				
			(layer_on "PUNCH")	
			(layer_on "PUNCH_OM")
                        (setvar "clayer" "PUNCH")


			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)

        (if (= (strcase layer_name) "PART")
		(progn
			(layer_off "*")
				
			(layer_on "PART")
                        (layer_on "PART_M")
                        (setvar "clayer" "PART")	
			    
		
			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)

	(if (= (strcase layer_name) "U_SET1")
		(progn
			(layer_off "*")
			(layer_on "COVER*")	(layer_on "UP*")	(layer_on "UB*")
			(layer_on "PH_*")	(layer_on "DIE2*")      (layer_on "UB0*")
			(layer_on "PPS*")	(layer_on "PS")	        (layer_on "PS_*")
			(layer_on "mater")	(layer_on "0")		(layer_on "PRESS_U")
			(layer_on "PRESS_M")	(layer_on "U1*")	(layer_on "U2*")
			(layer_on "PH")
			(layer_off "*_?M")
                        
			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)

	(if (= (strcase layer_name) "U_SET2")
		(progn
			(layer_off "*")
			(layer_on "UP*")	(layer_on "UB*")	(layer_on "DIE2*")
			(layer_on "PH_*")	(layer_on "COVER*")	(layer_on "PH")
			(layer_on "U1*")	(layer_on "U2*")        (layer_on "UB0*")
			(layer_off "*_?M")
                        
		
			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)

	(if (= (strcase layer_name) "D_SET1")
		(progn
			(layer_off "*")
			(layer_on "GUID*")	(layer_on "LP*")	(layer_on "LB*")
			(layer_on "PRESS_D")	(layer_on "DIE_*")	(layer_on "PH2*")
			(layer_on "mater")	(layer_on "1")
			(layer_on "B1*")	(layer_on "B2*")	(layer_on "PS2*")
			(layer_on "DIE")
			(layer_off "*_?M")
                             

			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)
      
	(if (= (strcase layer_name) "D_SET2")
		(progn
			(layer_off "*")
			(layer_on "LP*")	(layer_on "LB*")
			(layer_on "DIE_*")	(layer_on "PH2*")	(layer_on "DIE")
			(layer_on "B1*")	(layer_on "B2*")
			(layer_off "*_?M")
                               

			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)
      
	(if (and (> (find layer_name "_") 0 ) (= (find layer_name "*") 0 ) )
		(progn
			(setq pos (find layer_name "_") )
			(setq mainlayer (substr layer_name 1 (- pos 1) ) )
			(layer_off "*")
			(layer_on mainlayer)
			(layer_on (strcat layer_name "M"))
		  (layer_on (strcat layer_name "*_*"))   ;陔崝;;;;;;;;;;;;;;;;;
			(layer_on layer_name)
                        
			(setvar "CMDECHO" echo )
;			(exit)
		)
;	)

	(if ( = (strcase layer_name) "8TH")
		(progn
			(layer_off "*");
			(layer_on "1ST");
			(layer_on "2ND");
			(layer_on "3RD");
			(layer_on "4TH");
			(layer_on "5TH");
			(layer_on "6TH");
			(layer_on "7TH");
			(layer_on "8TH");
                           
;			(exit)
		)
;	)
	(if ( = (strcase layer_name) "7TH")
		(progn
			(layer_off "*");
			(layer_on "1ST");
			(layer_on "2ND");
			(layer_on "3RD");
			(layer_on "4TH");
			(layer_on "5TH");
			(layer_on "6TH");
			(layer_on "7TH");
                        
;			(exit)
		)
;	)
	(if ( = (strcase layer_name) "6TH")
		(progn
			(layer_off "*");
			(layer_on "1ST");
			(layer_on "2ND");
			(layer_on "3RD");
			(layer_on "4TH");
			(layer_on "5TH");
			(layer_on "6TH");
                           
;			(exit)
		)
;	)
	(if ( = (strcase layer_name) "5TH")
		(progn
			(layer_off "*");
			(layer_on "1ST");
			(layer_on "2ND");
			(layer_on "3RD");
			(layer_on "4TH");
			(layer_on "5TH");
                         
;			(exit)
		)
;	)
	(if ( = (strcase layer_name) "4TH")
		(progn
			(layer_off "*");
			(layer_on "1ST");
			(layer_on "2ND");
			(layer_on "3RD");
			(layer_on "4TH");
                            
;			(exit)
		)
;	)
	(if ( = (strcase layer_name) "3RD")
		(progn
			(layer_off "*");
			(layer_on "1ST");
			(layer_on "2ND");
			(layer_on "3RD");
                        
;			(exit)
		)
;	)
	(if ( = (strcase layer_name) "2ND")
		(progn
			(layer_off "*");
			(layer_on "1ST");
			(layer_on "2ND");
                         
;			(exit)
		)
;	)
	(if ( = (strcase layer_name) "1ST")
		(progn
			(layer_off "*");
			(layer_on "1ST");
                          
;			(exit)
		)
;	)

	(if ( = (strcase layer_name) "PRESS" )
		(progn
			(layer_off "*" )
			(layer_on "PRESS_U" )
			(layer_on "PRESS_M" )
			(layer_on "PRESS_D" )
			(layer_on "PRESS" )
                        
;			(exit)
		)
;	)


(if ( = (strcase layer_name) "HANDLE" )
		(progn
			(layer_off "*" )
			(layer_on "HANDLE_O" )
			(layer_on "HANDLE" )
                        
;			(exit)
		)
		(progn
			(layer_off "*")
			(layer_on "CENTER")
			(layer_on (strcat layer_name "_O") )
			(layer_on (strcat layer_name "_W") )
			(layer_on layer_name)
                       
		)
	))))))))))))))))))))
(layer_on (strcat layer_name "_*_*"))   ;陔崝;;;;;;;;;;;;;;;;;
	(setvar "CMDECHO" echo )
	(princ)
)

(defun l_ctrll( layer_name / tbdata lname echo data c62 q62 pos mainlayer )
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 )
        (if (and (> (find layer_name "_") 0 ) (= (find layer_name "*") 0 ) )
		(progn
			(setq pos (find layer_name "O") )
			(setq mainlayer (substr layer_name 1 (- pos 2) ) )
			(layer_off "*")
		        (layer_on mainlayer)
			(layer_on layer_name)
                        
			(setvar "CMDECHO" echo )
;			(exit)
		)
	  	(progn
			(layer_off "*")
			(layer_on "CENTER")
			(layer_on (strcat layer_name "_O") )
			(layer_on (strcat layer_name "_W") )
			(layer_on layer_name)
                       
		))
	     (setvar "CMDECHO" echo )
	     (princ)
             )

(defun l_ctrlx( layer_name / tbdata lname echo data c62 q62 pos mainlayer )
	(setq echo (getvar "CMDECHO") )
	(setvar "CMDECHO" 0 )
        (if (and (> (find layer_name "_") 0 ) (= (find layer_name "*") 0 ) )
		(progn
			(setq pos (find layer_name "_") )
			(setq mainlayer (substr layer_name 1 (- pos 1) ) )
			(layer_off "*")
                        (layer_on (strcat layer_name "_O") )
                        (layer_on layer_name)
			(setvar "CMDECHO" echo )
;			(exit)
		))
	     (setvar "CMDECHO" echo )
	     (princ)
             )

(defun c:0() (lay_on "0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:1() (lay_on "1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:2() (lay_on "2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:3() (lay_on "3")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:4() (lay_on "4")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:5() (lay_on "5")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:6() (lay_on "6")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:7() (lay_on "7")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:8() (lay_on "8")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:UP() (lay_on "UP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:UB() (lay_on "UB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:UB2() (lay_on "UB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PH() (lay_on "PH")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PH2() (lay_on "PH2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PPS() (lay_on "PPS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PS() (lay_on "PS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PS2() (lay_on "PS2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:DIE() (lay_on "DIE")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:UB0() (lay_on "UB0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:DIE2() (lay_on "DIE2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:LB() (lay_on "LB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:LB2() (lay_on "LB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:LP() (lay_on "LP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))

(defun c:UPD() (lay_on "UP")(layer_on "UP_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:UBD() (lay_on "UB")(layer_on "UB_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:UB0D() (lay_on "UB0")(layer_on "UB0_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:UB2D() (lay_on "UB2")(layer_on "UB2_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PHD() (lay_on "PH")(layer_on "PH_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PH2D() (lay_on "PH2")(layer_on "PH2_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PPSD() (lay_on "PPS")(layer_on "PPS_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PSD() (lay_on "PS")(layer_on "PS_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:PS2D() (lay_on "PS2")(layer_on "PS2_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:DIED() (lay_on "DIE")(layer_on "DIE_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:DIE2D() (lay_on "DIE2")(layer_on "DIE2_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:LBD() (lay_on "LB")(layer_on "LB_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:LB2D() (lay_on "LB2")(layer_on "LB2_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:LPD() (lay_on "LP")(layer_on "LP_OM")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))

(defun c:T0() (T_layer "0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T1() (T_layer "1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T2() (T_layer "2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T3() (T_layer "3")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T4() (T_layer "4")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T5() (T_layer "5")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T6() (T_layer "6")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T7() (T_layer "7")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:T8() (T_layer "8")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))

(defun c:TU1() (T_layer "U1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TU2() (T_layer "U2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TCV() (T_layer "COVER")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TUP() (T_layer "UP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TUB() (T_layer "UB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TUB0() (T_layer "UB0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TUB2() (T_layer "UB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TPH() (T_layer "PH")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TPH2() (T_layer "PH2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TPPS() (T_layer "PPS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TPS() (T_layer "PS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TPS2() (T_layer "PS2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TDIE() (T_layer "DIE")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TDIE2() (T_layer "DIE2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TLB() (T_layer "LB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TLB2() (T_layer "LB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TLP() (T_layer "LP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TB1() (T_layer "B1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TB2() (T_layer "B2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TPR() (T_layer "PURCHASE")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:TPU() (T_layer "PUNCH")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))

(defun c:APH() (add_layer "PH_P")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:APH2() (add_layer "PH2_P")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:APS() (add_layer "PS_P")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:APS2() (add_layer "PS2_p")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:ADIE() (add_layer "DIE_P")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:ADIE2() (add_layer "DIE2_P")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))

(defun c:PSP() (sin_plate "PS_P")(layer_on "PS_PM")(setvar "cmdecho" 0)(setvar "cmdecho" 1)(princ))
(defun c:UB0P() (sin_plate "UB0_P")(layer_on "UB0_PM")(setvar "cmdecho" 0)(setvar "cmdecho" 1)(princ))
(defun c:PS2P() (sin_plate "PS2_P")(layer_on "PS2_PM")(setvar "cmdecho" 0)(setvar "cmdecho" 1)(princ))
(defun c:PHP() (sin_plate "PH_P")(layer_on "PH_PM")(setvar "cmdecho" 0)(setvar "cmdecho" 1)(princ))
(defun c:PH2P() (sin_plate "PH2_P")(layer_on "PH2_PM")(setvar "cmdecho" 0)(setvar "cmdecho" 1)(princ))
(defun c:DIEP() (sin_plate "DIE_P")(layer_on "DIE_PM")(setvar "cmdecho" 0)(setvar "cmdecho" 1)(princ))
(defun c:DIE2P() (sin_plate "DIE2_P")(layer_on "DIE2_PM")(setvar "cmdecho" 0) (setvar "cmdecho" 1)(princ))

(defun c:O0() (s_number "0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O1() (s_number "1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O2() (s_number "2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O3() (s_number "3")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O4() (s_number "4")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O5() (s_number "5")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O6() (s_number "6")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O7() (s_number "7")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:O8() (s_number "8")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))

(defun c:OU1() (s_plate "U1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OU2() (s_plate "U2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OCV() (s_plate "COVER")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OUP() (s_plate "UP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OUB() (s_plate "UB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OUB0() (s_plate "UB0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OUB2() (s_plate "UB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OPH() (s_plate "PH")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OPH2() (s_plate "PH2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OPPS() (s_plate "PPS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OPS() (s_plate "PS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OPS2() (s_plate "PS2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:ODIE() (s_plate "DIE")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:ODIE2() (s_plate "DIE2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OLB() (s_plate "LB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OLB2() (s_plate "LB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OLP() (s_plate "LP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OB1() (s_plate "B1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OB2() (s_plate "B2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OPR() (s_plate "PURCHASE")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OM() (s_plate "MATER")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:OPU() (s_plate "PUNCH")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))

(defun c:F0() (fre_plate "0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F1() (fre_plate "1")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F2() (fre_plate "2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F3() (fre_plate "3")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F4() (fre_plate "4")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F5() (fre_plate "5")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F6() (fre_plate "6")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F7() (fre_plate "7")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:F8() (fre_plate "8")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FUP() (fre_plate "UP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FUB() (fre_plate "UB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FUB0() (fre_plate "UB0")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FUB2() (fre_plate "UB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FPH() (fre_plate "PH")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FPH2() (fre_plate "PH2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FPPS() (fre_plate "PPS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FPS() (fre_plate "PS")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FPS2() (fre_plate "PS2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FDIE() (fre_plate "DIE")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FDIE2() (fre_plate "DIE2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FLB() (fre_plate "LB")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FLB2() (fre_plate "LB2")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:FLP() (fre_plate "LP")(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ))
(defun c:AX() (l_ctrl "U_SET1" )(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ) )
(defun c:DX() (l_ctrl "D_SET1" )(setvar "cmdecho" 0)(command "-layer" "")(setvar "cmdecho" 1)(princ) )
(defun c:OO() (command "ucs" "o""\\" ) (princ) )


(defun ai_tiledvp_chk (new)
  (setq m:err *error* *error* *merrmsg*)

  (if (= (getvar "TILEMODE") 0)
    (progn
      (princ "\n** Command not allowed unless TILEMODE is set to 1 (On) **")
      (princ)
    )
    (progn
      (if new
        (menucmd "I=ACAD.IMAGE_VPORTI")
        (menucmd "I=IMAGE_VPORTI")
      )
      (menucmd "I=*")
    )
  )
  (setq *error* m:err m:err nil)
  (princ)
)

(defun ai_tiledvp (num ori / ai_tiles_g ai_tiles_cmde)
  (setq m:err *error* *error* *merrmsg*
        ai_tiles_cmde (getvar "CMDECHO")
        ai_tiles_g (getvar "GRIDMODE")
  )
  (ai_undo_push)
  (setvar "CMDECHO" 0)
  (setvar "GRIDMODE" 0)
  (cond ((= num 1)
         (command "_.VPORTS" "_SI")
         (setvar "GRIDMODE" ai_tiles_g)
        )
        ((< num 4)
         (command "_.VPORTS" "_SI")
         (command "_.VPORTS" num ori)
         (setvar "GRIDMODE" ai_tiles_g)
        )
        ((= ori nil)
         (command "_.VPORTS" "_SI")
         (command "_.VPORTS" num)
         (setvar "GRIDMODE" ai_tiles_g)
        )
        ((= ori "_L")
         (command "_.VPORTS" "_SI")
         (command "_.VPORTS" "2" "")
         (setvar "CVPORT" (car (cadr (vports))))
         (command "_.VPORTS" "2" "")
         (command "_.VPORTS" "_J" "" (car (cadr (vports))))
         (setvar "CVPORT" (car (cadr (vports))))
         (command "_.VPORTS" "3" "_H")
         (setvar "GRIDMODE" ai_tiles_g)
         (setvar "CVPORT" (car (cadddr (vports))))
         (setvar "GRIDMODE" ai_tiles_g)
         (setvar "CVPORT" (car (cadddr (vports))))
         (setvar "GRIDMODE" ai_tiles_g)
         (setvar "CVPORT" (car (cadddr (vports))))
         (setvar "GRIDMODE" ai_tiles_g)
        )
        (T
         (command "_.VPORTS" "_SI")
         (command "_.VPORTS" "2" "")
         (command "_.VPORTS" "2" "")
         (setvar "CVPORT" (car (caddr (vports))))
         (command "_.VPORTS" "_J" "" (car (caddr (vports))))
         (setvar "CVPORT" (car (cadr (vports))))
         (command "_.VPORTS" "3" "_H")
         (setvar "GRIDMODE" ai_tiles_g)
         (setvar "CVPORT" (car (cadddr (vports))))
         (setvar "GRIDMODE" ai_tiles_g)
         (setvar "CVPORT" (car (cadddr (vports))))
         (setvar "GRIDMODE" ai_tiles_g)
         (setvar "CVPORT" (car (cadddr (vports))))
         (setvar "GRIDMODE" ai_tiles_g)
        )
  )
  (ai_undo_pop)
  (setq *error* m:err m:err nil)
  (setvar "CMDECHO" ai_tiles_cmde)
  (princ)
)
 
(defun ai_popmenucfg (param)
  (set (read param) (getcfg (strcat "CfgData/Menu/" param)))
)

(defun ai_putmenucfg (param cfgval)
  (setcfg (strcat "CfgData/Menu/" param) cfgval)
)

(defun *merr* (msg)
  (ai_sysvar nil)
  (setq *error* m:err m:err nil)
  (princ)
)

(defun *merrmsg* (msg)
  (princ msg)
  (setq *error* m:err m:err nil)
  (princ)
)


(defun ai_showedge_alert ()
   (alert "Invisible edges will be shown after next Regeneration.")
   (princ)
)

(defun ai_hideedge_alert ()
   (alert "Invisible edges will be HIDDEN after next Regeneration.")
   (princ)
)

(defun ai_sysvar (vars / savevar pair varname varvalue varlist)

   (setq varlist nil)

   (defun savevar (varname varvalue / pair)
      (cond
         ((not varvalue) 
            (setq varlist 
               (cons 
                   (cons varname (getvar varname)) 
                    varlist
               )
            )
             )
         ((/= (getvar varname) varvalue)
             (setq varlist 
                (cons 
                   (cons varname (getvar varname)) 
                    varlist
                )
             )
             (setvar varname varvalue)
             )
                 (T nil)
          )
   )

   (cond
      ((not vars)
         (foreach pair varlist
            (setq  varname (car pair)  
                  varvalue (cdr pair)
            )
            (setvar varname varvalue)
                 )
         (setq varlist nil) 
          )

          ((not (eq 'LIST (type vars)))
              (princ "\nAI_SYSVAR: Bad argument type.\n")
          )

      ((eq 'STR (type (car vars)))
         (savevar (car vars) (cdr vars))

          )
      ((and 
                (eq 'LIST (type (car  vars)))
            (eq 'STR  (type (caar vars)))
           )
         (foreach pair vars
            (setq  varname (car pair)  
                  varvalue (cdr pair)
            )
            (if (not (eq 'STR (type varname)))
                (princ "\nAI_SYSVAR: Bad argument type.\n")
                (savevar varname varvalue)
            )
                 )
          )
      
      (T (princ "\nAI_SYSVAR: Error in first argument.\n"))
   
   )

   (setq ai_sysvar 
      (cons (car ai_sysvar) 
              (cons (list 'setq 'varlist (list 'quote varlist))
                        (cddr ai_sysvar)
                  )
          )
   )

   varlist

)

(defun ai_entsnap (msg osmode / entpt)
   (while (not entpt)
          (setq entpt (last (entsel msg)))
   )
   (if osmode 
      (setq entpt (osnap entpt osmode))
   )
   entpt
)

(defun ai_undo_push()
  (ai_sysvar '("cmdecho" . 0))
  (setq undo_init (getvar "undoctl"))
  (cond
    ((and (= 1 (logand undo_init 1))   ; enabled
          (/= 2 (logand undo_init 2))  ; not ONE (ie ALL is ON)
          (/= 8 (logand undo_init 8))   ; no GROUP active
     )
      (command "_.undo" "_group")
    )
    (T)
  )  
  (if (= 4 (logand 4 undo_init))
      (command "_.undo" "_auto" "_off")
  )
  (ai_sysvar NIL)
)

(defun ai_undo_pop()
  (ai_sysvar '("cmdecho" . 0))
  (cond 
    ((and (= 1 (logand undo_init 1))   ; enabled
          (/= 2 (logand undo_init 2))  ; not ONE (ie ALL is ON)
          (/= 8 (logand undo_init 8))   ; no GROUP active
     )
      (command "_.undo" "_end")
    )
    (T)
  )  
  (if (= 4 (logand undo_init 4))
    (command "_.undo" "_auto" "_on")
  )  
  (ai_sysvar NIL)
)

(defun ai_rootmenus ()
  (setq T_MENU 0)
  (menucmd "S=S")
  (menucmd "S=ACAD.S")
  (princ)
)

(defun c:ai_fms ( / fmsa fmsb)
  (setq m:err *error* *error* *merr*)
  (ai_undo_push)
  (if (getvar "TILEMODE") (setvar "TILEMODE" 0))
  (setq fmsa (vports) fmsb (nth 0 fmsa))
  (if (member 1 fmsb)
    (if (> (length fmsa) 1)
      (command "_.mspace")
      (progn
        (ai_sysvar '("cmdecho" . 1))
        (command "_.mview")
        (while (eq 1 (logand 1 (getvar "CMDACTIVE")))
          (command pause)
        )
        (ai_sysvar NIL)
        (command "_.mspace")
      )
    )
  )
  (ai_undo_pop)
  (setq *error* m:err m:err nil)
  (princ)
)

(defun c:ai_propchk ( / a)
  (setq m:err *error* *error* *merr*)
  (if (not (setq a (ssget  "_I")))
      (setq a (ssget))
  )
  (if (> (sslength a) 1)
    (progn
      (if (not ddchprop)
        (load "ddchprop")
      )
      (setq *error* m:err m:err nil)
      (ddchprop a)
    )
    (progn
      (if (not ddmodify)
        (load "ddmodify")
      )
      (setq *error* m:err m:err nil)
      (ddmodify (ssname a 0))
    )
  )
  (princ)
)

(defun ai_onoff (var)
  (setvar var (abs (1- (getvar var))))
  (princ)
)

(defun c:ai_pspace ()
  (ai_undo_push)
  (if (/= 0 (getvar "tilemode"))
    (command "_.tilemode" 0)
  )
  (if (/= 1 (getvar "cvport"))
    (command "_.pspace")
  )
  (ai_undo_pop)
  (princ)
)

(defun c:ai_tilemode1 ()
  (ai_undo_push)
  (if (/= 1 (getvar "tilemode"))
    (command "_.tilemode" 1)
  )
  (ai_undo_pop)
  (princ)
)

(defun ai_dim_cen (/ ai_sysvar ai_dim_ss)
  (setq ai_sysvar (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (cond
    ((setq ai_dim_ss (ssget  "_P" '((0 . "DIMENSION"))))
      (command "_.dimoverride" "_dimjust" 0 "" ai_dim_ss "" 
               "_.dimtedit" ai_dim_ss "_h")
    )
    (T nil)
  )
  (setvar "cmdecho" ai_sysvar)
  (princ)
)

(defun ai_circtanstart()
   (setq m:err *error* *error* *merr*)
   (ai_sysvar 
      (list '("cmdecho" . 0)
         ;; make sure _tan pick for CIRCLE gets same entity
         (cons "aperture"  (getvar "pickbox"))
          )
   )
   ;; prompts are the same as CIRCLE/TTR command option
   (setq pt1 (ai_entsnap "\nEnter Tangent spec: "  nil))
   (setq pt2 (ai_entsnap "\nEnter second Tangent spec: " nil))
   (setq pt3 (ai_entsnap "\nEnter third Tangent spec: "  nil))
)

(defun c:ai_circtan (/ pt1 pt2 pt3)
   (ai_circtanstart)

   (ai_sysvar '("osmode" . 256))
   (command "_.circle" "_3p" "_tan" pt1 "_tan" pt2 "_tan" pt3)
   
   (ai_sysvar nil)
   (setq *error* m:err m:err nil)
   (princ)
)

(defun ai_circtan (/ pt1 pt2 pt3)
   (ai_circtanstart)

   (ai_sysvar '("osmode" . 256))
   (command "_3p" pt1  pt2  pt3)
   
   (ai_sysvar nil)
   (setq *error* m:err m:err nil)
   (princ)
)

(defun c:ai_molc(/ old_error end_undo old_cmdecho set1 ent undo_control)
  
  (defun molc_error (s)
	(if old_error (setq *error* old_error))
	(if (eq end_undo 1) (command "_.undo" "_end"))
	(if old_cmdecho (setvar "cmdecho" old_cmdecho))
	(princ)
  )
  (setq old_error *error*)
  (setq *error* molc_error)
  (setq old_cmdecho (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (if (and (eq 1 (logand 1 (getvar "pickfirst")))
            (setq set1 (ssget  "_i"))
	        (eq 1 (sslength set1))
	  )
     (progn 
	   (setq ent (entget (ssname set1 0)))   	 
       (sssetfirst nil nil)
	 )
	 (progn 
	   (sssetfirst nil nil)
	   (setq ent (entget (car (entsel "\nSelect object whose layer will become current: "))))
	 )
  )
  
  (setq undo_control (getvar "undoctl"))
  
  (setq end_undo 0)
  (if (and (= 1 (logand 1 undo_control))   ; undo on
           (/= 2 (logand 2 undo_control))  ; not single undo
      	   (/= 8 (logand 8 undo_control))  ; no active group
	  )
	  (progn 
		(command "_.undo" "_begin")
	    (setq end_undo 1)
      )
  )
  (setvar "clayer" (cdr (assoc '8 ent)))
  (princ (strcat "\n" (getvar "clayer") " is now the current layer."))
  (if (eq 1 end_undo)
    (command "_.undo" "_end")
  )
  (setvar "cmdecho" old_cmdecho)
  (setq *error* old_error)
  (princ)
)

(defun ai_draworder (option / ss )

  (setq m:err *error* *error* *merr*)
  (ai_sysvar '("cmdecho" . 0))

  (if (setq ss (ssget "_I"))
    (command "_.draworder" option)
    (if (setq ss (ssget))
      (command "_.draworder" ss "" option)
    )
  )
  (ai_sysvar NIL)
  (setq *error* m:err m:err nil)

  (princ)
)

(defun c:za()(command "zoom" "all"))
(defun c:ze()(command "zoom" "Extents"))
(defun c:zs()(command "zoom" "1.5x"))
(defun c:zx()(command "zoom" "0.6x"))
(defun c:zz()(command "zoom" "windows"))
(defun c:zw()(command "zoom" "windows"))
(defun c:zq()(command "zoom" "Previous"))
(defun c:OPH1() (layer_off "*") (layer_on "PH_O2"))
(defun c:OPH21() (layer_off "*") (layer_on "PH2_O2"))
(defun c:OPPS1() (layer_off "*") (layer_on "PPS_O2"))
(defun c:OPS1() (layer_off "*") (layer_on "PS_O2"))
(defun c:ODIE21() (layer_off "*") (layer_on "DIE2_O2"))
(defun c:ODIE1() (layer_off "*") (layer_on "DIE_O2"))
(defun c:OLB1() (layer_off "*") (layer_on "LB_O2"))
(defun c:OPPS21() (layer_off "*") (layer_on "PPS2_O2"))
(defun c:OLB21() (layer_off "*") (layer_on "LB2_O2"))
(defun c:OPS21() (layer_off "*") (layer_on "PS2_O2"))

(princ "loaded.")
(princ)

(if init_bnsmenu (init_bnsmenu)) (princ)

(defun c:open_all()
       (command "-layer" "F" "*" "")
       (command "-layer" "T" "*" "")
	(layer_on "*")
        ;(command "regen")
)

(defun c:open_pla()
        (command "-layer" "F" "*" "")
       (command "-layer" "T" "*" "")
	(layer_on "*")
        (layer_off "*_?M")
        (layer_off "part")
        (layer_off "DIM")
        ;(command "regen")
)


;12/9/04 9:39     
(defun chang_layer (lay_name   /	      ss	 len	    n
		;|ent|;	   ent_data   ent_lname	 number	    ent_lname2
		;|lay_chname|; find_l     l_data	 lay_name3
	       )
  (setvar "cmdecho" 0)
  (setq ss (ssget "_i"))
  (setq len (sslength ss))
  (setq n 0)
  (repeat len
    (progn
      (setq ent (ssname ss n))
      (setq ent_data (entget ent))
      (setq ent_lname (cdr (assoc 8 ent_data)))
      (setq number (find ent_lname "_"))
      (if (/= number 0)
	(progn
	  (setq ent_lname2 (substr ent_lname number 8))
	  (setq lay_chname (strcat lay_name ent_lname2))
	  ;(setq find_l(find_layer lay_chname))
	  (setq find_l 0)
	  (setq l_data (tblnext "layer" t))
	  (while l_data
	    (setq lay_name3 (strcase (cdr (assoc 2 l_data))))
	    (if	(or (= lay_name3 (strcase lay_chname))
		    (= lay_name3 (strcase (strcat lay_chname "")))
		)
	      (progn
		(setq l_data nil)
		(setq find_l 0)	
	      )
	      (progn
		(setq find_l 1)
		(setq l_data (tblnext "layer"))
	      )
	    )
	  )
	  (if (= find_l 1)
	    (setq lay_chname ent_lname);;;;???
	  )
	)
	(setq lay_chname lay_name)
      )
      (command "chprop" ent "" "la" lay_chname "")
      ;(command "change" ent "" "p" "la" lay_chname "")
      (setq n (1+ n))
    )
  )
)

(defun copy_layer (lay_name   /	      ss	 len	    n
		ent	   ent_data   ent_lname	 number	    ent_lname2
		lay_chname find_l     l_data	 lay_name3
	       )
  (setvar "cmdecho" 0)
  (setq ss (ssget "_i"))
  (setq len (sslength ss))
  (setq n 0)
  (repeat len
    (progn
      (setq ent (ssname ss n))
      (setq ent_data (entget ent))
      (setq ent_lname (cdr (assoc 8 ent_data)))
      (setq number (find ent_lname "_"))
      (if (/= number 0)
	(progn
	  (setq ent_lname2 (substr ent_lname number 8))
	  (setq lay_chname (strcat lay_name ent_lname2))
	  ;(setq find_l(find_layer lay_chname))
	  (setq find_l 0)
	  (setq l_data (tblnext "layer" t))
	  (while l_data
	    (setq lay_name3 (strcase (cdr (assoc 2 l_data))))
	    (if	(or (= lay_name3 (strcase lay_chname))
		    (= lay_name3 (strcase (strcat lay_chname "")))
		)
	      (progn
		(setq l_data nil)
		(setq find_l 0)	
	      )
	      (progn
		(setq find_l 1)
		(setq l_data (tblnext "layer"))
	      )
	    )
	  )
	  (if (= find_l 1)
	    (setq lay_chname ent_lname)
	  )
	)
	(setq lay_chname lay_name)
      )
      (copy_ent lay_chname ent)
      ;(command "_lay_copy" ent "" lay_chname "")
      (setq n (1+ n))
    )
  )
)  

(defun c:w()
  (setq c_layer (getvar "clayer"))
  (layer_off "*")
  (lw_on(strcat c_layer "_W"))
   )

(defun find_layer (find_name / l_data lay_name3 find_l)
  (setq find_lay 0)
	  (setq l_data (tblnext "layer" t))
	  (while l_data
	    (setq lay_name3 (strcase (cdr (assoc 2 l_data))))
	    (if	(or (= lay_name3 (strcase find_name))
		    (= lay_name3 (strcase (strcat find_name "")))
		)
	      (progn
		(setq l_data nil)
		(setq find_lay 0)	
	      )
	      (progn
		(setq find_lay 1)
		(setq l_data (tblnext "layer"))
	      )
	    )
	  )
  )

(defun c:c1()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C1")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C1"))
  (layer_on(strcat c_layer "_C1"))
    )
   )
(defun c:c2()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C2")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C2"))
  (layer_on(strcat c_layer "_C2"))
    )
   )
(defun c:c3()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C3")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C3"))
  (layer_on(strcat c_layer "_C3"))
    )
   )
(defun c:c4()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C4")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C4"))
  (layer_on(strcat c_layer "_C4"))
    )
   )
(defun c:c5()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C5")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C5"))
  (layer_on(strcat c_layer "_C5"))
    )
   )
(defun c:c6()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C6")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C6"))
  (layer_on(strcat c_layer "_C6"))
    )
   )
(defun c:c7()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C7")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C7"))
  (layer_on(strcat c_layer "_C7"))
    )
   )
(defun c:c8()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C8")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C8"))
  (layer_on(strcat c_layer "_C8"))
    )
   )
(defun c:c9()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C9")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C9"))
  (layer_on(strcat c_layer "_C9"))
    )
   )
(defun c:c10()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C10")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C10"))
  (layer_on(strcat c_layer "_C10"))
    )
   )
(defun c:c11()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C11")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C11"))
  (layer_on(strcat c_layer "_C11"))
    )
   )
(defun c:c12()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C12")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C12"))
  (layer_on(strcat c_layer "_C12"))
    )
   )
(defun c:c13()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C13")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C13"))
  (layer_on(strcat c_layer "_C13"))
    )
   )
(defun c:c14()
  (setq cc_layer (getvar "clayer"))
  (setq layer_text(find cc_layer "_"))
  (setq name_len (- layer_text 1))
  (setq c_layer (substr cc_layer 1 name_len))
  (setq find_end (find_layer (strcat c_layer "_C14")))
  (layer_off "*")
  (if (= find_end 0)
    (lw_on (strcat c_layer "_C14"))
  (layer_on(strcat c_layer "_C14"))
    )
   )

(defun lw_on (layer_name / tbdata lname echo data c70 c62 q70 q62 have)
  (setq have 0)
  (setq tbdata (tblnext "layer" T))
  (while tbdata
    (setq lname (cdr (assoc 2 tbdata)))
    (if	(wcmatch lname (strcase layer_name))
      (progn
	(setq data (entget (tblobjname "layer" lname)))
	(setq c70 (logand (cdr (assoc 70 data)) 122))
	(setq c62 (abs (cdr (assoc 62 data))))
	(setq q70 (cons 70 c70))
	(setq q62 (cons 62 c62))
	(setq data (subst q70 (assoc 70 data) data))
	(setq data (subst q62 (assoc 62 data) data))
	(entmod data)
	(setq have 1)
      )
    )
    (setq tbdata (tblnext "layer"))
    (setvar "Sortents" 112)
  )
  (if (= have 0)
    (progn (prompt (strcat "\n羶衄膘蕾" layer_name "脯"))
	   (layer_on "*")
	   (exit)
    )
  )
  (if (and (= (find layer_name "*") 0)
	   (= (find layer_name "?") 0)
      )
    (setvar "Clayer" layer_name)
  )
  (princ)
)

(defun copy_ent (layer_name ent)
  (command "layer" "n" layer_name "")
  (command "copy" ent "" "@" "@")
  (command "chprop" ent "" "la" layer_name "")
)

;;;(defun chang_ent (layer_name ent3)
;;;;(setq ss (ssget))
;;;;;;(setq counter 0)
;;;;;;(while
;;;;;;  (setq e (ssname ent3 counter))
;;;;;;  (setq l (cdr (assoc 8 (entget e))))
;;;;;;  (setq S (ssget "X" (list (cons 8 l))))
;;;;;;(progn
;;;  (command "chprop" ent3 "" "la" layer_name "" "")
;;;;;;  (setq counter (+ counter 1))
;;;;;;  (princ l)(princ ", ") 
;;;  (princ))
	