#|

Auto Loft

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)

;; you can use the file:    wpset.dat   as example data file 

(sd-defdialog 'auto_loft
  :dialog-title "Auto Loft"
  :variables
  '(;;- - - - - local variables - - - - -
    (+RETURN-STATE+ :initial-value (sd-set-model-checkpoint))
    (+MATCH-VERTICES+ :initial-value nil)
    (+WPSET-PATH+ :initial-value nil)
    ;;- - - - - visible variables - - - - -
    (LOFT_PART :value-type :part-incl-new
	       :title "Part")
    (FILENAME :value-type :filename
	      :direction :input
	      :after-input (create-loft-wpset)))
  :local-functions
  '((create-loft-wpset ()
      (let ((file-stream (open (first filename) :direction :input))
	    (wpset-name (sd-gen-obj-basename :wpset :prefix "auto_loft"))
	    origin normal u-dir pnt_1 pnt_2
	    (vertex nil))
	
	(setq +wpset-path+ (sd-pathlist-to-pathname (list "/" wpset-name)))
	;; create loft wpset:
	(sd-call-cmds (create_wpset :name wpset-name))

	;; read data file:
	(loop
	 ;; 1. wp origin
	 (setq origin (read file-stream nil 'eof))
	 (when (eq origin 'eof) (return))

	 ;; 2. wp normal
	 (setq normal (read file-stream nil 'eof))
	 (when (eq normal 'eof) (return))
	 
	 ;; 3. wp u direction
	 (setq u-dir (read file-stream nil 'eof))
	 (when (eq u-dir 'eof) (return))

	 ;; 4. first rectangle point (taken as match vertex)
	 (setq pnt_1 (read file-stream nil 'eof))
	 (when (eq pnt_1 'eof) (return))

	 ;; 5. second rectangle point
	 (setq pnt_2 (read file-stream nil 'eof))
	 (when (eq pnt_2 'eof) (return))

	 ;; create workplane:
	 (sd-call-cmds (create_workplane :new 
					 :owner +wpset-path+
					 :pt_dir :origin origin
					 :normal normal
					 :u_dir u-dir))

	 ;; create rectangle:
	 (sd-call-cmds (rectangle pnt_1 pnt_2))

	 ;; remember match vertex on list:
	 (setq vertex (first (sd-call-cmds 
			      (get_selection 
			       :focus_type *sd-match-vertex-2d-seltype*
			       :select pnt_1))))
	 (when (sel_item-p vertex)
	   (push vertex +match-vertices+))
	 )
	(close file-stream)

	(when +match-vertices+
	  (sd-call-cmds (apply #'create_match_line (nreverse +match-vertices+))))
	)
      )
    )
  :ok-action '(sd-call-cmds (add_loft :part loft_part :tool +wpset-path+))
  :cancel-action '(sd-return-to-model-checkpoint +return-state+)
  :exception '(sd-return-to-model-checkpoint +return-state+))

;;