ArcPy CAD Layers Iteration to GeoDataBase

4211
3
08-07-2013 02:51 PM
JeffJudycki
New Contributor
I am trying to extract an Ex-Pave layer out of a whole years worth of CAD drawings and I cannot seem to get the code to work
I was able to find the following yet have not had an success. If anyone could shed some light on the error listed below it would be greatly appreciated. Thank you

 Name: ImportCADandMerge.py
# Description: Imports and merges polylines from one workspace into a single feature class

# Import system modules
import arcpy 
from arcpy import env

env.workspace = "C:\Users\jjudycki\Desktop\Vectren\130103.00 - VEDO 2014 Groups"

# Create a value table that will hold the input feature classes for Merge
vTab = arcpy.ValueTable()

# Step through each dataset in the list
for fd in arcpy.ListDatasets("*.dwg", "CAD"):
    layername = fd + "_Layer"
    # Select only the Polyine features on the drawing layer EX-PAVE
    arcpy.MakeFeatureLayer_management(fd + "/Polyline", layername, "\"Layer\" = 'EX-PAVE'")
    vTab.addRow(layername)

# Merge the CAD features into one feature class
arcpy.Merge_management(vTab, "C:\Users\jjudycki\Desktop\Vectren\130103.00 - VEDO 2014 Groups\VEDO_2014_Drawings.gdb")


File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run
    exec cmd in globals, locals
  File "C:\Student\PythEveryone10_1\CreatingScripts\CADtoLayer.txt", line 5, in <module>
    import arcpy
TypeError: 'NoneType' object is not iterable
Tags (2)
0 Kudos
3 Replies
StacyRendall1
Occasional Contributor III
Jeff, how are you running this? Most of the traceback refers to PythonWin debugger, and the last part shows the file "C:\Student\PythEveryone10_1\CreatingScripts\CADtoLayer.txt" as causing some problem.

Can you run it directly from the command line? This will eliminate some of the confusion. Here are instructions on finding your Arcpy Python install and calling it from the command line.

I would also recommend taking a step back and testing your code with lots of print statements i.e.:
# Name: ImportCADandMerge.py
# Description: Imports and merges polylines from one workspace into a single feature class

# Import system modules
import arcpy 
from arcpy import env

env.workspace = "C:\Users\jjudycki\Desktop\Vectren\130103.00 - VEDO 2014 Groups"

# Create a value table that will hold the input feature classes for Merge
vTab = arcpy.ValueTable()

print '1: ', arcpy.ListDatasets("*.dwg", "CAD")

# Step through each dataset in the list
for fd in arcpy.ListDatasets("*.dwg", "CAD"):
    print '2: ', fd
    layername = fd + "_Layer"
    # Select only the Polyine features on the drawing layer EX-PAVE
    arcpy.MakeFeatureLayer_management(fd + "/Polyline", layername, "\"Layer\" = 'EX-PAVE'")
    print '3: ', layername
    vTab.addRow(layername)

# Merge the CAD features into one feature class
arcpy.Merge_management(vTab, "C:\Users\jjudycki\Desktop\Vectren\130103.00 - VEDO 2014 Groups\VEDO_2014_Drawings.gdb")


This will let you check each part. If the first statement prints 1: None, then there are possibly no *.dwg files in the specified workspace.
0 Kudos
JeffJudycki
New Contributor
I was able to run the python script from the command line yet I am still getting similar error 'NoneType' object is not iterable?
Any ideas
[ATTACH=CONFIG]26578[/ATTACH]

And thank you for your help
0 Kudos
StacyRendall1
Occasional Contributor III
That looks better.

You should read the Docs for ListDatasets. You are calling it with this: arcpy.ListDatasets("*.dwg", "CAD"), but "CAD" is not a valid feature type for this tool, so it returns a value of None.

You can tell that something is fishy because of this in your test:
>>> print '1: ', arcpy.ListDatasets("*.dwg", "CAD")
1: None


That means your next line, for fd in arcpy.ListDatasets("*.dwg", "CAD"): is actually being evaluated as for fd in None:, which doesn't make sense, so it causes an error.

I hope it is clear to you what is actually going on here...


To fix it, you can try the code below. I have just removed the "CAD" part in the ListDatasets, it will default to listing all files with a .dwg extension.

# Name: ImportCADandMerge.py
# Description: Imports and merges polylines from one workspace into a single feature class

# Import system modules
import arcpy 
from arcpy import env

env.workspace = "C:\Users\jjudycki\Desktop\Vectren\130103.00 - VEDO 2014 Groups"

# Create a value table that will hold the input feature classes for Merge
vTab = arcpy.ValueTable()

print '1: ', arcpy.ListDatasets("*.dwg")

# Step through each dataset in the list
for fd in arcpy.ListDatasets("*.dwg"):
    print '2: ', fd
    layername = fd + "_Layer"
    # Select only the Polyine features on the drawing layer EX-PAVE
    arcpy.MakeFeatureLayer_management(fd + "/Polyline", layername, "\"Layer\" = 'EX-PAVE'")
    print '3: ', layername
    vTab.addRow(layername)

# Merge the CAD features into one feature class
arcpy.Merge_management(vTab, "C:\Users\jjudycki\Desktop\Vectren\130103.00 - VEDO 2014 Groups\VEDO_2014_Drawings.gdb")
0 Kudos