Hi everyone,
I need a script that can can iterate through folders that contains all my CAD files and authomatically convert each CAD file to a feadture dataset and store them in a specified geodatabase.
I already created a script that is able to pick up a specified CAD file rum the given folder and converts it to a geodatabase with feature dataset, but i need to iterate through the folder to convert all the other files to feature database
>>> import arcpy ... from arcpy import env ... env.workspace = "C:/data1" ... input_cad_dataset = "C:/CADdata/94a88W01.dwg" ... output_gdb_path = "c:/data/cadfile.gdb" ... output_dataset_name = "cad9488" ... reference_scale = "500" ... spatial_reference = "NAD_1983_10TM115" ... arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, output_dataset_name, reference_scale) ... reference_scale = "2500" ... arcpy.CADToGeodatabase_conversion(input_cad_dataset, output_gdb_path, output_dataset_name, reference_scale) ...
I tried inserting the line for using a "d" to prefix the output feature dataset name in my script, the script did run, created some datasets and errored out at one of the dwg files, i am not able to figure out why it is not able to create the annotation for that file and why it is erroring out at that point.
outDS = arcpy.ValidateTableName("d" + os.path.splitext(os.path.basename(file))[0])
(regarding looping attempt) i guess i am missing something because it is not even creating the GDBs.
gdbName = "d{0}.gdb".format(year) arcpy.CreateFileGDB_management("C:/data",gdbName)
# Import system modules import arcpy import glob import os # Set workspace and variables gdbName = "d{0}.gdb".format(1990) for year in range(1990,2005): # 1990-2004 inFolder = r"{0}.gdb".format(1990)# 1991_dwg arcpy.env.workspace = gdbName # Create a FileGDB for the fds arcpy.CreateFileGDB_management("C:/data", "d{0}.gdb") reference_scale = "1500" for year in range(1990,2005): # 1990-2004 inFolder = r"N:\{0}_dwg".format(1990) # 1990_dwg gdbName = "d{0}.gdb".format(year) # d1990.gdb for file in glob.glob(r"N:\{0}_dwg"): outDS = arcpy.ValidateTableName("d" + os.path.splitext(os.path.basename(file))[0])
This one just creates the geodatabase name d{0}.gdb (it does not substitute the 0 with the names specified in the format(), i dont understand why)
Check the value of outDS you're getting with a print statement -- but also make sure your output GDB is entirely empty. arcpy.env.overwriteOutput = True is a good idea too!
Perhaps this syntax will work better for you:outDS = arcpy.ValidateTableName("d" + os.path.splitext(os.path.basename(file))[0])
Yes - Look up the syntax on .format() in the python help.
You need to substitute that year into the gdb name using .format:gdbName = "d{0}.gdb".format(year) arcpy.CreateFileGDB_management("C:/data",gdbName)
This one just creates the geodatabase name d{0}.gdb (it does not substitute the 0 with the names specified in the format(), i dont understand why)
arcpy.CreateFileGDB_management("C:/data", "d{0}.gdb"
arcpy.CreateFileGDB_management("C:/data", "d{0}.gdb".format(year)
This line in your code is wrong:
arcpy.CreateFileGDB_management("C:/data", "d{0}.gdb"
Should be
arcpy.CreateFileGDB_management("C:/data", "d{0}.gdb".format(year)
and also should be inside the loop.
Python 2.7 docs: string methods
#Import system modules import arcpy import glob import os # Set workspace and variables for year in range(1990,2009): # 1990-2009 inFolder = r"c:\data\cadfiles\{0}_dwg".format(year) # 1990_dwg gdbName = "d{0}.gdb".format (1990,2009) # d1990.gdb arcpy.env.workspace = gdbName # Create a FileGDB for the fds arcpy.CreateFileGDB_management("C:/data", "d{0}.gdb".format(year)) reference_scale = "1500" for file in glob.glob(r"N:\{0}_dwg"): outDS = arcpy.ValidateTableName(os.path.splitext("d" + os.path.basename(file))[0]) arcpy.CADToGeodatabase_conversion(file, gdbName, outDS, reference_scale)