1

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
  • Shapefiles don't support annotation features... you should be able to convert the simple geometries (point, polyline, polygon) with feature class to feature class resources.arcgis.com/en/help/main/10.2/index.html#//… .. to access each geometry type use f + '\\point', f + '\\polyline' and f + '\\polygon'. Be sure to get a count of features resources.arcgis.com/en/help/main/10.2/index.html#//… so you're not exporting empty feature classes. – Michael Stimson Aug 1 '17 at 1:44
  • it is ok but I have hundreds of drawing files which need to be convert them to shape files and this is task to be done every day. Ii is time saving if there is a python script to convert all drawing files in to shape files from folder at single shot.Can any one provide script? – Arb Aug 1 '17 at 13:30
1

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.

  • Hi Michael Stimson, this code is working great and save lot of time in my work. thank you. – Arb Aug 3 '17 at 15:13
  • I'm glad it helps. Just on a side note there's another tool that imports a single CAD file into a (new) file geodatabase, this tool should import annotation features if that is (or becomes) important. Read more resources.arcgis.com/en/help/main/10.2/index.html#//… – Michael Stimson Aug 3 '17 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.