Do nothing on Cancel (Dialog Box)

Do nothing on Cancel (Dialog Box)

pmercader
Advocate Advocate
2,073 Views
6 Replies
Message 1 of 7

Do nothing on Cancel (Dialog Box)

pmercader
Advocate
Advocate

Hello,

 

I have incorporated a dialog box on a lisp this community has helped me create, I added this dialog box on an insert lisp but I am having trouble with canceling out from the dialog box. 

 

Initially when I click on cancel, what the lisp does is just carry on the previous action.

 

ie:

 

If I click "ok", it inserts the block called for.

then if I call the lisp again and this time click "cancel" the lisp just inserts the previous block inserted.

 

So I thought to add an "(exit)" in the cancel action_tile like so

 

(action_tile
    "cancel"
    "(done_dialog)
	 (setq result nil)
	 (exit)
	"
  

it does the job of not carrying out the previous action but I get this error

 

 

; error: quit / exit abort

 

I can work with that but I dont want it to be an "error".

 

How can I fix this? Below is the DCL and the Lisp.

 

DCL :

inputbox : dialog
		{

	label = "Block Lookup Tool";
	width = 50;

	: text { key = "prompt"; }
	: edit_box { key = "ebox1"; allow_accept = true; }

	ok_cancel;
     		 
		}

 

LISP :

(defun inputbox (prompt title default)
  
  (setq dcl_id (load_dialog "inputbox.dcl"))
  (if (not (new_dialog "inputbox" dcl_id))
    (exit)
  )

  (set_tile "prompt" prompt)
  (set_tile "title" title)
  (set_tile "ebox1" default)
  (mode_tile "ebox1" 2)
  
  (action_tile
    "accept"
    "(setq inputvalue (get_tile \"ebox1\"))
     (done_dialog)
     (setq result T)"
  )
  (action_tile
    "cancel"
    "(done_dialog)
	 (setq result nil)
	 (exit)
	"
  

  )
  (start_dialog)
  (unload_dialog dcl_id)
  (princ)
  
)
 
(princ)

 

 

 

0 Likes
2,074 Views
6 Replies
Replies (6)
Message 2 of 7

dlanorh
Advisor
Advisor

@pmercader wrote:

Hello,

 

I have incorporated a dialog box on a lisp this community has helped me create, I added this dialog box on an insert lisp but I am having trouble with canceling out from the dialog box. 

 

Initially when I click on cancel, what the lisp does is just carry on the previous action.

 

ie:

 

If I click "ok", it inserts the block called for.

then if I call the lisp again and this time click "cancel" the lisp just inserts the previous block inserted.

 

So I thought to add an "(exit)" in the cancel action_tile like so

 

(action_tile
    "cancel"
    "(done_dialog)
	 (setq result nil)
	 (exit)
	"
  

it does the job of not carrying out the previous action but I get this error

 

 

; error: quit / exit abort

 

I can work with that but I dont want it to be an "error".

 

How can I fix this? Below is the DCL and the Lisp.

 

DCL :

inputbox : dialog
		{

	label = "Block Lookup Tool";
	width = 50;

	: text { key = "prompt"; }
	: edit_box { key = "ebox1"; allow_accept = true; }

	ok_cancel;
     		 
		}

 

LISP :

(defun inputbox (prompt title default)
  
  (setq dcl_id (load_dialog "inputbox.dcl"))
  (if (not (new_dialog "inputbox" dcl_id))
    (exit)
  )

  (set_tile "prompt" prompt)
  (set_tile "title" title)
  (set_tile "ebox1" default)
  (mode_tile "ebox1" 2)
  
  (action_tile "accept" "(progn (setq inputvalue (get_tile \"ebox1\")) (done_dialog 1))")
  (action_tile "cancel" "(done_dialog 0)")  

  (setq x_val (start_dialog))
(unload_dialog dcl_id) (if (= x_val 1) (setq result T) (setq result nil)) )

 

 

 


Try this. If called like so (setq rtn (inputbox "prompt"  "title" "default")) the input box function will return T or nil which will be set in variable rtn (rtn = T or nil). You will need to make x_val a local variable.

I am not one of the robots you're looking for

0 Likes
Message 3 of 7

pmercader
Advocate
Advocate

Man, as usual - here comes dlanorh to the rescue. You are my autolisp hero. 😛 

 

I am in a middle of some work here but when I get a chance I will test this out.

 

Thanks again!

0 Likes
Message 4 of 7

scot-65
Advisor
Advisor
Two items...

When you make a call to done_dialog, nothing is evaluated afterwards.
I am (now) surprised that it does. However, follow this fundamental
principal when using done_dialog inside an action_tile. It makes
reading the program easier...

Example:
(action_tile "accept" "(GET_All_Dialog_Tile_States_Here)(done_dialog 1)")
(action_tile "cancel" "(done_dialog 0)")
;|
Let's take advantage of the optional return value of done_dialog to
determine weather to continue on with the program. The return value
is sent to start_dialog.
|;
(setq sd (start_dialog))
(if dcl_id (setq dcl_id (unload_dialog dcl_id)))
;|
Now that you have cleanly exited the user input section of your code,
determine the state of "sd" for the next section - "Execute" - of your code:
|;
(if (and sd (> sd 0))
(progn
[in your case, command INSERT goes here]
);progn
);if

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 5 of 7

pmercader
Advocate
Advocate

I tried doing dlanorh's advice but when clicking cancel it does go through with the insert. 😞 Maybe if i give more details you guys can help me better.

 

Here is a flow chart of what I want the lisp to do and attached is a zip file of the lisp and dcl file. 

 

Apologies if have butchered the lisp, coding isnt really my field but I got it to do what i want except for the "click cancel" part.

 

Lisp Flow ChartLisp Flow Chart

0 Likes
Message 6 of 7

Sea-Haven
Mentor
Mentor

The Ok and Cancel key can be checked need to look up my 2012 book, the Key of the  "Cancel" button is "cancel" so you can check the value of this key.

 

Look into the dcl programming help.

 

something like this

(action_tile "accept" "(done_dialog)")

(action_tile "cancel"  "(setq optout "Yes" )   (done_dialog)")

0 Likes
Message 7 of 7

_gile
Consultant
Consultant

Hi,

 

Typically, we use modal dialog boxes (the only ones availble with DCL) to get user input(s).

It is a good practice to separate concerns an the dialog box should interact with the calling function the same way as other input ( getXXX) function, i.e. just return a value (or a list or values) if the user clicked oK, or nil if the user cancelled.

Handling the done_dialog value returned by start_dialog is a good way to get the "dialog result" which works whatever the dialog box.

 

Here's a little example with an input box which can be called as a getXXX function:

(if (setq val (inputbox "Hello" "Enter your first name:" ""))
  (alert (strcat "Hello " val "!"))
)
(defun inputbox	(title msg val / temp file dcl_id result)

  ;; Create a temporary DCL file
  (setq	temp (vl-filename-mktemp "Tmp.dcl")
	file (open temp "w")
	ret  ""
  )

  ;; Write the file
  (write-line
      "InputBox:dialog{
          key = \"title\"; initial_focus = \"val\";
          spacer;
          :text{
              key = \"msg\";}
          spacer;
          :edit_box{
              key = \"val\"; edit_width = 36; allow_accept = true;}
          spacer;
          ok_cancel;}"
    file
  )
  (close file)

  ;; Open the dialog title
  (setq dcl_id (load_dialog temp))
  (if (not (new_dialog "InputBox" dcl_id))
    (exit)
  )
  (set_tile "title" title)
  (set_tile "msg" msg)
  (set_tile "val" val)
  (action_tile "val" "(setq val $value)")
  (action_tile "accept" "(done_dialog 1)")
  (action_tile "cancel" "(done_dialog 0)")
  (setq result (start_dialog))
  (unload_dialog dcl_id)

  ;; Delete the file
  (vl-file-delete temp)

  ;; Return the value if user clicked OK; else, nil
  (if (= 1 result)
    val
  )
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes