Select to view content in your preferred language

better way to write this script?

530
2
09-14-2012 05:46 AM
DanielErklauer
Deactivated User
Hello, I have a CAD DWG drawing that has layers that I want to export into individual shape files.  I have written my python script and it works but I do not believe it is the most efficient way to write it. My specefic question is I wrote a command line for every layer having an input layer name and an ouput shapefile.  Is there a way to write this so that the script identifies every unique layer name and outputs the shape file without having to write a command for each CAD layer?

# Import arcpy module
import arcpy


# Local variables:
centerline_grid_dwg_Polygon__2_ = "centerline_grid.dwg Group Layer\\centerline_grid.dwg Polygon"
centerline_grid_dwg_Polygon__3_ = "centerline_grid.dwg Group Layer\\centerline_grid.dwg Polygon"
Dan = "C:\\Dan"

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(centerline_grid_dwg_Polygon__2_, "NEW_SELECTION", "\"Layer\" = 'segment _1A_Horizontal Control'")

# Process: Feature Class to Feature Class
arcpy.FeatureClassToFeatureClass_conversion(centerline_grid_dwg_Polygon__3_, Dan, "test5556.shp", "", "Entity \"Entity\" true true false 16 Text 0 0 ,First,#,N:\\Inter Departmental Data\\centerline_grid.dwg\\Polygon,Entity,-1,-1;Layer \"Layer\" true true false 255 Text 0 0 ,First,#,N:\\Inter Departmental Data\\centerline_grid.dwg\\Polygon,Layer,-1,-1;Color \"Color\" true true false 2 Short 0 0 ,First,#,N:\\Inter Departmental Data\\centerline_grid.dwg\\Polygon,Color,-1,-1;Linetype \"Linetype\" true true false 255 Text 0 0 ,First,#,N:\\Inter Departmental Data\\centerline_grid.dwg\\Polygon,Linetype,-1,-1;Elevation \"Elevation\" true true false 8 Double 0 0 ,First,#,N:\\Inter Departmental Data\\centerline_grid.dwg\\Polygon,Elevation,-1,-1;LineWt \"LineWt\" true true false 2 Short 0 0 ,First,#,N:\\Inter Departmental Data\\centerline_grid.dwg\\Polygon,LineWt,-1,-1;RefName \"RefName\" true true false 255 Text 0 0 ,First,#,N:\\Inter Departmental Data\\centerline_grid.dwg\\Polygon,RefName,-1,-1", "")


this is a snippet the above two lines are duplicated many more time times changing the query statment to match a new layer name and the output file name....
Tags (2)
0 Kudos
2 Replies
FabianBlau
Deactivated User
You could use a loop. Put your outnames and your query-statements in lists:
outnames = ["outfile1", "outfile2", ...]
queries = ["query1", "query2", ...]
for ii in range(len(outnames)):
    arcpy.SelectLayerByAttribute_management(... outnames[ii] ... queries[ii] ... "NEW SELECTION")
    arcpy.FeatureClassToFeatureClass_conversion(...

Sorry, i have no more time, but maybe this shows you the way.
0 Kudos
GeorgeNewbury
Frequent Contributor
You could also use a python dictionary. Something akin to:

exportDictionary = {}
exportDictionary['layer name 1'] = [layer1.shp, 'query 1']   #I'm not familiar with DWG layers 
exportDictionary['layer name 2'] = [layer2.shp, 'query 2']
...

# if you look up what a python dictionary is you'll see references to key, value pairs
out_path = r"C:\Dan"
for key, value in exportDictionary:
    arcpy.FeatureClassToFeatureClass_conversion(key, out_path, value[0], value[1])


I didn't see why you needed the 'SelectByAttributes' function since the where clause in the FeatureClassToFeatureClass should select what you need. Also if you didn't want to specify the output shapefile name, you could use various methods to build the name off of the input layer. E.g.

mylayer= 'myInputLayer.dwg' 
outShp = os.path.splitext(myLayer)[0] + ".shp"
0 Kudos