;*****************************************************;
;* This is "epin_ws.lsp" program 		     *;
;* Complete compiled in 02/06/98                     *;
;* Last change in 02/09/99    			     *;
;* ZX Mold Ltd  XY.Liao    			     *;
;*****************************************************;

;************************************* Main function start **************************************;

(defun epin_ws( / err_msg1	err_msg2)

  (load "epin_lib")
;---------------------------------------------------------------------
;initializing
;---------------------------------------------------------------------
  (setq ws_conlb  1
	last_d_m  0
	last_d_b  0
	last_l_m  0
	last_l_b  0
	mbf       1
	if_h_v    "0"
  )
  (if (not epin_count)
    (setq epin_count 1)
  )
;-------------------------------------------------------------------
;starting dialog
;-------------------------------------------------------------------
  (while (= 1 ws_conlb)
    (if (not (new_dialog "wsdlg" epin_dcl_id))
      (*error* "Can't open \"Epin.dcl\"!")
    )
    (ini_dcl_ws)
    (action_tile "WSM"     "(setq mbf 0) (ini_dcl_ws)")
    (action_tile "WSB"     "(setq mbf 1) (ini_dcl_ws)")
    (action_tile "WSTYP"   "(typ_act)")
    (action_tile "WSLEN"   "(len_act)")
    (action_tile "WSITEM"  "(setq rep_r $reason)  (valid_rep)")
    (action_tile "WSSPEC"  "(setq spec_r $reason) (valid_spec)")
    (action_tile "HIDDENV" "(ch_hidden) (start_ws_image)")
    (action_tile "cancel"  "(done_dialog 0)")
    (action_tile "accept"  "(done_dialog 1)")
    (action_tile "ws_z"    "(done_dialog 2)")
    (action_tile "ws_c"    "(done_dialog 3)")
    (action_tile "BACK"    "(done_dialog 4)")
    (action_tile "LOOK"    "(done_dialog 5)")
    (setq ws_next (start_dialog))
    (cond
      ((= 0 ws_next)
        (command "undo" "b")
        (setq ws_conlb 0)
        (setq epin_conlb 0)
      )
      ((= 1 ws_next)
        (setq ws_conlb 0)
        (setq epin_conlb 0)
      )
      ((= 2 ws_next)
        (epin_ws_zst)
      )
      ((= 3 ws_next)
        (epin_cst)
      )
      ((= 4 ws_next)
        (setq ws_conlb 0)
      )
      ((= 5 ws_next)
        (command "zoom" "")
      )
    )
  )
)
;*************************************** end of function ****************************************;


;**************************** Define dialog control functions group *****************************;
;-----------------------------------------
;Set hidden flag
;-----------------------------------------
(defun ch_hidden()                      
  (setq if_h_v (get_tile "HIDDENV"))
)

;----------------------------------------
;Initialize dialog
;----------------------------------------
(defun ini_dcl_ws()
  (cond 
    ((= 1 mbf)
      (setq mb "B")
      (set_tile "WSB" "1")
      (ini_list "WSTYP" (lib_ep 9))
      (ini_list "WSLEN" (lib_ep 13))
      (set_tile "WSTYP" (itoa last_d_b))
      (set_tile "WSLEN" (itoa last_l_b))
      (setq epin_type (nth last_d_b (lib_ep 9)))
      (setq epin_len  (nth last_l_b (lib_ep 13)))
      (setq d  (nth last_d_b (lib_ep 5)))
      (setq hd 6.35)
      (setq dd 3.175)
      (setq b  3.175)
    )
    ((= 0 mbf)
      (setq mb "M")
      (set_tile "WSM" "1")
      (ini_list "WSTYP" (lib_ep 8))
      (ini_list "WSLEN" (lib_ep 11))
      (set_tile "WSTYP" (itoa last_d_m))
      (set_tile "WSLEN" (itoa last_l_m))
      (setq epin_type (nth last_d_m (lib_ep 8)))
      (setq epin_len  (nth last_l_m (lib_ep 11)))
      (setq d  (atof epin_type))
      (setq hd 6.0)
      (setq dd 3.0)
      (setq b  4.0)
    )
  )
  (setq rep (strcat "E" (itoa epin_count)))
  (set_tile "WSSPEC" (strcat epin_type "X" epin_len))
  (set_tile "WSITEM" rep)
  (set_tile "HIDDENV" if_h_v)
  (start_ws_image)
  (if err_msg1
    (set_tile "MSG" err_msg1)
    (if err_msg2
      (set_tile "MSG" err_msg2)
    )
  )
)
;-------------------------------------------
;Initialize image button
;-------------------------------------------
(defun start_ws_image(/ pic_fn)
  (cond 
    ((= "0" if_h_v)
      (foreach pic_fn '("ws_z" "ws_c")
	(start_image pic_fn)
	(fill_image 0 0 (dimx_tile pic_fn) (dimy_tile pic_fn) 0)
	(end_image)
      )
      (foreach pic_fn '("ws_z" "ws_c")
	(start_image pic_fn)
	(slide_image 0 0 (dimx_tile pic_fn) (dimy_tile pic_fn) (strcat "ac_lxy(" pic_fn ")"))
	(end_image)
      )
    )
    ((= "1" if_h_v)
      (foreach pic_fn '("ws_z" "ws_c")
	(start_image pic_fn)
	(fill_image 0 0 (dimx_tile pic_fn) (dimy_tile pic_fn) 0)
	(end_image)
      )
      (foreach pic_fn '("ws_z" "ws_c")
	(start_image pic_fn)
	(slide_image 0 0 (dimx_tile pic_fn) (dimy_tile pic_fn) (strcat "ac_lxy(" pic_fn "_h)"))
	(end_image)
      )
    )
  )
)
;------------------------------------------------
;Ejector pin diameter change function
;------------------------------------------------
(defun typ_act()
  (cond
    ((= "B" mb)
      (setq last_d_b (atoi (get_tile "WSTYP")))
      (setq epin_type (nth last_d_b (lib_ep 9)))
      (setq d  (nth last_d_b (lib_ep 5)))
      (setq hd 6.35)
      (setq dd 3.175)
      (setq b  3.175)
    )
    ((= "M" mb)
      (setq last_d_m (atoi (get_tile "WSTYP")))
      (setq epin_type (nth last_d_m (lib_ep 8)))
      (setq d  (atof epin_type))
      (setq hd 6.0)
      (setq dd 3.0)
      (setq b  4.0)
    )
  )
  (set_tile "WSSPEC" (strcat epin_type "X" epin_len))
)
;------------------------------------------------
;Ejector pin length change function
;------------------------------------------------
(defun len_act()
  (cond
    ((= "B" mb)
      (setq last_l_b (atoi (get_tile "WSLEN")))
      (setq epin_len (nth last_l_b (lib_ep 13)))
    )
    ((= "M" mb)
      (setq last_l_m (atoi (get_tile "WSLEN")))
      (setq epin_len (nth last_l_m (lib_ep 11)))
    )
  )
  (set_tile "WSSPEC" (strcat epin_type "X" epin_len))
)
;------------------------------------------------
;Item number valid verify
;------------------------------------------------
(defun valid_rep()
  (setq rep (lxy_trim (get_tile "WSITEM")))
  (if (= 2 rep_r)
    (if (< (strlen rep) 2)
      (setq err_msg1 "Invalid item number!")
      (if (/= "E" (strcase (substr rep 1 1)))
        (setq err_msg1 "Invalid item number!")
        (if (= 0.0 (atof (substr rep 2)))
          (setq err_msg1 "Invalid item number!")
          (progn
            (setq epin_count (fix (atof (substr rep 2))))
            (setq rep (strcat "E" (itoa epin_count)))
            (set_tile "WSITEM" rep)
	    (setq err_msg1 nil)
          )
        )
      )
    )
  )
  (if err_msg1
    (progn 
      (mode_tile "WSITEM" 2)
      (set_tile "MSG" err_msg1)
    )
    (if (not err_msg2)
      (set_tile "MSG" "")
    )
  )
)
;------------------------------------------------
;specification box reset
;------------------------------------------------
(defun valid_spec()
  (if (= 2 spec_r)
    (set_tile "WSSPEC" (strcat epin_type "X" epin_len))
  )
)
;*********************************** End of functions group *************************************;


