# ============================================================== # str_index.rb # # Walt Akers # July 9, 2021 # # Function str_index(idx, str) # # This function takes as a parameters two values - idx and str. # idx is an index integer that represents a specific value in the # comma delimited list provided in str. The index starts at 0. # # Example: # # Called with str_index(1, "2,3,4") # will return the string "3" # # Called with str_index(0, "2,3,4,5,6,7,8") # will return the string "2" # # This function is particularly useful when the array of strings # is in one field of the dynamic component and the index is in # another. # # When using this function with a COPY as the index and # referencing an array in another field, it is sometimes # necessary to "wrap-it" in a Value command, for instance: # =Value(str_index(COPY, Parent!ListArray)) # ============================================================== require 'sketchup' # Open SketchUp's Dynamic Component Functions (V1) class. class DCFunctionsV1 protected # Grab an indexed string from the text # # Usage: str_index(idx,str) def str_index(a) idx = a[0].to_i str = a[1].to_s vals = str.split(',', -1) return vals[idx] end end