Select to view content in your preferred language

Python Script to

4295
13
08-08-2012 12:17 PM
IreneEgbulefu
Deactivated User
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)
... 
Tags (2)
0 Kudos
13 Replies
curtvprice
MVP Alum
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.


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])



(regarding looping attempt) i guess i am missing something because it is not even creating the GDBs.


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)
0 Kudos
IreneEgbulefu
Deactivated User
Hi Curtiv,
I have been working on this script since yesterday and uptil this morning I have not been able to get it to populate the geodatabase with  datasets. I also observed that it is not picking information from the format() as supposed.
I am actually having some difficulties on how best to set the format parameters because the parameters are supposed to be folders (subdirectories) within a directory and and each of them has a suffix at the end. Each time i insert the foldernme as it is within the format() parameter, the script does not recorgnise it and so it gives me a syntax error.
Please see how my folders are arranged. I have gone through a lot python format() function but couldnt find any that is similar to mine.
[ATTACH=CONFIG]17033[/ATTACH]

Here is my script and the errors Im getting

# 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)
0 Kudos
curtvprice
MVP Alum

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)  


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
0 Kudos
IreneEgbulefu
Deactivated User
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


Hi Curtis, It appears the script is not recorgnising the years as just foldernames within a director. When I run the script it simply picks up the year just before the last, e.g if i specifiy range(1990,2009) it will just create a gdb folder for 2008 and thats all, even when i just put one year (1998) in the parameter for the format it will just create the an empty gdb in the name 1998 without datasets in it

It does not create and populate the gdb with feature datasets and it does not iterate through the folders in the directory.
Please can you check my script to know where im not getting it right.

Thanks

#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)
0 Kudos