;****************************** Call create pin function function *******************************;
(defun epin_cst()
  (load "epwscst")
  (initget 32)
  (setvar "osmode" os_old)
  (setq err_msg2 nil)
  (if (setq pt1 (getpoint "\nPlease enter first point:"))
    (if (setq pt2 (getpoint pt1 "\nPlease enter second point:"))
      (if (setq pt3 (getpoint pt2 "\nPlease enter third point:"))
	(if (setq ob_ent (nentsel))
          (progn
            (setvar "osmode" 0)
            (setq epin_app "NEW")
            (ws_cst)
            (setvar "osmode" os_old)
          )
          (setq err_msg2 "Invalid entity!")
        )
        (setq err_msg2 "Invalid point!")
      )
      (setq err_msg2 "Invalid point!")
    )
    (setq err_msg2 "Invalid point!")
  )
  (if (not err_msg2)
    (progn
      (if (/= len epin_len)
        (cond
          ((= "B" mb)
            (setq last_l_b (xh_get len (lib_ep 12)))
          )
          ((= "M" mb)
            (setq last_l_m (xh_get len (lib_ep 10)))
          )
        )
      )
      (setq epin_sel_zst (ssget "x" '((0 . "INSERT") (-3 ("EPIN")))))
      (setq count_all (sslength epin_sel_zst))
      (setq kkk 0)
      (while (< kkk count_all)
        (setq epin_ent_zst (ssname epin_sel_zst kkk))
        (setq epin_data (cdr (cadr (cadr (assoc -3 (entget epin_ent_zst '("epin")))))))
        (setq epin_data (strtolst epin_data))
        (setq rep_zst  (nth 0 epin_data)
	      zc_zst   (nth 1 epin_data)
              type_zst (nth 2 epin_data)
	      len_zst  (nth 3 epin_data)
              typ_zst  (nth 5 epin_data)
        )
        (if (and (= "Z" zc_zst) (= rep rep_zst))
          (if (or (/= "W" typ_zst) (/= epin_type type_zst) (/= len len_zst))
            (progn
	      (setq cenpt (cdr (assoc 10 (entget epin_ent_zst))))
              ;(setq cenpt (wtou cenpt))
		(setq cenpt cenpt)
	      (entdel epin_ent_zst)
              (setq typ "W")
	      (setq epin_len len)
              (load "epin_zst")
	      (epin_zst)
            )
          )
        )
        (setq kkk (+ 1 kkk))
      )
      (setq epin_count (+ 1 epin_count))
    )
  )
)
;*************************************** end of function ****************************************;

;****************************** Call create pin function function *******************************;
(defun epin_ws_zst()
  (load "epin_zst")
  (setvar "osmode" os_old)
  (while (setq cenpt (getpoint "\nPlease enter center point:"))
    (setvar "osmode" 0)
    (setq typ "W")
    (epin_zst)
    (setq err_msg2 nil)
    (setvar "osmode" os_old)
  )
  (setq epin_count (+ 1 epin_count)) ;set epin counter
)
;*************************************** end of function ****************************************;