The script below does a series of things, and towards the end it is supposed to write values pulled from a list to a new field, if the feature class name is found anywhere in a list element. It does write values but there are strange characters that appear, or it doesn't write the entire list element value. I can't tell what the problem is?
import os, shutil,arcpy,arcgisscripting
gp = arcgisscripting.create()
path = r'C:\Temp\FME_share'
copy_dir = r'C:\test'
gdb_dir = copy_dir+'\\'+'CAD_Export_to_GDB'
BB_gdb_path = gdb_dir+'\\'+'BoundBoxes.gdb'
file_ext_lst = ['.2dm','.2de','.3dm','.3de','.dgn']
ext_to_chng = ['.2dm','.2de','.3dm','.3de']
file_lst = []
TxtVal_lst = []
repr_lst = []
found = dict()
arcpy.env.overwriteOutput = 'TRUE'
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
fullPath = os.path.join(dirpath, filename)
name,ext = os.path.splitext(filename)
if ext not in file_ext_lst:
continue
if name not in found:
found[name] = fullPath
print 'creating unique list of file names...'
for name,path in found.iteritems():
file_lst.append(path)
print 'copying files to local drive...'
for f in file_lst:
file_nm = f.split('\\')[-1]
shutil.copyfile(f, copy_dir+'\\'+file_nm)
print 'changing file extensions of copied files to .dgn...'
for dirpath, dirnames, copiedfiles in os.walk(copy_dir):
for copyfile in copiedfiles:
c_name,c_ext = os.path.splitext(copyfile)
filename_new = copy_dir+'\\'+c_name+'.dgn'
if c_ext in ext_to_chng:
os.rename(copy_dir+'\\'+copyfile, filename_new)
print 'exporting dgn files to geodatabase and exploding complex features...'
for dirpath, dirnames, renamed_files in os.walk(copy_dir):
for rn_file in renamed_files:
rn_name, rn_ext = os.path.splitext(rn_file)
in_dgn = dirpath+'\\'+rn_file
out_gdb = gdb_dir+'\\'+rn_name+'.gdb'
if rn_ext == '.dgn':
gp.ImportCAD_conversion(in_dgn,out_gdb,'','Explode_Complex')
print 'renaming geodatabase output to include original dgn file names...'
for dirpath, dirnames, filenames in arcpy.da.Walk(gdb_dir):
for filename in filenames:
gdb_name = dirpath.split('\\')[3]
orig_dgn_nm = gdb_name.split('.')[0]
if not dirpath.endswith('.gdb'):
arcpy.Rename_management(dirpath+'\\'+filename,dirpath+'\\'+filename+'_'+orig_dgn_nm)
print 'joining dgn text attributes to point feature classes, then adding text values to a list for searching out coordinate system information...'
for dirpath, dirnames, filenames in arcpy.da.Walk(gdb_dir):
for filename in filenames:
fullPath = dirpath+'\\'+filename
if 'Point' in filename:
Point_FC = dirpath+'\\'+filename
arcpy.JoinField_management(Point_FC,'EntID',dirpath.replace('CADStaging','')+'TxtProp','EntID','TxtValue')
values = [row[0] for row in arcpy.da.SearchCursor(Point_FC,'TxtValue')]
unique_vals = set(values)
unique_vals_str = ' '.join(str(uv) for uv in unique_vals)
if ('CCS 83' or 'CCS83') and 'ZONE 3' and 'METERS' in unique_vals_str:
print 'Trying to assign coordinate system to',dirpath,'....'
arcpy.DefineProjection_management(dirpath,26943)
elif ('CCS 83' or 'CCS83') and 'ZONE 3' and 'FEET' in unique_vals_str:
print 'Trying to assign coordinate system to',dirpath,'....'
arcpy.DefineProjection_management(dirpath,2227)
elif ('CCS27' or'CCS 27') and 'ZONE 3' in unique_vals_str:
print 'Trying to assign coordinate system to',dirpath,'....'
arcpy.DefineProjection_management(dirpath,26743)
elif ('CCS 83' or 'CCS83') and 'ZONE 2' and 'METERS' in unique_vals_str:
print 'Trying to assign coordinate system to',dirpath,'....'
arcpy.DefineProjection_management(dirpath,26942)
elif ('CCS 83' or 'CCS83') and 'ZONE 2' and 'FEET' in unique_vals_str:
print 'Trying to assign coordinate system to',dirpath,'....'
arcpy.DefineProjection_management(dirpath,2226)
elif ('CCS27' or 'CCS 27') and 'ZONE 2' in unique_vals_str:
print 'Trying to assign coordinate system to',dirpath,'....'
arcpy.DefineProjection_management(dirpath,26742)
else:
print 'Couldn\'t find coordinate system'
print 'creating minimum bounding box for line feature classes...'
for dirpath, dirnames, filenames in arcpy.da.Walk(gdb_dir):
for filename in filenames:
if 'Line' in filename:
BB_in_fc = dirpath+'\\'+filename
BB_out_fc = BB_gdb_path+'\\'+'BoundBox'+filename.replace('Line','')
arcpy.MinimumBoundingGeometry_management(BB_in_fc,BB_out_fc,'RECTANGLE_BY_AREA','ALL')
print 'adding and calculating SHEET_NAME and SHEET_PATH fields to bounding box feature classes, then reprojecting to WGS84...'
for dirpath, dirnames, filenames in arcpy.da.Walk(BB_gdb_path):
for filename in filenames:
sheet = filename.replace('BoundBox_','')
BB_fullpath = dirpath+'\\'+filename
arcpy.AddField_management(BB_fullpath,'SHEET_NAME','STRING')
arcpy.CalculateField_management(BB_fullpath,'SHEET_NAME',"'"+sheet+"'","PYTHON_9.3","#")
arcpy.AddField_management(BB_fullpath,'SHEET_PATH','STRING',200)
for f in file_lst:
if sheet in f:
f_str = str(f)
print f_str
arcpy.CalculateField_management(BB_fullpath,'SHEET_PATH','"'+f_str+'"',"PYTHON_9.3","#")
BB_desc = arcpy.Describe(BB_fullpath)
BB_CS = BB_desc.spatialReference
BB_repr_file = BB_gdb_path+'\\'+filename+'_repr'
BB_repr_CS = arcpy.CreateSpatialReference_management('WGS 1984')
if '1927' in BB_CS.name:
arcpy.Project_management(BB_fullpath,BB_repr_file,BB_repr_CS,'NAD_1927_To_WGS_1984_79_CONUS')
else:
arcpy.Project_management(BB_fullpath,BB_repr_file,BB_repr_CS,'NAD_1983_To_WGS_1984_1')
repr_lst.append(BB_repr_file)
merge_fc = BB_gdb_path+'\\'+'BoundBoxes_All'
print 'finally, merging all bounding boxes into one feature class!'
arcpy.Merge_management(repr_lst,merge_fc)