[GIS] Converting multiple drawing files to shapefiles

arcpycadconvert

I have multiple dwg files and I need to convert them from CAD file to shapefile using Arcpy. I am trying to export each layer in .dwg file (i.e. annotation, point, polygon, polyline and Multipath layers, etc.) to shapefile as contains subsets of data within it.

I don't know how to access each layer of drawing and convert all drawing files to shapefiles. I have searched all existing but did not get sufficient information.

Any help?

Here is my code but I know this is not correct. Even I am not successful to access and print each layer in CAD. My ultimate goal is to convert multiple files dwg files to separate shapefiles using ArcPy.

import arcpy    
from arcpy import env


env.workspace = r"C:DATA\DWG"
datasets = arcpy.ListDatasets()
for dataset in datasets:
    filesets = arcpy.ListFiles(dataset)    
    for f in filesets:
            print f.name

Best Answer

Following on from where you started...

import arcpy, os    
from arcpy import env

env.overwriteOutput = True
env.workspace       = r"C:\DATA\DWG"
outFolder           = r'C:\Your\Output\Path'

datasets = arcpy.ListDatasets()
for dataset in datasets:
    filesets = arcpy.ListFiles(dataset)    
    for f in filesets:
        print f
        InPoint = os.path.join(env.workspace,f,"Point")    # the CAD points
        InLine  = os.path.join(env.workspace,f,"Polyline") # the CAD polylines
        InPoly  = os.path.join(env.workspace,f,"Polygon")  # the CAD polygons
        SaniF   = f.replace(" ","_").replace("-","_").replace(".","_")  # remove invalid chars..

        if int(arcpy.GetCount_management(InPoint).getOutput(0)) == 0:
            arcpy.AddWarning("- No points")
        else:
            outPointShape = '{}_Point.shp'.format(SaniF)
            arcpy.AddMessage('- Exporting Points')
            arcpy.FeatureClassToFeatureClass_conversion(InPoint,outFolder,outPointShape)

        if int(arcpy.GetCount_management(InLine).getOutput(0)) == 0:
            arcpy.AddWarning("- No lines")
        else:
            outLineShape = '{}_Line.shp'.format(SaniF)
            arcpy.AddMessage('- Exporting Line')
            arcpy.FeatureClassToFeatureClass_conversion(InLine,outFolder,outLineShape)

        if int(arcpy.GetCount_management(InPoly).getOutput(0)) == 0:
            arcpy.AddWarning("- No polygons")
        else:
            outPolyShape = '{}_Poly.shp'.format(SaniF)
            arcpy.AddMessage('- Exporting Polygons')
            arcpy.FeatureClassToFeatureClass_conversion(InPoly,outFolder,outPolyShape)

You need to populate your output folder from your filesystem.

You can see from this example how the feature classes within the CAD file can be accessed by their feature type; CAD files feature classes always exist but may have no features so it's important to get a count of features before exporting.

I have set the overwriteOutput to True so you might need to be a bit careful with the creation of output names, shapefiles have name limitations so I have to sanitize the output name from the input name replacint dashes, spaces and points.. you might need to add other replaces depending on what you see in your data, the file should also start with a letter and not be the same as a SQL reserved word.