Author Topic: RadioButton DCL within LISP - Credits to Lee Mac  (Read 3858 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 495
RadioButton DCL within LISP - Credits to Lee Mac
« on: February 23, 2016, 11:47:23 PM »
I have learnt so many things from this forum and especially gurus like Lee Mac, CAB, Kerry, Fixo (I miss him), RibarM, Gile, YMG and so many others.
I have made a LISP for showing a Dialog Box within lisp (No seperate DCL File Needed) having Radio Buttons which returns the selected value. I learnt this from Lee Mac's code at : http://www.theswamp.org/index.php?topic=49169.0

Thanks to all forum members who helped me to be what I am today.

Code: [Select]
;; Makes temp DCL FIle with Radio Buttons and returns selected value (only 1 value can be selected)
(defun MM:RadioButtons (dlglbl rclbl lst defval / file tmp dcl_id dcl des return selval cntxx)
  ;; Inspired from Lee Mac - my mentor for AutoLISP, http://www.theswamp.org/index.php?topic=49169.0
  ;; INPUT VALUES :
  ;; ------------
  ;; dlglbl = Dialog Box Label
  ;; rclbl = Boxed Radio Column Label
  ;; lst = List containing labels of Value of each Radio Button (dynamic number of items possible)
  ;; defval = Default Value will be Highlighted while loading Dialog Box
  ;; RETURN VALUE :
  ;; ------------
  ;; If nothing is selected, then NIL, otherwise returns selected option.
  (defun *error* (msg)
    (if (and (= 'int (type dcl_id)) (< 0 dcl_id))
      (unload_dialog dcl_id)
    )
    (if (= 'file (type des))
      (close des)
    )
    (if (and (= 'str (type dcl)) (setq dcl (findfile dcl)))
      (vl-file-delete dcl)
    )
    (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
      (princ (strcat "\nError: " msg))
    )
    (princ)
  )
  (defun saveVars ()
    (if (not selval)
      (setq selval defval)
    )
  )
  (setq dcl (vl-filename-mktemp "tmp.dcl"))
  (setq des (open dcl "w"))
  (write-line
    (strcat "RBBox : dialog { //dialog name
      label = \""       dlglbl
    "\" ; //give it a label
       :boxed_radio_column { //define radio column
       label = \""       rclbl
    "\" ; //give it a label"
   )
    des
  )
  (setq cntxx 0)
  (foreach xx lst
    (setq cntxx (1+ cntxx))
    (write-line
      (strcat ": radio_button { //define radion button
        key = \""
      (strcat "rb" (itoa cntxx))
      "\" ; //give it a name
        label = \""
      xx
      "\" ; //give it a label"
      (if (equal (strcase defval) (strcase xx))
"\nvalue = \"1\" ; //switch it on"
"\nvalue = \"0\" ; //switch it off"
      )
      "\n} //end definition"
      )
      des
    )
  )
  (write-line
    (strcat
      "} //end radio column
     ok_cancel ; //predifined OK/Cancel
     : row { //define row
     : paragraph { //define paragraph
     : text_part { //define text
     label = \"Text Label. \"; //give it some text
     } //end text
     } //end paragraph
     } //end row
     }"
    )
    des
  )
  (not (setq des (close des)))
  (< 0 (setq dcl_id (load_dialog dcl)))
  (new_dialog "RBBox" dcl_id)
  (setq cntxx 0)
  (foreach xx lst
    (setq cntxx (1+ cntxx))
    (action_tile (strcat "rb" (itoa cntxx)) (strcat "(setq selval \"" xx "\")"))
  )
  (action_tile "accept" "(saveVars)(done_dialog 2)")
  (action_tile "cancel" "(done_dialog 1)(setq selval nil)")
  (start_dialog)
  (unload_dialog dcl_id)
  (if (setq dcl (findfile dcl))
    (progn (vl-file-delete dcl))
  )
  selval
)

Testing Function :

Code: [Select]
(defun c:test (/ RBSEL)
  (vl-load-com)
  (if (setq rbsel (MM:RadioButtons "Select a Value" "Values" (list "1" "2" "3" "4" "5" "6") "3"))
    (alert (strcat "Selected Value is : " rbsel))
    (alert "Nothing Selected.")
  )
  (princ)
)
« Last Edit: February 23, 2016, 11:51:39 PM by mailmaverick »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: RadioButton DCL within LISP - Credits to Lee Mac
« Reply #1 on: February 24, 2016, 04:06:54 AM »
Nice work!

I have two remarks:

You can also attach an action to the radio cluster tile. In this case the radio_column:
Code: [Select]
(action_tile "radioColumnKey" "(print $value)")The $value variable will contain the key of the selected radio_button.

By default (done_dialog 0) is used for the Cancel button and (done_dialog 1) for the OK button. This is not very important, but I would stick to those default values whenever possible.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: RadioButton DCL within LISP - Credits to Lee Mac
« Reply #2 on: February 24, 2016, 06:30:19 AM »
Really nice work!
Those guys are developing the world !
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: RadioButton DCL within LISP - Credits to Lee Mac
« Reply #3 on: February 24, 2016, 03:35:09 PM »
I prefer the foreach function to create the dcl file example

Code - Auto/Visual Lisp: [Select]
  1.       (setq tmp (vl-filename-mktemp nil nil ".dcl")
  2.             des (open tmp "w")
  3.             )
  4.       (foreach line
  5.         '(
  6.             "dcl-name"
  7.             "  : dialog { "
  8.             "     label= \"Insert text here\";"
  9.             "    etc...."
  10.             "   }"
  11.                )
  12.         (write-line line des)
  13.         );foreach
  14.  

This way it doesn't matter how long the file is,  using the "STRCAT" method limits the length of the input line.
otherwise nice work.
« Last Edit: February 24, 2016, 03:45:31 PM by snownut2 »

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: RadioButton DCL within LISP - Credits to Lee Mac
« Reply #4 on: February 24, 2016, 05:47:20 PM »
Well done mailmaverick, and thank you for your kind words  :-)

mailmaverick

  • Bull Frog
  • Posts: 495
Re: RadioButton DCL within LISP - Credits to Lee Mac
« Reply #5 on: March 01, 2016, 10:22:13 AM »
Lee Mac, you are my mentor !!!
« Last Edit: March 01, 2016, 10:33:34 AM by mailmaverick »