; -*-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Description:  MoldDesignAdvisor Customization: Functions for calculating the 
;;               geometric feature variables
;; Language:     Lisp
;; Package:      MOLD-UI
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package :mold-ui)
(use-package '(:oli :frame2))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;         Formulas originally used for the Simple Boss calculation
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defparameter *simple-boss-formula-list* 
    (list :default-formular-group :original
          :original (list :regi-name *original-group*
		          :bottom-thickness 'simpleb-bottom-thickness
		          :bottom-thickness-low 'simpleb-bottom-thickness-lower-limit
                          :bottom-thickness-high 'simpleb-bottom-thickness-upper-limit
		          :hole-draft-angle 'simpleb-hole-draft-angle
		          :hole-draft-angle-low 'simpleb-hole-draft-angle-lower-limit
		          :hole-draft-angle-high 'simpleb-hole-draft-angle-upper-limit
		          :hole-blend-radius 'simpleb-hole-blend-radius
		          :hole-blend-radius-low 'simpleb-hole-blend-radius-lower-limit
		          :hole-blend-radius-high 'simpleb-hole-blend-radius-upper-limit
		          :boss-dia 'simpleb-boss-diameter
		          :boss-dia-low 'simpleb-boss-diameter-lower-limit
		          :boss-dia-high 'simpleb-boss-diameter-upper-limit
		          :boss-height 'simpleb-boss-height
		          :boss-height-low 'simpleb-boss-height-lower-limit
		          :boss-height-high 'simpleb-boss-height-upper-limit
		          :boss-draft-angle 'simpleb-boss-draft-angle
		          :boss-draft-angle-low 'simpleb-boss-draft-angle-lower-limit
		          :boss-draft-angle-high 'simpleb-boss-draft-angle-upper-limit
		          :boss-blend-radius 'simpleb-boss-blend-radius
		          :boss-blend-radius-low 'simpleb-boss-blend-radius-lower-limit
		          :boss-blend-radius-high 'simpleb-boss-blend-radius-upper-limit
		          )))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                      Original Formula Definition
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;; Hole Bottom Thickness ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun simpleb-bottom-thickness(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :bottom-thickness)))
    (unless ref-wall-thick
      (return-from simpleb-bottom-thickness nil))
    (setq val (* 0.6 ref-wall-thick))
    (list :bottom-thickness val) 
    ))
;; calculate lower limit: 
(defun simpleb-bottom-thickness-lower-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :bottom-thickness-low)))
    (unless ref-wall-thick
      (return-from simpleb-bottom-thickness-lower-limit nil))
    (setq val (* 0.5 ref-wall-thick))
    (list :bottom-thickness-low val) 
    ))
;; calculate upper limit: 
(defun simpleb-bottom-thickness-upper-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :bottom-thickness-high)))
    (unless ref-wall-thick
      (return-from simpleb-bottom-thickness-upper-limit nil))
    (setq val (* 0.7 ref-wall-thick))
    (list :bottom-thickness-high val) 
    ))

;;;;;;;;;;;;;;;;;;;;;;; Hole Draft Angle ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun simpleb-hole-draft-angle(known-args &aux val)
  (setq val (sd-deg-to-rad 0.5))
  (list :hole-draft-angle val) 
  )
;; calculate lower limit: 
(defun simpleb-hole-draft-angle-lower-limit(known-args &aux val)
  (setq val (sd-deg-to-rad 0.25))
  (list :hole-draft-angle-low val) 
  )
;; calculate upper limit: 
(defun simpleb-hole-draft-angle-upper-limit(known-args &aux val)
  (setq val (sd-deg-to-rad 10.0))
  (list :hole-draft-angle-high val) 
  )

;;;;;;;;;;;;;;;;;;;;;;; Hole Blend Radius ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun simpleb-hole-blend-radius(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :hole-blend-radius))
	(hole-dia (inq-known-variable known-args :inq-var :hole-dia 
		                                 :calc-var :boss-dia)))
    (unless (and ref-wall-thick hole-dia)
      (return-from simpleb-hole-blend-radius nil))
    (setq val (* 0.25 ref-wall-thick))
    (unless (< val (* hole-dia 0.5))          ;; when hole-blend-radius >= hole-radius
      (setq val (* hole-dia 0.4)))
    (list :hole-blend-radius val) 
    ))
;; calculate lower limit: 
(defun simpleb-hole-blend-radius-lower-limit(known-args &aux val)
  (setq val 0.25)
  (list :hole-blend-radius-low val) 
  )
;; calculate upper limit: 
(defun simpleb-hole-blend-radius-upper-limit(known-args &aux val)
  (setq val 1.6)
  (list :hole-blend-radius-high val) 
  )

;;;;;;;;;;;;;;;;;;;;;;; Boss Diameter ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun simpleb-boss-diameter(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :boss-dia))
	(hole-dia (inq-known-variable known-args :inq-var :hole-dia 
		                                 :calc-var :boss-dia)))
    (unless (and ref-wall-thick hole-dia)
      (return-from simpleb-boss-diameter nil))
    (setq val (+ (* 1.4 ref-wall-thick) hole-dia))
    (list :boss-dia val) 
    ))
;; calculate lower limit: 
(defun simpleb-boss-diameter-lower-limit(known-args &aux val)
  (setq val 8.75)
  (list :boss-dia-low val) 
  )
;; calculate upper limit: 
(defun simpleb-boss-diameter-upper-limit(known-args &aux val)
  (setq val -1.0)
  (list :boss-dia-high val) 
  )

