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

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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;         Formulas originally used for the Gusett calculation
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defparameter *gusset-formula-list* 
    (list :default-formular-group :original
          :original (list :regi-name *original-group*
		          :gusset-thickness 'gusset-thickness
		          :gusset-thick-low 'gusset-thick-lower-limit
		          :gusset-thick-high 'gusset-thick-upper-limit
		          :gusset-width 'gusset-width
		          :gusset-width-low 'gusset-width-lower-limit
		          :gusset-width-high 'gusset-width-upper-limit
		          :gusset-height 'gusset-height
		          :gusset-height-low 'gusset-height-lower-limit
		          :gusset-height-high 'gusset-height-upper-limit
		          :gusset-blend 'gusset-blend
		          :gusset-blend-low 'gusset-blend-lower-limit
		          :gusset-blend-high 'gusset-blend-upper-limit
		          )))

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

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

;;;;;;;;;;;;;;;;;;;;;;; Gusset Width ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun gusset-width(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :gusset-width)))
    (unless ref-wall-thick
      (return-from gusset-width nil))
    (setq val (* 2.0 ref-wall-thick))
    (list :gusset-width val) 
    ))
;; calculate lower limit: 
(defun gusset-width-lower-limit(known-args &aux val)
  (let ((gusset-height (inq-known-variable known-args :inq-var :gusset-height
			                              :calc-var :gusset-width-low)))
    (unless gusset-height
      (return-from gusset-width-lower-limit nil))
    (setq val (* 0.3 gusset-height))
    (list :gusset-width-low val) 
    ))
;; calculate upper limit: 
(defun gusset-width-upper-limit(known-args &aux val)
  (let ((gusset-height (inq-known-variable known-args :inq-var :gusset-height
			                              :calc-var :gusset-width-high)))
    (unless gusset-height
      (return-from gusset-width-upper-limit nil))
    (setq val gusset-height)
    (list :gusset-width-high val) 
    ))

;;;;;;;;;;;;;;;;;;;;;;; Gusset Height ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun gusset-height(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :gusset-height)))
    (unless ref-wall-thick
      (return-from gusset-height nil))
    (setq val (* 2.5 ref-wall-thick))
    (list :gusset-height val) 
    ))
;; calculate lower limit: 
(defun gusset-height-lower-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :gusset-height-low)))
    (unless ref-wall-thick
      (return-from gusset-height-lower-limit nil))
    (setq val ref-wall-thick)
    (list :gusset-height-low val) 
    ))
;; calculate upper limit: 
(defun gusset-height-upper-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :gusset-height-high)))
    (unless ref-wall-thick
      (return-from gusset-height-upper-limit nil))
    (setq val (* 4.0 ref-wall-thick))
    (list :gusset-height-high val) 
    ))

;;;;;;;;;;;;;;;;;;;;;;; Gusset Blend Radius ;;;;;;;;;;;;;;;;;;;;;;
;;
;; calculate value 
(defun gusset-blend(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :gusset-blend)))
    (unless ref-wall-thick
      (return-from gusset-blend nil))
    (setq val (* 0.4 ref-wall-thick))
    (list :gusset-blend val) 
    ))
;; calculate lower limit: 
(defun gusset-blend-lower-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :gusset-blend-low)))
    (unless ref-wall-thick
      (return-from gusset-blend-lower-limit nil))
    (setq val (* 0.25 ref-wall-thick))
    (list :gusset-blend-low val) 
    ))
;; calculate upper limit: 
(defun gusset-blend-upper-limit(known-args &aux val)
  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
			                               :calc-var :gusset-blend-high)))
    (unless ref-wall-thick
      (return-from gusset-blend-upper-limit nil))
    (setq val (* 0.6 ref-wall-thick))
    (list :gusset-blend-high val) 
    ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Registration of User Defined Formula Sets
;;
;; A user defined formula set for calculating the parameters of the Gusset 
;; 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 Gusett Feature the user is only 
;;                       allowed to specify it as *gusett-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 
;;                       *gusett-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 function 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 *gusset-formula-list*
;;                         :redef-formulas (list :gusset-width 'my-gusset-width)
;;                         :as-default t
;;  )
;;
;;(defun my-gusset-width(known-args &aux val)
;;  (let ((ref-wall-thick (inq-known-variable known-args :inq-var :ref-wall-thick
;;			                               :calc-var :gusset-width)))
;;    (unless ref-wall-thick
;;      (return-from gusset-width nil))
;;    (setq val ref-wall-thick)
;;    (list :gusset-width val) 
;;    ))
