TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: andrew_nao on May 19, 2009, 04:52:30 PM

Title: repopulate list
Post by: andrew_nao on May 19, 2009, 04:52:30 PM
im working on modifing a lisp file to allow for user input
the lisp reads a file and lines of this file in a dcl list for printing.
i was able to get the adding to this file via user input to work
but im having trouble clearing the dcl list and repopulating it with the new list
can anyone help?
TIA

there is alot of code to post so ill just post what i think should be good to understand what im trying to do

Code: [Select]
;for user input
(action_tile "add_info" "(setq add_info $value) (add_other)")


Code: [Select]
;; clear the list add to the external file then reload the file

(defun clear ()
        (start_list "files_list")
        (mapcar 'add_list nil)
        (end_list)
)


 (defun add_other ()
   (clear)
   (setq open_file (open datafile "a")); open file
   (write-line add_info open_file)
   (close open_file)
(reload "files_list" file_content_list)

 )

(defun reload (files_list file_content_list)
        (setq ofile (open datafile "r"))

        (while
          (setq curline (read-line ofile))
          (setq file_content_list (cons curline file_content_list))
        )
        (close ofile)

        (start_list "files_list")
        (mapcar 'add_list file_content_list)
        (end_list)

)
Title: Re: repopulate list
Post by: Lee Mac on May 19, 2009, 05:00:38 PM
You don't need to "clear" the list, just use the "3" code with Start_list to delete old contents and start new list:

Code: [Select]
(defun add_other ()
   (setq open_file (open datafile "a")); open file
   (write-line add_info open_file)
   (close open_file)
(reload "files_list" file_content_list)

 )

(defun reload (files_list file_content_list)
        (setq ofile (open datafile "r"))

        (while
          (setq curline (read-line ofile))
          (setq file_content_list (cons curline file_content_list))
        )
        (close ofile)

        (start_list "files_list" 3)  ; use the 3 code to start new list
        (mapcar 'add_list file_content_list)
        (end_list)

)
Title: Re: repopulate list
Post by: CAB on May 19, 2009, 05:07:27 PM
FYI
As 3 is the default mode you don't need to specify that mode.
Title: Re: repopulate list
Post by: CAB on May 19, 2009, 05:44:28 PM
This is how I would approach the task:

Change to this
Code: [Select]
;;  update lisp variable & call the update function with the value
(action_tile "add_info" "(add_other (setq add_info $value))")

Use these subroutines:
Code: [Select]
;;  append data to the file & update the DCL list
(defun add_other (add_info)
  (setq open_file (open datafile "a")) ; open file & append
  (write-line add_info open_file)
  (close open_file)
 
  (start_list "files_list" 2) ; append to existing list in DCL
  (add_list add_info)
  (end_list)
)

;;  use this to load the dialog the first time
;;  reads data from the file
(defun load_files_list (files_list file_content_list)
  (setq ofile (open datafile "r"))
  (while (setq curline (read-line ofile))
    (setq file_content_list (cons curline file_content_list))
  )
  (close ofile)

  (start_list "files_list")
  (mapcar 'add_list file_content_list)
  (end_list)
)
Title: Re: repopulate list
Post by: andrew_nao on May 20, 2009, 09:01:22 AM
thanks for the replies.

CAB
i got an error with your suggestion, too many arguments


using the 2 and 3 codes only visually updates the list in the DCL window.
this code reads a txt file and allows for the user to add to this text file. it also allows the user to select a line or multiple lines in the DCL window then rights it to a new txt file with additional info.

so i need it to be able to clear what is displayed in the DCL window and repopulate the DCL window with the new info that was added (if the user added new info)

i hope that makes sense

Title: Re: repopulate list
Post by: CAB on May 20, 2009, 09:27:27 AM
Another version below. You can modify the data list sent to the function LoadListBox
any way you want. This code relies on a global variable file_content_list
I am not sure of your intent.
Code: [Select]
;;  use this to load the dialog the first time
;;  reads data from the file
(defun load_files_list (files_list file_content_list)
  (setq ofile (open datafile "r"))
  (while (setq curline (read-line ofile))
    (setq file_content_list (cons curline file_content_list))
  )
  (close ofile)
  (LoadListBox "files_list" file_content_list)
)


;;  append data to the file & update the DCL list
(defun add_other (add_info)
  (setq open_file (open datafile "a")) ; open file & append
  (write-line add_info open_file)
  (close open_file)
  (setq file_content_list (append file_content_list (list add_info)))
  (LoadListBox "files_list" file_content_list)
)


;;  populate a list box in the DCL
(defun LoadListBox (ListName ListContent)
  (start_list ListName)
  (mapcar 'add_list ListContent)
  (end_list)
)

<edit> extra )'s removed
Title: Re: repopulate list
Post by: andrew_nao on May 20, 2009, 11:31:45 AM
CAB,
I get a syntax error when i add your code

im attaching the lisp so you can see the whole thing

Title: Re: repopulate list
Post by: CAB on May 20, 2009, 12:00:41 PM
Fixed my typo's.
Try again.
Title: Re: repopulate list
Post by: andrew_nao on May 20, 2009, 12:14:13 PM


too few argument

Title: Re: repopulate list
Post by: CAB on May 20, 2009, 12:39:07 PM
Try this.
Title: Re: repopulate list
Post by: andrew_nao on May 20, 2009, 01:35:31 PM
Try this.

thank you that works


what did ya do?

did i have something backwards?
please tell me i was on the right train but wrong track
Title: Re: repopulate list
Post by: CAB on May 20, 2009, 02:43:44 PM
This was not changed.
Code: [Select]
(action_tile "add_info" "(add_other (setq add_info $value))")
Title: Re: repopulate list
Post by: Lee Mac on May 20, 2009, 03:03:01 PM
sometimes the simplest things give the most trouble it seems...   :angel:
Title: Re: repopulate list
Post by: andrew_nao on May 20, 2009, 04:47:29 PM
This was not changed.
Code: [Select]
(action_tile "add_info" "(add_other (setq add_info $value))")

gah! its always something stupid with me

I always forget the obvious
appreciate your help