Python script that can do a batch import of CAD files into a Geodatabase.

4888
6
Jump to solution
08-06-2012 12:44 PM
IreneEgbulefu
New Contributor II
Hello everyone,

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)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChristopherThompson
Occasional Contributor III
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.

View solution in original post

0 Kudos
6 Replies
ChristopherThompson
Occasional Contributor III
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.
IreneEgbulefu
New Contributor II
Hi Thompson, I tried running the script here below but it keeps giving me a parsing error on line 2. I dont understand why it is expecting a : on this line.
Here is the extent i could go
>>> 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.
0 Kudos
ChristopherThompson
Occasional Contributor III
Some days i really hate how finicky python is about indents.  The error it is finding is related to def parse_cadname function as it doesn't like the indentation used there.  I know it looks like you have those lines below the def statement indented but I'd go back into that code in your and add an indent in front of each line below the def statement so it ends up looking like this:
def parse_cadname(dwg):
...  split_path = dwg.split('\\')
...  split_name = split_path[-1].split('.')
...  return split_name[0] 


It helps to use an IDE that clearly shows spaces and indents (PythonWin and Notepad++ are good editors for this, IDLE not so much).  I know this function works because I've used this and variants of it a lot to get file names from an object returned with a full path.
0 Kudos
IreneEgbulefu
New Contributor II
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.
0 Kudos
ChristopherThompson
Occasional Contributor III
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.
0 Kudos
IreneEgbulefu
New Contributor II
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.


This has now been achieved; I am able to create a geodatabase with the same name as the folder and convert the CAD files into Feature dataset loaded with feature dataclasses using the script below. It worked perfectly well. Thanks Thompson and Curtvprice for all your inputs.
# 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)

I  still have one more task to complete in this topic and that is to write a script that will iterate through my CAD drive (which stores my CAD data according to the year the were acquired) and convert each CAD directory folder to a geodatabase and populate it with the related feature classes as obtained when implementing it as individual folders as seen above.
0 Kudos