



(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")
					;------------------------------------------
					;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 "")
  )

  (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
					;------------------------------------------
  (if (> (getvar "useri1") 0)
    (progn
      (setq inspt (getpoint "\nPlease get a insert point:"))
      (if (not inspt)
	(progn
	  (setq bpt (wtou (list (getvar "userr1") (getvar "userr2"))))
	  (if ent_sel_mxb
	    (setq i (+ (getvar "useri1") 1))
	    (setq i 0)
	  )
	  (setq inspt (polar bpt (* pi 0.5) (* 8 i)))
	)
      )
    )
    (while (not inspt)
      (setq inspt (getpoint "\nPlease get a insert point:"))
    )
  )
					;------------------------------------------
					;Initializing
					;------------------------------------------
  (cso_tran)
  (setvar "osmode" 0)
  (princ
    "\nAnalyzing entity... \nBegin to create Epin bill table, please wait..."
  )
  (if (not (tblsearch "appid" "mxb_standard"))
    (regapp "mxb_standard")
  )
  (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
					;----------------------------------------------

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

  (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 "#" (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)
  (setq spec_var (strcat chin cot))
					;(if (= "Z" zc)
  (progn
					;#####################################
    (transtoeppos "epin")
    (if	(= 1 ins_lb)
      (if (> (atoi (substr rep 2)) epin_count)
	(setq epin_count (atoi (substr rep 2)))
      )
    )
  )
					;)
)
					;********************************** 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))))
  (epid_insert)
  (setq
    pin_data (cdr (cadr (cadr (assoc -3 (entget ent_nam '("pin"))))))
  )
  (setq pin_data (strtolst pin_data))
  (progn
    (setq rep	   (nth 0 pin_data)
	  mb	   (nth 6 pin_data)
	  type_old (nth 2 pin_data)
	  len_old  (nth 3 pin_data)
	  zc	   (nth 1 pin_data)
	  if_h_v   (nth 4 pin_data)
	  typ_old  (nth 5 pin_data)
	  cot	   (strcat "%%c" type_old )
	  
    )
    (setq chin "镶针")
    (setq desc "EJECTOR PIN")
   
  )
					;(setq spec_var (strcat chin cot))
					;(if (= "Z" zc)
					;(progn
  (transtoeppos "pin")
  (if (= 1 ins_lb)
    (if	(> (atoi (substr rep 2)) pin_count)
      (setq pin_count (atoi (substr rep 2)))
    )
  )
					;)
					;)
)
					;********************************** End of function ***********************************;




					;********************************** Share 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 **********************************;
(defun transtoeppos (app_name	/	   entn	      ent_sel
		     ent_chin	ent_qty	   ent_rep    ent_obv
		     ent_cot	ent_amt	   ent_desc   spec
		    )
  (setq ins_lb 1)
  (if all_mxb_ent
    (foreach entn all_mxb_ent
      (progn
	(setq ent_sel (handent entn))
	(setq ent_rep (entnext ent_sel))
	(setq ent_qty (entnext ent_rep))
	(setq ent_chin (entnext ent_qty))
	(setq ent_desc (entnext ent_chin))
	(setq ent_mat (entnext ent_desc))
	(setq ent_cot (entnext ent_mat))
	(setq ent_obv (entnext ent_cot))
	(setq spec (strcat (cdr (assoc 1 (entget ent_chin)))
			   (cdr (assoc 1 (entget ent_cot)))
		   )
	)
	(if (= rep (cdr (assoc 1 (entget ent_rep))))
	  (if (= spec_var spec)
					;(add_qty)
	    (progn
	      (command "undo" "b")
	      (redraw ent_nam 3)
	      (*error* (strcat "\""
			       rep
			       "\""
			       "编号中含两种不同规格零件"
			       "\n\n     \""
			       spec
			       "\"\n     \""
			       spec_var
			       "\"
		               \n\n"
			       "请检查图纸."
			      )
	      )
	    )
	  )
	)
      )
    )
  )
					;(if (= 1 ins_lb)
  (progn

					;(setq n (- (atoi (substr rep 2)) 1))
    (setq
      pti (polar (polar inspt (/ pi 2) (* (+ ipnum inum) 8)) pi 140)
    )
    (setq xd
	   (list
	     (list
	       -3
	       (list "mxb_standard" (cons 1000 (strcat app_name " "
						       rep " "
						       chin " "
						       type_old " "
						       len_old)))
	     )
	   )
    )
    (command "insert" "ep_pos.dwg" pti "" "" ""	epid chin x cot	y)

    (mxdata (entlast) xd)
					;(if all_mxb_ent
					; (setq all_mxb_ent (append all_mxb_ent (list (cdr (assoc 5 (entget (entlast)))))))
					; (setq all_mxb_ent (list (cdr (assoc 5 (entget (entlast))))))
					;)
					; )
  )
)
					;********************************** End of function ***********************************;


(defun add_qty (/ qty_new)
  (setq qty_new (itoa (+ 1 (atoi (cdr (assoc 1 (entget ent_qty)))))))
  (mod_ent ent_qty qty_new)
  (setq ins_lb 0)
)
					;********************************** End of function ***********************************;

					;********************************** 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))
					;-----------------------------------------------------
					;Dim specification and item number
					;-----------------------------------------------------
  (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 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" (itoa (getvar "useri2")))
	cenpt
	sel_ent
  )
  (setq	xd (strcat rep	   " "		;item number
		   "Z"	   " "		;view flag
		   epin_type	   " "	;dim diameter
		   epin_len	   " "	;dim length
		   if_h_v  " "		;hidden flag
		   typ	   " "		;type flag
		   mb
		  )
  )					;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 ini_epnum(/ epin_count)
  (setq block_list '())
  (setq new_search nil)
  (while (setq new_search (tblnext "BLOCK" (not block_list)))
    (if new_search
      (setq block_list (cons (cdr (assoc 2 new_search)) block_list))
    )
  )
  (if block_list 
    (foreach str block_list
      (progn
        (setq i (strlen str))
        (if (> i 11)
	  (progn
            (setq strtmp (substr str 1 11))
	    (if (= "AC_XYQ_BLKE" (strcase strtmp))
	      (if epin_count 
		(if (> (atoi (substr str 12)) epin_count)
		  (setq epin_count (atoi (substr str 12)))
		)
		(setq epin_count (atoi (substr str 12)))
	      )
	    )
	    (if (= "AC_XYQ_BLKP" (strcase strtmp))
	      (if pin_count 
		(if (> (atoi (substr str 12)) pin_count)
		  (setq pin_count (atoi (substr str 12)))
		)
		(setq pin_count (atoi (substr str 12)))
	      )
	    )
	  )
        )
      )
    )  
  )
  
)