

(defun cso_tran	(/ cso)
  (command "ucs" "w")
  (setq old_cso (ssget "X" '((0 . "INSERT") (2 . "cso"))))
  (if (= nil old_cso)
    (progn
      (setq cso (getpoint "选取基准点"))

      (command "insert" "cso.dwg" cso "" "" "")
      (command "ucs" "o" cso)
      (setq x1 (car cso))
      (setq y1 (car (cdr cso)))
    )
    (if	(= 1 (sslength old_cso))
      (progn
	(princ "!!!图形中已有基准坐标系!!!")
	(setq temp_cso (assoc '10 (entget (ssname old_cso 0))))
	(setq x1 (car (cdr temp_cso)))
	(setq y1 (cadr (cdr temp_cso)))
      )
      (princ "!!!图形中已有多基准坐标系!!!")
    )
  )
)








(defun ES_POS(/		    inspt	  screw_set	ent_count
	      i		    ent_nam	  all_mxb_ent	
	     all_count	old_set
	      old_set_nstd  all_mxb_ent    
	     )
					;------------------------------------------
					;shut off command echo and set undo mark
					;------------------------------------------
  (setvar "cmdecho" 0)
  (command "undo" "m")
  (setq os_old (getvar "osmode"))	;------------------------------------------
					;save current system var
					;------------------------------------------
					;(setq os_old (getvar "osmode"))
  (setq la_old (getvar "clayer"))
					;------------------------------------------
					;set user system var
					;------------------------------------------
  (if (not (tblsearch "layer" "pinID"))
    (command "_.layer"	 "_new"	     "pinID"	 "_color"
	     "red"	 "pinID"     "_ltype"	 "continuous"
	     "mxb"	 ""
	    )
    (command "_.layer" "thaw" "pinID" "on" "mxb" "unlock" "mxb"	"")
  )
  (setvar "clayer" "pinID")
  (if (not (tblsearch "layer" "EJID"))
    (command "_.layer"	 "_new"	     "EJID"	 "_color"
	     "red"	 "ejID"	     "_ltype"	 "continuous"
	     "mxb"	 ""
	    )
  )
  (if (not (tblsearch "layer" "ES_C"))
    (command "_.layer"	 "_new"	     "ES_C"	 "_color"
	     "green"	 "ES_C"	     "_ltype"	 "continuous"
	     "mxb"	 ""
	    )
  )
  (setvar "clayer" "ejID")
					;(ini_useri2)

					;------------------------------------------
					;delete old bill table
					;------------------------------------------
  (if (not (tblsearch "appid" "mxb_epid"))
    (regapp "mxb_epid")
  )
  (if (not (tblsearch "appid" "epid"))
    (regapp "epid")
  )
  (setq old_set (ssget "X" '((0 . "INSERT") (2 . "ep_pos"))))
  (if old_set
    (command "erase" old_set "")
  )

  ;(setq old_set (ssget "X" '((0 . "INSERT") (8 . "ejid"))))
  ;(if old_set
    ;(command "erase" old_set "")

  ;)
  (command "purge" "b" "ac_xyq_blk*" "n")

					;------------------------------------------
					;get bill table insert point
					;------------------------------------------
					;------------------------------------------
					;Initializing
					;------------------------------------------
  (cso_tran)
  (setvar "osmode" 0)
  (princ
    "\nAnalyzing entity... \nBegin to create Epin bill table, please wait..."
  )
  (if (not (tblsearch "appid" "mxb_epid"))
    (regapp "mxb_sepid")
  )
  (setq	all_mxb_ent
	 nil
	epin_count 0
	ES_count 0
	pin_count 0
  )
 
(ini_epnum)					
  (setq ES_sel (ssget))
  (setq i 0)
  (setq ip 0)
  (setq inum 0)
  (setq ipnum 0)
  (setq esnum 0)
  (if ES_count
    (setq esnum es_count)
  )
  
  (if ES_sel
    (progn
      (setq cirnum 0)
      (setq ent_count (sslength ES_sel))

      (while (< i ent_count)
	(setq ent_nam (ssname ES_sel i))
	(setq ent_type_list (assoc '0 (entget ent_nam)))
	
	;****************************
	(if (= "CIRCLE" (cdr ent_type_list)) 
	  (progn
	    (setq pin_data (entget ent_nam))
	    (setq cirnum (+ cirnum 1))
	    (entdel ent_nam)
	    ;(epposdeal_Epin_c "E")
	    
	    (princ "EPIN\ ")
	    (setq inum (+ inum 1))
	  )
	)
	(setq i (+ i 1))
      )
      (setq all_count (+ all_count epin_count))
      (if (> epin_count 0)
	(princ (strcat "\nFound"
		       " "
		       (itoa epin_count)
		       " "
		       "kinds of ejector pin!"
	       )
	)
      )
    )
    (princ "\nNot found any ejector pin!")
  )





  (ac_lxy_set)
  (prin1)
)
					;********************************** End of function ***********************************;







					;************************* Begin create ejector pin bill table ************************;
(defun epposdeal_Epin_c	(hole_type /)
  (setq cenpt (cdr (assoc '10 (entget ent_nam))))
  (setq ox (car cenpt))
  (setq oy (car (cdr cenpt)))
  (setq x (rtos (- ox x1) 2 3))		;X座标值
  (setq y (rtos (- oy y1) 2 3))		;Y坐标什
  
  (progn
    (setq pos (cdr (assoc '10 pin_data))
	  dia (* (cdr (assoc '40 pin_data)) 2)


	  cot (strcat "%%c" (rtos dia))
    )
    (if	(= "P" hole_type)
      (setq num ipnum)
      (setq num inum)
    )
    (setq epid (strcat hole_type (itoa (+ num 1))))
    
  )
  (pid_insert hole_type)

  (princ "ok" )
)
					;********************************** End of function ***********************************;








(defun mod_ent (ent_name mod_data / ent_data)
  (setq ent_data (entget ent_name))
  (setq	ent_data (subst	(cons 1 mod_data)
			(assoc 1 ent_data)
			ent_data
		 )
  )
  (entmod ent_data)
  (entupd ent_name)
)
					;********************************** End of function ***********************************;


					;********************************* Create bill table **********************************;
					;********************************** End of function ***********************************;



					;********************************** End of function ***********************************;

					;******************** Main function start ************************;
(defun pid_insert (p_type/ )
  (setvar "osmode" 0)
  (setq sel_ent (ssadd))
  (setq insterpt (list (atof x) (atof y) 0))
  (command "ucs" "")
					;if
  (command "insert" "epid.dwg" cenpt dia "" "")
  (setq sel_ent (ssadd (entlast) sel_ent))

  (if (not (tblsearch "style" "lxy2"))
    (command "-style" "lxy2" "txt" "" 0.6 "" "" "" "")
    (setvar "textstyle" "lxy2")
  )
  (setq textpt (polar cenpt (* (/ pi 4) -1) (+ (/ dia 2) 2.5)))
  
  (command "text" "j" "mc" textpt 2.5 0 (strcat "#" epid))
  (setq sel_ent (ssadd (entlast) sel_ent))
  (command "text"
	   "j"
	   "ml"
	   (polar textpt (* (/ pi 2) -1) 2)
	   1.0
	   0
	   (strcat "(X" x)
  )
  (setq sel_ent (ssadd (entlast) sel_ent))
  (command "text"
	   "j"
	   "ml"
	   (polar textpt (* (/ pi 2) -1) 3.2)
	   1.0
	   0
	   (strcat "Y" y ")")
  )
  (setq sel_ent (ssadd (entlast) sel_ent))

					;(setvar "textstyle" st_old)
					;-----------------------------------------------------
					;Make screw block and add extend data in it
					;-----------------------------------------------------
  
  (mblk	(strcat "AC_xyq_blk" epid)
	cenpt
	sel_ent
  )
  (setq	xd (strcat epid	" "		;item number
		   x " "
		   y " "
		   (rtos Dia) " "	;dim diameter
		
)
  )					;meter or inch flag
  (setq xd (list (list -3 (list "epid" (cons 1000 xd)))))
  (mxdata (entlast) xd)
  (setvar "useri2" (+ 1 (getvar "useri2")))
  (setvar "osmode" os_old)
)
					;*************************************** End of function ****************************************;


(defun cir_list (/ )
  (setq cenpt (cdr (assoc '10 (entget ent_nam))))
  (setq ox (car cenpt))
  (setq oy (car (cdr cenpt)))
  (setq x (rtos (- ox x1) 2 3))		;X座标值
  (setq y (rtos (- oy y1) 2 3))		;Y坐标什
  (setq	pos (cdr (assoc '10 pin_data))
	dia (* (cdr (assoc '40 pin_data)) 2)


	cot (strcat "%%c" (rtos dia))
  )
  (setq c_list (cons c_list (cons 