;; see  Problem mit mutual-exclusion 
;;      http://ww3.cad.de/foren/ubb/Forum92/HTML/000475.shtml

(in-package  :custom)
(use-package :OLI)

(sd-defdialog 'pw_vp_dokumentation
	:dialog-title "Ansicht fuer Dokumentation"
	:variables
	'( 	
		(VIEWPORT1	:range ("dummy")
					:initial-value (if (equal (sd-get-current-application) "Annotation")
                                      (progn 
                                        (sd-set-range 'VIEWPORT1 '("3D VP"))
                                        (DOCU::SHOW-3DVP) ; see am_avail_cmds.cmd -> "Aux 3D Viewport"
                                        "3D VP")
                                       ;; else       
                                      (progn
                                        (sd-set-range 'VIEWPORT1 (sd-inq-vp-names))
                                        (sd-inq-current-vp))
                                      )
					:title "Viewport:"
					:prompt-text "Viewport angeben."
		)
		("Winkelvoreinstellung")
		(ANGX_15_30	:title "X, L15, R30"
					:value-type :grouped-boolean
					:size :half
		)
		(ANGX_30_15	:title "X, L30, R15"
					:value-type :grouped-boolean
					:size :half
		)
		(ANGY_15_30	:title "Y, L15, R30"
					:value-type :grouped-boolean
					:size :half
					:initial-value t
		)
		(ANGY_30_15	:title "Y, L30, R15"
					:value-type :grouped-boolean
					:size :half
		)
		(ANGZ_15_30	:title "Z, L15, R30"
					:value-type :grouped-boolean
					:size :half
		)
		(ANGZ_30_15	:title "Z, L30, R15"
					:value-type :grouped-boolean
					:size :half
		)
		("Achsrichtung vertikal")
		(XP_DIR		:title "+X"
					:value-type :grouped-boolean
					:size :half
		)
		(XN_DIR		:title "-X"
					:value-type :grouped-boolean
					:size :half
		)
		(YP_DIR		:title "+Y"
					:value-type :grouped-boolean
					:size :half
					:initial-value t
		)
		(YN_DIR		:title "-Y"
					:value-type :grouped-boolean
					:size :half
		)
		(ZP_DIR		:title "+Z"
					:value-type :grouped-boolean
					:size :half
		)
		(ZN_DIR		:title "-Z"
					:value-type :grouped-boolean
					:size :half
		)
		("Richtung der Ansicht")
		(X_DIR_NEG  :value-type :boolean
					:title "X-Richtungskomponente umkehren"
					:toggle-type :wide-toggle
		)
		(Y_DIR_NEG	:value-type :boolean
					:title "Y-Richtungskomponente umkehren"
					:toggle-type :wide-toggle
		)
		(Z_DIR_NEG  :value-type :boolean
					:title "Z-Richtungskomponente umkehren"
					:toggle-type :wide-toggle
		)
		("GÏltige Kombinationen:")
		(KOMBX1		:title " X1"
					:toggle-type :grouped-toggle
					:push-action
						(progn 
                            (setq-all-nil-workaround)
							(setq ANGX_15_30 t)
							(setq XP_DIR t)
						)
		)
		(KOMBX2		:title " X2"
					:toggle-type :grouped-toggle
					:push-action
						(progn 
                            (setq-all-nil-workaround)
							(setf ANGX_30_15 t)
							(setf XP_DIR t)
						)
		)
		(KOMBY1		:title " Y1"
					:toggle-type :grouped-toggle
					:push-action
						(progn 
                            (setq-all-nil-workaround)
							(setq ANGY_15_30 t)
							(setq YP_DIR t)
						)
		)
		(KOMBY2		:title " Y2"
					:toggle-type :grouped-toggle
					:push-action
						(progn 
                            (setq-all-nil-workaround)
							(setq ANGY_30_15 t)
							(setq YP_DIR t)
						)
					
		)
		(KOMBZ1		:title " Z1"
					:toggle-type :grouped-toggle
					:push-action
						(progn 
                            (setq-all-nil-workaround)
							(sd-set-variable-status 'ANGZ_15_30 :value t)
							(sd-set-variable-status 'ZP_DIR :value t)
						)
		)
		(KOMBZ2		:title " Z2"
					:toggle-type :grouped-toggle
					:push-action
						(progn 
                            (setq-all-nil-workaround)
							(sd-set-variable-status 'ANGZ_30_15 :value t)
							(sd-set-variable-status 'ZP_DIR :value t)
						)
		)
		("-")
		(START
			:title "Ûbernehmen"
			:push-action (pw_vp_dokumentation_go)		
		)
	);end variables
	:mutual-exclusion '(
						(XP_DIR XN_DIR YP_DIR YN_DIR ZP_DIR ZN_DIR)
						(ANGX_15_30 ANGX_30_15 ANGY_15_30 ANGY_30_15 ANGZ_15_30 ANGZ_30_15)
						)

	:local-functions
		'((setq-all-nil-workaround ()
		   (setq XP_DIR nil XN_DIR nil YP_DIR nil
                 YN_DIR nil ZP_DIR nil ZN_DIR nil)
		   (setq ANGX_15_30 nil ANGX_30_15 nil ANGY_15_30 nil 
                 ANGY_30_15 nil ANGZ_15_30 nil ANGZ_30_15 nil)
           )      
          (pw_vp_dokumentation_go ()
			(let (VP_COORDINATES_X X_BASE X_DIRECTION
				  VP_COORDINATES_Y Y_BASE Y_DIRECTION
				  VP_COORDINATES_Z Z_BASE Z_DIRECTION
                  AXIS_UP VP_CAMERA)
				(cond
                  (ANGX_15_30
                    (setq X_BASE -3.9098095
						  Y_BASE -7.5532243
 						  Z_BASE -5.1456069))
         		  (ANGX_30_15
                    (setq X_BASE -3.9098095
                          Y_BASE -5.1456069
                          Z_BASE -7.5532243))
                  (ANGY_15_30
                    (setq X_BASE -5.1456069
                          Y_BASE -3.9098095
                          Z_BASE -7.5532243))
                  (ANGY_30_15
					(setq X_BASE -7.5532243
                          Y_BASE -3.9098095
                          Z_BASE -5.1456069))
                  (ANGZ_15_30
					(setq X_BASE -7.5532243
                          Y_BASE -5.1456069
                          Z_BASE -3.9098095))
                  (ANGZ_30_15
					(setq X_BASE -5.1456069
                          Y_BASE -7.5532243
                          Z_BASE -3.9098095))
				) ;; end cond ANG._._.
                
                (setq AXIS_UP
                  (cond 
                    (XP_DIR (gpnt3d  1 0  0))
                    (XN_DIR (gpnt3d -1 0  0))
                    (YP_DIR (gpnt3d 0  1  0))
                    (YN_DIR (gpnt3d 0 -1  0))
                    (ZP_DIR (gpnt3d 0  0  1))
                    (ZN_DIR (gpnt3d 0  0 -1))
                    (T      (gpnt3d  1 0  0))  ;; fall back
                    )  ;; end cond .._DIR
				)

				(setq X_DIRECTION (if X_DIR_NEG -1 1))
				(setq Y_DIRECTION (if Y_DIR_NEG -1 1))
				(setq Z_DIRECTION (if Z_DIR_NEG -1 1))

				(setq VP_COORDINATES_X (* X_BASE X_DIRECTION))
				(setq VP_COORDINATES_Y (* Y_BASE Y_DIRECTION))
				(setq VP_COORDINATES_Z (* Z_BASE Z_DIRECTION))

                ;; now apply these directions.. 
				(setq VP_CAMERA
					(OLI::make-sd-vp-camera-struct
						:from-pt (gpnt3d 0 0 0)
						:to-pt (gpnt3d VP_COORDINATES_X VP_COORDINATES_Y VP_COORDINATES_Z)
						:up-dir AXIS_UP
						:back-clip 0
						:front-clip 0
						:projection 1
						:view-field 4
					)
				)
				(sd-call-cmds
					(sd-set-vp-camera VIEWPORT1 VP_CAMERA :smooth nil)
				)
				(sd-call-cmds
					(update_screen)
				)
				(FIT_VP VIEWPORT1)
			)
		) ;end progn
	) ;pw_vp_montageanleitungen_go
	:ok-action 
	'()
) ;end sd-dialog