Hi all. I have a list of image names to be inserted to Civil3D. The image names have a format of 00X where the 00 indicates the national coordinate grid number (1-1000) and the letter indicates where in the grid the image is supposed to be inserted (a-d). My scripting works fine as long as there is no gap between my images [1a, 1b, 1c, 1d, 2a, 2b, 2c, 2d, 3a, … nd]. As soon as i have an image missing from the list [1c, 1d, 2a, 2c, 3a, … nd], the other images get inserted in the wrong location.
Now, is there a way to achieve something like this?
I need to somehow fill the gaps in the sequence so that ExternalReference.CreateImageReference node will not insert anything for this one and go to the next image. Hope I make sense.
With the help of ChatGPT, I managed to get this code working:
# Define the function
def insert_null_values(sequence):
# Define the maximum number
max_number = IN[1]
# Initialize variables
output = []
expected_index = 0
# Iterate over the sequence
for i in range(1, max_number + 1):
for letter in ['a', 'b', 'c', 'd']:
expected_item = str(i) + letter
if expected_index < len(sequence) and sequence[expected_index] == expected_item:
output.append(sequence[expected_index])
expected_index += 1
else:
output.append(None)
return output
# Example input sequence (you should modify this according to your setup)
input_sequence = IN[0]
# Call the function
output_sequence = insert_null_values(input_sequence)
# Output the result
OUT = output_sequence
The gaps appear becuase I don’t have the image. So I just wanted to have something in the list so that the node downstream skips it and inserts nothing. Otherwise the inserted image would be in the wrong location.
I’m trying to figure scenarios when this wouldn’t work. Perhaps when the inputs are wrong but that shouldn’t happen. The image names will always come in the same format (00X) and, unless the List.Count node, which feeds into ‘‘max_number = IN[1]’’, stops working for whatever reason, the code should always work, or not?
Is this the correct use of try,except?
try:
# Define the function
def insert_null_values(sequence):
# Define the maximum number
max_number = IN[1]
# Initialize variables
output = []
expected_index = 0
# Iterate over the sequence
for i in range(1, max_number + 1):
for letter in ['a', 'b', 'c', 'd']:
expected_item = str(i) + letter
if expected_index < len(sequence) and sequence[expected_index] == expected_item:
output.append(sequence[expected_index])
expected_index += 1
else:
output.append(None)
return output
# Example input sequence (you should modify this according to your setup)
input_sequence = IN[0]
# Call the function
output_sequence = insert_null_values(input_sequence)
# Output the result
OUT = output_sequence
except:
print("Check for inputs")