#|

Licenced Slot User Action

Note: This is an example file showing concepts of the Integration Kit. The code itself is not supported and will never be supported.
|#
(in-package :examples)
(use-package :oli)

;; define a new license module:
(sd-define-licensing-module "VARNAME-ADD-ON-MODULE-1" ;; module name
			    "VAR1XYZ!"   ;; partner key
			    "EXAMPLES")  ;; product key

;; create activation function which will be called, when module is activated:
(defun varname-add-on-module-1-activation-function (&rest args)
  (declare (ignore args))
  (sd-enable-button "TOOLBOX-COMMANDS-SLOT-TB"))

;; subscribe function to activation event:
(sd-subscribe-event (sd-module-activation-event "VARNAME-ADD-ON-MODULE-1")
		    #'varname-add-on-module-1-activation-function)


(sd-defdialog 'slot
  :dialog-control :sequential-loop
  :module "VARNAME-ADD-ON-MODULE-1"
  :precondition '(let ((curr-wp (sd-inq-curr-wp)))
		   (when (and curr-wp 
			      (not (sd-inq-obj-contents-read-only-p curr-wp)))
		     :ok))
  :variables
  '((FBACK)
    (POINT_1 :value-type :point-2d
	     :prompt-text "Pick first center point."
	     :after-input (progn
			    (setq fback (sd-start-rubberline-feedback point_1))
			    (sd-call-cmds (c_point point_1))))
    (POINT_2 :value-type :point-2d
	     :prompt-text "Pick second center point."
	     :after-input (progn
			    (sd-end-feedback fback)
			    (sd-call-cmds (c_point point_2))))
    (WIDTH :value-type :positive-length
	   :prompt-text "Specify width of slot."
	   :after-input 
	   (progn
	     (sd-call-cmds (delete_2d :c_user_point :all_2d))
	     (sd-end-feedback fback)
	     (sd-call-cmds (create-slot point_1 point_2 width))
	     (setq point_1 nil)
	     (setq point_2 nil))))
  :cleanup-action
  '(progn
     (sd-end-feedback fback)
     (when point_1
       (sd-call-cmds (delete_2d :c_user_point point_1)))
     (when point_2
       (sd-call-cmds (delete_2d :c_user_point point_2)))))


(defun create-slot (p1 p2 width)
  (let (p1p2
	normal
	offset-vec)
    
    ;;----- Calculation: -----
    (setq p1p2 (sd-vec-subtract p2 p1))
    
    ;; Calculate Normal of p1p2 vector:
    (setq normal (make-gpnt2d :x (- (gpnt2d_y p1p2)) :y (gpnt2d_x p1p2)))
     
    ;; Normalize Normal:
    (setq normal (sd-vec-normalize normal))
     
    ;; Calculate Offset Vector:
    (setq offset-vec (sd-vec-scale normal (/ width 2.0)))
     
    ;; Create infinite construction lines through the center points:
    (c_line_inf :two_points p1 p2)
    (c_line_inf :two_points p1 (sd-vec-add p1 offset-vec))
    (c_line_inf :two_points p2 (sd-vec-add p2 offset-vec))
     
    ;; Create Slot:
    (line :two_points (sd-vec-add p1 offset-vec) (sd-vec-add p2 offset-vec))
    (line :two_points (sd-vec-subtract p1 offset-vec)
	              (sd-vec-subtract p2 offset-vec))
    (arc :center p1 (sd-vec-add p1 offset-vec)
		    (sd-vec-subtract p1 offset-vec))
    (arc :center p2 (sd-vec-subtract p2 offset-vec)
	            (sd-vec-add p2 offset-vec))))


;; disable Slot Toolbox button, because module is not activated yet.
;; button will be enabled as soon as the module is activated

(sd-disable-button "TOOLBOX-COMMANDS-SLOT-TB")

;; To activate the  module, the following command has to be called
;; either immediately or when needed
;;
;; (sd-module-activate "VARNAME-ADD-ON-MODULE-1")

;