6

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)

2 Answers 2

8

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()
1

You can use the code below to search a given folder and subfolders for .dwg files and then use this list as an input to your script.

# Creates a list of all the files with the given list of extensions and search strings
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could affect all your disk files.


#Licence: Creative Commons
#Created by: George Corea; [email protected], [email protected]

import os, sys, datetime, arcpy

top = os.getcwd() # change to a specific path if required.
# This otherwise starts with the directory the script is in.
RootOutput = top # change if you want output somewhere else
FileTypes=['dwg'] # add filetypes or as required. '' for all files (remember .shp has 4 or 5 other associated files)
SearchStrings=[''] # add strings as required use format ['search1','search2'] etc.

filecount=0
#successcount=0
#errorcount=0

print "Working in: "+os.getcwd()

List =[]

for root, dirs, files in os.walk(top, topdown=False):
    for fl in files:
      currentFile=os.path.join(root, fl)
      for FileType in FileTypes:
          status= str.endswith(currentFile,FileType)
          if str(status) == 'True':
              for SearchString in SearchStrings:
                  if str(SearchString in currentFile) == 'True':
                    #print str(currentFile)+str(status)       
                    List.append(currentFile)
      filecount=filecount+1

print 'File List: ' + str(List) + " with "+ str(len(List)) + " objects."
# Replace following with whatever arcpy. tool you want to run using the list as an input.

then you can do the following

for item in List
 item=input_cad_dataset
 arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, output_dataset_name, reference_scale)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.