[Closed] Dropdown list items from textfile
Ok, so here’s what I want to do.
Being in Archviz, I want to create a small tool with a dropdown list
the dropdown list should contains ralnumbers that I want it to get outof a textfile
on one of the items selected it should create a material for the selected object with rgb values that are in the textfile as well
so the textfile should look something like this:
Ral 1007 – Daffodil yellow
230,145,0
Ral 1013 – Oyster white
228,219,197
so i’ve been fooling around with openfile and readvalues and such, but I think I might be overlooking something.
I figure i would need an array to hold the values and loop through the textfile to append the array.
heres the small start I managed to get searching this forum [gota love the searchfunction]
F = "d:\ est.txt"
O = openFile F
Ral_Array = #()
while not eof O do
(
append Ral_Array (readline O)
)
try destroyDialog RalMaterials
catch ()
Rollout RalMaterials "Ral Materials"
(
Dropdownlist ddRalMats "Materials" height:30 Items:Ral_Array
)
createDialog Ralmaterials width:200
so how to proceed ? I’d like to filter out the rgb values from the dropdownlist but on a listitem selected i would like to get the rgb value that follows the selected ralname in the textfile
[did that make sence ?]
oh well, any insight or directions would be nice.
Hi Yves,
I can suggest a couple of things. Maybe you should consider using a struct, so:
[size=2]struct RALcolour (name, number, r, g, b)
#Struct:RALcolour(
Number:<data>,
b:<data>,
name:<data>,
r:<data>,
g:<data>)
[/size]
This will enable you to create colours like:
c=RALcolour name:"daffodil yellow" number:1007 r:230 g:145 b:0
RALcolour name:"daffodil yellow" Number:1007 r:230 g:145 b:0
and then access them easily:
[size=2]c.r
230
[/size]
I would also structure you txt file as a CSV, ie a comma delimited file like you can make from excel, so:
dafodil yellow, 1007, 230, 145, 0
this will allow you to read them in to your program very easily, and then cerate an array of their names or any other detail you want. So:
struct RALcolour (name, number, r, g, b)
clrs=#()
d=maxfilepath
f="colourchart.txt"
file=d+f
o=openfile file
while not eof o do
(
nme =readdelimitedstring o ","
nmbr =readdelimitedstring o ","
r =readdelimitedstring o ","
g =readdelimitedstring o ","
b =readdelimitedstring o ","
skiptonextline o
newcolour=RALcolour name:nme number:nmbr r:r g:g b:b
append clrs newcolour
)
close o
RALnames=#()
for c in clrs do append RALnames c.name
Hope this helps,
Josh.
ok, cool, that seems to work. funny thing is i started to play with readDelimitedString before, just forgot about it. that’s what being a novice at scripting does to one I guess.
So Im gonna try and find out how to link the selected dropdownlist item to the rgb values.
If I fail you’ll see me back here, if I succeed you wil as well with a working script hehe.
thx sofar man.
BTW, is it common that I have to have a blank line at the end of my textfile for things to work ?
if I don’t have a blank line it comes back with an emptry dropdownlist.
Maybe because my script uses skiptonextline as the last thing it does when reading the file. you probably don’t need to have this, since it will read to the next “,” anyway. I would be careful of having carriaeg returns in you text file because they will be read in also! you of course search for and remove them easily enough using replace.
Good luck,
J.
well, I’m back managed to get “somewhere’ but not “there” yet so to speak.
here’s what I have so far
------- Get Info from Textfile
Struct Ral_Color (name, r, g, b)
F = "d:\ est.txt"
O = openFile F
Name_Array = #()
while not eof O do
(
name = readdelimitedstring O ","
r = readdelimitedstring O ","
g = readdelimitedstring O ","
b = readdelimitedstring O ","
newColor = Ral_color name:name r:r g:g b:b
skipToNextLine O
append Name_Array newColor
)
DD_Array = #()
for items in Name_Array do append DD_Array items.name
------- Create Rollout
try destroyDialog RalMaterials
catch ()
Rollout RalMaterials "Ral Materials"
(
------- INTERFACE
Dropdownlist ddRalMats "Select Ral Color" height:30 Items:DD_Array
label lb1 "Currently selected color :"
colorpicker cpk1 fieldwidth:120 height: 15 offset:[23, 0] color:[0,0,0]
button btn_Apply "Apply" width:75 across:2
button btn_Cancel "Cancel" width:75
------- ACTIONS
on ddRalMats selected i do
(
seek O ddRalMats.selection
name = readdelimitedstring O ","
r = readdelimitedstring O ","
g = readdelimitedstring O ","
b = readdelimitedstring O ","
cpkColor = Ral_Color r:r g:g b:b
rc = cpkColor.r as integer
gc = cpkColor.g as integer
bc = cpkColor.b as integer
cpk1.color = [rc,gc,bc]
)
on btn_Cancel pressed do destroyDialog RalMaterials
)
createDialog Ralmaterials pos:[150,250] width:200
I’m trying to use seek to set the line where to look for the rgb to set the colorpicker [cpk]
but I have the feeling that instead of doing lines, seek works with characters ?
because I ahve 3 testlines in the texfile and it doesn’t matter which one i choose it always returns the color of the 1st one.
I attached the test.txt file so it works as it should
further assistance needed
Hey Yves, good to see you back in scripting-land again
Hope this will help you:
------- Ral Color structure
Struct Ral_Color (name, r, g, b)
------- Create Rollout
try destroyDialog RalMaterials catch ()
Rollout RalMaterials "Ral Materials"
(
-- this array will hold all the entries from the file..
-- declaring it as a local at the top of the rollout
-- will make sure you can access it from anywhere within
-- the rollout.
local ral_array = #()
------- INTERFACE
Dropdownlist ddRalMats "Select Ral Color" height:30 Items:#()
label lb1 "Currently selected color :"
colorpicker cpk1 fieldwidth:120 height: 15 offset:[23, 0] color:[0,0,0]
button btn_Apply "Apply" width:75 across:2
button btn_Cancel "Cancel" width:75
------- ROLLOUT OPEN EVENT
on RalMaterials open do
(
-- open the file
ral_file = openFile "d:\ est.txt"
-- read entire file
while not eof ral_file do
(
-- next ral entry
name = readdelimitedstring ral_file ","
r = readdelimitedstring ral_file ","
g = readdelimitedstring ral_file ","
b = readdelimitedstring ral_file ","
skiptonextline ral_file
-- append to array
append ral_array (Ral_Color name:name r:(r as integer) g:(g as integer) b:(b as integer))
)
-- update the dropdown list
ddRalMats.items = (for ral in ral_array collect ral.name)
)
------- ACTIONS
on ddRalMats selected i do
(
-- no need to open and read the file again here
-- as the entries are already stored in <ral_array> :)
-- also, since the dropdownlist is directly derrived from
-- <ral_array> you can use the selected dropdownlist index (i)
-- to get the item from <ral_array>
--
-- so:
--
-- ddRalMats.items[i] is identical to ral_array[i].name
--
-- hope that makes sense :g
cpk1.color = color ral_array[i].r ral_array[i].g ral_array[i].b
)
on btn_Cancel pressed do destroyDialog RalMaterials
)
createDialog Ralmaterials pos:[150,250] width:200
Cheers!
Martijn
Always cool te get help from my old ‘mentor’
yeah back in scripting, finally found a scriptidea that’s actuialy usefull in everyday life for me and my coworkers, so that makes experimenting with it mroe fun to ahve an actual usable result.
I figured i was taking the long way around to get somewhere. good to see how you shortened and cleanded it. thx
btw, gonna move soon, so when all’s ready, there’ll be dinner for you + partner