Please 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 insert a loop into the script to search through a given folder for all the cardfiles in it and import and classify them under different feature datasets and then store 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)
Solved! Go to Solution.
# Description: get a list of CAD drawings, iterate through each to create a feature dataset in specified geodatabase # Author: Irene # Import system modules import arcpy, glob from arcpy import env # Set workspace and variables dwg_folder = "C:/data1" env.workspace = dwg_folder #you'll need to determine if these next are always the same or create a way to substitute the right values output_gdb_path = 'xxxx' reference_scale = "1500" spatial_reference = "Nad_1983_10TM" # a function to return a portion of the cad file name to be used as the fds name def parse_cadname(dwg): ... split_path = dwg.split('\\') ... split_name = split_path[-1].split('.') ... return split_name[0] # Create a FileGDB for the fds arcpy.CreateFileGDB_management("C:/data1", "cadfile.gdb") #create a list of dwg files and process them into a geodatabase dwg_list = glob.glob(dwg_folder + '\\*.dwg') for dwg in dwg_list: input_cad_dataset = dwg output_dataset_name = parse_cadname(dwg) ...other logic classifying your cad file data... arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, output_dataset_name, reference_scale)
>>> import arcpy, glob >>> from arcpy import env >>> dwg_folder = "C:/data1" >>> env.workspace = dwg_folder >>> out_gdb_path = "c:/data/cadfile.gdb" >>> reference_scale = "2500" >>> spatial_reference = "NAD_1983_10TM" >>> def parse_cadname(dwg): ... split_path = dwg.split('\\') ... split_name = split_path[-1].split('.') ... return split_name[0] ... Parsing error <type 'exceptions.IndentationError'>: expected an indented block (line 2) >>>
Do you also have to search subfolders? If not then the 'easy' solution is to use the glob module in python to create a list of drawings then use a for loop to iterate that list and supply each list item to you exportToGeodatabase tool.. this might look something like this:
# Description: get a list of CAD drawings, iterate through each to create a feature dataset in specified geodatabase # Author: Irene # Import system modules import arcpy, glob from arcpy import env # Set workspace and variables dwg_folder = "C:/data1" env.workspace = dwg_folder #you'll need to determine if these next are always the same or create a way to substitute the right values output_gdb_path = 'xxxx' reference_scale = "1500" spatial_reference = "Nad_1983_10TM" # a function to return a portion of the cad file name to be used as the fds name def parse_cadname(dwg): ... split_path = dwg.split('\\') ... split_name = split_path[-1].split('.') ... return split_name[0] # Create a FileGDB for the fds arcpy.CreateFileGDB_management("C:/data1", "cadfile.gdb") #create a list of dwg files and process them into a geodatabase dwg_list = glob.glob(dwg_folder + '\\*.dwg') for dwg in dwg_list: input_cad_dataset = dwg output_dataset_name = parse_cadname(dwg) ...other logic classifying your cad file data... arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, output_dataset_name, reference_scale)
I haven't fully tested this so treat the above as 'concept' and debug or modify as fits your specific situation.
def parse_cadname(dwg): ... split_path = dwg.split('\\') ... split_name = split_path[-1].split('.') ... return split_name[0]
Hi Thompson,
I completed the script but it is not accepting the output_dataset_name. It is saying that it is not define. Here is my script and the error from the execution. Thanks for helping.
>>> import arcpy, glob >>> from arcpy import env >>> dwg_folder = "C:\CADdata" >>> env.workspace = dwg_folder >>> output_gdb_path = "C:/data/cadfile.gdb" >>> reference_scale = "2500" >>> spatial_reference = "Nad_1983_10TM" >>> def parse_cadname(dwg): split_path = dwg.split('\\') split_name = split_path[-1].split('.') return split_name[0] >>> arcpy.CreateFileGDB_management("C:/data", "cadfile.gdb") >>> arcpy.CreateFileGDB_management("C:/data", "cadfile1.gdb") <Result 'C:/data\\cadfile1.gdb'> >>> dwg_list = glob.glob(dwg_folder + '\\*.dwg') >>> for dwg in dwg_list: input_cad_dataset = dwg output_dataset_name = parse_cadname(dwg) arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, output_dataset_name, reference_scale)Traceback (most recent call last):
File "<pyshell#21>", line 3, in <module>
output_dataset_name = parse_cadname(dwg)
NameError: name 'parse_cadname' is not defined
My understanding of the parse_cadname(dwg) is that it will help to avoid hard-coding of the output_dataset_name such that the dataset generated for each cadfile will be separately stored with the dwg name (each Cadfile generates 4 feature classes in the dataset - point, line, polygon and annotation) and all will be stored within the specified geodatabase name.
some that function isn't being recognized - make sure that the spacing and everything is just right.. i have noticed that with PythonWin that you can be a space off at the >>> prompt .. either one closer to it or one farther away and it will act like it accepts that code, but then it doesn't compile like it is supposed to and then won't run. try re-entering that information (maybe do it in a text editor window then copy/past it into your interactive window) then right after that test it by typing parse_cadname(... once you put in the left parentheses it should prompt you for the arguments (dwg) - if it doesn't do that.. you'll have to try it again.. otherwise it should be good to go. You might be wise to try and put this all into a text window and run it from there just because its easier to manage than from the command prompt, and if something goes wrong, you don't have to retype everything again.
# Import system modules import arcpy import glob import os # Set workspace and variables gdb = r"C:\data\DGW2007.gdb" arcpy.env.workspace = gdb # Create a FileGDB for the fds arcpy.CreateFileGDB_management("C:/data", "DGW2007.gdb") reference_scale = "1500" for file in glob.glob(r"N:\2007_dwg\*.dwg"): outDS = arcpy.ValidateTableName(os.path.splitext("d" + os.path.basename(file))[0]) arcpy.CADToGeodatabase_conversion(file, gdb, outDS, reference_scale)