I realized why the loop made no sense to me. It is written so that every time the loop writes to a field that can have its value set, it also writes to the ouput OID field provided by the user (if the input has an OID field). So if there are 10 writable fields, the user OID field is writen 10 times per record (more writeable fields only makes processing the loop worse due to twice as many write attempts). My VBA training from ESRI on loops has taught me to place as much of my testable conditions outside of the loop as possible and then minimize writing to a cursor within the loop to maximize speed and efficiency.To achieve those objectives I set some variables outside the loop that have pre-tested whether a write of the user provided OID field should even be attemped within the loop. I then only attempt to write to the user field when the input OID field is being read from the input (if the input has no OID field the user provided field never gets a write attempt and a related warning is reported at the end). All other non-OID fields being read from the input will bypass the user provided field and use your normal field writing code. This revison of the loop results in only a single write attempt for each field in a record. This should definitely improve overall speed of the cursor writing process.My revision of the loop is as follows:
inputLayerOidFieldName = dsc.oidfieldname
#Only attempt to write to the users OID field if the a field name is provided and the input has an OID field.
if originalOidFieldName not in ["","#"," "] and inputLayerOidFieldName > " ":
tryOidFieldWrite = True
origOidPopulateSuccessFlag = True
else:
tryOidFieldWrite = False
origOidPopulateSuccessFlag = False
while searchRow:
insertRow = insertRows.newrow()
for fieldName in inputLayerFieldList:
try: #you can't write the OID, length, area, etc. fields!
if outShpDbfFlag == False:
# Only attempt to write to the user OID field when the input OID field is being read
if tryOidFieldWrite == True and inputLayerOidFieldName == fieldName:
try:
insertRow.setvalue(originalOidFieldName, searchRow.getvalue(inputLayerOidFieldName))
except:
origOidPopulateSuccessFlag = False
pass
# All fields other than the input OID field are passed to this code. The input OID will also be passed if the user did not provide an output OID field name, but being non-writable it will fail.
else:
insertRow.setvalue(fieldName, searchRow.getvalue(fieldName))
else:
# Only attempt to write to the user OID field when the input OID field is being read
if tryOidFieldWrite == True and inputLayerOidFieldName == fieldName:
try:
insertRow.setvalue(originalOidFieldName[0:10], searchRow.getvalue(inputLayerOidFieldName))
except:
origOidPopulateSuccessFlag = False
pass
# All fields other than the input OID field are passed to this code. The input OID will also be passed if the user did not provide an output OID field name, but being non-writable it will fail.
else:
insertRow.setvalue(fieldName[0:10], searchRow.getvalue(fieldName))
except:
pass
recordCount = recordCount + 1
insertRows.insertrow(insertRow)
searchRow = searchRows.next()
message = "Successfully wrote " + str(recordCount) + " records!"; showPyMessage()
# Only publish a warning if the OID field write attempt should have been made and failed or if the user provided a field but the input had no OID field.
if originalOidFieldName not in ["","#"," "] and tryOidFieldWrite == True and origOidPopulateSuccessFlag == False:
message = "WARNING: For some reason, the original OIDs could not be preserved in the output!"; showPyWarning()
if originalOidFieldName not in ["","#"," "] and tryOidFieldWrite == False:
message = "WARNING: The input had no OID field and could not be preserved in the output!"; showPyWarning()
I hope that these revisions meet with your approval and can be incorporated into your final code.Rich