; -*-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Description:  MoldDesignAdvisor Customization: Functions for calculating the 
;;               Solid Boss variables 
;; Language:     Lisp
;; Package:      MOLD-UI
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(in-package :mold-ui)
(use-package '(:oli :frame2))

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

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

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

;;;;;;;;;;;;;;;;;;;;;;; Boss Height ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun solidb-boss-height(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :boss-height)))
    (unless ref-wall-thick
      (return-from solidb-boss-height nil))
    (setq val (* 1.5 ref-wall-thick))
    (list :boss-height val) 
    ))
;; calculate lower limit: 
(defun solidb-boss-height-lower-limit(known-args &aux val)
  (setq val 0.0)
  (list :boss-height-low val) 
  )
;; calculate upper limit: 
(defun solidb-boss-height-upper-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :boss-height-high)))
    (unless ref-wall-thick
      (return-from solidb-boss-height-upper-limit nil))
    (setq val (* 3.0 ref-wall-thick))
    (list :boss-height-high val) 
    ))

;;;;;;;;;;;;;;;;;;;;;;; Boss Draft Angle ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun solidb-boss-draft-angle(known-args &aux val)
  (setq val (sd-deg-to-rad 2.0))
  (list :boss-draft-angle val) 
  )
;; calculate lower limit: 
(defun solidb-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 solidb-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 solidb-boss-blend-radius(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :boss-height-high)))
    (unless ref-wall-thick
      (return-from solidb-boss-blend-radius nil))
    (setq val (* 0.25 ref-wall-thick))
    (list :boss-blend-radius val) 
    ))
;; calculate lower limit: 
(defun solidb-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 solidb-boss-blend-radius-lower-limit nil))
    (setq val (* 0.12 ref-wall-thick))
    (list :boss-blend-radius-low val) 
    ))
;; calculate upper limit: 
(defun solidb-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 solidb-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
;;
;; A user defined formula set for calculating the parameters of the Solid 
;; 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 Solid Boss Feature the user is only 
;;                       allowed to specify it as *solid-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 
;;                       *solid-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 *solid-boss-formula-list*
;                         :redef-formulas (list :boss-height 'my-solidb-boss-height
;                                               :boss-blend-radius 'my-solidb-boss-blend-radius)
;                         :as-default t
;  )
;
;(defun my-solidb-boss-height(known-args &aux val)
;  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
;			                               :calc-var :boss-height)))
;    (unless ref-wall-thick
;      (return-from solidb-boss-height nil))
;    (setq val (* 2.5 ref-wall-thick))
;    (list :boss-height val) 
;    ))
;
;(defun my-solidb-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 solidb-boss-blend-radius nil))
;   (setq val (* 0.4 ref-wall-thick))
;   (list :boss-blend-radius val) 
;   ))
;
