Jump to content

LEE LISP... List Box Synchronisation to run without a  DCL File 


Recommended Posts

Posted

Hello all
Hello to @Lee Mac
Is it possible to modify the code  List Box Synchronisation   to run without a  DCL File   
Please help from you

http://www.lee-mac.com/listboxsync.html

 

(defun c:listboxexample ( / *error* UpdateList data dclfile dclhandle )

  ;; This program demonstrates how to update a secondary list_box based
  ;; on user selection from a primary list_box.

  ;; Example courtesy of Lee Mac © 2010 (www.lee-mac.com)

  ;; Accompanying DCL File:

  (setq dclFile "listboxexample.dcl")
  
  ;; Ensure this file resides in an AutoCAD Support Path

  (defun *error* ( msg )

    ;; Error Handler - will unload the dialog
    ;; from memory should the user decide to hit Esc

    (if dclHandle (unload_dialog dclHandle))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ)
  )

  (defun UpdateList ( key lst )

    ;; This function updates the list_box associated with the specified key
    ;; using the contents of the supplied lst

    (start_list key)
    (mapcar 'add_list lst)
    (end_list)
  )

  ;; Data used to Populate List_Boxes:
  ;; I've chosen to use this list structure because it suits the data,
  ;; but there are many other possibilities.

  (setq Data
   '(
     ("Audi"    ("TTS Quattro" "TT RS" "S3 Quattro" "S4 Quattro" "S5 Quattro" "RS5 Quattro" "RS6 Quattro"))
     ("BMW"     ("M3" "M5" "M6" "X5" "Z3" "Z4"))
     ("Porsche" ("911" "924" "928" "930" "944" "Boxster" "Cayenne" "Cayman"))
     ("Jaguar"  ("XF" "XJ6" "XJR" "XKR" "X-Type" "S-Type"))      
    )
  )

  ;; Start of Main Routine
  ;; Lots of Error Trapping to make sure the Dialog Loads:

  (cond
    ( (<= (setq dclHandle (load_dialog dclFile)) 0)

      (princ "\n--> DCL File not Found.")
    )
    ( (not (new_dialog "listboxexample" dclHandle))

      (setq dclHandle (unload_dialog dclHandle))
      (princ "\n--> Dialog Definition not Found.")
    )
    ( t
      ;; Dialog Loaded Successfully.

      (or *Make*  (setq *Make*  "0"))
      (or *Model* (setq *Model* "0"))

      ;; Set up some default selections, for the first-time running of the program.
      ;; The variables *Make* & *Model* are intended to be global and hence will
      ;; remember the user's last selections.

      ;; Populate the List_Boxes:

      ;; List_Box 'lst1'
     
      (UpdateList "lst1" (mapcar 'car Data))
      (set_tile "lst1" *Make*)

      ;; List_Box 'lst2'

      (UpdateList "lst2" (cadr (nth (atoi *Make*) Data)))
      (set_tile "lst2" *Model*)

      ;; Action_tile Statements:
      ;; These control what happens when the user interacts with a tile in the dialog.

      (action_tile "lst1"
        (strcat
          "(UpdateList \"lst2\" (setq lst2 (cadr (nth (atoi (setq *Make* $value)) Data))))"
          "(setq *Model*"
          "  (set_tile \"lst2\""
          "    (if (< (atoi *Model*) (length lst2)) *Model* \"0\")"
          "  )"
          ")"
        )
      )

      ;; Here, when the user selects an item from 'lst1', the UpdateList subfunction
      ;; is fired to update the items in list_box 'lst2'.

      ;; list_box 'lst2' is also set to the value of *Model* if the index is
      ;; available for the selected item, else the first item is selected.

      ;; Note that $value is a string containg the index of the item
      ;; that the user has selected.

      (action_tile "lst2" "(setq *Model* $value)")

      ;; Dialog setup, lets start it:

      (start_dialog)
      (setq dclHandle (unload_dialog dclHandle))
    )
  )
  (princ)
)

 

DCL File

// DCL File to be saved as listboxexample.dcl
// Example courtesy of Lee Mac © 2010 (www.lee-mac.com)

lbox : list_box { width = 25; fixed_width = true; alignment = centered; }

listboxexample : dialog { label ="List Box Synchronisation"; spacer;
  : row {
    : lbox { key = "lst1"; label = "Make" ; }
    : lbox { key = "lst2"; label = "Model"; }
  }
  ok_only;
}

 

 

 

I am looking for an example of her on the next page, but I do not know how to link them

https://www.theswamp.org/index.php?topic=49169.0

(defun c:example ( / *error* dch dcl des idx ls1 ls2 )
 
    (defun *error* ( msg )
        (if (and (= 'int (type dch)) (< 0 dch))
            (unload_dialog dch)
        )
        (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)
    )
 
    (repeat (setq idx 15)
        (setq ls1 (cons (strcat "Item " (itoa idx)) ls1)
              idx (1- idx)
        )
    )
 
    (if
        (and
            (setq dcl (vl-filename-mktemp "tmp.dcl"))
            (setq des (open dcl "w"))
            (foreach str
               '(
                    "listbox : list_box"
                    "{"
                    "    width = 30;"
                    "    height = 20;"
                    "    fixed_width = true;"
                    "    fixed_height = true;"
                    "}"
                    "example : dialog"
                    "{"
                    "    label = \"List Box Example\";"
                    "    spacer;"
                    "    : row"
                    "    {"
                    "        : listbox { key = \"ls1\"; }"
                    "        : listbox { key = \"ls2\"; }"
                    "    }"
                    "    ok_only;"
                    "}"
                )
                (write-line str des)
            )
            (not (setq des (close des)))
            (< 0 (setq dch (load_dialog dcl)))
            (new_dialog "example" dch)
        )
        (progn
            (fill_list "ls1" ls1)
           
            (action_tile "ls1"
                (vl-prin1-to-string
                   '(if (= 4 $reason)
                        (setq ls2 (fill_list "ls2" (append ls2 (list (nth (atoi $value) ls1))))
                              ls1 (fill_list "ls1" (LM:removenth (atoi $value) ls1))
                        )
                    )
                )
            )
            (action_tile "ls2"
                (vl-prin1-to-string
                   '(if (= 4 $reason)
                        (setq ls1 (fill_list "ls1" (append ls1 (list (nth (atoi $value) ls2))))
                              ls2 (fill_list "ls2" (LM:removenth (atoi $value) ls2))
                        )
                    )
                )
            )
            (if (and (= 1 (start_dialog)) ls2)
                (progn
                    (princ "\nThe user selected the following items:")
                    (foreach x ls2 (terpri) (princ x))
                )
                (princ "\n*Cancel*")
            )
        )
    )
    (*error* nil)
    (princ)
)
 
