In this ruby script will select pipes in our network based on the width dimensions we specify.
We are selecting the current network and then we are using the clear_selection method to remove any currently selected objects from our network.
We are now using the method row_objects and looking at the hw_conduit table within InfoWorks. We are assigning this result to a variable called pipes. We can not think of pipes being the table of conduits in our network.
We are assigning a value of 0 to a variable called i. This is common practice when we want to loop through existing information in a table.
while i<pipes.length pipe=pipes[i] if pipe.conduit_width.nil? else if pipe.conduit_width <200 pipe.selected=true end end
We apply the method length on our variable pipes. This gives us the number of pipes in our pipes table. We then say while the value for i is less than that number, continue… At this point, our value for i is 0, so the script will therefore continue.
We then create a new variable called pipe to which we assign the value of the ith value in our pipes table. Our current i value is 0, so we are looking at the very first item in our array or table. So the first length in our pipes table.
We then apply the method conduit_width (which tells us the width of the conduit) to that value and ask if it is nil? (Ruby struggles with blank values so this is a requirement). If it’s not nil, then we move on using the else statement and use the conduit_width method to enquire if that i value is less than 200. If so, it uses the method selected = true to highlight it. Otherwise it ends the statement.
We now increase the i value to move onto the next item in our pipes array. this goes on until we have gone through all the conduits in our table -i.e. while i is less than pipes.length value. This process therefore selects all the conduits in the table where the above conditions are met.
Upon running this script, all pipes in your network which are less than 200 should now be highlighted.
A way of updating the code section above to make it slightly easier on the eye would be to use AND and NOT statements. In Ruby and AND statement is written as &&. OR = II and Not = !
if pipe.conduit_width.nil? else if pipe.conduit_width <200 pipe.selected=true
could be written as..
if (!pipe.conduit_width.nil? && pipe.conduit_width<200) pipe.selected=true