View Full Version : Selected Items in A List
BeKirra
2009-11-23, 10:48 PM
Hi ALL,
I start an idea to write a routine.
I can't select a range of items from the list & need your helps.
Supposed I have a txt data file. The items in the data file have 3 categories:
Note_A_...
Note_A_...
Note_A_...
Note_A_...
Note_A_...
...
Note_B_...
Note_B_...
Note_B_...
Note_B_...
Note_B_...
...
Note_C_...
Note_C_...
Note_C_...
Note_C_...
Note_C_...
...
Each category may have different number of items & this is a long list.
This list will be shown in a dialog box.
I don't want to display the long list in the dialog box at the same time.
So I decide to add some conditions to achieve it:
Condition_01 to select & only display category "Note_A_"
Condition_02 to select & only display category "Note_B_"
Condition_03 to select & only display category "Note_C_"
Could you please give a help to do this task?
Your helps are much appreciated.
BeKirra
2009-11-25, 04:33 AM
In other words, I want to show the selected items in the list box in the dialog & hide (filter out) the rest of them because the list is very long.
I don't want to separate the list into small txt files.
Can it be done in lisp & how?
Thanks for your time.
BTW, I haven't started this routine actually but only the idea. It won't be easy if I don't know the certain ideas can be achieved in lisp.
steve.ashton
2009-11-25, 07:15 AM
You could create a seperate list for each of the prefixs of the text. Read the text file into a list and then create individual lists by cycling through each item and add to new list if text matches. You can then select which list to use with another drop down box or radio buttons.
irneb
2009-11-30, 06:11 AM
The trick would be in how you read the TXT file. Is there some way you can programmatically figure out that a "new" group starts from the currently read line?
There's various ways of doing such, e.g. inside INI files the "group names" are always preceded with a colon ( : ) to destinguish them from a normal line. Another option could be that a group always starts following an empty line. So then when you do a read-line you can check if the line is empty (e.g. (= lineread "")) - then you know the group is finished.
What I'd do is to save the entire file into a list of lists. E.g. say the TXT file looks something like this:
Group 1
Item 1.1
Item 1.2
...
Item 1.N
Group 2
Item 2.1
...
Item 2.NThen after reading the file I'd want a list looking like this:
(
("Group 1" "Item 1.1" "Item 2.2" ... "Item 1.N")
("Group 2" "Item 2.1" ... "Item 2.N")
)Then you could have all the car's of the list in a drop-down. This drop down would thus display Group 1 & Group 2. Add an action to the drop-down to check if the user selects a new group. This action should basically figure out from the drop-down's current value which group. In which case you could use nth (or maybe even assoc) to get the correct "sub-list" showing the group's items. Then you use nth starting from position 1 and add each to the listbox (use start_list's default option 3).
BeKirra
2009-12-03, 05:06 AM
Group 1
Item 1.1
Item 1.2
...
Item 1.N
Group 2
Item 2.1
...
Item 2.NThen after reading the file I'd want a list looking like this:
(
("Group 1" "Item 1.1" "Item 2.2" ... "Item 1.N")
("Group 2" "Item 2.1" ... "Item 2.N")
)
Yes, this is the list structure I'd have.
I tried but I couldn't figure it out.
Based on the list above in txt file, could you please give a simplest lisp?
So I can start from there.
Thanks for your help.
irneb
2009-12-04, 06:03 AM
Firstly to read the file. I'm assuming your file is as per my previous email.
;; Function to read grouped list from file
(defun readgroups (fn / f str lst grp)
(vl-load-com) ;Ensure VL extensions are loaded
(if (setq fn (findfile fn)) ;Check if the file exists
(progn
(setq f (open fn "r")) ;Open it for reading
;; Continue reading file
(while (setq str (vl-string-trim " \t\n" (read-line f))) ;Read line & remove blank spaces
(if (= str "") ;Reached end of group
(if grp ;Check if group's been started
(setq lst (cons (reverse grp) lst) ;Add the group to the full list
grp nil ;Clear the temp grp variable
) ;_ end of setq
) ;_ end of if
(setq grp (cons str grp)) ;Add the line to the temp grp variable
) ;_ end of if
) ;_ end of while
lst ;Return the list
) ;_ end of progn
nil ;If file not found return nil
) ;_ end of if
) ;_ end of defun
You'd call this with the filename (which you could obtain through a getfiled call). It returns a list containing sub-lists - each of which 1st item would be the group name. Say you store it in a variable called lst in your main defun which opens displays the dialog, then you'd setup the PopUp box as such:
;; Setup the PopUp with group names
(start_list "PopUp_List_Key")
(foreach item lst
(add_list (car item))
) ;_ end of foreach
(end_list)
Now to let a change in the PopUp's selection alter the listbox showing the group's items, you'll need a function which would be called through a DCL action. I'd advise looking in the help to see how these work, but this is basically what you'll have:
;; Function to update the listbox when PopUp changes
(defun ShowGroupItems ($value / group item)
(setq group (nth (atoi $value) lst)) ;Get the selected group from the list
;; Setup the Items inside the selected group
(start_list "Listbox_Key")
(foreach item (cdr group) ;Step through group's items only - don't show the group name
(add_list item)
) ;_ end of foreach
(end_list)
) ;_ end of defun
And then finally you'll need to setup the PopUp to fire the action each time it changes:
;; Set action to PopUp
(action_tile "PopUp_List_Key" "(ShowGroupItems $value)")
Edit: Oh and by the way, you'll note I use the lst variable inside the ShowGroupItems defun without declaring it. This is because you should place the ShowGroupItems defun inside the main function showing the dialog. Then the lst variable if the main function is "global" to ShowGroupItems, and doesn't need to be declared globally / passed each time the action occurs. This way it's as efficient as you can get (for very long lists), while still having an automatic cleanup of variables after the dialog is closed.
BeKirra
2009-12-07, 11:04 PM
Thank you very much.
I am busy for a couple of days & will try it later.
BeKirra
2009-12-14, 12:45 AM
Firstly to read the file. I'm assuming your file is as per my previous email.
...
Something was needed to be done & sorry I didn't have time to think this.
Let me have a smple list for the start:
Level_01
Door_Type_01 3
Door_Type_02 10
Door_Type_03 12
Window_Type_01 4
Window_Type_02 6
Window_Type_03 13
Window_Type_04 2
Window_Type_05 3
Level_02
Door_Type_01 1
Door_Type_02 15
Door_Type_03 9
Window_Type_01 4
Window_Type_02 6
Window_Type_03 13
Window_Type_04 2
Window_Type_05 3
This list will be saved as a txt file.
Could you please check it if there is something wrong?
Thanks.
irneb
2009-12-14, 07:26 AM
Could you post what you have already?
What's not clear is the numbers following each of the items inside the groups. Are all these simply separated by spaces? And are all the names full-words with underscores instead of spaces?
BeKirra
2009-12-16, 05:46 AM
Could you post what you have already?
What's not clear is the numbers following each of the items inside the groups. Are all these simply separated by spaces? And are all the names full-words with underscores instead of spaces?
Actually I need 2 columns of notes for each item under the group.
A sample below shows 2 columns of numbers after the item name.
However, they can be numbers or short length of description in words (may be 2~3 words).
I will then grab the note1 or note2 for the drawing by using the routine ("nth" method).
Level_01
Door_Type_01 3 3
Door_Type_02 10 4
Door_Type_03 12 5
Window_Type_01 4 6
Window_Type_02 6 7
Window_Type_03 13 8
Window_Type_04 2 9
Window_Type_05 3 10
Level_02
Door_Type_01 1 12
Door_Type_02 15 13
Door_Type_03 9 14
Window_Type_01 4 15
Window_Type_02 6 16
Window_Type_03 13 17
Window_Type_04 2 18
Window_Type_05 3 19
Alternatively the data can be shown without the group title. Will the routine be easier to write & shorter in length?
L01_Door_Type_01 3 3
L01_Door_Type_02 10 4
L01_Door_Type_03 12 5
L01_Window_Type_01 4 6
L01_Window_Type_02 6 7
L01_Window_Type_03 13 8
L01_Window_Type_04 2 9
L01_Window_Type_05 3 10
L02_Door_Type_01 1 12
L02_Door_Type_02 15 13
L02_Door_Type_03 9 14
L02_Window_Type_01 4 15
L02_Window_Type_02 6 16
L02_Window_Type_03 13 17
L02_Window_Type_04 2 18
L02_Window_Type_05 3 19
I think this routine is too hard for me.
The only thing I figured out was the dialog box shown as below:
GroupSelection : dialog {
label = "List";
:row {
:column { alignment = top;
fixed_height = true;
: boxed_column { label = "Level No.";
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// : popup_list {
// key = "SingleGroup";
// edit_width = 15;
// value = "0";
// } // end of popup_list
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// I think the radio_buttonS would be better.
: radio_button {
key = "Group01";
label = "Level 1";
} // end of radio_button
: radio_button {
key = "Group02";
label = "Level 2";
} // end of radio_button
} // end of boxed_column
: list_box {
label = "Check List:";
key = "GroupItems";
width = 15;
height = 25;
} // end of list_box
} // end of column
} // end of row
ok_cancel;
} // end of dialog
Thanks for your help.
irneb
2009-12-16, 10:37 AM
OK, here's a working model. It's rather difficult using radio buttons, as it's not easy figuring out which is selected. It's possible, but code is more complex. Also you'll be hard put if you need more than 2 levels - i.e. you'll need to change the DCL file, the LSP file, as well as the TXT file. So I did the next best thing: used a list_box instead.
I suggest using TABS to split the number portions in the TXT file. That way the selected items could even use spaces in their names. Also it makes it a lot easier to setup the list of items.
I've included 2 helper functions to convert the items from strings containing tabs to a list with 3 values (name val1 val2), and visa versa.
Save all 3 files to a folder on your support paths. Load the ListSel.LSP file and issue the ListSel command. I've added comments to show what each line of code does so you can figure out what's going on. If the user clicks OK then it prints out the list containing the item selected in the Check list listbox. This list contains the name and the 2 values.
Edit: There were some errors in my previous post. E.g. the TXT file wasn't closed, the lst was returned in reverse order, and the last group of the list wasn't added. These are fixed in the attached.
BeKirra
2009-12-18, 05:05 AM
OK, here's a working model. It's rather difficult using radio buttons, as it's not easy figuring out which is selected. It's possible, but code is more complex. Also you'll be hard put if you need more than 2 levels - i.e. you'll need to change the DCL file, the LSP file, as well as the TXT file. So I did the next best thing: used a list_box instead.
I suggest using TABS to split the number portions in the TXT file. That way the selected items could even use spaces in their names. Also it makes it a lot easier to setup the list of items.
I've included 2 helper functions to convert the items from strings containing tabs to a list with 3 values (name val1 val2), and visa versa.
Save all 3 files to a folder on your support paths. Load the ListSel.LSP file and issue the ListSel command. I've added comments to show what each line of code does so you can figure out what's going on. If the user clicks OK then it prints out the list containing the item selected in the Check list listbox. This list contains the name and the 2 values.
Edit: There were some errors in my previous post. E.g. the TXT file wasn't closed, the lst was returned in reverse order, and the last group of the list wasn't added. These are fixed in the attached.
Thanks for your help. Your code works perfectly.
I need more time to learn from your code.
I just have a question:
It is eaiser when reading the rows through the list by using "nth" method.
But why it is so hard to marks things work when trying to read the columns (in my case it is the first column)? :shock:
It would be good if AutoLisp had an additional function to do the job. :)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.