(defun fill_list ( key lst )
    (start_list key)
    (foreach itm lst (add_list itm))
    (end_list)
    lst
)
 
;; Remove Nth  -  Lee Mac
;; Removes the item at the nth index in a supplied list
 
(defun LM:removenth ( n l )
    (if (and l (< 0 n))
        (cons (car l) (LM:removenth (1- n) (cdr l)))
        (cdr l)
    )
)
 
(princ)

 


Thank you 

Posted

Command line menu?

 

[Audi/Bmw/Porsche/Jaguar]:

B

BMW [M3/M5/M6/X5/Z3/Z4]:

Z4

z4 command?

Posted

Thank you 

 

I'am sorry I did not understand
What is the change in the code

Posted

Are you saying you want the dcl to be created as required from your code, it does not exist, simple just add the write dcl to file to your code. 

 

(setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w"))

 

Just look at Multi radio buttons.lsp in "Downloads". This method is used by lots of us plenty of examples.

Posted

Since writing that particular tutorial, I have developed my DCL List Tile Dependency function, which extrapolates & generalises the concept to extend to and accommodate any number of dependent/synchronised DCL list tiles (whether they be list boxes or popup lists), nested to any number of levels deep.

 

Furthermore, the ListTileDependencyDemo.lsp demo program available to download from that page incorporates the DCL definition as part of the AutoLISP program, writing & loading the supporting DCL file during the evaluation of the AutoLISP function.

  • Like 1
  • Thanks 1
Posted

@Lee Mac

Thank you 

Yes, this is what I want
Thank you for the support and what you do for us and others

 

; DCL List-Tile Dependency  -  V1.1 (2019-04-27)  -  Lee Mac
;; Configures action_tile statements for the list of keys to enabled dependency between the DCL tiles.
;; key     - [lst] List of DCL tile keys in order of dependency
;; lst-sym - [sym] Quoted variable containing list data
;; rtn-sym - [sym] Quoted variable containing initial selection indexes

;;; http://www.lee-mac.com/listtiledependency.html

(defun LM:dcld:action ( key lst-sym rtn-sym )
    
    (defun LM:dcld:addlist ( key lst )
        (start_list key)
        (foreach itm lst (add_list itm))
        (end_list)
    )
    (defun LM:dcld:getlists ( idx lst )
        (if (cdr idx)
            (cons (mapcar 'car lst) (LM:dcld:getlists (cdr idx) (cdr (nth (car idx) lst))))
            lst
        )
    )
    (defun LM:substnth ( itm idx lst )
        (if lst
            (if (zerop idx)
                (cons itm (mapcar '(lambda ( x ) 0) (cdr lst)))
                (cons (car lst) (LM:substnth itm (1- idx) (cdr lst)))
            )
        )
    )
    (defun LM:dcld:actions ( key lst-sym rtn-sym lvl / fun )
        (setq fun
            (if (cdr key)
                (list 'lambda '( val lst )
                    (list 'setq rtn-sym (list 'LM:substnth '(atoi val) lvl rtn-sym))
                    (list 'LM:dcld:addlist (cadr key) (list 'nth (1+ lvl) (list 'LM:dcld:getlists rtn-sym 'lst)))
                    (list
                        (LM:dcld:actions (cdr key) lst-sym rtn-sym (1+ lvl))
                        (list 'set_tile (cadr key) "0") 'lst
                    )
                )
                (list 'lambda '( val lst )
                    (list 'setq rtn-sym (list 'LM:substnth '(atoi val) lvl rtn-sym))
                )
            )
        )
        (action_tile (car key) (vl-prin1-to-string (list fun '$value lst-sym)))
        fun
    )
    (mapcar 'LM:dcld:addlist key (LM:dcld:getlists (eval rtn-sym) (eval lst-sym)))
    (   (eval (LM:dcld:actions key lst-sym rtn-sym 0))
        (set_tile (car key) (itoa (car (eval rtn-sym))))
        (eval lst-sym)
    )
    (princ)
)

;; DCL List-Tile Dependency  -  Get Items  -  Lee Mac
;; Returns a list of the items selected from each dependent list tile.
;; idx - [lst] List of selection indexes
;; lst - [lst] List data

(defun LM:dcld:getitems ( idx lst / tmp )
    (if (cdr idx)
        (cons (car (setq tmp (nth (car idx) lst))) (LM:dcld:getitems (cdr idx) (cdr tmp)))
        (list (nth (car idx) (car lst)))
    )
)

;; Two List Tile Dependency Example  -  Lee Mac
;; Requires ListTileDependency.lsp to be loaded.

(defun c:test1 ( / *error* dch dcl des lst rtn )

    (defun *error* ( msg )
        (if (= 'file (type des))
            (close des)
        )
        (if (< 0 dch)
            (unload_dialog dch)
        )
        (if (and (= 'str (type dcl)) (findfile dcl))
            (vl-file-delete dcl)
        )
        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )

    (setq lst
       '(
            (
                "Item 1"
                (
                    "Item 1-a"
                    "Item 1-b"
                    "Item 1-c"
                )
            )
            (
                "Item 2"
                (
                    "Item 2-a"
                    "Item 2-b"
                    "Item 2-c"
                    "Item 2-d"
                    "Item 2-e"
                )
            )
            (
                "Item 3"
                (
                    "Item 3-a"
                    "Item 3-b"
                )
            )
        )
    )

    (if
        (and
            (setq dcl (vl-filename-mktemp "tmp.dcl"))
            (setq des (open dcl "w"))
            (foreach str
               '(
                    "lbx : list_box"
                    "{"
                    "    alignment = centered;"
                    "    fixed_width = true;"
                    "    fixed_height = true;"
                    "    width = 20;"
                    "    height = 15;"
                    "}"
                    "test : dialog"
                    "{"
                    "    label = \"List Box Dependency Example\";"
                    "    spacer;"
                    "    : row"
                    "    {"
                    "        : lbx { key = \"lb0\"; label = \"List Box 1\"; }"
                    "        : lbx { key = \"lb1\"; label = \"List Box 2\"; }"
                    "    }"
                    "    spacer;"
                    "    ok_cancel;"
                    "}"
                )
                (write-line str des)
            )
            (not (setq des (close des)))
            (< 0 (setq dch (load_dialog dcl)))
            (new_dialog "test" dch)
        )
        (progn           
            (setq rtn '(0 0))
            (LM:dcld:action '("lb0" "lb1") 'lst 'rtn)
            (if (= 1 (start_dialog))
                (princ
                    (strcat "\nThe user selected:"
                        (substr
                            (apply 'strcat
                                (mapcar '(lambda ( x ) (strcat ", " x))
                                    (LM:dcld:getitems rtn lst)
                                )
                            )
                            2
                        )
                    )
                )
                (princ "\n*Cancel*")
            )
        )
    )
    (*error* nil)
    (princ)
)

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...