



(defun eppos (/		    inspt	  screw_set	ent_count
	      i		    ent_nam	  all_mxb_ent	screw_count
	      epin_count    apn_count	  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"	 ""
	    )
    (command "_.layer" "thaw" "ejID" "on" "mxb"	"unlock" "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
	all_count 0
	pin_count 0
  )
  ;(setq inshdpt inspt)
  ;»(setq inspt (polar inspt (* pi 0.5) 8.0))

					;---------------------------------------------------------
					;Find out ejector pin entities and create ejector pin bill
					;---------------------------------------------------------
  ;(setq inspt (polar inshdpt (/ pi 2) (* 8 (+ 1 all_count))))
					;(setq epin_set (ssget "X" '((0 . "INSERT") (-3 ("epin")))))
  (setq epin_set (ssget))

  (setq i 0)
  (setq ip 0)
  (setq inum 0)
  (setq ipnum 0)
  (if epin_set
    (progn
      (setq ent_count (sslength epin_set))

      (while (< i ent_count)
	(setq ent_nam (ssname epin_set i))

	(if (= "epin"
	       (car (car (cdr (assoc '-3 (entget ent_nam '("EPIN"))))))
	    )
	  (progn
	    (epposdeal_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!")
  )
  

					;---------------------------------------------------------
					;Find out pin entities and create pin bill table
					;---------------------------------------------------------
					;(setq inspt(polar inshdpt (/ pi 2) (* 8 (+ 1 all_count))))
					;(setq pin_set (ssget "X" '((0 . "INSERT") (-3 ("pin")))))


  (if epin_set
    (progn
      (setq ent_count (sslength epin_set))
					;(setq ip 0)
					;****************Ñ­»·*************************
      (while (< ip ent_count)
	(setq ent_nam (ssname epin_set ip))
					;(if (= "pins" (cdr (assoc '8(entget ent_nam))))
	(if (= "pin"
	       (car (car (cdr (assoc '-3 (entget ent_nam '("pin"))))))
	    )
	  (progn
	    (epposdeal_pin)
	    (setq ipnum (+ ipnum 1))
	  )
	)

	(setq ip (+ ip 1))
      )
      (setq all_count (+ all_count pin_count))
      (if (> pin_count 0)
	(princ
	  (strcat "\nFound" " " (itoa pin_count) " " "kinds of pin!")
	)
      )
    )
    (princ "\nNot found any pin!")
  )

					;----------------------------------------------
					;Reset system variable
					;----------------------------------------------

  

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




					;************************* Begin create ejector pin bill table ************************;
(defun epposdeal_epin ()
  (setq cenpt (cdr (assoc '10 (entget ent_nam))))
  (setq ox (car cenpt))
  (setq oy (car (cdr cenpt)))
  (setq x (rtos (- ox x1) 2 3))
  (setq y (rtos (- oy y1) 2 3))
  (setq dia (cdr (assoc '41 (entget ent_nam '("EPIN")))))
  (setq epid (strcat "E" (itoa (+ inum 1))))
  
					;(transtoeppos "epin")
  (setq	epin_data
	 (cdr (cadr (cadr (assoc -3 (entget ent_nam '("EPIN"))))))
  )
  (setq epin_data (strtolst epin_data))
  (progn
    (setq rep	   (nth 0 epin_data)
	  mb	   (nth 6 epin_data)
	  type_old (nth 2 epin_data)
	  len_old  (nth 3 epin_data)
	  zc	   (nth 1 epin_data)
	  if_h_v   (nth 4 epin_data)
	  typ_old  (nth 5 epin_data)
	  cot	   (strcat "%%c" type_old)
    )
    (cond
      ((= "N" typ_old)
       (setq chin "Ö±Éí¶¥Õë")
       (setq desc "EJECTOR PIN")
      )
      ((= "W" typ_old)
       (setq chin "ÓÐÍÐ¶¥Õë")
       (setq desc "STEP EJECTOR PIN")
    
      )
    )
  )
  (epid_insert)
  
					;(if (= "Z" zc)
  
					;)
)
					;********************************** End of function ***********************************;




					;***************************** Begin create pin bill table ****************************;
(defun epposdeal_pin ()
  (setq cenpt (cdr (assoc '10 (entget ent_nam))))
  (setq ox (car cenpt))
  (setq oy (car (cdr cenpt)))
  (setq x (rtos (- ox x1) 2 3))
  (setq y (rtos (- oy y1) 2 3))
  (setq dia (cdr (assoc '41 (entget ent_nam '("pin")))))
  (setq epid (strcat "p" (itoa (+ ipnum 1))))
  (setq	epin_data
	 (cdr (cadr (cadr (assoc -3 (entget ent_nam '("pin"))))))
  )
  (setq epin_data (strtolst epin_data))
  (progn
    (setq rep	   (nth 0 epin_data)
	  mb	   (nth 6 epin_data)
	  type_old (nth 2 epin_data)
	  len_old  (nth 3 epin_data)
	  zc	   (nth 1 epin_data)
	  if_h_v   (nth 4 epin_data)
	  typ_old  (nth 5 epin_data)
	  cot	   (strcat "%%c" type_old)
    )
    (cond
      ((= "N" typ_old)
       (setq chin "Ö±Éí¶¥Õë")
       (setq desc "EJECTOR PIN")
      )
      ((= "W" typ_old)
       (setq chin "ÓÐÍÐ¶¥Õë")
       (setq desc "STEP EJECTOR PIN")
    
      )
    )
  )
  (epid_insert)
  			
)
					;********************************** 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 ***********************************;



(defun cso_tran	(/ cso)
  (setq cso (getpoint "Ñ¡È¡»ù×¼µã"))

  (command "insert" "cso.dwg" cso "" "" "")
  (command "ucs" "o" cso)
  (setq x1 (car cso))
  (setq y1 (car (cdr cso)))

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

					;******************** Main function start ************************;
(defun epid_insert (/ sel_ent)
  (setvar "osmode" 0)
  (setq sel_ent (ssadd))
  (setq insterpt (list (atof x) (atof y) 0))
  (command "ucs" "")
  (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 "#" (substr epid 2)))
  (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       " "
		   "Z"	   " ";view flag
		   chin	   " "	;dim diameter
		   type_old	   " "	;dim length
		   len_old  " "		;hidden flag
		   rep
		  )
  )					;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 ****************************************;

