Modelbuilder workflow involving CAD .DWG File

3611
10
Jump to solution
02-10-2016 12:04 PM
CraigPrisland1
New Contributor II

Hello all,

I'm trying to build a model through modelbuilder with the following workflow and haven't found a good way to go about this yet.  Any input would be greatly appreciated:

I have a .dwg file with a certain name, for example "Baseline.dwg".  I have several feature class in a File geodatabse in a certain location with one of them being named "Baseline" as well.  The "Baseline" feature class in the geodatabase is a Line feature.  What I am looking to do is appending the data from the Polyline layer in the DWG into this "Baseline" feature class based on the same name. 

In short, based on the name of the DWG file and the feature class I would like to append this data into this feature class.  Please keep in mind that I have several feature classes so I was wondering if some sort of iterate could help with this by looking through the names of the feature classes and matching the one with the same name as the DWG.

Thanks in advance!

Craig

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

I'm not sure how to do this in ModelBuilder, but here's a Python script which should do what you're after.

>>> import arcpy, os # import libraries
...
... dwg_folder = r'C:\junk' # folder containing dwgs
... arcpy.env.workspace = dwg_folder
... dwg_files = arcpy.ListFiles('*.dwg') # list of dwgs in folder
...
... gdb_location = r'C:\junk\FILE_GDB.gdb' # path to GDB
... arcpy.env.workspace = gdb_location
... gdb_fcs = arcpy.ListFeatureClasses(feature_type='Polyline') # list of polyline feature classes in GDB
...
... for file in dwg_files: # loop through dwgs
...     for fc in gdb_fcs: # loop through feature classes
...         if file[:-4] == fc: # compare the dwg name (minus '.dwg') to the feature class name
...             arcpy.Append_management(os.path.join(file,'Polyline'),fc,"NO_TEST") # append polyline layer inside dwg to matching feature class

General equivalences are:

- Iterate Files to arcpy.ListFiles()

- Iterate Feature Classes to arcpy.ListFeatureClasses

- you'll also need inline variable substitution and submodels

View solution in original post

10 Replies
KyleBalke__GISP
Occasional Contributor III

Craig...based on what you described it sounds like you have a one to one relationship (one feature class and one DWG).  Why not just use the Append tool...seems like building a model is a bit overkill?

0 Kudos
CraigPrisland1
New Contributor II

Kyle,

You are correct.  I can do a simple Append on this one-to-one relationship however I may not have explained this correctly.  I will have several feature classes (i.e. Test1, Test2, Test3, Test4, Test5, etc...) and what will happen is a user will input a DWG file (parameter) with the same exact name as one of the feature classes (Test1, Test2, etc...).  What I am looking for is a way to append the line data in this DWG with the appropriate feature class of the same name.  I am looking for the only model parameter to be the input DWG file.

Hopefully this helps.  Thanks again.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I'm not sure how to do this in ModelBuilder, but here's a Python script which should do what you're after.

>>> import arcpy, os # import libraries
...
... dwg_folder = r'C:\junk' # folder containing dwgs
... arcpy.env.workspace = dwg_folder
... dwg_files = arcpy.ListFiles('*.dwg') # list of dwgs in folder
...
... gdb_location = r'C:\junk\FILE_GDB.gdb' # path to GDB
... arcpy.env.workspace = gdb_location
... gdb_fcs = arcpy.ListFeatureClasses(feature_type='Polyline') # list of polyline feature classes in GDB
...
... for file in dwg_files: # loop through dwgs
...     for fc in gdb_fcs: # loop through feature classes
...         if file[:-4] == fc: # compare the dwg name (minus '.dwg') to the feature class name
...             arcpy.Append_management(os.path.join(file,'Polyline'),fc,"NO_TEST") # append polyline layer inside dwg to matching feature class

General equivalences are:

- Iterate Files to arcpy.ListFiles()

- Iterate Feature Classes to arcpy.ListFeatureClasses

- you'll also need inline variable substitution and submodels

CraigPrisland1
New Contributor II

Thanks for all your help.  This Python script is exactly what I was looking for.

0 Kudos
MitchHolley1
MVP Regular Contributor

What a service Darren does for this group!  Thanks

0 Kudos
CraigPrisland1
New Contributor II

Darren,

I have a simple question for you and I apologize in advance as I am not too advanced in Python.  How would this script alter if I wanted a user input parameter for them to select the .dwg (getparameterastext) and whatever the name of the dwg is, it would append that data to an already created feature class with the same name.

I also agree that you do quite a great service to this group.  Thanks again!

0 Kudos
DarrenWiens2
MVP Honored Contributor

If you want the user to specify a single dwg file, do something like the following. GetParameterAsText(0) returns your first parameter in the tool dialog. The return value will be the path to a feature class or layer name of a feature layer.

>>> import arcpy, os # import libraries  
...  
... dwg = arcpy.GetParameterAsText(0) # a dwg file. e.g. 'C:\junk\blahblah.dwg'
... 
... gdb_location = r'C:\junk\FILE_GDB.gdb' # path to GDB  
... arcpy.env.workspace = gdb_location  
... gdb_fcs = arcpy.ListFeatureClasses(feature_type='Polyline') # list of polyline feature classes in GDB  
... 
... for fc in gdb_fcs: # loop through feature classes  
...     if dwg[:-4] == fc: # compare the dwg name (minus '.dwg') to the feature class name  
...         arcpy.Append_management(os.path.join(dwg,'Polyline'),fc,"NO_TEST") # append polyline layer inside dwg to matching feature class
0 Kudos
CraigPrisland1
New Contributor II

Darren,

Thank you for this script.  When I trying running it against my data, it appears as if the script runs (I don't get any errors) but it only runs for less than a second and my output that the DWG layer should append to remains empty.  I have been playing around with the script some with no success to this point.  Would you have any suggestions?  Thanks again.

0 Kudos
CraigPrisland1
New Contributor II

I am also looking to append only certain features in the dwg in which a field (in this case "Layer") is attributed as "TEXT" only.  Would this get added in near the end of the script that you mentioned above around the "if dwg[:-4] ==fc:" line?

0 Kudos