;;;;;;;;;;;;;;;;;;;;;;; Boss Height ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun simpleb-boss-height(known-args &aux val)
  (let ((boss-dia (inq-known-variable known-args :inq-var :boss-dia 
                                                 :calc-var :boss-height)))
    (unless boss-dia
      (return-from simpleb-boss-height nil))
    (setq val (* 2.5 boss-dia))
    (list :boss-height val) 
    ))
;; calculate lower limit: 
(defun simpleb-boss-height-lower-limit(known-args &aux val)
  (let ((boss-dia (inq-known-variable known-args :inq-var :boss-dia 
                                                 :calc-var :boss-height-low)))
    (unless boss-dia
      (return-from simpleb-boss-height-lower-limit nil))
    (setq val (* 2.5 boss-dia))
    (list :boss-height-low val) 
    ))
;; calculate upper limit: 
(defun simpleb-boss-height-upper-limit(known-args &aux val)
  (let ((boss-dia (inq-known-variable known-args :inq-var :boss-dia 
                                                 :calc-var :boss-height-high)))
    (unless boss-dia
      (return-from simpleb-boss-height-upper-limit nil))
    (setq val (* 3.0 boss-dia))
    (list :boss-height-high val) 
    ))

;;;;;;;;;;;;;;;;;;;;;;; Boss Draft Angle ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun simpleb-boss-draft-angle(known-args &aux val)
  (setq val (sd-deg-to-rad 0.5))
  (list :boss-draft-angle val) 
  )
;; calculate lower limit: 
(defun simpleb-boss-draft-angle-lower-limit(known-args &aux val)
  (setq val (sd-deg-to-rad 0.5))
  (list :boss-draft-angle-low val) 
  )
;; calculate upper limit: 
(defun simpleb-boss-draft-angle-upper-limit(known-args &aux val)
  (setq val (sd-deg-to-rad 10.0))
  (list :boss-draft-angle-high val) 
  )

;;;;;;;;;;;;;;;;;;;;;;; Boss Blend Radius ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun simpleb-boss-blend-radius(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :boss-blend-radius)))
    (unless ref-wall-thick
      (return-from simpleb-boss-blend-radius nil))
    (setq val (* 0.25 ref-wall-thick))
    (list :boss-blend-radius val) 
    ))
;; calculate lower limit: 
(defun simpleb-boss-blend-radius-lower-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :boss-blend-radius-low)))
    (unless ref-wall-thick
      (return-from simpleb-boss-blend-radius-lower-limit nil))
    (setq val (* 0.12 ref-wall-thick))
    (list :boss-blend-radius-low val) 
    ))
;; calculate upper limit: 
(defun simpleb-boss-blend-radius-upper-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
		                                       :calc-var :boss-blend-radius-high)))
    (unless ref-wall-thick
      (return-from simpleb-boss-blend-radius-upper-limit nil))
    (setq val (* 0.5 ref-wall-thick))
    (list :boss-blend-radius-high val) 
    ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Registration of User Defined Formula Sets for Simple Boss
;;
;; A user defined formula set for calculating the parameters of the Simple 
;; Feature will be regisetered here
;; 
;; What is needed to be done by the user?
;; ======================================
;;
;; 1. Register the new formula set by invoking register-a-function-set() and 
;;    make the following specifications:
;;      :regi-keyword    a key word for the new formula set
;;      :regi-list       name of a global list which contains defined formula 
;;                       sets for calculating the parameters of a plastic feature.
;;                       (!) For creating Simple Boss Feature the user is only 
;;                       allowed to specify it as *simple-boss-formula-list* (it
;;                       is defined above in this file, each different plastic 
;;                       feature will have itsown such gloable list)
;;      :redef-formulas  a p-list which contains the parameter key word and 
;;                       the new defined function to calculate the parameter
;;                       (the parameter keywords could be seen above when the 
;;                       *simple-boss-formula-list* is defined and the meaning of 
;;                       the parameters is shown in the feature dialog) 
;;      :as-default      boolean value type to specify if the new formula set is 
;;                       desired to be the default formula set
;;
;; 2. Define the new functions for calculating the feature parameters. The
;;    following function could be used for the definition:
;;
;;    - function inq-known-variable(known-args &key inq-var calc-var)
;;
;;      checks if a input parameter used for calculating the variable has been 
;;      defined. known-args is a p-list which is passed in originally by the  
;;      feature dialog and contains the parameters needed for the calculation,
;;      inq-var gives the keyword of the parameter being checked, and calc-var 
;;      gives the variable to be calculated. If check fails, a error message will 
;;      be shown in SolidDesigner, otherwise the function returns the parameter value
;; 
;;
;; Example:
;; ========
;;
;(register-a-function-set :regi-keyword :my-formulas 
;                         :regi-list *simple-boss-formula-list*
;                         :redef-formulas (list :bottom-thickness 'my-simpleb-bottom-thickness
;				               :boss-height 'my-simpleb-boss-height)
;                         :as-default t
;  )
;
;(defun my-simpleb-bottom-thickness(known-args &aux val)
;  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick 
;		                                       :calc-var :bottom-thickness)))
;    (unless ref-wall-thick
;      (return-from my-simpleb-bottom-thickness nil))
;    (setq val (* 0.7 ref-wall-thick))
;    (list :bottom-thickness val) 
;    ))
; 
;(defun my-simpleb-boss-height(known-args &aux val)
;  (let ((boss-dia (inq-known-variable known-args :inq-var :boss-dia 
;                                                 :calc-var :boss-height)))
;    (unless boss-dia
;      (return-from my-simpleb-boss-height nil))
;    (setq val (* 2.8 boss-dia))
;    (list :boss-height val) 
;    ))
;
