[GIS] Python script to implement a batch import of CAD files into Geodatabase

arcpycadfile-geodatabase

Does anyone know where I can get a python script that can do a batch import of CAD files into a Geodatabase.

I have one I wrote here, but it is only able to import one CAD file at a time. It imports the CAD files and split it into feature classes and stores it in the geodatabase via the feature dataset.
It works perfectly well but I need to loop the script to search through a given folder for all the cardfiles in it and import them under different feature datasets and put all the datasets into one geodatabase.

# Name: CadtoGeodatabase.py
# Description: Create a feature dataset
# Author: Irene
# Import system modules
import arcpy
from arcpy import env

# Set workspace
env.workspace = "C:/data1"

# Set local variables
input_cad_dataset = C:\Users\iegbulefu\Documents\info\91036c01.dwg" output_gdb_path = "c:/data/cadfile.gdb"
output_dataset_name = "cadresults"
reference_scale = "1500"
spatial_reference = "Nad_1983_10TM"

# Create a FileGDB for the fds
arcpy.CreateFileGDB_management("C:/data1", "cadfile.gdb")

# Execute CreateFeaturedataset 
arcpy.CreateFileGDB_management("C:/data", "cadfile.gdb")
arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, output_dataset_name, reference_scale)

Best Answer

You'll just need to use a for loop and if statement to find all the files you need. I haven't tested the code below with CAD files but it should be what you're after (or at least provide the structure to do so).

# Import system modules
import arcpy, os
from arcpy import env

# Set local variables
input_cad_folder = "C:\Users\iegbulefu\Documents\info"

output_gdb_folder = "C:\data"
output_gdb = "cadfile.gdb"
output_gdb_path = os.path.join(output_gdb_folder, output_gdb)

reference_scale = "1500"
spatial_reference = "Nad_1983_10TM"

# Create a FileGDB for the fds
try:
    arcpy.CreateFileGDB_management(output_gdb_folder, output_gdb)
except:
    pass

# For loop iterates through every file in input folder
for found_file in os.listdir(input_cad_folder):
    # Searches for .dwg files
    if found_file.endswith(".dwg"):

        print "Converting: "+found_file
        input_cad_dataset = os.path.join(input_cad_folder, found_file)
        try:
            arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, found_file[:-4], reference_scale, spatial_reference)
        except:
            print arcpy.GetMessage()