



(defun epid_mxb (/		    	  screw_set	ent_count
	      i		    ent_nam	  all_mxb_ent	screw_count
	      epid_count    all_count	old_set
	      old_set_nstd  all_mxb_ent epid_head_pos
	     )
					;------------------------------------------
					;shut off command echo and set undo mark
					;------------------------------------------
  (setvar "cmdecho" 0)
  (command "undo" "m")
					;------------------------------------------
					;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")
  )
  (setq old_set (ssget "X" '((0 . "INSERT") (2 . "ep_pos"))))
  (if old_set
    (command "erase" old_set "")
  )

  
  (command "purge" "b" "ac_xyq_blk*" "n")
  (setq epid_head_pos nil)
  (setq	epid_head_set
	 (ssget "x" '((0 . "insert") (-3 ("mxb_epid"))))
  )
  
  (if epid_head_set
    (progn
      (setq epid_head_name (ssname epid_head_set 0))
      (setq epid_head_pos (cdr (assoc '10 (entget epid_head_name))))

    )
  )
  ;------------------------------------------
  					;get bill table insert point
					;------------------------------------------
  
  (setq pos epid_head_pos)
  (if (not epid_head_pos)
    (progn
      (setq epid_head_pos (getpoint "\nPlease get a insert point:"))
      (if (not epid_head_pos)
	(progn
	  (setq bpt (wtou (list (getvar "userr1") (getvar "userr2"))))
	  (if ent_sel_mxb
	    (setq i (+ (getvar "useri1") 1))
	    (setq i 0)
	  )
	  (setq epid_head_pos (polar bpt (* pi 0.5) (* 8 i)))
	)
      )
    )
    (while (not epid_head_pos)
      (setq epid_head_pos (getpoint "\nPlease get a insert point:"))
      
    )
  )
					;------------------------------------------
					;Initializing					;-----------------------------------------
  (setvar "osmode" 0)
  
 
  (setq	all_mxb_ent nil
	epin_count 0
	pin_count 0
	all_count 0
  )
  (setq inshdpt epid_head_pos)
  (setq epid_head_pos (polar epid_head_pos (* pi 0.5) 8.0))

					;---------------------------------------------------------
					;Find out ejector pin entities and create ejector pin bill
					;---------------------------------------------------------
  ;(setq inspt (polar inshdpt (/ pi 2) (* 8 (+ 1 0))))
  (setq epid_set (ssget "X" '((0 . "INSERT") (-3 ("epid")))))
  
  
  (setq ip 0)
  (setq ipnum 0)
  (if epid_set
    (progn
      (setq epid_count (sslength epid_set))
      (setq ii 0)
      (setq inum 0)
      
      (while (< ii epid_count)
	
	(setq ent_nam (ssname epid_set ii))
	
	(setq epid_all_mxb (cdr (car (cdr (car (cdr (assoc '-3 (entget ent_nam '("epid")))))))))
	
	(setq epid_data (strtolst epid_all_mxb))

	(if (= "E" (substr (nth 0 epid_data) 1 1))
	  (progn
	    (transtoepidmxb)
	    (setq inum (+ inum 1))
	    
	  )
	)
	(setq ii (+ ii 1))
      )
      
      ;(setq all_count (+ all_count epid_count))
      (if (> epin_count 0)
	(princ (strcat "\nFound"
		       " "
		       (itoa epin_count)
		       " "
		       "kinds of ejector pin!"
	       )
	)
      )
    )
    (princ "\nNot found any ejector pin!")
  )
  

(if (not pos)
  (progn
    (command "insert" "epid_head.dwg" inshdpt "" "" "")
    (setq
      xd (list (list -3 (list "mxb_epid" (cons 1000 "mxb_head")))
	 )
    )
    (mxdata (entlast) xd)
  )

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

  

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







					;***************************** Begin create pin bill table ****************************;




					;********************************* Create bill table **********************************;
(defun transtoepidmxb (/	   entn	      ent_sel
		     ent_chin	ent_qty	   ent_rep    ent_obv
		     ent_cot	ent_amt	   ent_desc   spec
		    )
  (if epid_data
      (progn
	(setq epid_ (nth 0 epid_data))
	(setq pos_x (nth 1 epid_data))
	(setq pos_y (nth 2 epid_data))
	(setq epid_type (nth 4 epid_data))
	(setq epid_dia (nth 5 epid_data))
	(setq epid_len (nth 6 epid_data))
      )
  )
  (progn				
    (setq pti (polar (polar epid_head_pos (/ pi 2) (* (+ ipnum inum) 8)) pi 140))
    (setq epid_xd
	   (list
	     (list
	       -3
	       (list "mxb_epid" (cons 1000 (strcat epid_ " "
						       pos_x " "
						       pos_y " "
						    epid_type " "
						   epid_dia   " "
						   epid_len
						   )
				      )
		     )
	     )
	   )
    )
    (command "insert" "ep_pos.dwg" pti "" "" ""	epid_ epid_type pos_x epid_dia pos_y)

    (mxdata (entlast) epid_xd)
					
  )
)


(defun ini_head	(/
		 epid_head_pos
		 epid_head_set
		 epid_head_name
		)
  
  (setq	epid_head_set
	 (ssget "x" '((0 . "insert") (-3 ("mxb_epid"))))
  )
  
  (if epid_head_set
    (progn
      (setq epid_head_name (ssname epid_head_set 0))
      (setq epid_head_pos (assoc '10 (entget epid_head_name)))

    )
  )

)