Importing Individual CAD Layers into GIS using ArcPy

Complex CAD files often come into the GIS software with an overwhelming amount of layers.   This makes it very difficult to work with the data to identify useful layers and often drastically slows down your computer.  This post will walk through a way to convert only the necessary CAD layers into a geodatabase, instead of sifting through the unneeded layers, lines, and details in GIS.  The demo script walks through my process of importing the room polygons of Temple’s Alter Hall and converting them to a geodatabase to later join with our university room database.

Step 1:  #Import System Modules

import arcpy

Don’t forget to import ArcPy!

 

Step 2: #Establish Workspace Environment

arcpy.env.workspace = “C:/Users/tug28727.TU/Desktop/cad_dwgs/alter”

This line establishes your workspace.  Select the location where your CAD Drawings are.  For my script I used the folder location for Alter Hall.

 

Step 3: # Create a value table

vTab = arcpy.ValueTable()

A value table is necessary to hold input feature classes when you run a merge later in the script.

 

Step 4: # Create Geodatabase

arcpy.CreateFileGDB_management(“C:/Users/tug28727.TU/Desktop/cad_dwgs/alter”, “alter.gdb”)

A geodatabase needs to be create to store all of the features.

 

Step 5: #Identify CAD drawings and Create Features from CAD Layers

for fd in arcpy.ListDatasets(“*.dwg”):
     layername = fd + “_Layer”
     # Select only the Polygon features on the drawing layer rooms
     arcpy.MakeFeatureLayer_management(fd + “/Polygon”, layername, “\”Layer\” = ‘RM$'”)
     vTab.addRow(layername)

First, this cycles through all of the files in the workspace and identifies those files that end in .dwg- which are the CAD files.  Next, it creates and layer name.  After that, it creates features searching for whatever CAD details you want to add (here I am searching for Polygons on the RM$ layer).  Lastly, the features are populated on the value table created in step 3.

 

Step 6: #Merge into one Feature Class

arcpy.Merge_management(vTab, “C:/Users/tug28727.TU/Desktop/cad_dwgs/alter/alter.gdb/rooms”)

This tool merges all of the CAD features from Step 5 into one feature class in the geodatabase we created in Step 4.  If you wanted to merge the value table with other data you would add brackets and commas separating the data to merge where the vTab is located (ex. [vTab, “alter_rooms.shp”], “C:/fake_cad_folder/all_buildings”).

 

This script should successfully search through your workspace files to identify CAD drawings, and bring in the layers you want in a geodatabase.   Step 5 is where you are able to change which CAD layer you are looking for.   This script hopefully streamlines your process of import CAD files into GIS.  ArcGIS has plenty of resources on CAD and GIS integration located here.

 

 

Leave a Reply